// Video gallery machine
var videoID = "";
function insertVideo(videoID){
  var container = '<object style="height: 390px; width: 640px">'+
    '<param name="movie" value="http://www.youtube.com/v/'+videoID+'?enablejsapi=1&version=3">'+
    '<param name="allowFullScreen" value="true">'+
    '<param name="allowScriptAccess" value="always">'+
    '<embed src="http://www.youtube.com/v/'+videoID+'?enablejsapi=1&version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390">'+
  '</object>';
  $("#overlay").append("<div id=\"video\">"+container+"<a id=\"MoveMoreToday\"></a><a id=\"close\"></a></div>");
}
$(document).ready(function () {
  // Start carousel
  $('.infiniteCarousel').infiniteCarousel();
  
  $(".infiniteCarousel .wrapper a").click(function(){
    var height = screen.height;
    insertVideo($(this).attr("rel"));
    $("#overlay").height(height);
    $("#overlay").fadeIn("fast");
  });
  $("#close").live("click", function(){
    $("#video").remove();
    $("#overlay").fadeOut("fast");
  });
  
  // Hide/show video gallery
  var galleryStatus = "closed";
  var gallerySpeed = 400;
  var galleryFade = 300;
  $("#videos-toggle").click(function(){
    if(galleryStatus=="closed"){
      // Change wrapper class
      $("#wrap-videos").addClass("closed").removeClass("open");
      // Fade out wrapper and arrows
      $("#videos .wrapper").fadeOut(galleryFade);
      $("#videos .arrow").fadeOut(galleryFade);
      // Move down videos
      $("#videos").animate({
        "margin-bottom": "0px"
      }, gallerySpeed);
      // Move down wrapper
      $("#wrap-videos").animate({
        "margin-bottom": "-130px"
      }, gallerySpeed);
      galleryStatus = "open";
      // Change button
      $("#videos-toggle").removeClass("open").addClass("closed");
    } else {
      // Change wrapper class
      $("#wrap-videos").addClass("open").removeClass("closed");
      // Move down videos
      $("#videos").animate({
        "margin-bottom": "130px"
      }, gallerySpeed);
      // Move down wrapper
      $("#wrap-videos").animate({
        "margin-bottom": "0px"
      }, gallerySpeed);
      // Delay the loading for 0.3s
      setTimeout(function(){
        $("#videos .wrapper").fadeIn(galleryFade);
        $("#videos .arrow").fadeIn(galleryFade);
        $("#videos").css("margin-bottom", "130px");
      }, 300);
      galleryStatus = "closed";
      // Change button
      $("#videos-toggle").removeClass("closed").addClass("open");
    }
  });
  // Close gallery at the beginning
  $("#videos-toggle").click();
});

