var prompt = 'Rechercher un rayon par mot-clé. Ex: bois, cables, etc';
var s;
var countRayons;
var currentRayon = 0;

var countSpecialistes;
var currentSpecialiste = 0;

var shownSpecialiste = -1;

$(document).ready(function(){
	//$('.rayons li').hide().css('margin-top','60px');
	//$('.specialistes li').hide().css('margin-left', '60px');
	//$('#contactForm').hide();
	countRayons = $('.rayons li').length;
	countSpecialistes = $('.specialistes li').length;

	s = new Suggest('spe-search', 'content/externals/autocomplete.php?type=rayon');
	$('#searchRayon input').focus(function(){
		if($(this).hasClass('prompt')){
			$(this).attr('value', '');
			$(this).removeClass('prompt');
			$('.suggest').fadeIn('slow');
		}
	}).blur(function(){
		if($(this).attr('value') == ''){
			$(this).attr('value', prompt);
			$(this).addClass('prompt');
			$('.suggest').fadeOut('fast');
		}
	})
	
	$('.specialistes li').mouseover(function(){
		$(this).find('ul.sousRayons').show();
	}).mouseout(function(){
		$(this).find('ul.sousRayons').hide();
	})
	/*
	var result = window.location.hash.match(/\#([0-9]{1,})/);
	if (result != null){
		currentSpecialiste = result[1];
		hideSpecialistes(currentSpecialiste);
	} else {
		showSpecialistes();
	showRayons();
	}*/
})

function showRayons(){
	$('.rayons li').eq(currentRayon).fadeIn(30, showRayonsComplete)
	$('.rayons li').eq(currentRayon).animate({marginTop:0},30);
}

function showRayonsComplete(){
	currentRayon++;
	if(currentRayon < countRayons){
		showRayons();
	}
}

function showSpecialistes(){
	$('#contactForm').fadeOut("fast");
	$('.specialistes li#'+currentSpecialiste).fadeIn(40, showSpecialistesComplete)
	$('.specialistes li#'+currentSpecialiste).animate({marginLeft:0},40);
}

function showSpecialistesComplete(){
	currentSpecialiste++;
	if(currentSpecialiste < countSpecialistes){
		showSpecialistes();
	}
}

function hideSpecialistes(except){
	if (except != shownSpecialiste){
		shownSpecialiste = except;
		$("#idSpecialiste").attr('value', except);
		currentSpecialiste = 0;
		$('.specialistes li').fadeOut(50, function(){
			$('.specialistes li#'+except).fadeIn();
			$('#contactForm').fadeIn();
		})
	}
}

function search(){
	search = ($("#search").attr("value") != undefined) ? 'specialiste/' + $('#spe-search').attr('value') : $("#search").attr("value");
	$("#hiddenSearch").attr('value', search);
	$("#searchRayonForm").submit();
	return true;
}
