var defaultLocationPhrase = 'City, State';

/**
 * Additional onload functionality
 */
$(document).ready(function() {
	if($('#keyword_input').isDefined()) {
		$('#location_input').suggest();
		$('#keyword_input').inputToggle('Search');
		$('#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').fadeOut();
		    }
		  });
	}
	
	$('#popular-searches a.psuedo-link').click(function (e) {
		$('.popsearch-popup').hide();
	    var $div = $(e.target).next('.popsearch-popup');
	    var pos = $(e.target).position();
		$div.css({
			top: pos.top - $div.height()/2 + "px",
			left: pos.left - $div.width()/2 + "px"});
		$div.fadeIn();
	    e.preventDefault();
	    return false;
	});
	
	$('.rounded').corner();
	
	if(typeof(pageMode)=='undefined') return;
	
	$('#fast-searches a.psuedo-link').click(function (e) {
		$('.popsearch-popup').hide();
	    var $div = $(e.target).next('.popsearch-popup');
	    var pos = $(e.target).position();
		$div.css({
			top: pos.top - $div.height()/2 + "px",
			left: pos.left - $div.width()/2 + "px"});
		$div.fadeIn();
	    e.preventDefault();
	    return false;
	});
	
});

function popSearchLink(phrase) {
	$('#keyword_input').val(phrase);
	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();
	} else {
		$('#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);
	});
}