//jquery Carrousel plugin
//Daniel Many, 2010
//Version 1.9

/*****************************
VARIABLES THAT CAN BE USED IN INSTANTIATION
int rotation_internal
int fade_in_time
int nb_promo
******************************/


var ROTATION_INTERVAL; //Carrousel auto-rotation in millisecond
var FADE_IN_TIME;
var NB_PROMO; //Nombre de promos maximum à afficher par défaut

var casinoList=["montreal","leamy","charlevoix","tremblant"];

var timer;	
var selectedUrl = "";
var textIsReady = true;
var carrouselObject;
var animCompleted = true;
var currentIndex;
var lastvisibleIndex;

(function($){  
  $.fn.carrousel = function(options) {  	 
	 var defaults = {	 	
	 	rotation_internal: 8000,
	 	fade_in_time: 2000,	 
	 	nb_promo: 3	 
	 	};
	 	
	 	var options = $.extend(defaults, options);
	   
     return this.each(function() {           	        	
         	ROTATION_INTERVAL = options.rotation_internal;
         	FADE_IN_TIME = options.fade_in_time;
         	NB_PROMO = options.nb_promo;
         	carrouselObject = $(this);  
			initCarrousel();       
     });  
  };  
})(jQuery); 


function initCarrousel(){	
	
		
	NB_PROMO = $(carrouselObject).find("li").size();
	//alert("NB_PROMO: " + NB_PROMO);
									
	$(carrouselObject).find(".promo").each(function(index){
		$(this).addClass("positionPromo");
		
	
		if (index < NB_PROMO) {
			//$(this).find("div").attr("id", "promo_txt" + index);
			
			var zIndex = NB_PROMO - index;
			$(this).css('z-index', zIndex);
						
			var thisImage = $(this).find("img");
			
			thisImage.addClass("positionPromo");	
			thisImage.css('z-index', zIndex);			
			thisImage.attr('rang', zIndex);
			thisImage.attr("id", "promo_" + index);
			
			//var thisMediaText = $(this).find("div"); 			
			//thisMediaText.css('z-index', zIndex + 1);
			//thisMediaText.attr('rang', zIndex + 1);
		}
		 
	});		
	
	$(carrouselObject).find("li img").addClass("unselectedThumb");
	$(carrouselObject).find("li img:first").removeClass("unselectedThumb");	
	$(carrouselObject).find("li img:first").addClass("selectedThumb");	
	
	$(carrouselObject).find("li img").each(function(index){
		if (index < NB_PROMO) {
			$(this).attr("id", index);
		}
	});		
			
	$(carrouselObject).find("ul").css("display", "block");
		
	$(carrouselObject).find("li img").mouseover(function() {					
		clearInterval(timer);									
		displayPromo($(this).attr("id"));						
		
	});	
	
	$(carrouselObject).find("li img").mouseout(function() {		
		setAutoRotate();						
	});		
	
	
	displayPromo(0);
	
	setAutoRotate();			
	
	
	
	/******************** Thumbs slider  *******************/
	var NB_VISBILE_THUMB = 3;
	lastvisibleIndex = NB_VISBILE_THUMB;
				
	if (NB_PROMO - lastvisibleIndex > 0) {	
		$("img#flecheDroite").css('display', 'block');
	}
	
	$("img#flecheDroite").mousedown(function(){					
		if (lastvisibleIndex < NB_PROMO){	
			$("img#flecheGauche").css('display', 'block');
			var currentTopPos = $("div#zonepromo").find("ul#thumbsList").css('left');		
			var newTopPos = parseInt(currentTopPos) - 120;		
		    $("div#zonepromo").find("ul#thumbsList").css('left', newTopPos+'px');
		    //$("div#zonepromo").find("ul#thumbsList").animate({left: newTopPos+'px'}, 1000);
		   
		    lastvisibleIndex++;
		}
	    if (lastvisibleIndex >= NB_PROMO){
	    	//$("img#flecheDroite").css('display', 'none');
	    }		
		
   	});

	$("img#flecheGauche").mousedown(function(){		
		//alert("lastvisibleIndex: " + lastvisibleIndex);
		if (lastvisibleIndex <= NB_PROMO && lastvisibleIndex > NB_VISBILE_THUMB){	
			$("img#flecheDroite").css('display', 'block');			
			var currentTopPos = $("div#zonepromo").find("ul#thumbsList").css('left');
			var newTopPos = parseInt(currentTopPos) + 120;		
	      	$("div#zonepromo").find("ul#thumbsList").css('left', newTopPos+'px'); 
	      	//$("div#zonepromo").find("ul#thumbsList").animate({left: newTopPos+'px'}, 1000);
	      	lastvisibleIndex--;	      
	    }
	    if (lastvisibleIndex <= NB_VISBILE_THUMB){
	    	//$("img#flecheGauche").css('display', 'none');
	    }
	   	
    });		
					
}


function displayPromo(selectedIndex) {	
	
	if (currentIndex != selectedIndex) {
		
		$(carrouselObject).find(".promo").each(function(index){
			
			var thisImage = $(this).find("img");
			var thisMediaText = $(this).find("div"); 
			
			if (index < NB_PROMO) {					
				if ($(carrouselObject).find("li img#" + index).is('.selectedThumb')) {			
					$(this).css('z-index', "96");
					thisImage.css('z-index', "96");
					//thisMediaText.css('z-index', "97");						
				} else if (index == selectedIndex) {		
					thisImage.css('display', 'none');
					$(this).css('z-index', "98");						
					thisImage.css('z-index', "98");
					//thisMediaText.css('display', 'none');
					//thisMediaText.css('z-index', "99");																				
				} else {
					$(this).css('z-index', thisImage.attr("rang"));
					thisImage.css('z-index', thisImage.attr("rang"));
					//thisMediaText.css('z-index', thisMediaText.attr("rang"));
				}										
			}				
		});	
			
		$(carrouselObject).find("li img").removeClass("selectedThumb");
		$(carrouselObject).find("li img").addClass("unselectedThumb");
		$(carrouselObject).find("li img#" + selectedIndex).removeClass("unselectedThumb");
		$(carrouselObject).find("li img#" + selectedIndex).addClass("selectedThumb");	

		currentIndex = selectedIndex;
		
		$(carrouselObject).find("img#visuelCasino").attr("src", "images/accueil/visuelCasino_" + casinoList[selectedIndex] + ".jpg");
		
		$("div.promo div").css('display', 'none');
		applyEffect(selectedIndex);
	}

}

/*Effect to apply to element(s) with a paticular index*/
function applyEffect(selectedIndex) {
	animCompleted = false;
	$(carrouselObject).find("img#promo_" + selectedIndex).fadeIn(FADE_IN_TIME, function(){		
		//$(carrouselObject).find("div#promo_txt" + selectedIndex).css({'height' : '0px', 'top' : '247px', 'display' : 'none'});
		//$(carrouselObject).find("div#promo_txt" + selectedIndex).animate({height: '62px',  top: '185px'}, 1000);		
		animCompleted = true;	
	});   	
}

function setAutoRotate(){	
	timer =  setInterval("rotatePub()", ROTATION_INTERVAL);
}			

function rotatePub(){

	var casinoIndex = 0;
	var rotated = false;		
	var lastIndex = $(carrouselObject).find("li img").size() - 1;
																	
	$(carrouselObject).find("li img").each(function(index){
																	
		if ($(this).is('.selectedThumb') && !rotated) {					
			
			if (index == lastIndex) {																	
				displayPromo($(carrouselObject).find("li img:first").attr("id"));
				casinoIndex = 0;
			} else {								
				displayPromo(index + 1);
				casinoIndex = index + 1;				
			}
			
			rotated = true;					
		}				
	});	
	
	$(carrouselObject).find("img#visuelCasino").attr("src", "images/accueil/visuelCasino_" + casinoList[casinoIndex] + ".jpg");			
	
}


