// requires jQuery
// add to toprail.js
jQuery(document).ready(function() {
	/**
	 * Switches toprail tabs to show selected search form
	 */
	jQuery("#ToprailSearchTab1,#ToprailSearchTab2").click(function() {
		jQuery(this).addClass("search_tab_on");
		jQuery(jQuery(this).children()).removeClass("mo");
		jQuery(jQuery(this).children()).addClass("tab");
		if (jQuery(this).attr("id")=="ToprailSearchTab1") {
			jQuery("#ToprailSearchTab2").removeClass("search_tab_on");
			jQuery("#ToprailSearchTab2 > *").removeClass("tab");
			jQuery("#Toprail_BusinessSearch").css("display", "none");
			jQuery("#Toprail_Search").css("display", "inline");
		} else {
			jQuery("#ToprailSearchTab1").removeClass("search_tab_on");
			jQuery("#ToprailSearchTab1 > *").removeClass("tab");
			jQuery("#Toprail_BusinessSearch").css("display", "inline");
			jQuery("#Toprail_Search").css("display", "none");
		}
	});
	/**
	 * Handles toprail search tabs hover states
	 */
	jQuery("#ToprailSearchTab1,#ToprailSearchTab2").hover(
		function(e) {
			if (jQuery(this).hasClass("search_tab_on")==false) {
				jQuery(jQuery(this).children()).addClass("mo");
			}
		},
		function(e) {
			if (jQuery(this).hasClass("search_tab_on")==false) {
				jQuery(jQuery(this).children()).removeClass("mo");
			}
		}
	);
});

// add this to MSIE 6 code in toprail.js
jQuery(document).ready(function() {
	jQuery(".imageGo_toprail").hover(
		function () { jQuery(this).addClass("imageGo_toprail_mo"); },
		function () { jQuery(this).removeClass("imageGo_toprail_mo"); }
	);
	jQuery(".imageGo").hover(
		function () { jQuery(this).addClass("imageGo_mo"); },
		function () { jQuery(this).removeClass("imageGo_mo"); }
	);
});

// ----------------------------------------------------------------------------------------------------
// replace var SiteSearchFieldDefault and functions prepSiteSearch and filterSiteSearch with this code
// plus add the two new functions, isDefaultSearchValue and checkSearchValues
// ----------------------------------------------------------------------------------------------------
/* Search field functions */
var SiteSearchFields = [];
SiteSearchFieldClass = function (obj) {
	this.id = obj.id;
	this.defaultVal = obj.value;
}

function isDefaultSearchValue (objInput)
{
	var json = {idMatch:false, isDefault:false, index:0};
	for (var i=0; i<SiteSearchFields.length; i++) {
		if (SiteSearchFields[i].id == objInput.id) {
			json.idMatch = true;
			if (SiteSearchFields[i].defaultVal == objInput.value || objInput.value=='') {
				json.isDefault = true;
			}
			break;
		}
	}
	json.index = i;
	return json;
}

function checkSearchValues (frm)
{
	var inputs = jQuery("form[id='"+frm.id+"'] :text");
	inputs.each(function () {
		var test = isDefaultSearchValue(this);
		if (test.isDefault || !test.idMatch)
			this.value = '';
	});
}

function prepSiteSearch (objSearchField,fieldHasFocus) 
{
	var searchTest = isDefaultSearchValue(objSearchField);
	var i = searchTest.index;
	if (!searchTest.idMatch) SiteSearchFields[i] = new SiteSearchFieldClass(objSearchField);
	if (objSearchField.value == SiteSearchFields[i].defaultVal) {
		objSearchField.value = "";
	} else if (objSearchField.value.search(/[0-9A-z]+/g) == -1) {
		objSearchField.value = SiteSearchFields[i].defaultVal;
	}
}

function filterSiteSearch ( site, field ) 
{
	var test = isDefaultSearchValue(field);
	if (test.isDefault || !test.idMatch) { location.href = "http://search.nola.com/"; return false; }
	var regExp = /\b[Bb][Ll]([0-9]{5,8})\b/g;
	if (!regExp.test(field.value)) return true;
	location.href = "http://businessfinder.nola.com/BL/Profile.aspx?adv="+ RegExp.$1; return false;
}

function updateLinks ()
{
	document.getElementById("Toprail_Search").action="http://search.nola.com/sp";
	document.getElementById("Toprail_BusinessSearch").getElementsByTagName('a')[0].href="http://businessfinder.nola.com/";
}
