$(document).ready(function() {
	
	//Formulaire de recherche de boutique:
	
	
	$("#alphabetical div.dropDown").click(function(){
		dropDown_toggle("alphabetical");
	});
	
	
	
	$("#textField div.buttonKeyword").click(function(){
		if($("form.keyword input").attr("value") !="" && $("form.keyword input").attr("value") != $("form.keyword input").attr("default")){
			//$("form.keyword").submit();
			
			window.location = baseSearch + '/mots:' + $("form.keyword input").attr("value");
			
		};
		return false;
	});
	$("form.keyword").submit(function(){
		if($("form.keyword input").attr("value") !="" && $("form.keyword input").attr("value") != $("form.keyword input").attr("default")){
			//$("form.keyword").submit();
			
			window.location = baseSearch + '/mots:' + $("form.keyword input").attr("value");
			
		};
		return false;
	
	});
	/*$("#category div.buttonDropDown").click(function(){
		if(counter_Cat == false){
		dropDown("category");
		counter_Cat = true;
		}else{
		dropDown_close("category");
		counter_Cat = false;	
		};
	});*/
	
	
	
	$("#category div.dropDown").click(function(){
		dropDown_toggle("category");
	});
	
	$("div.searchBoutique ul li.clickable").click(function(){
		$(this).parent().removeClass('openedDropDown');
		dropDown_close($(this).parent().attr("class"));
		
		$("span."+$(this).parent().attr("class")).text($(this).text());
		if($(this).attr("val"))
			value2 = $(this).attr("val");
		else
			value2 = $(this).text();
		if($(this).parent().attr("class")=="alphabetical"){
			counter_Alph = false;
		}else{
			counter_Cat = false;
		};
		window.location = baseSearch + '/' + $(this).parent().attr("class") + ":" + value2;
		//$("form."+$(this).parent().attr("class")).submit();
		
	});
	
	//Page d'accueil rotation du carrousel:
	if(pics){
		var children = $("#carrousel").children();
		for(var i=0; i<children.length; i++){
			children.eq(i).addClass('slide').addClass('slide'+i).hide();
			y="<img src='"+root+"img/carrousel/greyDotTransparent.png' id='dot"+i+"'>";
			$("#pagination").prepend(y);
			$("#"+i).hide();
		};
		$("#carrousel .slide0").show();
		$("#dot0").attr("src", root+"img/carrousel/greyDot.png");
		counter=0;
		l= counter;
		l++;
		t=setTimeout("loadImage("+l+")",5000);
	}
	$("#pagination > img").click(function(){
		var a=$(this).attr("id");
		var b=a.slice(3);
		loadImage(b);
		});
	$("input.keyword").focus(function(){
		if($(this).val() == $(this).attr("default")){
			$(this).val('');
		}
	});
	$("input.keyword").focusout(function(){
		if($(this).val() == ""){
			$(this).val($(this).attr("default"));
		}
	});

});


//Formulaire pour chercher une boutique :

var counter_Alph = new Boolean();
counter_Alph = false;
var counter_Cat = new Boolean();
counter_Cat = false;


function dropDown_toggle(m){
	if($("ul."+m+":visible").length){
		dropDown_close(m);
	}else{
		dropDown(m);
	}
};

function dropDown(m){
	$("ul.openedDropDown").hide().removeClass("openedDropDown");
	$("ul."+m).show().addClass('openedDropDown');
	
	//console.log('bind');
	$("body").bind('click',onClickOutside);
};


function dropDown_close(m){
	$("body").unbind('click',onClickOutside);
	$("ul."+m).hide().removeClass('openedDropDown');
	$("ul.openedDropDown").hide().removeClass("openedDropDown");
};

function onClickOutside(e){
	if(!$(e.target).closest('.openedDropDown').length && !$(e.target).closest('.dropDown').length){
		console.log('close');
		$('.openedDropDown').hide().removeClass('openedDropDown')
		$("body").unbind('click',onClickOutside);
	}
}

//Carrousel :



var counter=0;
var t;
var l= new Number();


function loadImage(n){
	clearTimeout(t);
	if(n!=counter){
	$("#dot"+counter).attr("src", root+"img/carrousel/greyDotTransparent.png");
	$("#dot"+counter).css("border", "none");
	$("#dot"+n).attr("src", root+"img/carrousel/greyDot.png");
	$("#dot"+n).css("border", "1px hidden #f0f");
	$("#carrousel .slide"+counter).fadeOut(1500);
	$("#carrousel .slide"+n).fadeIn(1200);
	counter=n;
	//alert(pics.length);
	var children = $("#carrousel").children();
	if(counter<=children.length-2){
	l = counter
	l++;
	t=setTimeout("loadImage("+l+")",5000);
	}else{
	t=setTimeout("loadImage(0)", 5000);
	};
	};
};
