var slide = 0;
var slideAncien = -1;
var slidenew = 0;
function imgSuivante(){
	slide++;
	slidenew = Math.floor(Math.random()*slideshow.length);
	while(slidenew == slideAncien){
		slidenew = Math.floor(Math.random()*slideshow.length);
	}
	slideAncien = slidenew;
	$('#slideshow').append('<div class="slideshow"><img id="slideshow_'+slide+'" src="'+url+'/images/head/'+slideshow[slidenew]+'" alt="" /></div>');
	
	$('#slideshow_'+slide).hide();
	$('#slideshow_'+slide).load(function() {
		$('#slideshow_'+slide).fadeIn();
		setTimeout(imgSuivante,10000);
	});
	
}

$(document).ready(function() {
	
	var redimWindow = false;
	function resizeWindow(prems){
		var larg = 0;
		var haut = 0;
		if (document.body){
			haut = (document.body.clientHeight);
		}else{
			haut = (window.innerHeight);
		}
		if(haut<=700){
			if(!redimWindow){
				if(!prems){
					$('#panier').animate({'left':'230px','top':'10px'},500);
					$('#menu').animate({'top':'155px'},500);
					$('#header').animate({'height':'155px'},500);
					$('#header_in').animate({'height':'155px'},500);
					$('#main').animate({'paddingTop':'205px'},500);
					$('#slideshow').animate({'top':'-70px'},500);
				}else{
					$('#panier').css({'left':'230px','top':'10px'});
					$('#menu').css({'top':'155px'});
					$('#header').css({'height':'155px'});
					$('#header_in').css({'height':'155px'});
					$('#main').css({'padding-top':'205px'});
					$('#slideshow').css({'top':'-70px'});
				}
				redimWindow = true;
			}
		}else{
			if(redimWindow){
				$('#panier').animate({'left':'10px','top':'155px'},500);
				$('#menu').animate({'top':'300px'},500);
				$('#header').animate({'height':'300px'},500);
				$('#header_in').animate({'height':'300px'},500);
				$('#main').animate({'paddingTop':'350px'},500);
				$('#slideshow').animate({'top':'0px'},500);
				redimWindow = false;
			}
		}
	}
	resizeWindow(true);
	
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizeWindow,100,false);
	});
	
	
	
/*---------- TARTES ----------*/
	
	
	$('.tarte .annuler').click(function(){
		parcourir = "NO";
	});
	$('.tarte').click(function(){
		if(parcourir != "NO"){
			parcourir = $(this).attr('id').replace('prod_','');
		}else{
			parcourir = "";
		}
	});
	$('.tarte .tarte_info').css({'opacity':0.85});
	$('.tarte .tarte_prix').css({'opacity':0});
	$('.tarte').hover(function(){
		$(this).children('.tarte_info').stop().fadeTo('fast',0.95);
		$(this).children('.tarte_prix').stop().fadeTo('fast',1);
		if($(this).children('.tarte_image2').length<=0){
			$(this).children('.tarte_image').before('<img class="tarte_image2" style="display:none;position:absolute;left:0px;top:0px;" src="'+url+'/produits/images/'+$(this).children('.tarte_image').attr('name')+'" alt="">');
			$(this).children('.tarte_image2').load(function(){
				$(this).fadeIn('fast');
			});
		}else{
			$(this).children('.tarte_image2').stop().fadeTo('fast',1);
		}
	},function(){
		var id = $(this).attr('id').replace('prod_','');
		if(id!=parcourirAncien){
			$(this).children('.tarte_info').stop().fadeTo('fast',0.85);
			$(this).children('.tarte_prix').stop().fadeTo('fast',0);
			$(this).children('.tarte_image2').stop().fadeTo('fast',0);
		}
	});
	if (!$.browser.msie){
		$('.tarte .tarte_hover').css({'opacity':0});
	}
	$('.tarte .tarte_hover').hide();
	
	$('.tarte .tarte_prix2').click(function(){
		var id = $(this).parent('.tarte_prix').parent('.tarte').attr('id').replace('prod_','');
		$('#tarte_prix1_'+id+' option[value=0]').attr("selected", "selected");
		$('#tarte_prix2_'+id+' option[value=1]').attr("selected", "selected");
	});
	$('.tarte .tarte_prix1').click(function(){
		var id = $(this).parent('.tarte_prix').parent('.tarte').attr('id').replace('prod_','');
		$('#tarte_prix2_'+id+' option[value=0]').attr("selected", "selected");
		$('#tarte_prix1_'+id+' option[value=1]').attr("selected", "selected");
	});
	
	$(document).click(function(e) {
		actualiseClic();
	});
});
var parcourir="";
var parcourirAncien="";
function actualiseClic(){
	if(parcourir!=parcourirAncien){
		
		if ($.browser.msie){
			$('.tarte .tarte_hover').hide();
			$('#prod_'+parcourirAncien+' .tarte_select').show();
			$('#prod_'+parcourirAncien+' .tarte_select').css({'opacity':1});
			$('#prod_'+parcourirAncien+' .tarte_add').show();
			$('#prod_'+parcourirAncien+' .tarte_add').css({'opacity':1});
			$('#prod_'+parcourirAncien+' .confirm').css({'opacity':0});
			$('#prod_'+parcourirAncien+' .confirm').hide();
			$('#tarte_prix1_'+parcourirAncien+' option[value=0]').attr("selected", "selected");
			$('#tarte_prix2_'+parcourirAncien+' option[value=0]').attr("selected", "selected");
			
		}else{
			$('.tarte .tarte_hover').stop().fadeTo('normal',0,function(){
				$(this).hide();
				var id = $(this).parent('.tarte').attr('id').replace('prod_','');
				$('#tarte_prix1_'+id+' option[value=0]').attr("selected", "selected");
				$('#tarte_prix2_'+id+' option[value=0]').attr("selected", "selected");
				
				$('#prod_'+id+' .tarte_select').show();
				$('#prod_'+id+' .tarte_select').css({'opacity':1});
				$('#prod_'+id+' .tarte_add').show();
				$('#prod_'+id+' .tarte_add').css({'opacity':1});
				$('#prod_'+id+' .confirm').css({'opacity':0});
				$('#prod_'+id+' .confirm').hide();
			});
		}
		$('.tarte h3').css({'cursor':'pointer'});
		$('.tarte .tarte_info').css({'cursor':'pointer'});
		
		$('.tarte .tarte_info').stop().fadeTo('fast',0.85);
		$('.tarte .tarte_prix').stop().fadeTo('fast',0);
		$('.tarte .tarte_image2').stop().fadeTo('fast',0);
		
		if(parcourir!=""){
			$('#prod_'+parcourir+' .tarte_info').stop().fadeTo('fast',1);
			$('#prod_'+parcourir+' .tarte_prix').stop().fadeTo('fast',1);
			$('#prod_'+parcourir+' .tarte_image2').stop().fadeTo('fast',1);
			if ($.browser.msie){
				$('#prod_'+parcourir+' .tarte_hover').show();
			}else{
				$('#prod_'+parcourir+' .tarte_hover').show();
				$('#prod_'+parcourir+' .tarte_hover').stop().fadeTo('normal',1);
			}
			$('#prod_'+parcourir+' .tarte_info').css({'cursor':'default'});
			parcourirAncien = parcourir;
			parcourir = "";
		}else{
			parcourirAncien = parcourir;
			parcourir = "";
		}
	}else{
		parcourir = "";
	}
}
