var defaultLocationPhrase = 'Search Where';

/**
 * 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();
		    }
		  });
	}

	$('.popularsearch 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 + "px",
			left: pos.left + "px"});
		$div.slideToggle('slow');
	    e.preventDefault();
	    return false;
	});
	$('#other-popular-searches a.psudeo-link').click(function (e) {
		$('.popsearch-popup').hide();
	    var $div = $(e.target).next('.popsearch-popup');
	    var pos = $(e.target).position();
		$div.css({
			top: pos.top + "px",
			left: pos.left + "px"});
		$div.slideToggle('slow');
	    e.preventDefault();
	    return false;
	});
	
	$('.rounded').corner('5px');
	
	$("ul#navigation a.psuedo-link").click(function() {
		$(this).parent().find("ul.subnav").slideDown('fast').show();
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow');
		});
		}).hover(function() {
			$(this).addClass("subhover");
		}, function(){
			$(this).removeClass("subhover");
	});
	
	if(typeof(pageMode)=='undefined') return;
	
});

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);
	});
}