// Youtube on Player Ready
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById(playerId);
  //Load an initial video into the player
  ytplayer.cueVideoById(videoId);
  ytplayer.playVideo();
}

function muteVideo() {
  if(ytplayer) {
    ytplayer.mute();
    $("a.sound").addClass("off");
  }
}

function unMuteVideo() {
  if(ytplayer) {
    ytplayer.unMute();
    $("a.sound").removeClass("off");
  }
}

// Scrolling horizontal on collections
var scrolling = false;

$(document).ready(function() {
  // Cufon Fonts
  runCufon();

  $("#body-background").ezBgResize();

  $("#collections-link").click(function () {
    if($("#collections-list").is(":hidden")) {
      $("#collections-list").slideDown();
    } else {
      $("#collections-list").hide();
    }
    return false;
  });

  $("#newsletter-link").click(function () {
    if($("#newsletter-list").is(":hidden")) {
      $("#newsletter-list").slideDown();
    } else {
      $("#newsletter-list").hide();
    }
    return false;
  });

  // Return to available models in the online store
  $(".return-models").click(function () {
    history.back();
    return false;
  });

  $(".timeout").miniZoomPan({
    sW: 400,
    sH: 250,
    lW: 366,
    lH: 720,
    loaderContent: "<img src='/images/ajax-loader.gif' />"
  });

  $(".monocle").miniZoomPan({
    sW: 400,
    sH: 250,
    lW: 630,
    lH: 390,
    loaderContent: "<img src='/images/ajax-loader.gif' />"
  });

  $(".oficina").miniZoomPan({
    sW: 400,
    sH: 250,
    lW: 1275,
    lH: 1754,
    loaderContent: "<img src='/images/ajax-loader.gif' />"
  });

  $(".sabado").miniZoomPan({
    sW: 400,
    sH: 250,
    lW: 561,
    lH: 423,
    loaderContent: "<img src='/images/ajax-loader.gif' />"
  });

  $(".attitude-mag").miniZoomPan({
    sW: 400,
    sH: 250,
    lW: 547,
    lH: 720,
    loaderContent: "<img src='/images/ajax-loader.gif' />"
  });

  $(".feiras-francas").miniZoomPan({
    sW: 400,
    sH: 250,
    lW: 430,
    lH: 720,
    loaderContent: "<img src='/images/ajax-loader.gif' />"
  });

  // Video sound
  $("a.sound").click(function() {
    if(ytplayer) {
      if($(this).hasClass('off')) {
        unMuteVideo();
      } else {
        muteVideo();
      }
    }
  });

  // Scroll links
  $("#title .back a").click(function() {
    $("#container .scroller").animate({left: '+=500'},500, function() {
      leftVal = parseInt($(this).css('left').replace(/px$/, ""));
      // Animation complete
      scrolling = false;

      if(leftVal > 0) {
        $(this).css({'left':'0'});
      }
    });
    return false;
  });

  $("#title .forward a").click(function() {
    $("#container .scroller").animate({left: '-=500'},500, function() {
      leftVal = parseInt($(this).css('left').replace(/px$/, ""));
      maxVal = $(this).width() -$(this).children('div:last').width() -10;
      // Animation complete
      scrolling = false;

      if(leftVal < -maxVal) {
        $(this).css({'left':'-'+maxVal+'px'});
      }
    });
    return false;
  });

  // Spring/Summer 2010
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player",
    "player", "100%", "100%", "8", null, null, {allowScriptAccess:"always", menu:"false", wmode:"transparent"}, {id:"player"});

  // Scrolling
  $("#container .scroller").draggable({axis: 'x', cursor: 'move', stop: function(event, ui) {
      leftVal = parseInt($(this).css('left').replace(/px$/, ""));
      maxVal = $(this).width() -$(this).children('div:last').width() -10;
      if(leftVal > 0) {
        $(this).css({'left':'0'})
      } else if(leftVal < -maxVal) {
        $(this).css({'left':'-'+maxVal+'px'})
      }
    }
  });

  $("body.collections").mousewheel(function(event, delta) {
    if(!scrolling) {
      scrolling = true;
      if(delta > 0) {
        // mousewheel is going down
        $("#container .scroller").animate({left: '+=500'},500, function() {
          leftVal = parseInt($(this).css('left').replace(/px$/, ""));
          // Animation complete
          scrolling = false;

          if(leftVal > 0) {
            $(this).css({'left':'0'});
          }
        });
      } else {
        // mousewheel is going up
        $("#container .scroller").animate({left: '-=500'},500, function() {
          leftVal = parseInt($(this).css('left').replace(/px$/, ""));
          maxVal = $(this).width() -$(this).children('div:last').width()-10;
          // Animation complete
          scrolling = false;

          if(leftVal < -maxVal) {
            $(this).css({'left':'-'+maxVal+'px'});
          }
        });
      }
    }
    return false;
  });
});

// Cufon replacement
function runCufon() {
  Cufon.replace('h1', {});
  Cufon.replace('h2', {});
}

