/**
 * Auto Complete 3.1
 * August 22, 2009
 * Corey Hart @ http://www.codenothing.com
 */
(function(b){var a=0;b.fn.autoComplete=function(c)
	{return this.each(function(){var l=b(this).attr("autocomplete","off"),k,e,g,d,h=b.extend({opt:-1,inputval:"",mouseClick:false,dataName:"ac-data",inputIndex:++a,ajax:"ajax.php",list:"auto-complete-list",rollover:"auto-complete-list-rollover",width:l.outerWidth(),postVar:"value",postData:{},minChars:1,maxRequests:0,requests:0,onMaxRequest:function(){},onSelect:function(){},onRollover:function(){},onBlur:function(){},onFocus:function(){},preventEnterSubmit:true,enter:true,delay:100,selectFuncFire:true,useCache:true,cacheLimit:50,cacheLength:0,cache:{}},c||{},b.metadata?l.metadata():{});var i=b("ul."+h.list)[0]?b("ul."+h.list):b("<ul/>").appendTo("body").addClass(h.list).hide();l.keyup(function(n){var m=n.keyCode;h.mouseClick=false;if((m==13||m==9)&&k){h.opt=-1;if(h.selectFuncFire){h.selectFuncFire=false;h.onSelect.call(l[0],k.data(h.dataName),k,i);if(g){clearTimeout(g)}g=setTimeout(function(){h.selectFuncFire=true},1000)}i.hide()}else{if(m==38){if(h.opt>0){h.opt--;k=b("li",i).removeClass(h.rollover).eq(h.opt).addClass(h.rollover);l.val(k.data(h.dataName).value||"");h.onRollover.call(l[0],k.data(h.dataName),k,i)}else{h.opt=-1;l.val(h.inputval);i.hide()}}else{if(m==40){if(h.opt<b("li",i).length-1){h.opt++;k=b("li",i.show()).removeClass(h.rollover).eq(h.opt).addClass(h.rollover);l.val(k.data(h.dataName).value||"");h.onRollover.call(l[0],k.data(h.dataName),k,i)}}else{h.opt=-1;h.inputval=l.val();if(h.inputval.length>=h.minChars){if(e){clearTimeout(e)}e=setTimeout(function(){f()},h.delay)}else{if(m==8){i.html("").hide()}}}}}}).blur(function(){h.enter=true;d=setTimeout(function(){if(h.mouseClick){return false}h.opt=-1;h.onBlur.call(l[0],h.inputval,i);i.hide()},150)}).focus(function(){h.enter=false;if(h.inputIndex!=i.data("ac-input-index")){i.html("").hide()}h.onFocus.call(l[0],i)}).parents("form").eq(0).submit(function(event){var noV=h.preventEnterSubmit?h.enter:true;event.noValidation=!noV; return noV;});function f(){if(h.maxRequests&&++h.requests>=h.maxRequests){return h.requests>h.maxRequests?false:h.onMaxRequest.call(l[0],h.inputval,i)}if(h.useCache&&h.cache[h.inputval]){return j(h.cache[h.inputval])}h.postData[h.postVar]=h.inputval;b.post(h.ajax,h.postData,function(m){if(h.useCache){h.cacheLength++;h.cache[h.inputval]=m;if(h.cacheLength>h.cacheLimit){h.cache={};h.cacheLength=0}}j(m)},"json")}function j(p){if(!p||p.length<1){return i.html("").hide()}var q=l.offset();i.data("ac-input-index",h.inputIndex).html("").css({top:q.top+l.outerHeight(),left:q.left,width:h.width});var m=0,n;for(n in p){if(p[n].value){b("<li/>").appendTo(i).html(p[n].display||p[n].value).data(h.dataName,p[n]).data("ac-index",m++)}}i.show().mouseout(function(){b("li."+h.rollover,i).removeClass(h.rollover);if(!h.mouseClick&&h.selectFuncFire){l.val(h.inputval)}}).children("li").mouseover(function(){k=b(this);b("li."+h.rollover,i).removeClass(h.rollover);l.val(k.addClass(h.rollover).data(h.dataName).value);h.onRollover.call(l[0],k.data(h.dataName),k,i);h.opt=k.data("ac-index")}).click(function(){h.mouseClick=true;if(d){clearTimeout(d)}h.onSelect.call(l[0],k.data(h.dataName),k,i);i.hide();l.focus()})}})}})(jQuery);

var autocomplete_timer = 0;

$(document).ready(function(){
	$("input.townAutoComplete").autoComplete({delay : 500, ajax:"/rsc/lib/towns.php5", onRollover: onTownSelect});
});

function onTownSelect(data) {
	$("#" + this.id + "_id").val(data.id);

	// lien avec la géoloc (zoom auto sur la ville)
	if (typeof(centerMapOnCity) != "undefined")
	{
		autocomplete_timer++;
		setTimeout(function(){
			if (--autocomplete_timer == 0) {
				centerMapOnCity(data.lat, data.lng);
				var adresse = $("#addresse").val() + ' ' + data.zipcode + ' ' + data.value;
			
				var geocoder = new GClientGeocoder();
				geocoder.getLatLng(adresse, function(latLng) {
					if (latLng) {						
						centerMapAndPlaceMarker(latLng, 12);
						$("#divGmapOverlay, #divGmapOverlayTooltip").slideUp();
					}
				});
			}
		}, 1000);
	}
}
