(function($) {
    $.fn.onThisVal = function()
    {
	var baseVal = $(this).attr('value');
	$(this).focusin(function(){
	    this.value='';
	});
	$(this).focusout(function(){
	    if(this.value=='') this.value=baseVal;
	});
    }
})(jQuery);

$(document).ready(function(){
	
    $('input[type="text"]').onThisVal();

    var currentPos	= 0;
    var currentPos2	= 0;
    var totWidth	= 0;
    var totWidth2	= 0;	
    var totSucre	= 0;
    var totSel	= 6;
    var totSucre2	= 0;
    var tailleProduits=0;
    var compteurProduits=0;
    var positions	= new Array();
    var positions2	= new Array();
	
    $('#galerie .cti, #carObj .ctii, #hist .realisation').each(function(i){
	totSucre+=1;
	positions[i]=totWidth;
	
	if($(this).attr('class')=='realisation')
	totWidth+=$(this).width()+62;
	else
	totWidth+=$(this).width();
	
	//console.log(i+' '+totSucre+' '+totWidth);
    });	
    $('#galerie, #carObj, #hist').width(totWidth);
	
    $('.th_thumbz').each(function(i){
	totSucre2+=1;
                
	positions2[i]=totWidth2;
	totWidth2+=$(this).width();
    });

    $('.produit').each(function(){
	tailleProduits+=$(this).width()+20;
	compteurProduits++;
    });

    $('.container').width(tailleProduits);
    

    $('#ctThumb div').width(totWidth2).css('position', 'absolute');
	
    var animation = function()
    {		

    
	var thisPoz = currentPos;
	
	$('.controle, .controle1, .controls').unbind('click', animation);
		
	if($(this).attr('class')!='th_thumbz')(this.id=='right' || this.id=='right1' || this.id=='navRight') ? currentPos+=1 : currentPos-=1;
	else currentPos=this.id.replace('th_','');
		
	if(thisPoz!=currentPos)
	{
	    console.log(thisPoz+' '+currentPos+' '+positions[currentPos]);
		
	    if(totSucre2==0)
		$('#galerie, #hist').stop().animate({
		    marginLeft:-positions[currentPos]+'px'
		    },1000,function()

		    {
		    $('.controle, .controle1, .controls').bind('click', animation);
		});
			
	    else if($(this).attr('class')=='th_thumbz')
	    {

                            
		$('#carObj').stop().animate({
		    marginLeft:-positions[currentPos]+'px'
		    },1000,function()

		    {
		    $('.controle, .controle1, .controls').bind('click', animation);
		});
		$('#ctThumb div').stop().animate({
		    left:-positions2[currentPos]+'px'
		    },1000,function()

		    {
		    $('.controle, .controle1, .controls').bind('click', animation);
		});
	    }
	    else
	    {
		totSucre=0;
		//alert(thisPoz);
		$('#ctThumb div').stop().animate({
		    left:-positions2[currentPos]+'px'
		    },1000,function()

		    {
		    $('.controle, .controle1, .controls').bind('click', animation);
		});
	    }
		
	    $('#'+(currentPos+1)).addClass('survol');
		
	    $('.controle, .controle1, .controls').fadeIn();
		
	    if(currentPos==0) $('#left, #left1, #navLeft').fadeOut();
	    if(currentPos==totSucre-1) $('#right, #right1, #navRight').fadeOut();
		if(currentPos==totSucre2-4) $('#right, #right1, #navRight').fadeOut();
	}
    };
		
    $('#right, #navRight, #right1').bind('click', animation);
	
	$('#left1').hide();		
		
    ///////
  	var curtPoz = 0;
 	var totStages = 0;
	var stagesHeight = 364;
	
	$('.pageCdiv').each(function(i){ totStages+=1; });
	
    var togSlide = function() {
	$('.navUD').unbind('click', togSlide);
	(this.id=='navUp') ? curtPoz-=stagesHeight : curtPoz+=stagesHeight;
	
	$('#contactDiv #divSlideCt').stop().animate({top:-curtPoz+'px'},1000,function(){
	    if(curtPoz==0)
	    {
			$('#navUp').unbind('click', togSlide).css("background-position", "-16px top");
			$('#navDown').bind('click', togSlide).css("background-position", "left -16px");
	    }
	    else if(curtPoz==(stagesHeight*(totStages-1)))
	    {
			$('#navUp').bind('click', togSlide).css("background-position", "left top");
			$('#navDown').unbind('click', togSlide).css("background-position", "-16px -16px");
	    }
		else
	    {
			$('#navUp').bind('click', togSlide).css("background-position", "left top");
			$('#navDown').bind('click', togSlide).css("background-position", "left -16px");
	    }
	});
		
    };
	
    $('.navUD').bind('click', togSlide);	
    $('#navUp').unbind('click', togSlide).css("background-position", "-16px top");
	
});
