var REDIRECT_URL = '/promo-express.html';
var ajaxTimer = ajax = null;
jQuery(document).ready(function () {
	jQuery('#deptFrenchMap').attr('autocomplete', 'off');

	jQuery('#deptFrenchMap').focus(function () {
		jQuery(this).val('');
	});
	jQuery('#deptFrenchMap').blur(function () {
		jQuery(this).val(DEFAULT_INPUT_DPT);
		window.setTimeout(function () {jQuery('#suggestBox').hide();}, 500);
	});
	jQuery('#deptFrenchMap').keyup(function () {
		jQuery('#suggestBox').hide();
		var elem = jQuery(this);
		var val = elem.val();
		if (val.length >= 2)
		{
			if (ajaxTimer) {
				window.clearTimeout(ajaxTimer);
			}
			ajaxTimer = window.setTimeout(function () { ajaxCall(val, elem) }, 450);
		}
	});
});
function ajaxCall(val, elem)
{
	if (ajax) ajax.abort();
	ajax = jQuery.ajax({ 
		url : '/recherche-zone.html', 
		dataType: 'json', 
		//timeout: 5000, 
		data: 'ajax=1&search='+val, 
		success: function (data) {
			updateZone(data, elem);
		},
		error: function (xhr, text, errorThrown) {
			xhr.abort();
		}
	});
}
function updateZone(data, elem)
{
	if (!data)
	{
		return false;
	}
	makeSuggestBox(elem);
	var html = '<ul>';
	for (var i in data)
	{
		html += '<li><a href="'+REDIRECT_URL+'?zone='+i+'">'+data[i].name+'</a></li>';
	}
	html += '</ul>';
	jQuery('#suggestBox').html(html);
	jQuery('#suggestBox').show();
}
function makeSuggestBox(elem)
{
	if (jQuery('#suggestBox').length > 0)
	{
		return;
	}
	var offset = elem.offset();
	jQuery('body').append('<div id="suggestBox"></div>');
	jQuery('#suggestBox').css({position: 'absolute', 'z-index': 100, top: (offset.top + elem.height())+'px', left: offset.left+'px'});
}


