(function($) {
	
	$.fn.checked = 0;
	$.fn.delay = 600;
	
	$.fn.multi_menu = function(params){
		
		var intervalIdShow = -1;
		var intervalIdHide = -1;
		var currentOverElement = null;
		var currentOpenedElement = null;
		
		function showCurrent()
		{
			clearTimeout(intervalIdShow);
			clearTimeout(intervalIdHide);
			
			if (currentOverElement != null && currentOverElement != currentOpenedElement)
			{
				if (currentOpenedElement != null)
					currentOpenedElement.hide();
				
				currentOverElement.show();
				currentOpenedElement = currentOverElement;
				currentOverElement = null;
			}
		}
		
		function hideCurrent()
		{
			clearTimeout(intervalIdShow);
			clearTimeout(intervalIdHide);
			
			$("#menu_v").children().find('ul').hide();
			$("#menu_v").find('div.ssmenu_v_2_bas').hide();
		}
		
		// recuperation de l'element appelant
		var elements=$(this);
		
		elements.each(function(){
			
			current = $(this);
			
			if (current.children('li').length != 0 ) {
				
				current.children('li').each(function(){
					
					li = $(this);
					
					if (li.children('ul, div').length!=0) {
						
						li.children('ul, div').each(function(){
							$(this).css('display','none');
						});
						
						li.mouseenter(function(){
							
							// Affichage enfant
							$(this).children('a').addClass('select');
							
							if (intervalIdShow > -1)
								clearTimeout(intervalIdShow);
							
							if (intervalIdHide > -1)
								clearTimeout(intervalIdHide);
							
							intervalIdShow = setTimeout(showCurrent, $.fn.delay);
								
							currentOverElement = $(this).children('ul, div');
						});
						
						li.mouseleave(function(){
							
							// Masquage enfant
							$(this).children('a').removeClass('select');
							
							if (intervalIdShow > -1)
								clearTimeout(intervalIdShow);
							
							if (intervalIdHide > -1)
								clearTimeout(intervalIdHide);
							
							intervalIdHide = setTimeout(hideCurrent, $.fn.delay);
						});
						
						if (li.children('ul').length!=0) li.children('ul').multi_menu();
					}
				});
			}
		});
	};
	
	$.fn.onglet_openclose = function(params){
		var elements=$(this);
		var content=null;
		var affichTime=null;
		elements.each(function(){
			content=$(this).find(".blockWidget1_content");
			$(this).find(".blockWidget1_header").click(function(){
				if ($(content).css("display")=="none"){
					$(this).find(".icFleche_openclose").css("background-position", "0 -16px");
				} else {
					$(this).find(".icFleche_openclose").css("background-position", "0 0");
				}
				$(content).slideToggle(250);
			})
			compterebourd();
		});
		/* compte a rebourd*/
		function compterebourd(){	
			affichTime = setInterval(function(){
					clearInterval(affichTime);
					affichTime=null;
					$(content).fadeOut("slow");
			}, 3000);
		}
	};
	
	$.fn.sliderItemOver = function(params){
		$(this).find(".item_vignette").each(function(){
			$(this).click(function(){
				var lien=$(this).find(".itemsImg a").attr("href");
				window.location.href = lien;
				return false;
    		});
			$(this).hover(function(){
				$(this).find(".itemsImg").hide();
				$(this).find(".itemsOver").slideDown(200);
 			},function(){
				$(this).find(".itemsImg").show();
				$(this).find(".itemsOver").hide();
 			});
		});
	}
	
	$.fn.sliderAffichBigVisuel = function(params){
		var visuelBig=$("p#visuelProduit");
		$(this).find(".items a").each(function(){
			$(this).click(function(){
				return false;
    		});
			$(this).hover(function(){
				
				var visuel=$(this).attr("rel");
				$(visuelBig).find("img").attr("src", visuel);
				
				var infos = $(this).attr("name").split("||");
				$(visuelBig).find("a").attr("href", infos[0]);
				$(visuelBig).find("a").attr("title", infos[1]);
				
 			},function(){
 				
 			});
		});
	}
	
	$.fn.limitCheckedBox = function(params){
		
		$(this).find("input").each(function(){
			
			var divParent=$(this).parent().parent("div");
			
			if ($(this).attr("checked")) {
				if ($.fn.checked < 3) {	
					if ($(divParent).attr("id") != "curProduct")
						$(divParent).addClass("vignetteSelected");
					$.fn.checked++;
				} else {
					$(this).attr("checked", false);
					if ($(divParent).attr("id") != "curProduct")
						$(divParent).removeClass("vignetteSelected");
				}
			}
			
			$(this).click(function(){
				if ($(this).attr("checked")) {
					if ($.fn.checked < 3) {
						$.fn.checked++;
						if ($(divParent).attr("id") != "curProduct")
							$(divParent).addClass("vignetteSelected");
					} else {
						$(this).attr("checked", false);
						if ($(divParent).attr("id") != "curProduct")
							$(divParent).removeClass("vignetteSelected");
					}
				} else {
					if ($.fn.checked > 0) {
						$.fn.checked--;
						if ($(divParent).attr("id") != "curProduct")
							$(divParent).removeClass("vignetteSelected");
					}
				}
    		});
		});
	}
	
})(jQuery);


$(document).ready(function() {
	
	$(".menu_v_1").multi_menu();
	$(".blockWidget1").onglet_openclose();
	
	$(".produits").click(function(){
		$(".bloc_produits").toggleClass('block');	
		$(".produits").toggleClass('produits_border');	
		
		return false;
    });	
	
	$("#sliderThematique").find(".scrollable").scrollable({
		size: 3, clickable: false
	}).mousewheel();
	$("#sliderThematique").sliderItemOver();
	
	$("#prodSimilaire").find(".scrollable").scrollable({
		size: 1, clickable: false
	}).mousewheel();
	
	$("#compareForm").limitCheckedBox();
	
	$("#sliderCapacite").slider({
		animate: true,
		range: true,
		min: 0,
		max: 280,
		step:5,
		values: [0, 280]
	});
	
	$("#produit").find(".scrollable").scrollable({
		size: 1 					 
	}).mousewheel();
	$("#produit").sliderAffichBigVisuel();
	
	$(".slider_produit").find(".scrollable").scrollable({
		size: 2, clickable: false
	}).mousewheel();
	
	$('#visuelProduit a').lightBox( 
			{ 
				txtImage:'', txtOf:'/',
				imageLoading:'/extension/eurocave/design/eurocave/images/lightbox-ico-loading.gif',
				imageBtnPrev:'/extension/eurocave/design/eurocave/images/lightbox-btn-prev.gif',
				imageBtnNext:'/extension/eurocave/design/eurocave/images/lightbox-btn-next.gif',
				imageBtnClose:'/extension/eurocave/design/eurocave/images/lightbox-btn-close.gif',
				imageBlank:'/extension/eurocave/design/eurocave/images/lightbox-blank.gif'
			}
		);
	
	$('#otherDiaporamas .scrollable a').lightBox( 
		{ 
			txtImage:'', txtOf:'/',
			imageLoading:'/extension/eurocave/design/eurocave/images/lightbox-ico-loading.gif',
			imageBtnPrev:'/extension/eurocave/design/eurocave/images/lightbox-btn-prev.gif',
			imageBtnNext:'/extension/eurocave/design/eurocave/images/lightbox-btn-next.gif',
			imageBtnClose:'/extension/eurocave/design/eurocave/images/lightbox-btn-close.gif',
			imageBlank:'/extension/eurocave/design/eurocave/images/lightbox-blank.gif'
		}
	);
});

function sendCapacity(id)
{
	newLoc($("#"+id)[0].value + '/(capacity)/' + $("#sliderCapacite").slider('values', 0) + "," + $("#sliderCapacite").slider('values', 1));
}

function compareProducts(id)
{
	$("#"+id)[0].submit();
}

function newLoc(loc, useHttp)
{
	if (useHttp)
		loc = "http://" + loc;
	document.location = loc;
}

