// we will add our javascript code here.  When DOM loaded, code executes in .ready                               
 $(document).ready(function(){

	var a1;
	var lState;
	var lLongitude;
	var lLatitude;
	var lRadius="";
	var lSearchMode;
	var lZipCode;
	var lSearchCat;
	var lQueryPage;
	var lExtraSQL;
	
	
	jQuery(function() {
	
	var onAutocompleteSelect = function(value, data, userQuerr) 
	{
	  // Note : The Form passes the data via GET (?q=toto) when user did not accept one of the selections
	  // Redirect to search results
	  // Note : escape does the URLEncoding (example space becomes '%20'
	  // Note : The javascript url encode (escape()) does not encode '+' or '/'.  The form 
	  //        encodes a space as a '+' while javascript encodes the space as '%20'
	  // MAJ - The '&state' name must match the hidden form element with EXACT SAME NAME
	  
	try 
	{ 
		if( lRadius.length > 0)  
		{
		  window.location = lQueryPage + "?zipcode="+lZipCode + "&radius="+lRadius + "&q="+escape(value);
		}
	}
	catch(e) 
	{     // Radius not defined
		  window.location = lQueryPage + "?state="+lState + "&q="+escape(value);		  
	}
	  
	  // This can add stuff to the page based on suggestions results - when user accepted one of the suggestions.
	  // $('#selection').html('<img src="\/global\/flags\/small\/' + data + '.png" alt="" \/> ' + value);
	  // alert(userQuerr);
	  // alert(value);
	}
	

	// Value of hidden for element in animal-search.php containing state to filter on which is passed in via php
	// #state is the id= in <input name="state" type="hidden" id="state" value="<?php echo $query_state;?>" /> - same for others
	lState = $('#state').val(); // MAJ
	lLongitude = $('#long').val(); // MAJ	
	lLatitude = $('#lat').val(); // MAJ		
	lRadius = $('#radius').val(); // MAJ	
	lZipCode = $('#zipcode').val(); // MAJ	
	lSearchCat = $('#cat').val(); // MAJ	
	lQueryPage = $('#query_page').val(); // MAJ
	lExtraSQL = $('#dbc').val(); // MAJ

	// MAJ - When radius is defined in calling page we send radius=30&long=-90&lat=40
	//       When radius not defined in calling page we send state=FL
	var options;

	try 
	{ 
		if( lRadius.length > 0)  
		{
			options = {
			  serviceUrl: 'http://www.lepetshop.us/php/auto_suggest_processing.php',
			  width: 460,
			  delimiter: /(,|;)\s*/,
			  zIndex: 9999,
			  onSelect: onAutocompleteSelect,
			  deferRequestBy: 0, //miliseconds
			  params: { radius: lRadius, long: lLongitude, lat: lLatitude, cat: lSearchCat, dbc: lExtraSQL  }
			};
		}
	} 
	catch(e) 
	{   // If no radius we end up here
		options = {
		  serviceUrl: 'http://www.lepetshop.us/php/auto_suggest_processing.php',
		  width: 460,
		  delimiter: /(,|;)\s*/,
		  zIndex: 9999,
		  onSelect: onAutocompleteSelect,
		  deferRequestBy: 0, //miliseconds
		  params: { state: lState, cat: lSearchCat, dbc: lExtraSQL  }
		};
	
	} 
		
	

	
	// MAJ the autocomplete function is defined in jquery.autocomplete.js
	a1     = $('#query').autocomplete(options);
	
	$('#navigation a').each(function() {
	  $(this).click(function(e) {
		var element = $(this).attr('href');
		$('html').animate({ scrollTop: $(element).offset().top }, 300, null, function() { document.location = element; });
		e.preventDefault();
	  });
	});

});

 });  
