$(document).ready(function (e){
    $(".navigation ul.subMenu:not('.open_at_load')").hide();
	$('.carousel_ul li:first').before($('.carousel_ul li:last'));
	interval = setInterval("carouselRight('promo')", 3000);
	interval2 = setInterval("carouselRight('nouveautes')", 3000);
	$('#promos').hover(function(){
		clearInterval(interval);
	},
    function(){
		interval = setInterval("carouselRight('promo')", 3000);
    });
	$('#nouveautes').hover(function(){
		clearInterval(interval2);
	},
    function(){
		interval2 = setInterval("carouselRight('nouveautes')", 3000);
    });
	$("#backgroundPopupMap").click(function(){  
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27){
			disablePopup();
		}
	});
});
/**
	Carousel acc
**/
function carouselRight(a){
	var item_width = $('.'+a+'_ul li').outerWidth() + 10;
	var left_indent = parseInt($('.'+a+'_ul').css('left')) - item_width;
	$('.'+a+'_ul:not(:animated)').animate({'left' : left_indent},500,function(){
		$('.'+a+'_ul li:last').after($('.'+a+'_ul li:first'));
		$('.'+a+'_ul').css({'left' : '-135px'});
	});
}
function carouselLeft(a){
	var item_width = $('.'+a+'_ul li').outerWidth() + 10;
	var left_indent = parseInt($('.'+a+'_ul').css('left')) + item_width;
	$('.'+a+'_ul:not(:animated)').animate({'left' : left_indent},500,function(){
		$('.'+a+'_ul li:first').before($('.'+a+'_ul li:last'));
		$('.'+a+'_ul').css({'left' : '-135px'});
	});
}
/**
	Modification qte produit
**/
function quantite(decalage, idProduit){
	var target = 'input[id=quantite_'+idProduit+']';
	var qt = decalage + parseInt($(target).val());
	if (qt < 1) return;        
	$(target).val(qt);
}
/**
	popup ajout panier
**/
function panier(idProd){		
	var qt = parseInt($('input[id=quantite_'+idProd+']').val());	
	$.post("app/design/content/panier/askPanier.php", { idProduit: idProd, quantite: qt},
	function(data) {
		$("#result").empty().append(data);
		centerPopup();
		loadPopup();		
	});						
}
function loadPopup(){
	var popupStatus = 0;
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupPanier").fadeIn("slow");
		popupStatus = 1;
	}
}
function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupPanier").height();
	var popupWidth = $("#popupPanier").width();
	$("#popupPanier").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}
/**
	popup commande
**/
function commande(){
	centerPopupCommande();
	loadPopupCommande();
}
function loadPopupCommande(){
	var popupStatus = 0;
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupCommande").fadeIn("slow");
		popupStatus = 1;
	}
}
function centerPopupCommande(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupCommande").height();
	var popupWidth = $("#popupCommande").width();
	$("#popupCommande").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}
/**
	popup map
**/
function map(lat, lon, type, i){
	centerPopupMap();
	loadPopupMap();
	var myLatlng = new google.maps.LatLng(lat,lon);
	var myOptions = {
		zoom: 15,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("popupMap"), myOptions);
	if(type == 'BPR'){
		var image = 'skin/img/background/sohomebpr.png';
	}else{
		var image = 'skin/img/background/sohomea2p.png';
	}
	var myLatLng = new google.maps.LatLng(lat, lon);
	var relaisMarker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image
	});
	var contenu = $("#infoRelais-"+i).html();
	var infowindow = new google.maps.InfoWindow({
		content: contenu
	});
	google.maps.event.addListener(relaisMarker, 'click', function(){
		infowindow.open(map,relaisMarker);
	});
	infowindow.open(map,relaisMarker);

}
function loadPopupMap(){
	$("#backgroundPopupMap").css({
		"opacity": "0.5"
	});
	$("#backgroundPopupMap").fadeIn("slow");
	$("#popupMap").fadeIn("slow");
}
function centerPopupMap(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupMap").height();
	var popupWidth = $("#popupMap").width();
	$("#popupMap").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});	
	$("#backgroundPopupMap").css({
		"height": windowHeight
	});
}
function disablePopup(){  
	$("#backgroundPopupMap").fadeOut("slow");  
	$("#popupMap").fadeOut("slow");  
	$("#popupChoixRelais").fadeOut("slow");  
}

/**
	popup choix Relais
**/
function chxRelais(){

}
