$(document).ready(function() {
    $('#id_locality').autocomplete('/location/autocomplete/simple/', {
        max: 8,
        scroll: false,
        formatItem: function(data, i, total) {
            //$.getJSON(data, function(data) {
            //    return data['ResultSet']['Result'][0]['name'];
            //});
            return data[0];
        }
    });
	
	// Custom delay execution
	// Delays the execution of jQuery code
	var delayedExecution = {
		execute: function(elem, func) {
			this.timeoutId = null;
			func.call(elem);
		},
		wait: function(elem, func) {
			if (this.timeoutId) {
				window.clearTimeout(this.timeoutId);
			}
			var delayedExecution = this;
			this.timeoutId = window.setTimeout(function(){
				delayedExecution.execute(elem, func);
			}, 1000);
		}
	}; 
	
	// When the input text field has been focused by the user
	// Focused either by mouse or keyboard (tabbed in)
	$("#id_locality").focus(function () {
		
		// This object (text field: id_locality)
		var $this = $(this);
		
		// If the text field has the class 'inputError'
		if($this.hasClass("inputError"))
		{
			// Select all text in the field for easy replacement
			$this.select();
			
			// Erase the text in the suburbInfo field
			$("#suburbInfo").text("");
			
			// Hide the suburbInfo element			
			$("#suburbInfo").hide();	    								
			
			// Remove the error class to remove the error message image
			$this.removeClass("inputError"); 
			
			// Remove error class from suburb info text
			$("#suburbInfo").removeClass("infoError");			
		}
	});
	
	$("#id_locality").keyup( function ()
	{
		// Delay hiding if no input and the loading class is the loading one on suburbInfo
		if($("#suburbInfo").hasClass("infoLoading"))
		{
			delayedExecution.wait(this, function() { $("#suburbInfo").hide(); });
		}		
		
		// Show the info span
		$("#suburbInfo").show();

		// Remove error class from suburb info text
		$("#suburbInfo").removeClass("infoError");

		// Add loading class to info span (animated image)
		$("#suburbInfo").addClass("infoLoading");			
		
		// Show loading text
		$("#suburbInfo").text("Loading suburbs list..."); 							  						
	});

	$("#id_locality").blur(function ()
	{		
		// Get the entered value of the text field
		var ele = $(this).val(); 												
									
		/* WE NOW DONT HAVE A COMMA - FOR COUNTRIES
                if(ele.search(/[A-Za-z]+([\,]+)/) == -1)
		{
			// Add error class to text field
			$(this).addClass("inputError");
			
			// Show the info span
			$("#suburbInfo").show();	
			
			// Remove the loading class if appended to info span
			$("#suburbInfo").removeClass("infoLoading"); 	
			
			// Add error class to the info spa
			$("#suburbInfo").addClass("infoError");     
			
			// Show error message
			$("#suburbInfo").html("Please re-type your suburb and postcode."); 
		}
		else
                */
		if (true) {
			$("#suburbInfo").removeClass("infoLoading");
			$("#suburbInfo").removeClass("infoError");
			$("#suburbInfo").hide();
		}
	});

	
});
