// setup the action for the search form and default it to recipe

try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {} // penicillin for a very bad IE6 browser bug

function setSearchType(s_type) {
	// Turn on active tab and set form radio checked value to which search is selected
	setTabsCookie(s_type);
	switch (s_type) {		
		case "recipe":				
			document.HeaderSearchForm.searchType[0].checked = true;
			document.HeaderSearchForm.searchType[1].checked = false;
			document.HeaderSearchForm.action = "http://web.foodnetwork.com/food/web/njsSearch";
			document.getElementById('recipes').className = "selected";
			document.getElementById('topics').className = "";
			break;			
		case "site":
			document.HeaderSearchForm.searchType[0].checked = false;
			document.HeaderSearchForm.searchType[1].checked = true;		
			document.forms["HeaderSearchForm"].action = "http://web.foodnetwork.com/food/web/searchResults?searchString=" + document.HeaderSearchForm.searchString.value + "&site=food&gosearch=&searchType=Site";
			document.getElementById('topics').className = "selected";
			document.getElementById('recipes').className = "";
			break;	
	}
}

function setTabsCookie(pValue) {
	var c_name = "recipeTopicTabs";
	// live cookie set
	document.cookie = escape(c_name) + "=" + escape(pValue) + "; path=/; domain=.foodnetwork.com";
	// dev cookie set
	//document.cookie = escape(c_name) + "=" + escape(pValue) + ";";
}

function flushTabs()
{
	// Flush tabs - set all tabs to inactive and enable all hover states
	
	document.getElementById("nav_link_home").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_home_norm_hover.gif)";
	document.getElementById("nav_link_cooking").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_cooking_norm_hover.gif)";
	document.getElementById("nav_link_holidays").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_holidays_norm_hover.gif)";
	document.getElementById("nav_link_quick").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_quick_norm_hover.gif)";
	document.getElementById("nav_link_healthy").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_healthy_norm_hover.gif)";
	document.getElementById("nav_link_tv").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_tv_norm_hover.gif)";
	document.getElementById("nav_link_shop").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_shop_norm_hover.gif)";
	document.getElementById("nav_link_videos").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_videos_norm_hover.gif)";
		
	document.getElementById("nav_link_home").style.cursor = "pointer";
	document.getElementById("nav_link_cooking").style.cursor = "pointer";
	document.getElementById("nav_link_holidays").style.cursor = "pointer";
	document.getElementById("nav_link_quick").style.cursor = "pointer";
	document.getElementById("nav_link_healthy").style.cursor = "pointer";
	document.getElementById("nav_link_tv").style.cursor = "pointer";
	document.getElementById("nav_link_shop").style.cursor = "pointer";
	document.getElementById("nav_link_videos").style.cursor = "pointer";	
	
	document.getElementById("recipe_box_login").style.visibility ="hidden";
}


function setNavActiveTab() {
	// Set Navigation Active Tab
	var activeTab = ""	
	if (document.globalSctnLineage) {
		//flushTabs();
		var tmpGlobalSctnLineage = document.globalSctnLineage + ",";
		activeTab = tmpGlobalSctnLineage.split(",");
	}
	for (var i=0; i<activeTab.length; i++) {
		switch (activeTab[i]) {		
			case "HOME":
				document.getElementById("nav_link_home").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_home_active.gif)";	
				document.getElementById("nav_link_home").style.cursor = "default";
				break;
			case "COOKING":
				document.getElementById("nav_link_cooking").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_cooking_active.gif)";
				document.getElementById("nav_link_cooking").style.cursor = "default";
				break;
			case "ENTERTAINING":
				document.getElementById("nav_link_holidays").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_holidays_active.gif)";
				document.getElementById("nav_link_holidays").style.cursor = "default";
				break;
			case "LF_QUICK_EASY":
				document.getElementById("nav_link_quick").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_quick_active.gif)";
				document.getElementById("nav_link_quick").style.cursor = "default";
				break;
			case "LF_HEALTH":
				document.getElementById("nav_link_healthy").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_healthy_active.gif)";
				document.getElementById("nav_link_healthy").style.cursor = "default";
				break;
			case "TV":
				document.getElementById("nav_link_tv").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_tv_active.gif)";
				document.getElementById("nav_link_tv").style.cursor = "default";
				break;
			case "SHOP":
				document.getElementById("nav_link_shop").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_shop_active.gif)";
				document.getElementById("nav_link_shop").style.cursor = "default";
				break;
			case "VIDEO_GUIDE":
				document.getElementById("nav_link_videos").style.backgroundImage = "url(http://images.foodnetwork.com/webfood/images/header/new_header_test/images/nav_videos_active.gif)";
				document.getElementById("nav_link_videos").style.cursor = "default";
				break;
			case "MY_RECIPE_BOX":
				document.getElementById("recipe_box_login").style.visibility ="visible";				
				break;
			case "":
				// do nothing because there is no indicating page variable present
				break;
			default:
				// do nothing
				break;
		}
	}
}

function set_fn_header_width () {
	if (document.getElementById('main_body_wrapper')) {
		main_body_wrapper_width = document.getElementById('main_body_wrapper').offsetWidth;
		<!-- set a minimum width -->
		if (main_body_wrapper_width < "800") main_body_wrapper_width = "800";
		<!-- set a maximum width -->
		if (main_body_wrapper_width > "988") main_body_wrapper_width = "988";
		document.getElementById('fn_header').style.width = main_body_wrapper_width + "px";
	} else {
	// CSS already sets default width of 800px and/or other css rules may be in place
	}
	
}

// form submit event handler
function checkSearch(pThis) {
	// create form var
	var foodSearchForm = document.getElementById("foodSearchForm");
	// create form field var
	var searchString = document.forms["HeaderSearchForm"].elements["searchString"];
	// validation vars
	var searchStringValue = searchString.value;
	var requiredErr = 'Please enter a term or phrase into the search field. We recommend you search by:';
	var recipeErr = 'We noticed you just searched for "' + searchStringValue + '". For better results, we recommend searching by ingredient (e.g. chicken, beef), chef or host (e.g. Rachael Ray, Paula Deen), holiday (e.g. Thanksgiving), or even technique (e.g. grilled).';
	// based on the user's search term, validation either alerts a required terms message, a search suggestion message, or subm its the form.
	switch(searchStringValue.toUpperCase()) {
		// if nothing is entered and the form is submitted
		case "":
			showRecipeError(requiredErr);
			searchString.focus();
			return false;
			break;
		// if a user tries to submit "recipe"
		case "RECIPE":
		case "RECIPES": {
			showRecipeError("");
			searchString.focus();
			return false;
			break;
		}
		// if a user submits a valid search term or phrase
		default:
			searchString.value = searchString.value.replace(/[^a-zA-Z 0-9"']+/g,'');
			searchString.value = searchString.value.replace(/['']+/g,'\'');
			setSearchCookie();
			dorewriteurl();
			trackHeaderClick('search');
	}
}

function showRecipeError(pPopupPhrase1) {
	var mPopupCookie = getPopupCookie("showPopup");
	var _padding = 0;
	var _iframeContainerPadding = 8;
	var _hostName = document.location.hostname;
	if (pPopupPhrase1 != "") {
		var _containerHeight = 175;
		document.getElementById("chkNoPopup").checked = false;
		var recipeErrorPopup = document.getElementById("popupIFrameContainer");
		var txtSearch = document.forms["HeaderSearchForm"].elements["searchString"];
		document.getElementById("poupPhrase1").innerHTML = pPopupPhrase1;
		recipeErrorPopup.style.position = "absolute";
		recipeErrorPopup.style.zIndex = getHighestZIndex() + 101;
		var minusVal = 80;
		if (document.all) minusVal = 200;
		recipeErrorPopup.style.left = findPosX(txtSearch) - minusVal + "px";
		recipeErrorPopup.style.top = findPosY(txtSearch) + 24 + "px";
		document.getElementById("chkLinksContainer").style.display = "none";
		document.getElementById("popupContainer").style.height = _containerHeight + _padding + "px";
		if (document.all) {
			if (_hostName == "www.foodnetwork.com") {
				document.getElementById("popupContainer").style.width = "434px";
				document.getElementById("popupContainer").style.height = "183px";
			}
		}
		document.getElementById("popupContainer").style.backgroundPosition = "top";
		document.getElementById("popupIFrame").style.height = _containerHeight + _padding + "px";
		document.getElementById("popupIFrameContainer").style.height = _containerHeight + _padding + _iframeContainerPadding + "px";
		recipeErrorPopup.style.display = "block";
		try {mPopupShadow.removeShadows();} catch(e) {}
		mPopupShadow = new CDEShadow("popupIFrameContainer", "black", 2, 45, 2, 20, 0);
		return;
	} else {
		if (mPopupCookie == "false" || mPopupCookie == null || mPopupCookie == "") {
			var _containerHeight = 226;
			document.getElementById("chkNoPopup").checked = false;
			var recipeErrorPopup = document.getElementById("popupIFrameContainer");
			var txtSearch = document.forms["HeaderSearchForm"].elements["searchString"];
			document.getElementById("poupPhrase1").innerHTML = "Searching for \"<span id='stFill1'></span>\" will return too many results.  We recommend you search by:";
			document.getElementById("chkLinksContainer").style.display = "block";
			document.getElementById("stFill1").innerHTML = txtSearch.value;
			document.getElementById("stFill2").innerHTML = txtSearch.value;
			recipeErrorPopup.style.position = "absolute";
			recipeErrorPopup.style.zIndex = getHighestZIndex() + 101;
			var minusVal = 80;
			if (document.all) minusVal = 200;
			recipeErrorPopup.style.left = findPosX(txtSearch) - minusVal + "px";
			recipeErrorPopup.style.top = findPosY(txtSearch) + 24 + "px";
			document.getElementById("popupContainer").style.height = _containerHeight + _padding + "px";
			if (document.all) {
				if (_hostName == "www.foodnetwork.com") {
					document.getElementById("popupContainer").style.width = "434px";
					document.getElementById("popupContainer").style.height = "234px";
				}
			}
			document.getElementById("popupContainer").style.backgroundPosition = "top";
			document.getElementById("popupIFrame").style.height = _containerHeight + _padding + "px";
			document.getElementById("popupIFrameContainer").style.height = _containerHeight + _padding + _iframeContainerPadding + "px";
			document.getElementById("chkContainer").style.marginTop = "3px";
			recipeErrorPopup.style.display = "block";
			try {mPopupShadow.removeShadows();} catch(e) {}
			mPopupShadow = new CDEShadow("popupIFrameContainer", "black", 2, 45, 2, 20, 0);
		} else {
			window.location.href = "http://search.foodnetwork.com/food/recipe/search.do?N=0";
		}
	}
}

function rewriteurl(pAction){
	var acStr; 
	var tmp = gupFmAction('searchString',pAction);
	if( tmp != undefined){
		tmp = tmp.replace(/\s+/g,'+');
		acStr = "food/recipe/"+tmp+"/search.do?";
	} else acStr = pAction;
	return acStr;
}

function dorewriteurl() {
	if (document.HeaderSearchForm.searchType[0].checked == true) {
		var tmpAction = document.HeaderSearchForm.action;
		var tmp = document.HeaderSearchForm.searchString.value;
		if ( tmp != undefined ){  
			tmp = tmp.replace(/\s+/g,'+');
			tmpAction = "http://search.foodnetwork.com/food/recipe/"+tmp+"/search.do?";
			document.HeaderSearchForm.action = tmpAction;
		}
	}
}

function seopath() {
	var tmp = gup('searchString');
	if (tmp != undefined) tmp = document.HeaderSearchForm.searchString.value;
	if (tmp != undefined) tmp = tmp.replace(/\s+/g,'+');
	return tmp;
}

function getHighestZIndex() {
	var allElements = document.getElementsByTagName("*");
	var mZindices = new Array();
	mZindices[0] = 0;
	for (var i=0; i<allElements.length; i++) {
		if (allElements[i].nodeType == 1) {
			if (document.all) {
				if (allElements[i].currentStyle) {
					mZIndex = allElements[i].currentStyle["zIndex"];
					if (!isNaN(mZIndex)) mZindices.push(mZIndex);
				} else if (window.getComputedStyle) {
					mZIndex = document.defaultView.getComputedStyle(allElements[i], null).getPropertyValue("zIndex");
					if (!isNaN(mZIndex)) mZindices.push(mZIndex);
				}
			} else {
				if (allElements[i].currentStyle) {
					mZIndex = allElements[i].currentStyle["z-index"];
					if (!isNaN(mZIndex)) mZindices.push(mZIndex);
				} else if (window.getComputedStyle) {
					mZIndex = document.defaultView.getComputedStyle(allElements[i], null).getPropertyValue("z-index");
					if (!isNaN(mZIndex)) mZindices.push(mZIndex);
				}
			}
		}
	}
	mZindices = mZindices.sort(sortZindex);
	return parseInt(mZindices[mZindices.length - 1]);
}

function sortZindex(a, b) {
	return a - b;
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	return curtop;
}

function setPopupCookie(c_name, value) {	
	document.cookie = escape(c_name) + "=" + escape(value) + "; path=/";
}

function getPopupCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1) { 
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";",c_start)
			if (c_end == -1) c_end = document.cookie.length
				return unescape(document.cookie.substring(c_start, c_end))
		} 
	}
	return "";
}

function setPopupValue(pThis) {
	setPopupCookie("showPopup", pThis.checked);
}

