/**

Generic Slider
It takes 2 parameters : direction & size of arrows

It adds itself in the page according to css classes

Css classes available :

Horizontal slider :
* slider-hori-small
* slider-hori-big

Vertical slider :
* slider-verti-small
* slider-verti-big

**/

//list of classes available
var slider_classes=new Array('slider-verti-small','slider-verti-big','slider-hori-big','slider-hori-small', 'slider-hori-small-img','slider-vert-model');
var valueClassOff='_off';

window.addEvent('domready',function(){
//load the slider on the page if there's a classname called slider-x-x
	//foreach classname
	for(var j=0;j<slider_classes.length;j++){
		var elementWithClassName=$$('.'+slider_classes[j]);
		//init the slider with the good classname
		if(elementWithClassName[0]!='' && elementWithClassName[0]!=undefined ){
			init_slider(slider_classes[j]);
		}
	}
}
);


function sliderize_afterAjax(element){
	for(var j=0;j<slider_classes.length;j++){
		var elementWithClassName=$$('#'+element+' .'+slider_classes[j]);
		//init the slider with the good classname
		if(elementWithClassName[0]!='' && elementWithClassName[0]!=undefined ){
			if(element=="content_bloc_ajax"){
				init_slider_ideeCadeaux(element);
			}else{
				init_slider(slider_classes[j]);
			}
		}
}
}




function init_slider_ideeCadeaux(element){
		
	var elementWithClassName=$$('#'+element+' .parent_slider-hori-small');
	var elementWithClassName2=$$('#'+element+' .slider-hori-small');
	
	bouton_top="button_right";
	bouton_bottom="button_left";
	create_buttons_hori(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom);
			 	
}


//Construct the slider by the classname
function init_slider(classname){
	
	var bouton_top='';	
	var bouton_bottom='';	
		
	var elementWithClassName=$$('.parent_'+classname);
	var elementWithClassName2=$$('.'+classname);
	
	
	//launch the function appropriate
		switch (classname)
		{
			//Vertical sliders -->
			case 'slider-verti-small':
				bouton_top="button_top";
				bouton_bottom="button_bottom";
				create_buttons_verti(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom);
			break;
			
			case 'slider-verti-big':
				bouton_top="button_top";
				bouton_bottom="button_bottom";
				create_buttons_verti(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom);
			break;
			//<--Endof Vertical Sliders
			
			//Horizontal sliders -->
			case 'slider-hori-big':
				bouton_top="button_right";
				bouton_bottom="button_left";
				create_buttons_hori(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom);
			break;
			case 'slider-hori-small':
				bouton_top="button_right";
				bouton_bottom="button_left";
				create_buttons_hori(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom);
			break;
			case 'slider-vert-small':
				bouton_top="button_top";
				bouton_bottom="button_bottom";
				create_buttons_verti(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom);
			break;
			//<-- endof horizontal sliders
			
		}

}

// Adds the buttons in the parent div for the vertical slider
function create_buttons_verti(elementWithClassName,elementWithClassName2,bouton_top,bouton_bottom) {

	for(var i=0;i<elementWithClassName.length;i++){
		
		var sonOfElement=elementWithClassName[i].getChildren();
		var divContainerImage=sonOfElement[0].getChildren()[0].getChildren()[0];
		if(divContainerImage !=undefined){
			var image=sonOfElement[0].getChildren()[0].getChildren()[0].getChildren()[0];
			var nbr_of_images_inDiv=sonOfElement[0].getChildren()[0].getChildren().length;
			if(image.offsetWidth>0){
				var nbr_of_images_byStep=Math.floor((sonOfElement[0].offsetHeight*sonOfElement[0].offsetWidth)/((image.offsetWidth+10)*(image.offsetHeight+10)));
				
				if(nbr_of_images_inDiv>nbr_of_images_byStep){
					
					bouton_final_top="<input type=\"button\" class=\""+bouton_top+"\" onclick=\"slider_top('"+elementWithClassName2[i].id+"',this,0);\" onfocus=\"this.blur();\" />";
					bouton_final_bottom="<input type=\"button\" class=\""+bouton_bottom+valueClassOff+"\" onclick=\"slider_bottom('"+elementWithClassName2[i].id+"',this,0);\" onfocus=\"this.blur();\" />";
					elementWithClassName[i].innerHTML=elementWithClassName[i].innerHTML+bouton_final_bottom+bouton_final_top;
				}
			}
		}	
	}
}


//Adds the buttons in the parent div for the horizontal slider
function create_buttons_hori(elementWithClassName,elementWithClassName2,bouton_right,bouton_left) {
	for(var i=0;i<elementWithClassName.length;i++){
		
		var sonOfElement=elementWithClassName[i].getChildren();
		var divContainerImage=sonOfElement[0].getChildren()[0].getChildren()[0];
		if(divContainerImage !=undefined){
			var image=sonOfElement[0].getChildren()[0].getChildren()[0].getChildren()[0];
			var nbr_of_images_inDiv=sonOfElement[0].getChildren()[0].getChildren().length;
			var nbr_of_images_byStep=Math.round((sonOfElement[0].offsetWidth)/((image.offsetWidth+5)));
			if(image.offsetWidth>0){	
				if(sonOfElement[0].id=="ideesCadeaux"){
					if(sonOfElement[0].getChildren()[0].getChildren().length>=2){
						bouton_final_right="<input type=\"button\" class=\""+bouton_right+"\" onclick=\"slider_right('"+elementWithClassName2[i].id+"',this,0);\" onfocus=\"this.blur();\" />";
						bouton_final_left="<input type=\"button\" class=\""+bouton_left+valueClassOff+"\" onclick=\"slider_left('"+elementWithClassName2[i].id+"',this,0);\" onfocus=\"this.blur();\" />";
						
						elementWithClassName[i].innerHTML=elementWithClassName[i].innerHTML+bouton_final_left+bouton_final_right;
					}
				}
				else if(nbr_of_images_inDiv>nbr_of_images_byStep){
					bouton_final_right="<input type=\"button\" class=\""+bouton_right+"\" onclick=\"slider_right('"+elementWithClassName2[i].id+"',this,0);\" onfocus=\"this.blur();\" />";
					bouton_final_left="<input type=\"button\" class=\""+bouton_left+valueClassOff+"\" onclick=\"slider_left('"+elementWithClassName2[i].id+"',this,0);\" onfocus=\"this.blur();\" />";
					
					elementWithClassName[i].innerHTML=elementWithClassName[i].innerHTML+bouton_final_left+bouton_final_right;
				}
			}
		}
	}
}

//function to Slide_up
function slider_top(id,button,counter){
	
	var elementToScroll=document.getElementById(id);
	
	//Calculate the number of possible scrolls
	var height=calcul_height_div(id);
	//Get the class of the button
	var classOrigine=button.className;
	
	//Get the previous element in the DOM
	var bottomButton=button.previousSibling;
	//If the counter < the number of possible scrolls increment the counter
	if (counter<height) {

		counter++;
		//Scroll the div
		new Fx.Scroll(elementToScroll.id).scrollTo(0,elementToScroll.offsetHeight*counter);
		
		button.onclick=function(){slider_top(id,this,counter);};
		bottomButton.onclick=function(){slider_bottom(id,this,counter);};
		
	}

	var classValue=bottomButton.className;
	var lengthOfClass=classValue.length-valueClassOff.length;
	
	// If there's the _off suffix in the classname of previous button
	// -> delete it 
	if (classValue.match(valueClassOff)!=null){
		bottomButton.className=classValue.substr(0,lengthOfClass);
	}
	
	//If it can't scroll anymore add the _off suffix to the button's classname
	if (counter==height && classOrigine.match(valueClassOff)==null){
		button.className=classOrigine+valueClassOff;
	}
}

//function to slide Down
function slider_bottom(id,button,counter){
	var elementToScroll=document.getElementById(id);
	//Get the class of the button
	var classOrigine=button.className;
	//Get the previous element in the DOM
	var topButton=button.nextSibling;

	
	//Calculate the number of possible scrolls
	var height=calcul_height_div(id);
	
	//If the counter >0 decrement the counter
	if (counter>0) {
		counter--;
		//Scroll the div
		new Fx.Scroll(elementToScroll.id).scrollTo(0,elementToScroll.scrollTop-elementToScroll.offsetHeight);
		
		topButton.onclick=function(){slider_top(id,this,counter);};
		button.onclick=function(){slider_bottom(id,this,counter);};
	}
	
	
	var classValue=topButton.className;
	var lengthOfClass=classValue.length-valueClassOff.length;

	// If there's the _off suffix in the classname of previous button
	// -> delete it 
	if (classValue.match(valueClassOff)!=null){
		topButton.className=classValue.substr(0,lengthOfClass);
	}
	
	//If it can't scroll anymore add the _off suffix to the button's classname
	if (counter==0 && classOrigine.match(valueClassOff)==null)  {
		button.className=classOrigine+valueClassOff;
	}
}

function slider_right(id,button,counter){
	var elementToScroll=document.getElementById(id);
	//Calculate the number of possible scrolls
	var possibleScrolls=calcul_width_div(id);
	//Get the class of the button
	var classOrigine=button.className;
	//Get the previous element in the DOM
	var leftButton=button.previousSibling;
	
	//If the counter < the number of possible scrolls increment the counter
	if (counter<possibleScrolls) {
		counter++;
		//Scroll the div
		if(elementToScroll.id=='ideesCadeaux')
		{
			new Fx.Scroll(elementToScroll.id).scrollTo((235)*counter,0);
		}
		else
		new Fx.Scroll(elementToScroll.id).scrollTo((elementToScroll.offsetWidth-2)*counter,0);
		button.onclick=function(){slider_right(id,this,counter);};
		leftButton.onclick=function(){slider_left(id,this,counter);};
	}

	var classValue=leftButton.className;
	var lengthOfClass=classValue.length-valueClassOff.length;
	
	// If there's the _off suffix in the classname of previous button
	// -> delete it 
	if (classValue.match(valueClassOff)!=null){
		leftButton.className=classValue.substr(0,lengthOfClass);
	}

	// If it can't scroll anymore add the _off suffix to the button's classname
	if (counter==possibleScrolls && classOrigine.match(valueClassOff)==null){
		button.className=classOrigine+valueClassOff;
	}
}


function slider_left(id,button,counter){
	var elementToScroll=document.getElementById(id);
	//Calculate the number of possible scrolls
	var possibleScrolls=calcul_width_div(id);

	//Get the class of the button
	var classOrigine=button.className;
	//Get the previous element in the DOM
	var rightButton=button.nextSibling;
	
	//If the counter < the number of possible scrolls increment the counter
	if (counter>0) {
		counter--;
		//Scroll the div
		if(elementToScroll.id=='ideesCadeaux')
		{
			new Fx.Scroll(elementToScroll.id).scrollTo((235)*counter,0);
		}
		else
		new Fx.Scroll(elementToScroll.id).scrollTo(elementToScroll.scrollLeft-(elementToScroll.offsetWidth-2),0);
		
		rightButton.onclick=function(){slider_right(id,this,counter);};
		button.onclick=function(){slider_left(id,this,counter);};
	}

	var classValue=rightButton.className;
	var lengthOfClass=classValue.length-valueClassOff.length;
	
	// If there's the _off suffix in the classname of previous button
	// -> delete it 
	if (classValue.match(valueClassOff)!=null){
		rightButton.className=classValue.substr(0,lengthOfClass);
	}

	//If it can't scroll anymore add the _off suffix to the button's classname
	if (counter==0 && classOrigine.match(valueClassOff)==null){
		button.className=classOrigine+valueClassOff;
	}
}

//Returns the number of possible scrolls for verical sliders
function calcul_height_div(id) {
	
	var elementToScroll=document.getElementById(id);
	var divContainer = document.getElementById(id).getElementsByTagName('div')[0];
	var listOfItems=divContainer.getElementsByTagName('img');
	var numberOfItems=listOfItems.length;
	var widthOfImages=listOfItems[0].width+10;
	var numberOfItemByLine=Math.round(elementToScroll.offsetWidth/widthOfImages);
	
	var numberOfItemByRow=Math.ceil(numberOfItems/numberOfItemByLine);
	
	var int=elementToScroll.offsetHeight/widthOfImages;

	var counter=Math.ceil(numberOfItemByRow/int-1);

	return counter;	
}

//Returns the number of possible scrolls for horizontal sliders
//3SITODO : If it's necessary to make a horizontal slider on many lines,review the counter calculation
function calcul_width_div(id) {
	var elementToScroll=document.getElementById(id);
	
	if(elementToScroll.id=='ideesCadeaux'){
		var divContainer = document.getElementById(id).getElementsByTagName('div')[0];
		var i = 0;
        var numberOfItems2=0;
        var width_div_article=0;
        var div = divContainer.getElementsByTagName("div");
        while (element = div[i++]) {
             if (element.className == "content_hl") {
            	 numberOfItems2++;
             }
        }
		var width_div_article=235;
		var numberOfItemByRow=Math.round(elementToScroll.offsetHeight/width_div_article);
		
		var numberOfItemByLine=Math.round(elementToScroll.offsetWidth/width_div_article);
		
		divContainer.style.width=Math.round((numberOfItems2*width_div_article)/numberOfItemByRow)+'px';
		
		var counter=Math.round(numberOfItems2/numberOfItemByLine-1);
		
		divContainer.style.width=numberOfItems2*(20+width_div_article);
		
		return counter;
	}
	
	
	
	var divContainer = document.getElementById(id).getElementsByTagName('div')[0];
	var listOfItems=divContainer.getElementsByTagName('img');
	var numberOfItems=listOfItems.length;
	var widthOfImages=listOfItems[0].width+5;

	if(elementToScroll.id=='newCreators'){
		divContainer.style.width="1224px";
		var counter=3;
		return counter;
	}

	var numberOfItemByLine=Math.ceil(elementToScroll.offsetWidth/widthOfImages);
	
	divContainer.style.width=Math.ceil((numberOfItems*widthOfImages))+'px';
	
	var counter=Math.ceil(numberOfItems/numberOfItemByLine-1);
	
	return counter;	
}


//function to Slide_up
function slider_top_simple(id, topButton, bottomButton,counter){
	
	var elementToScroll=document.getElementById(id);
	
	//Calculate the number of possible scrolls
	var height=calcul_height_div(id);
	//Get the class of the button
	var classOrigine = topButton.className;
	
	//If the counter < the number of possible scrolls increment the counter
	if (counter<height) {

		counter++;
		//Scroll the div
		new Fx.Scroll(elementToScroll.id).scrollTo(0,elementToScroll.offsetHeight*counter);
		
		topButton.onclick=function(){slider_top_simple(id,this, bottomButton, counter);};
		bottomButton.onclick=function(){slider_bottom_simple(id, topButton, this, counter);};
		
	}

	var classValue=bottomButton.className;
	var lengthOfClass=classValue.length-valueClassOff.length;
	
	// If there's the _off suffix in the classname of previous button
	// -> delete it 
	if (classValue.match(valueClassOff)!=null){
		bottomButton.className=classValue.substr(0,lengthOfClass);
	}
	
	//If it can't scroll anymore add the _off suffix to the button's classname
	if (counter==height && classOrigine.match(valueClassOff)==null){
		topButton.className=classOrigine+valueClassOff;
	}
}

//function to slide Down
function slider_bottom_simple(id, topButton, bottomButton,counter){
	var elementToScroll=document.getElementById(id);
	//Get the class of the button
	var classOrigine = bottomButton.className;

	
	//Calculate the number of possible scrolls
	var height=calcul_height_div(id);
	
	//If the counter >0 decrement the counter
	if (counter>0) {
		counter--;
		//Scroll the div
		new Fx.Scroll(elementToScroll.id).scrollTo(0,elementToScroll.scrollTop-elementToScroll.offsetHeight);
		
		topButton.onclick=function(){slider_top_simple(id, topButton, this,counter);};
		bottomButton.onclick=function(){slider_bottom_simple(id, this, bottomButton, counter);};
	}
	
	
	var classValue=topButton.className;
	var lengthOfClass=classValue.length-valueClassOff.length;

	// If there's the _off suffix in the classname of previous button
	// -> delete it 
	if (classValue.match(valueClassOff)!=null){
		topButton.className=classValue.substr(0,lengthOfClass);
	}
	
	//If it can't scroll anymore add the _off suffix to the button's classname
	if (counter==0 && classOrigine.match(valueClassOff)==null)  {
		bottomButton.className=classOrigine+valueClassOff;
	}
}

