// JavaScript Document
$(document).ready(function(){
	initGstVilleV();
	
	$('ul.result-list li img').click(function () {
		window.location = $(this).parent('div').parent('div').parent('div').parent('li').children('div.right').children('p').children('span.title').children('a').attr('href');
	});
	
	$('ul.result-list li p.texte').click(function () {
		window.location = $(this).parent('div').children('p').children('span.title').children('a').attr('href');
	});
	
	$('ul.result-list li .button-list li.fiche').click(function () {
		window.location = $(this).parent('ul').parent('div').parent('div.right').children('p').children('span.title').children('a').attr('href');
	});
});


function	initGstVilleV()
{
	$('#mtville input').click(function () {
		if ($(this).val() == 'Inscrire ville / département / région')
			$(this).val('');
	});
								
	$('#mtville input').blur(function () {
		if ($(this).val() == '')
		{
			$(this).val('Inscrire ville / département / région');

			if ($('#mtville  #sugSearchV').length > 0)
				$('#mtville #sugSearchV').remove();
		}
	});
	
	$('#mtville input').keydown(function (e) {
		if ($('#mtville #sugSearchV').length > 0)
		{
			switch (e.which)
			{
				// Flèche haut
				case 38	:	pos 	= parseInt($('#mtville #sugSearchV div ul li.actif').attr('pos'));
							nbElem	= parseInt($('#mtville #sugSearchV div ul li').length);
							
							if (nbElem > 1)
							{
								if (pos > 1)
									pos--;
								else
									pos = nbElem;
									
								$('#mtville #sugSearchV div ul li.actif').removeClass('actif');
								$('#mtville #sugSearchV div ul li[pos=' + pos + ']').addClass('actif');
							}
							return false;
							break;

				// Flèche bas
				case 40 :	pos 	= parseInt($('#mtville #sugSearchV div ul li.actif').attr('pos'));
							nbElem	= parseInt($('#mtville #sugSearchV div ul li').length);
							
							if (nbElem > 1)
							{
								if (nbElem < pos + 1)
									pos = 1;
								else
									pos++;
									
								$('#mtville #sugSearchV div ul li.actif').removeClass('actif');
								$('#mtville #sugSearchV div ul li[pos=' + pos + ']').addClass('actif');
							}
							return false;
							break;

				// Esc
				case 27	:	$('#mtville #sugSearchV').remove();
							$('#mtville input').val('');
							return false;
							break; 			

				// Entrée
				case 13	:	gotoElement($('#mtville #sugSearchV div ul li.actif'));
							return false;
							break; 			
			}
		}
		return true;
	});
	
	$('#mtville input').keyup(function (e) {
		if (e.which != 38 && e.which != 40 && e.which != 27 && e.which != 13)
		{
			switch ($(this).val().length)
			{
				case 0	:	if ($('#mtville #sugSearchV').length > 0)
									$('#mtville #sugSearchV').remove();
								break;
								
				case 1	:
				case 2	:	if ($('#mtville #sugSearchV').length == 0)
								createDivSearchVille('<div>Compléter votre recherche ...</div>');
							else
								$('#mtville #sugSearchV div').html('Compléter votre recherche ...');
							break;
							
				default	:	makeSearchVille();
							break;
			}
		}
	});
}


function	createDivSearchVille(texte)
{
	posInput = $('#mtville input').findPos();
	posDiv = $('#mtville').findPos();
	
	$('#mtville').append(jQuery('<div>', {'id': 'sugSearchV'}).css({'left': (parseInt(posInput.x) - parseInt(posDiv.x)) + 'px', 'top': (parseInt(posInput.y) - parseInt(posDiv.y) + $('#mtville input').height() + 9) + 'px'}).html(texte));
}


function	makeSearchVille()
{
	$.ajax({
		url		:	getPath('php') + 'home/aj_gstSuggestMoteur.php',
		type	:	'POST',
		data	:	'champ=' + $('#mtville input').attr('id') + '&' + $('#mtville input').serialize() + '&url=' + $.URLEncode(window.location),
		success	:	function(data)
		{
			if ($('#nav #sugSearch').length == 0)
				createDivSearchVille('<div></div>');

			tabData = data.split('¤');
			
			if (tabData[1] == 'KO')
				$('#mtville #sugSearchV div').html('<div>Aucun élément trouvé !</div>');
			else
			{
				$('#mtville #sugSearchV div').html(tabData[2]);
				$('#mtville #sugSearchV div ul li').mouseover(function () {
					$('#mtville #sugSearchV div ul li.actif').removeClass('actif');
					$(this).addClass('actif');
				});
				
				$('#mtville #sugSearchV div ul li').mouseout(function () {
					$(this).removeClass('actif');
					window.setTimeout('checkNoActifVille(' + $(this).attr('pos') + ')', 100);
				});
				
				$('#mtville #sugSearchV div ul li').click(function () {
					gotoElement($(this));
				});
			}
		}
	});
}


function	checkNoActifVille(pos)
{
	if ($('#mtville #sugSearchV div ul li.actif').length == 0)
		$('#mtville #sugSearchV div ul li[pos=' + pos + ']').addClass('actif');
}


function	gotoElement(oJq)
{
	$.ajax({
		url		:	getPath('php') + 'home/aj_gstSuggestMoteur.php',
		type	:	'POST',
		data	:	'ref=' + oJq.attr('ref') + '&type=' + (oJq.hasClass('vil') ? 'V' : (oJq.hasClass('dep') ? 'D' : 'R')),
		success	:	function(data)
		{
			window.location = data;
		}
	});
}
