// JavaScript Document

$(document).ready(function () {
	$("#content table tr:nth-child(odd) td").css("background-color", "#bab5b1");
	
	$("#content p,#content li,#content h1,#content h2").each(function(){
		$(this).html($(this).html().replace(/ (\S+)$/,'&nbsp;$1'));
		/*h2all = $(this).html();
		h2a = h2all.slice(0, h2all.lastIndexOf(' '));
		h2b = ' ' + h2all.slice(h2all.lastIndexOf(' ')+1);
		$(this).html(h2a + h2b);*/
	});
	
	$("#infobesteel area").mouseover(
		function(){
			var nom=$(this).attr("href").substring(1);
			$("#"+nom).fadeIn(250);
			$("#carte div").not($("#"+nom)).fadeOut(250);

			return false;
		}
	);
	
	
	
	$("#carte div").hover(
		function(){
			//$(this).show();
		},
		function(){
			//$("#carte div").hide();
			$(this).fadeOut(250);
		}
	);
	
	$("div.top-portion blockquote,div.bottom-portion blockquote").corner("10px");
	
	
	var currentPosition = 0;
	var slideH = 286;
	var slides = $('#images img');
	var numberOfSlides = slides.length;
	//$("#footer").append(slideH * numberOfSlides);
	
	$('#images-inner').css('width', (slideH * numberOfSlides)*2);
	$('div.show').css('width', slideH * numberOfSlides);
	//alert($("#images-inner").length)
	
	if($("#images-inner").length>0){	
		$('.control').bind('click',function(){
			bouge(this)
		});
		var cetteinterval=null;
		var cetteinterval=setInterval(autoBouge, 2000);
		//manageControls(currentPosition);
	}
	
	function bouge(ev){
		// Determine new position
		clearInterval(cetteinterval);
		
		currentPosition = ($(ev).attr('id')=='fl-bottom') ? currentPosition+1 : currentPosition-1;
		
		if(currentPosition==-1){
			$('div.show:first-child').clone().prependTo("#images-inner");
			$('div.show:first-child').css("margin-left",slideH * -numberOfSlides);
			
		}
		if(currentPosition==-1){
			currentPosition=15
		}
		
		$('div.show:first-child').animate({
			duration:200,
		  'marginLeft' : slideH*(-currentPosition)
  		})
		
		
		
		checkPositions();
		cetteinterval=setInterval(autoBouge, 2000);
	}
	
	function autoBouge(){
		currentPosition+=1;
		$('div.show:first-child').animate({
			duration:200,
		  'marginLeft' : slideH*(-currentPosition)
  		})
		
		//alert("a");
		checkPositions();
	}
	
	function checkPositions(){
		if(currentPosition==2){
			if($("div.show:only-child")){
			
			}else{
				$("div.show:first-child").remove();
			}
		}else if(currentPosition==12){
			if($("div.show").length>1){
				$("div.show:last-child").remove();
			}
		
		}else if(currentPosition==14){
			if($("div.show").length==1){
				//alert("yeah");
				$('div.show').clone().appendTo("#images-inner");
				$('div.show:last-child').css("margin-left",0);
			}
			
		}else if(currentPosition==17){
			//clearInterval(cetteinterval);
			if($("div.show").length>1){
				$("div.show:first-child").remove();
				$('div.show:first-child').css('margin-left', 0);
				currentPosition=0;
				//autoBouge();
				//cetteinterval=setInterval(autoBouge, 2000);
			}
			
		}
	}
});
