var defaultLocationPhrase = 'City, State or Zip Code';
var defaultKeywordPhrase = 'Business, Category or Keyword';

/**
 * Additional onload functionality
 */
$(document).ready(function() {
	if($('#location_input').isDefined()) {
		$('#keyword_input').suggestCat();
		$('#keyword_input').inputToggle(defaultKeywordPhrase);
		$('#location_input').inputToggle(defaultLocationPhrase);
		$('#location_input').keypress(function (e) {
		      if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25)
                      || (97 <= e.which && e.which <= 97 + 25)) {
		    	  if($('#location-error').isDefined()) $('#location-error').hide();
		    }
		  });
	}
	$('#searchFormHolder').submit(function() {
		if($('#keyword_input').val() == defaultKeywordPhrase) $('#keyword_input').val('');
		var loc_val = $('#location_input').val();
		if(loc_val == '' || loc_val == defaultLocationPhrase) {
			$('#location-error').html('<p>Please enter a location</p>');
			$('#location-error').show();
			$('#location_input').focus();
			return false;
		}
	});
	
	if(typeof(pageMode)=='undefined') return;
	
	if(pageMode=='index') {
		if($('#quickMapInput').isDefined()) $('#quickMapInput').inputToggle(defaultLocationPhrase);
		var map = new GMap2($('#mapContainer')[0]);
		map.setCenter(new GLatLng(35,-95), 3);
	}
});

function popSearchLink(phrase) {
	$('#keyword_input').val(phrase);
	$('#searchFormHolder').submit();
	
	return false;
}


jQuery.fn.isDefined = function() {
	return typeof($(this)[0]) != 'undefined';
}
jQuery.fn.inputToggle = function(phrase) {
	var t = $(this);
	if(t.val() == '') t.val(phrase);
	t.bind("focus", function(e) {
		if(t.val() == phrase) t.val('');
	});
	t.bind("blur", function(e) {
		if(t.val() == '') t.val(phrase);
	});
}