$j(document).ready(function(){
	 
	 var data = '/getAllLocation.php'
	 
	 // liste des options disponible sur:
	 // http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions	 
	 
	 //format des données (type, typeid, nom, code, shortname, parent, gdparent): voir atome_jquery.module
	 
	 $j("input#edit-location").autocomplete(
	 		data, 
			{	
				formatItem: function(item, pos, max, value, search){
					
					switch(item[0]){
						case "ville":
							if (isNaN(search)){
								return item[2] + ' ('+ item[3] + '), ' + item[4] + ', ' +item[5];
							}else{
								return item[3] + ' ' + item[2] + ', ' + item[4] + ', ' +item[5];
							}
							break
						case "département":
							return item[0] + ': ' + item[2] + ' ('+ item[3] + '), ' + item[4]
							break;
						case "région":
							return item[0] + ': ' + item[2]
							break;
						case "pays":
							return item[0] + ': ' + item[2]
							break;
						default:
							break;
					}
					
					//return item[0] + ' ('+ item[1] +'), ' + item[2] + ', ' + item[3];
				}
				,formatMatch: function(item, pos, max, search){
					return item[2];
				}
				,formatResult: function(item){
					switch(item[0]){
						case "ville":
							return item[2] + ' '+ item[3] ;
							break
						case "département":
							return item[2];
							break;
						case "région":
							return item[2];
							break;
						case "pays":
							return item[2];
							break;
						default:
							break;
					}
					
				}
				,highlight: function(value, search){
					var regex = new RegExp(search, "i");
					var prefix = '';
					
					if (value.indexOf('département') == 0){
						prefix = 'département: ';
						value = value.substr( prefix.length );
					}
					if (value.indexOf('région') == 0){
						prefix = 'région: ';
						value = value.substr( prefix.length );
					}
					if (value.indexOf('pays') == 0){
						prefix = 'pays: ';
						value = value.substr( prefix.length );
					}
					
					var toReplace = regex.exec(value);
					
					return prefix + value.replace( toReplace , '<strong>'+toReplace+'</strong>' );

				}
				,minChars: 2
				,max: 30
				,delay: 150
				,width: 350
				,scroll:true
			}
		)
		
	 $j("input#edit-location").result(function(event, data, formated){
	 	
	 	$j("#edit-location-type").val(data[0])
	 	
	 	//if data type == ville
	 	if (data[0] == 'ville'){
	 		$j("#edit-zone").attr("disabled","")
	 	}else{
	 		$j("#edit-zone").val(0)
	 		$j("#edit-zone").attr("disabled","disabled")
	 		
	 	}
	 })
});

