var defaultLocationPhrase = 'City, State OR Zip Code';

/**
 * Additional onload functionality
 */
$(document).ready(function() {
	initLytebox(); //just in case when this function called before Lytebox initialization.
	
	if(typeof(myLytebox)!='undefined') {
		myLytebox.autoResize = false;
	}
	
	$('#white_name').inputToggle('Name');
	$('#white_where').inputToggle('Where');
	
	if($('#location_input').isDefined()) {
		$('#location_input').suggest();
		$('#keyword_input').inputToggle('Search For');
		$('#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($('#location_input').val() == defaultLocationPhrase) $('#location_input').val('');
	});
	
	if(typeof(pageMode)=='undefined') return;
	
	$('#keyword_input').suggestCat({maxResults: 25});
	
	if(pageMode=='index') {
		$('.fastlinks li').find('a:first').click(function() {
			var myDiv = $(this).parent().find('div');
			var isH = myDiv.is(':hidden');
			
			$('.fastlinks li').find('div').hide();
			$('.fastlinks li').find('div').removeClass('fastlink_hover');
			if(isH) {
				myDiv.toggle();
				myDiv.addClass('fastlink_hover');
			}
			return false;
		});
	}
	
	if(pageMode=='search') {
		$('#related-searches-toggle').bind('click', function() {
			if($(this).text().match('.*More.*')) $(this).html($(this).html().replace('More','Less'));
			else $(this).html($(this).html().replace('Less','More'));
			$('#related-searches li.hidden').toggle();
			$(this).blur();
			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);
	});
}