var section ="";
var ms;
var shd;
var scd;
var sald;
var skt;
var ssi;
var ssm6;
var ssnk;
//var sscp;

var playerEdito;
var playerEvenements;
var playerNickel;
var playerCampus;

var currentLink;


var urlBase = "http://rentree-canalsat-2011.fr";

function loadContent(divToHide,divToShow,divToScroll,pageToLoad,onInit,onComplete)
{
	section="";
	$('#'+divToShow).load(pageToLoad, function(responseText, textStatus, XMLHttpRequest) {
		if(onInit) onInit();
		goto(divToHide,divToShow,divToScroll,onComplete);
	}).error(function(e){alert("error")});

}

function goto(divToHide,divToShow,divToScroll,onComplete)
{
	//alert($("#"+divToShow).outerHeight());
	$("#"+divToHide).parent().css("height",$("#"+divToHide).outerHeight());
	
	$("#"+divToShow).css("z-index","3");
	$("#"+divToShow).css("position","absolute");
	$("#"+divToShow).css("top","0");
	$("#"+divToShow).css("left","0");
	
	$("#"+divToHide).css("z-index","2");
	$("#"+divToHide).css("position","absolute");
	$("#"+divToHide).css("top","0");
	$("#"+divToHide).css("left","0");
	
	if(ms && section =="diversite") ms.stopDiapo();
	if(shd && section =="hd") shd.stop();
	if(scd && section =="cinedemande") scd.stop();
	if(sald && section =="canlsatdemande") sald.stop();
	if(skt && section =="kinect") skt.stop();
	if(ssi && section =="interface") ssi.stop();
	if(ssm6 && section =="m6") ssm6.stop();
	if(ssnk && section =="nickelodeon") ssnk.stop();
	//if(sscp && section =="campus") sscp.stop();
		
	$.scrollTo( '#'+divToScroll, 400 );
	setTimeout(function(){showDiv(divToShow,onComplete);},800);
	hideDiv(divToHide);
}

function showDiv(theDiv,onComplete)
{
	$("#"+theDiv).parent().animate({
	    height: $("#"+theDiv).outerHeight()
	  },400, function() {
	    // Animation complete.
	  });
	
	
	$("#"+theDiv).delay(400).fadeIn(800, function() {
		if(onComplete) onComplete();
		$("#"+theDiv).css("z-index","2");
      });
}

function hideDiv(theDiv)
{
	$("#"+theDiv).fadeOut(800, function() {
		$("#"+theDiv).css("z-index","1");
      });
	
	/*$("#"+theDiv).animate({
		    opacity: 0
		  }, 800, function() {
			  $("#"+theDiv).css("z-index","1");
		  });*/
}

function _onScrollerClick(e)
{
	e.preventDefault(); //empécher le comportement normal du click
	var target = $(e.currentTarget); // $(this);
	$.scrollTo( '#menu', 800 );
}

function _onClickMenuItem(e)
{
	e.preventDefault();
	var attr = $(e.currentTarget).attr("id");
	var link = attr.substring(5,attr.length);

	var target = $("#"+link);
	//alert(target);
	//alert("goooo !");
	var decal = $("#"+link).offset().top;
	//alert(decal);
	//$('html,body').animate({scrollTop: $("#"+link).offset().top},'slow');
	//alert($(window));
	//$(window).animate({scrollTop: $("#"+link).offset().top},'slow');
	$.scrollTo( '#'+link, 800 );
	//$(...).scrollTo( $('#chaines'), 800 );
	
	/*if(playerEsprit)
	{
		console.log(playerEsprit.video.src);
		playerEsprit.video.src = "http://video-js.zencoder.com/oceans-clip.mp4";
		playerEsprit.load();
		playerEsprit.play();
	}*/
}

function _onClickTg(e)
{
	e.preventDefault();
	var link = $(e.currentTarget).attr("rel");
	$.scrollTo( '#'+link, 800 );
}

function generateSlideshow()
{
	section="diversite";
	//console.log("generateSlideshow ... ");
	ms = new MultiSlideshow("diapos-chaines");
	ms.init();
}

function startSlideshow(value)
{
	//console.log("startSlideshow : "+value+(ms));
	if(ms)ms.startDiapo(value);
}
function addPlayerEvenements()
{
	//playerEvenements
	playerEvenements = VideoJS.setup("video_evenements", {
   	  controlsBelow: false, // Display control bar below video instead of in front of
         controlsHiding: true, // Hide controls when mouse is not over the video
         defaultVolume: 0.85, // Will be overridden by user's last volume if available
         flashVersion: 9, // Required flash version for fallback
         linksHiding: true // Hide download links when video is supported
     });
}
function addPlayerNickelodeon()
{
	//playerEvenements
	playerNickel = VideoJS.setup("video_nickel", {
   	  controlsBelow: false, // Display control bar below video instead of in front of
         controlsHiding: true, // Hide controls when mouse is not over the video
         defaultVolume: 0.85, // Will be overridden by user's last volume if available
         flashVersion: 9, // Required flash version for fallback
         linksHiding: true // Hide download links when video is supported
     });
}

function addPlayerEsprit()
{
	VideoJS.DOMReady(function(){
     playerEsprit = VideoJS.setup("video_esprit", {
    	  controlsBelow: false, // Display control bar below video instead of in front of
          controlsHiding: true, // Hide controls when mouse is not over the video
          defaultVolume: 0.85, // Will be overridden by user's last volume if available
          flashVersion: 10, // Required flash version for fallback
          linksHiding: true // Hide download links when video is supported
      });
     
     //if(autoPlay) playerEsprit.play();
    });
	
}

function addPlayerCampus()
{
	VideoJS.DOMReady(function(){
     playerCampus = VideoJS.setup("video_campus", {
    	  controlsBelow: false, // Display control bar below video instead of in front of
          controlsHiding: true, // Hide controls when mouse is not over the video
          defaultVolume: 0.85, // Will be overridden by user's last volume if available
          flashVersion: 10, // Required flash version for fallback
          linksHiding: true // Hide download links when video is supported
      });
    });
	
}

function addPlayerEdito()
{
	VideoJS.DOMReady(function(){
     playerEdito = VideoJS.setup("video_edito", {
    	  controlsBelow: false, // Display control bar below video instead of in front of
          controlsHiding: true, // Hide controls when mouse is not over the video
          defaultVolume: 0.85, // Will be overridden by user's last volume if available
          flashVersion: 9, // Required flash version for fallback
          linksHiding: true // Hide download links when video is supported
      });
     
    });
	
	
	// Must come after the video.js library

    // Add VideoJS to all video tags on the page when the DOM is ready
   // VideoJS.setupAllWhenReady();

    /* ============= OR ============ */

    // Setup and store a reference to the player(s).
    // Must happen after the DOM is loaded
    // You can use any library's DOM Ready method instead of VideoJS.DOMReady

    /*
    VideoJS.DOMReady(function(){
      
      // Using the video's ID or element
      var myPlayer = VideoJS.setup("example_video_1");
      
      // OR using an array of video elements/IDs
      // Note: It returns an array of players
      var myManyPlayers = VideoJS.setup(["example_video_1", "example_video_2", video3Element]);

      // OR all videos on the page
      var myManyPlayers = VideoJS.setup("All");

      // After you have references to your players you can...(example)
      myPlayer.play(); // Starts playing the video for this player.
    });
    */

    /* ========= SETTING OPTIONS ========= */

    // Set options when setting up the videos. The defaults are shown here.

    /*
    VideoJS.setupAllWhenReady({
      controlsBelow: false, // Display control bar below video instead of in front of
      controlsHiding: true, // Hide controls when mouse is not over the video
      defaultVolume: 0.85, // Will be overridden by user's last volume if available
      flashVersion: 9, // Required flash version for fallback
      linksHiding: true // Hide download links when video is supported
    });
    */

    // Or as the second option of VideoJS.setup
    
    /*
    VideoJS.DOMReady(function(){
      var myPlayer = VideoJS.setup("example_video_1", {
        // Same options
      });
    });
    */
}

function generateSlideshowHD()
{
	section="hd";
	shd = new Slideshow($("#diapo-hd"));
}

function startSlideshowHD()
{
	shd.start();
}

function initSlider()
{
	//slider = new Slider($("#diapo-hd"),145);
}

function generateSlideshowCineDemande()
{
	section="cinedemande";
	scd = new Slideshow($("#diapo-cine-demande"));
}

function startSlideshowCineDemande()
{
	scd.start();
}
function generateSlideshowAlaDemande()
{
	section="canalsatdemande";
	sald = new Slideshow($("#diapo-a-la-demande"));
}
function startSlideshowAlaDemande()
{
	sald.start();
}

function generateSlideshowKinect()
{
	section="kinect";
	skt = new Slideshow($("#diapo-kinect"));
}

function startSlideshowKinect()
{
	skt.start();
}
function generateSlideshowInterface()
{
	section="interface";
	ssi = new Slideshow($("#diapo-nouvelle-interface"));
}

function startSlideshowInterface()
{
	ssi.start();
}

function generateSlideshowM6()
{
	section="m6";
	ssm6 = new Slideshow($("#diapo-m6"));
}

function startSlideshowM6()
{
	ssm6.start();
}

function generateSlideshowNickel()
{
	section="nickelodeon";
	ssnk = new Slideshow($("#diapo-nickelodeon"));
}

function startSlideshowNickel()
{
	ssnk.start();
}

function generateSlideshowCampus()
{
	section="campus";
	sscp = new Slideshow($("#diapo-campus"));
}

function startSlideshowCampus()
{
	sscp.start();
}

function updateTitle(str,target)
{
	$("#dynamic-title").fadeOut(400, function() {
		$("#dynamic-title").html(str);
		$("#dynamic-title").delay(200).fadeIn(400);
      });
	if(currentLink)
	{
		currentLink.removeClass("active");
	}
	currentLink = $("#"+target);
	currentLink.addClass("active");
}

function changeVideoPlayer(videoName,posterName)
{
	var str = '';
	
	str += '<div class="video-js-box">';
		str += '<video id="video_esprit" width="429" height="293" class="video-js" controls="controls" preload="auto" poster="'+posterName+'" autoplay="autoplay" >';
		str += '<source src="/videos/mp4/'+videoName+'.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />';
		str += '<source src="/videos/webm/'+videoName+'.webm" type=\'video/webm; codecs="vp8, vorbis"\' />';
		str += '<source src="/videos/ogv/'+videoName+'.ogv" type=\'video/ogg; codecs="theora, vorbis"\' />';
		str += '<object id="flash_fallback_1" class="vjs-flash-fallback" width="429" height="293" type="application/x-shockwave-flash" data="swf/player.swf">';
			str += '<param name="movie" value="swf/player.swf" />';
			str += '<param name="allowfullscreen" value="true" />';
			str += '<param name="wmode" value="transparent" />';
			str += '<param name="flashvars" value="poster='+posterName+'&videoUrl=/videos/f4v/'+videoName+'.f4v&hover=0x004493&autoPlay=true" />';
		str += '</object>';
	str += '</div>';	
	
	$(".player-video-429").html(str);
	
	addPlayerEsprit();
	
}
