req = null;
function getCommenterName() {
	cValue = "";
	dc = document.cookie;
	cookies = dc.split(";");
	for (x=0;x<cookies.length;x++) {
		if (cookies[x].match(/commenter_name=/)) {
		cValue = cookies[x].replace(/commenter_name=/, "");
		}
	}
	if (cValue.length>1) {
		return unescape(cValue);
	} else {
		return "";
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
       var pair = vars[i].split("=");
       if (pair[0] == variable) {
           return pair[1];
       }
    }
    return "";
}

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See:  http://www.msc.cornell.edu/~houle/javascript/randomizer.html

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

/* -------- Navigation Logic BEGIN ----------- */
// create global timeout variables
var timeoutID;
var timeoutSectionID;
	
var navLoaded = false;
var navIntervalID = 0;

var activeSectionItem;
var activeSubNavItem;

navIntervalID = setInterval("setNavigation()", 100);

// once the navigation element is loaded, set it up for the site that is currently being viewed
function setNavigation()
{
	var navElementsLoaded = false;
	if (!navLoaded)
	{
		// check to see if the body loaded yet
		if($("body").length > 0) {
			// set the active state in the navigation and subnavigation
			if($("body").attr("class")) {
				// if the current page is in a main section, set the active nav and subnav items
				if ($("#header").length > 0) {
					// check to see if the element with "header" id has loaded yet, to get its class
					activeSectionItem = "ul#mainnav li#item-" + $("body").attr("class"); 
					activeSubNavItem = "ul#mainnav li#item-"+ $("#header").attr("class");
					// check if the nav elements have loaded and set the variable 
					if (($(activeSubNavItem).length > 0) && ($(activeSectionItem).length > 0) && ($(activeSectionItem).children("ul").length > 0)) {
						//$(activeSubNavItem).addClass("selected");
						// if the body is NOT SE, we need to set hover handler for section nav
						if (($("body").attr("class") != "SE") && ($("ul#sectionnav li.has-subnav").length > 0)) {
								$("ul#sectionnav li.has-subnav").hover(rollOverSectionNav,resetSectionNav);
								navElementsLoaded = true;
						} 
						if($("ul#sectionnav li.hasmore").length > 0){
							$("ul#sectionnav li.hasmore").hover(rollOverSectionNav,resetSectionNav);
							navElementsLoaded = true;
						} else {
							navElementsLoaded = true;
						}
					}
				} 
			}	else {
				// otherwise turn sites nav on 
				activeSectionItem = "ul#mainnav li#item-sites"; 
				//  AND check to select the main nav item that doesn't have a subnav
				if (($("ul#mainnav li#item-" + $("body").attr("id")).length > 0) && ($("body").attr("id") == "recipes")) {
					$("ul#mainnav li#item-" + $("body").attr("id")).addClass("selected");
					// attach the hover functions to the dropdowns
					if ($("ul#sectionnav li.has-subnav").length > 0) {
							$("ul#sectionnav li.has-subnav").hover(rollOverSectionNav,resetSectionNav);
							navElementsLoaded = true;
					}
						if($("ul#sectionnav li.hasmore").length > 0){
							$("ul#sectionnav li.hasmore").hover(rollOverSectionNav,resetSectionNav);
							navElementsLoaded = true;
						} else {
							navElementsLoaded = true;
						}
					activeSubNavItem = "";
				} else {  // for all other pages with all other body ids (we don't know what body ids are being used)
					activeSubNavItem = "ul#mainnav li#item-SE";
				}
				
				// check if the nav elements have loaded and set the variable 
				if (($(activeSubNavItem).length > 0) && ($(activeSectionItem).length > 0) && ($(activeSectionItem).children("ul").length > 0)) {
					navElementsLoaded = true;
				}
			}
			
			
			// if all the elements have loaded, show the active nav and subnav items and set handlers for hover on navigation
			if (navElementsLoaded) {
				if(activeSubNavItem != ""){
					$(activeSubNavItem).addClass("selected"); // is this ok to call in both cases?
				}
				$(activeSectionItem).addClass("selected");
				$(activeSectionItem).children("ul").addClass("selected");
				// set handlers for hover on navigation
				if ($("ul#mainnav li.has-subnav").length > 0) { 
					$("ul#mainnav li.has-subnav").hover(rollOverNav,resetNav);
					navLoaded = true;
				}
			}
		} 
	} else {
		// elements have loaded and navigation has been set so clear interval to stop running the function
		clearInterval(navIntervalID);
	} 
}
	
// when user hovers over a main navigation item, show the correct nav item and subnavigation
function rollOverNav(){
		// be sure to clear a previous timeout
		window.clearTimeout(timeoutID);
		$("ul#mainnav li.has-subnav").removeClass("selected");
		$("ul#mainnav li.has-subnav ul").removeClass("selected");
		hoverItem = "ul#mainnav li#"+$(this).attr("id");
		$(hoverItem).addClass("selected");
		$(hoverItem).children("ul").addClass("selected");
}
// when user leaves navigation, reset navigation to default active items	
function resetNav(){
// add a delay to execute the navigation reset
	timeoutID = window.setTimeout(function(){
		$("ul#mainnav li.has-subnav").removeClass("selected");
		$("ul#mainnav li.has-subnav ul").removeClass("selected");
		$(activeSectionItem).addClass("selected");
		$(activeSectionItem).children("ul").addClass("selected");
		if(activeSubNavItem != ""){
			$(activeSubNavItem).addClass("selected");
		}
	}, 200);
}
	
// when user hovers over a section navigation item, show the dropdown menu
function rollOverSectionNav(){
	$(this).addClass("selected");
}
// when the user leaves a section navigation item, hide the dropdown menu
function resetSectionNav(){
	$(this).removeClass("selected");
}	
/* -------- Navigation Logic END ----------- */

