$(function(){

  //slideshow
  $("#slides").slides({
      height: 291,
	   responsive: true,
	   navigation:false,
	   pagination: true,
	   fade: { interval: 0 },
	   slide: { interval: 500 }
  });


  
  $("#slideshow .thumbnail a").click(function(e){
    e.preventDefault();
    var _this = $(this).parent().index();
    $(".slidesPagination li a").filter(function(){
      return parseInt($(this).text()) == _this;
    }).click();
        
    $("#slideshow .thumbnail a").removeClass("current").filter($(this)).addClass("current");
  }).filter(":first").addClass("current");

  $("#home-onair-slider").slides({
      height: 246,
      width:300,
      autoHeight:true,
	   navigation:true,
      responsive: false,
	   pagination: false,
	   fade: { interval: 0 },
	   slide: { interval: 500 }
  });

  Cufon.replace(".thumb-hover, ul#shows-index li a, h1, h2, h3, h4, h5, button, a.button, nav, #showreel-btn, #heroimage a, #slideshow #slideshow-info", {hover:true});
  
  /* Jquery Tools overlay programmatic call */ 
  if($(".launch-overlay").size()){

    $(".launch-overlay").overlay({
      onLoad: function(){
          $("#gallery-overlay").css({
               marginLeft: -($("#gallery-overlay").outerWidth() / 2),
               marginTop: -($("#gallery-overlay").outerHeight() / 2)
            });
      },
      onClose: function(){
        
        //remove the selected class from current thumb
        $("li.selected").removeClass();        
        
      }
    
    });
  }
  
  //because IE7 won't do this with CSS
  $("ul#shows-index li:nth-child(3n+3), ul#gallery-images li:nth-child(5n+5)").css({marginRight: 0}).next().addClass("clear");

 
  /* Video media element */
 
 $("video").each(function(){
    
    
    
    if(	navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)){
    	    var video = new MediaElementPlayer($(this), {
    	        success: function (video) {
    	            $(".video-overlay .close").live("click", function(){
    	                video.pause();
    	            });
                }
    	    });
    	} else {
    	    var video = new MediaElementPlayer($(this), { 
    	        mode: 'shim', 
    	        success: function (video) {
    	            video.play();
		
        					$(".video-overlay .close").live("click", function(){
					
    	                video.pause();
    	            });
                }
            });
        }
  });
  
   
  /* ----------  Gallery overlay ---------- */

    $("ul#gallery-images li a").fancybox({
      
      cyclic: true,
      autoScale: false,
      type: 'image'
      
    });
      
      



/*

Old...

  $("ul#gallery-images li").click(function(){
      
    var img = $(this).addClass("selected").find("img");
    $("#gallery #gallery-viewer").html(img.clone());
    $("p#gallery-caption").text(img.attr("alt"));

    
      
  });
  
  //prev/next image
  
  $("#gallery a#next-image, #gallery #gallery-viewer").live("click", function(){    nextGalleryImg(); 
  });
  
  $("#gallery a#prev-image").click(function(){  prevGalleryImg();  });
  
});



function setGalleryImg(img){

    $("div#gallery-viewer").prepend(img.clone());
    $("div#gallery-viewer img").not(":first").css({
        position: "absolute",
        left: "50%",
        marginLeft: -($("div#gallery-viewer img").not(":first").width() / 2)
    }).hide(function(){
      
      $(this).remove();
      
      //size the overlay window appropriately
      var viewerheight = $("#gallery-viewer img").height();
      $("#gallery-viewer").css({ height: viewerheight});
    });
    
    
  
}

function nextGalleryImg(){
    
  if($("ul#gallery-images li.selected").next().html()){//if we're not at the end

    var nextImg = $("ul#gallery-images li.selected").next().find("img");
    
    //add selected class
    $("ul#gallery-images li.selected").removeClass("selected").next().addClass("selected");
  }
  else{
     
    var nextImg = $("ul#gallery-images li:first").find("img");
    
    //add selected class
    $("ul#gallery-images li").removeClass("selected");
    $("ul#gallery-images li:first").addClass("selected");
  }
  
  setGalleryImg(nextImg);
}


function prevGalleryImg(){
    
  if($("ul#gallery-images li.selected").prev().html()){//if we're not at the end

    var prevImg = $("ul#gallery-images li.selected").next().find("img");
    
    //add selected class
    $("ul#gallery-images li.selected").removeClass("selected").prev().addClass("selected");
  }
  else{
       
    var prevImg = $("ul#gallery-images li:last").find("img");
    
    //add selected class
    $("ul#gallery-images li").removeClass("selected");
    $("ul#gallery-images li:last").addClass("selected");
  }
  
  setGalleryImg(prevImg);
}


$(window).resize(function(){
    $("#gallery-overlay").css({
       marginLeft: -($("#gallery-overlay").outerWidth() / 2),
       marginTop: -($("#gallery-overlay").outerHeight() / 2)
    });
    
    var viewerheight = $("#gallery-viewer img").height();
      $("#gallery-viewer").css({ height: viewerheight});
    
});
*/


//function to change the slide title and link after image has cycled
function changeName(){
  
  var title = $(this).attr("title");
  var link = $(this).html();
  $("#slide-title").html(title);
  $("#read-more").html(link);
  Cufon.replace("#slide-title, #read-more");
}


//EOF
});
