// Namespace
var LNAHAccueil = (typeof LNAHAccueil=="object") ? LNAHAccueil : {};
var currentResumeThumbs = new Array();

LNAHAccueil.initMediaWall = function()
{		 
	//Setup Media Wall panels
	jQuery("#boxAlaUne .panels").each(function(indx)
	{
	    var thumbs = jQuery(".thumb_wrapper");
		jQuery(thumbs[indx]).find(".thumb").attr("alt", currentResumeThumbs[indx]);
		jQuery(thumbs[indx]).find(".thumb").attr("title", currentResumeThumbs[indx]);
		jQuery(thumbs[indx]).find(".thumb_border").attr("alt", currentResumeThumbs[indx]);
		jQuery(thumbs[indx]).find(".thumb_border").attr("title", currentResumeThumbs[indx]);
		
		if(indx==0) return true;
		jQuery(this).hide();
		jQuery(this).parent().get(0).currentPanel = 0;
		jQuery("#boxAlaUne").get(0).nextPanel = null;
	});

	// Media Wall animation function
	LNAHAccueil.mainRotate = function(indx)
	{
		var length = jQuery("#boxAlaUne .panels").length;

	    var current = jQuery("#boxAlaUne .panels").parent().get(0).currentPanel;
	    var next = (current>=length-1) ? 0 : current+1;
	    var thumbs = jQuery(".thumb_wrapper");

        if(jQuery("#boxAlaUne").get(0).nextPanel!=null)
	        next = jQuery("#boxAlaUne").get(0).nextPanel;
		
		/*    
		if(next!=null && length-1>1) 
		{
		*/ 
		    var nextPanelObj = jQuery("#boxAlaUne .panels").eq(next);		
		    LNAHAccueil.mainRotate.currentPanel = nextPanelObj;
    		
		    jQuery(".thumb_border").attr("src",LNAHAccueil.mainRotate.off_state.src);
		    jQuery("#boxAlaUne .panels").eq(current).fadeOut("fast");

		    document.getElementById("titreAlaUne").innerHTML = LNAHAccueil.mainRotate.currentTitle[next];
		    document.getElementById("texteAlaUne").innerHTML = LNAHAccueil.mainRotate.currentText[next];
    		
		    jQuery(nextPanelObj).fadeIn("fast");
		    jQuery(thumbs[next]).find(".thumb_border").attr("src",LNAHAccueil.mainRotate.on_state.src);
    		
		    jQuery("#boxAlaUne .panels").parent().get(0).currentPanel = next;
		    jQuery("#boxAlaUne").get(0).nextPanel=null;
    		
		    LNAHAccueil.mainRotate.count++;
		    if(LNAHAccueil.mainRotate.count>150)
		    {
			    clearInterval(LNAHAccueil.mainRotate.heartbeat);				
		    }
		/*
		}
		*/
	}

	// Store current panel during rotation
	// to display the game status bars for
	LNAHAccueil.mainRotate.currentPanel = "";
	LNAHAccueil.mainRotate.currentTitle = new Array();
	LNAHAccueil.mainRotate.currentText = new Array();

	// Panel duration in milliseconds
	LNAHAccueil.mainRotate.rotationSpeed = 8000;

	// Keep track of media wall rotations
	LNAHAccueil.mainRotate.count = 0;

	// Start Media Wall Rotation
	LNAHAccueil.mainRotate.heartbeat = setInterval(LNAHAccueil.mainRotate, LNAHAccueil.mainRotate.rotationSpeed); 

	// Pre-Cache Thumbnail border images
	LNAHAccueil.mainRotate.on_state = new Image();
	LNAHAccueil.mainRotate.on_state.src = "images/thumb_border_on.gif";
	LNAHAccueil.mainRotate.off_state = new Image();
	LNAHAccueil.mainRotate.off_state.src = "images/thumb_border_off.gif";
	LNAHAccueil.mainRotate.on_pause = new Image();
	LNAHAccueil.mainRotate.on_pause.src = "images/bPause.gif";
	LNAHAccueil.mainRotate.off_pause = new Image();
	LNAHAccueil.mainRotate.off_pause.src = "images/bPlay.gif";

	// Thumbnails
	var offset=8;
	jQuery("#thumb_nav #thumbs .thumb_wrapper").each(function(indx)
	{
		this.leftPosition = offset;
		jQuery(this).css({"left":offset,"top":"14px"});
		offset+=86;
	});

	// Thumb - mouseover
	jQuery("#thumb_nav #thumbs .thumb_wrapper").hover(
		function()
		{
			var index = jQuery("#thumb_nav #thumbs .thumb_wrapper").index(this);
			jQuery(this).css({"z-index":"9999"})
			.animate({width:"106px",height:"80px",top:"-=7px",left:this.leftPosition-8}, { queue:false, duration:"fast" })
			.find(".thumb").animate({width:"96px",height:"72px",top:"-=7px",left:this.leftPosition-8}, { queue:false, duration:"fast" });
		},
		function()
		{
			var current=jQuery("#boxAlaUne .panels").parent().get(0).currentPanel;
			jQuery(this).css({"z-index":"1"});
			jQuery(this).animate({width:"85px",height:"57px",top:"14px",left:this.leftPosition}, { queue:false, duration:"fast"})
			.find(".thumb").animate({width:"73px",height:"49px",top:"0",left:this.leftPosition}, { queue:false, duration:"fast" });
		}
	);

	//Thumb événement sur clic
	jQuery("#thumb_nav #thumbs .thumb_wrapper").click(function()
	{
		LNAHAccueil.mainRotate.setPlayPause();
		clearInterval(LNAHAccueil.mainRotate.heartbeat);
		jQuery(".thumb_border").attr("src",LNAHAccueil.mainRotate.off_state.src);
		jQuery("#boxAlaUne").get(0).nextPanel = jQuery("#thumb_nav #thumbs .thumb_wrapper").index(this);
		LNAHAccueil.mainRotate();
		jQuery(this).find(".thumb_border").attr("src",LNAHAccueil.mainRotate.on_state.src);		
		var thumbIndex=jQuery("#thumb_nav #thumbs .thumb_wrapper").index(this);
		thumbIndex=(thumbIndex*1)+1;
	});

	// Bouton Précédent
	jQuery(".previous").hover(
		function(){ jQuery(this).css({ "background-position" :"0 -21px" }); },
		function(){ jQuery(this).css({ "background-position" :"0 0" }); }
	);
	jQuery(".previous").bind('click', function()
	{
		LNAHAccueil.mainRotate.setPlayPause();
		clearInterval(LNAHAccueil.mainRotate.heartbeat);
		var length = jQuery("#boxAlaUne .panels").length;
		var current = jQuery("#boxAlaUne .panels").parent().get(0).currentPanel;
		var prev = (current==0) ? length-1 : current-1;
		jQuery("#boxAlaUne").get(0).nextPanel = prev;
		LNAHAccueil.mainRotate();	
	});
	
	LNAHAccueil.mainRotate.playPause = 0;	
	LNAHAccueil.mainRotate.setPlayPause = function(origin)
	{
		if(LNAHAccueil.mainRotate.playPause==0) 
		{
			jQuery("#pauseImg").attr("src",LNAHAccueil.mainRotate.off_pause.src);	
			clearInterval(LNAHAccueil.mainRotate.heartbeat);
			LNAHAccueil.mainRotate.playPause=1;
		} 
		else if(origin=="pause")
		{
			jQuery("#pauseImg").attr("src",LNAHAccueil.mainRotate.on_pause.src);		
			LNAHAccueil.mainRotate.heartbeat = setInterval(LNAHAccueil.mainRotate, LNAHAccueil.mainRotate.rotationSpeed);
			LNAHAccueil.mainRotate.playPause=0;
		}
	}
	
	jQuery("#pause").bind('click', function() 
	{
		LNAHAccueil.mainRotate.setPlayPause('pause');
	});	
	
	// Bouton Suivant
	jQuery(".next").hover(
		function(){ jQuery(this).css({ "background-position" :"-48px -21px" }); },
		function(){ jQuery(this).css({ "background-position" :"-48px 0" }); }
	);
	jQuery(".next").bind('click', function()
	{
		LNAHAccueil.mainRotate.setPlayPause();
		clearInterval(LNAHAccueil.mainRotate.heartbeat);
		
		var length = jQuery("#boxAlaUne .panels").length;
		var current = jQuery("#boxAlaUne .panels").parent().get(0).currentPanel;
		var next = (current>=length-1) ? 0 : current+1;
		jQuery("#boxAlaUne").get(0).nextPanel = next;
		
		LNAHAccueil.mainRotate();	
	});
}