/* LEFT NAV SCRIPTS */
var toplevel = "";
var sublevel = "";
var autoMatch = true;
var imgPrefix = "nav_";
var divPrefix = "lnav_";
var onSuffix = "_over";
var imgOn = new Array();
var imgOff = new Array();
var loaded = false;

function initNav(){
	preloadNav();
    //alert("Light Current:");	
	lightCurrent();
}

function preloadNav(){
	if (document.images){
		for (var i = 0; i < document.images.length; i++){
			if (document.images[i].id.indexOf(imgPrefix) == 0){
				imgOff[document.images[i].id] = new Image();
				imgOff[document.images[i].id].src = document.images[i].src;
				imgOn[document.images[i].id] = new Image();
				imgOn[document.images[i].id].src = document.images[i].src.substring(0,document.images[i].src.lastIndexOf(".")) + onSuffix + document.images[i].src.substring(document.images[i].src.lastIndexOf("."));
			}
		}
	}
	loaded = true;
}

function setCurrent(tlev, slev){
	autoMatch = false;
	toplevel = tlev;
	sublevel = slev;
	//alert("setCurrent: " + toplevel + "\nSub: " + sublevel);
}

function lightCurrent(){
	if (autoMatch){
		toplevel = getTopLevel();
		sublevel = getSubLevel();
	    //alert("Auto Match: " + toplevel + "\nSub: " + sublevel);
	}
	//alert("Top: " + toplevel + "\nSub: " + sublevel);
	if (toplevel != ""){
		//light(toplevel);
		setStyleOn(toplevel);
		show(toplevel);
	}
	if (sublevel != ""){
		//light(sublevel);
		bold(sublevel);
	}
}

function setStyleOn(divID){
	//alert("Style: " + divID); 
    var e = document.getElementById(divID);
    if (e) e.className+= "-on";
}

function bold(divID){
    //sublevelDiv.style.font-weight = "bold"
    var e = document.getElementById(divID);
    if (e) {
        e.style.fontWeight = 'bold';
        e.style.color = "#92342d";  // #773344
    }
}
function light(imgID){
	if (document.images && loaded){
		if (document.images[imgPrefix + imgID]){
			document.images[imgPrefix + imgID].src = imgOn[imgPrefix + imgID].src;
		}
	}
}

function dim(imgID){
	if (document.images && loaded && (imgID != toplevel) && (imgID != sublevel)){
		if (document.images[imgPrefix + imgID]){
			document.images[imgPrefix + imgID].src = imgOff[imgPrefix + imgID].src;
		}
	}
}

function show(divID){
	sublevelDiv = getObject(divPrefix + divID);
	if (sublevelDiv){
		sublevelDiv.style.display = "inline";
	    //alert("Show Div: " + divID + " Sub: " + sublevelDiv); 
	}
}

// Cross-Browser Object Reference Retriever for IE and Gecko/Netscape
function getObject(objectID){
	if (document.all!=null){
		return document.all[objectID];
	} else if (document.getElementById){
		return document.getElementById(objectID);
	}
}

function getTopLevel(){
	var thePath = location.pathname.toLowerCase();
	//alert("getTopLevel :" + thePath);
	// "/*.*" || "/" || "/*"
	if (thePath.lastIndexOf("/") == 0){
		// "/*.*"
		if (thePath.indexOf(".") != -1){
			thePath = thePath.substring(1, thePath.indexOf("."));
			if (thePath == "default"){
				return "";
			} else {
				return thePath;
			}
		// "/" || "/*"
		} else {
			return thePath.substring(1);
		}
	// "/*/" || "/*/*.*"
	} else {
		return thePath.substring(1, thePath.lastIndexOf("/"));
	}
}

function getSubLevel(){
	var thePath = location.pathname.toLowerCase();
	if (thePath.indexOf(".") == -1){
		//alert("getSubLevel :" + thePath);
		return "";
	} else {
		thePath = thePath.substring(thePath.lastIndexOf("/") + 1, thePath.indexOf("."));
		if (thePath == "default"){
			//alert("getSubLevel :" + thePath);
			return "";
		} else {
			return thePath;
			//alert("getSubLevel :" + thePath);
		}
	}
}

window.onload = initNav;

/* STORE LOCATOR SCRIPTS */
//Clear field descriptor to allow user input
function resetZip(textField){
	if ((textField.value == 'enter ZIP code') || (textField.value == 'invalid ZIP code')){
	  	textField.style.color = "#748468";
		textField.value = '';
	}
}

//Determine if zip/postal code is in a valid US format
function validZip(textField){
	var zip_code = textField.value;
   // US formats
   if (( zip_code.length < 5 ) ||
       ( zip_code.length == 5 && (isNaN(zip_code) || zip_code.valueOf() < 1 || zip_code.indexOf(" ", 0) >= 0) ) ||
       ((zip_code.length > 5) && !( zip_code.length == 10 && zip_code.indexOf("-", 0) == 5 ))
      ) {
	  textField.style.color = "#FF0000";
	  textField.value = 'invalid ZIP code';
      return false;
   } else {
      return true;
   }
}

function findStore(textField) {
	if (validZip(textField)) {
		window.location.href = 'store_locator.aspx?zip=' + zip;
	}
	return false;
}


/* IMAGE POPUP SCRIPTS */
var popUpWin;

function showImg(inPix){
	var pixWidth = 250;
	var pixHeight = 412;
	if ((inPix.indexOf("pastasauce_") != -1) || (inPix.indexOf("frozen_") != -1) || (inPix.indexOf("rth_") != -1)){
		pixHeight = 567;
	} else if ((inPix.indexOf("simmersauce_") != -1) || (inPix.indexOf("chocolates_") != -1)) {
		if ((inPix.indexOf("katahdin") != -1) || (inPix.indexOf("isle_of_skye") != -1)) {
			pixHeight = 623;
		} else {
			pixHeight = 521;
		}
	}
	var url = 'nutrition_facts.aspx?imgsrc=' + inPix ;
	if (popUpWin != null){
		if (popUpWin.closed){
			popUpWin = window.open(url,'nufacts',"width=" + pixWidth + ",height=" + pixHeight + "," + getCentered(pixWidth,pixHeight) + ",resizable=no");
		} else {
			popUpWin.location.href = url;
		}
	} else {
		popUpWin = window.open(url,'nufacts',"width=" + pixWidth + ",height=" + pixHeight + "," + getCentered(pixWidth,pixHeight) + ",resizable=no");
	}
	popUpWin.focus();
}

function getCentered(popupWidth, popupHeight){
	var indentNetscapeWidth = 'screenX=' + getIndent(popupWidth, false);
	var indentNetscapeHeight = 'screenY=' + getIndent(popupHeight, true);
	var indentMSIEWidth = 'left=' + getIndent(popupWidth, false);
	var indentMSIEHeight = 'top=' + getIndent(popupHeight, true);
	var centerCoordinates = indentNetscapeWidth + ',' + indentNetscapeHeight + ',' + indentMSIEWidth + ',' + indentMSIEHeight;
	return centerCoordinates;
}

function getIndent (popupDimension, isHeight){
	if (isHeight){
		return ((screen.availHeight - popupDimension) / 2);
	} else {
		return ((screen.availWidth - popupDimension) / 2);
	}
}
//-->
