﻿/* Top Navigation Highlighting */
//var tnavNames = ["home", "contest", "recipes", "crafts", "traditions", "ourfamily", "share"];
//var tnavMax = tnavNames.length;
//var tnavOn = new Array(tnavMax);
//var tnavPrefix = "images/upper_nav_";
//var tnavSuffix = "_on.png";

//// Preload Top Nav Images
//if (document.images){
//    for (var i = 0; i < tnavMax; i++){
//        tnavOn[i] = new Image();
//        tnavOn[i].src = tnavPrefix + tnavNames[i] + tnavSuffix;
//    }
//}

// Browser identity
var isMac = false;
var isSafari = false;
var isFirefox = false;
var isMSIE = false;
var isMSIE6 = false;

if (navigator.userAgent.indexOf("Macintosh") != -1){
    isMac = true;
}

if (navigator.userAgent.indexOf("Safari") != -1){
	isSafari = true;
} else if (navigator.userAgent.indexOf("MSIE") != -1){
    isMSIE = true;
    if (navigator.userAgent.indexOf("MSIE 6.0") != -1){
	    isMSIE6 = true;
    }
} else if (navigator.userAgent.indexOf("Firefox") != -1){
	isFirefox = true;
}

var macSafari = isMac;


// Highlight Current Top Nav Image
function tnavSelect(navIndex){
    if (document.images){
        document.images["tnav" + navIndex].src = tnavOn[navIndex - 1].src;
    }
}

/* POPUP WINDOW FUNCTIONS */
var popUpWin;
var popDefWidth = 570;
var popDefHeight = 400;
var lastWidth;
var lastHeight;
var popZIndex = 25;

//Open URL in new window with fixed dimensions
function openWin(url, pixWidth, pixHeight, canScroll){
	var winFeatures = "width=" + pixWidth + ",height=" + pixHeight + "," + getCentered(pixWidth,pixHeight) + ",resizable=no,scrollbars=";
	if (canScroll){
		winFeatures += "yes";
	} else {
		winFeatures += "no";
	}
	if (popUpWin != null){
		if (popUpWin.closed){
			popUpWin = window.open(url, "popwin", winFeatures);
			popUpWin.focus();
		} else if ((lastWidth == pixWidth) && (lastHeight == pixHeight)){
			popUpWin.location.href = url;
			popUpWin.focus();
		} else {
			popUpWin.close();
			var timeDelay = 0;
			if (navigator.userAgent.indexOf("Safari") != -1){
				timeDelay = 1000;
			}
			var winWait = window.setTimeout('popUpWin = window.open("' + url + '", "popwin", "' + winFeatures + '");popUpWin.focus();', timeDelay);
		}	
	} else {
		popUpWin = window.open(url, "popwin", winFeatures);
		popUpWin.focus();
	}
	lastWidth = pixWidth;
	lastHeight = pixHeight;
}

//Get coordinates for centering window
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;
}

//Determine top and left margins for window
function getIndent (popupDimension, isHeight){
	if (isHeight){
		return ((screen.availHeight - popupDimension) / 2);
	} else {
		return ((screen.availWidth - popupDimension) / 2);
	}
}

//Close popup and redirect parent to URL
function closeAndGo(url){
	var newWin;
	if (window.opener != null){
		if (window.opener.closed){
			newWin = window.open(url,'newwin','width=' + screen.availWidth + ',height=' + screen.availHeight + ',toolbar=yes,status=yes,directories=yes,location=yes,menubar=yes,personalbar=yes,resizable=yes,scrollbars=yes');
			newWin.focus();
		} else {
			window.opener.location.href = url;
			window.opener.focus();
		}
	} else {
		newWin = window.open(url,'newwin','width=' + screen.availWidth + ',height=' + screen.availHeight + ',toolbar=yes,status=yes,directories=yes,location=yes,menubar=yes,personalbar=yes,resizable=yes,scrollbars=yes');
		newWin.focus();
	}
	window.close();
}

function popWin(theURL){
	openWin(theURL, popDefWidth, popDefHeight);
}

//Gets Parameter Values from QueryString
function getParamValue(paramName){
	paramName += "=";
	var paramLength=paramName.length;
	var start = -1;
	if (location.search.indexOf("?" + paramName) != -1){
		start = location.search.indexOf("?" + paramName) + 1;
	} else if (location.search.indexOf("&" + paramName) != -1){
		start = location.search.indexOf("&" + paramName) + 1;
	}
	if (start != -1){
		if (location.search.indexOf("&",start + 1) != -1){
			tempValue = location.search.substring(start + paramLength, location.search.indexOf("&",start + 1));
		}
		else {
			tempValue = location.search.substring(start + paramLength);
		}
		return tempValue;
	}
	else {
		return null;
	}
}

function isInArray(theValue, theArray){
	for (var i = 0; i < theArray.length; i++){
		if (theValue == theArray[i]){
			return true;
		}
	}
	return false;
}

function getIndex(theValue, theArray){
	for (var i = 0; i < theArray.length; i++){
		if (theValue == theArray[i]){
			return i;
		}
	}
	return -1;
}



/* Coupon Functionality */
//var BrandCheckBoxes =  new Array();
function CheckAllBoxes(checkState) {
    for (var arrayIndex=0; arrayIndex<BrandCheckBoxes.length; arrayIndex++) {
        if (document.all) document.all[BrandCheckBoxes[arrayIndex]].checked=checkState;
        if (document.lauers) document.layers[BrandCheckBoxes[arrayIndex]].checked=checkState;
        if (document.getElementById) document.getElementById(BrandCheckBoxes[arrayIndex]).checked=checkState; }
}

function GetAllCheckedBoxes() {
    boxes = "";

    for (var arrayIndex=0; arrayIndex<BrandCheckBoxes.length; arrayIndex++) {
        if (document.all) {
            if(document.all[BrandCheckBoxes[arrayIndex]].checked) boxes += document.all[BrandCheckBoxes[arrayIndex]].name + ",";
        }
        if (document.layers) {
            if(document.layers[BrandCheckBoxes[arrayIndex]].checked) boxes += document.layers[BrandCheckBoxes[arrayIndex]].name + ",";
        }
        if (document.getElementById) {
            if(document.getElementById(BrandCheckBoxes[arrayIndex]).checked) boxes += document.getElementById(BrandCheckBoxes[arrayIndex]).name + ",";
        }
    }
        
    return boxes;
}

/* AddThis Configuration */
var addthis_wait;
var addthis_options = 'facebook, google, twitter, yahoomail, digg, stumbleupon, delicious, blogger, more';
var addthis_header_color = "#FFFFFF";
var addthis_header_background = "#859a4d";
var addthis_offset_top = 0;
var addthis_offset_left = -108;
var addthis_localize = {
    share_caption: "<strong>Share <i>Nestl&eacute; Family</i></strong>"
}

// FUNCTIONS FOR ADD THIS
function AddThisFunction1(obj, url) {
    addthis_open(obj, '', url, 'Nestl&eacute;&reg; Toll House&reg;');
    ReplaceHoverLinks();
    SetTimer();
    return true;
}

function AddThisFunction2() {
    var ret = addthis_sendto();
    ReplaceAddThisLinks();
    return ret;
}

function ShowAddThisPopup() {
    document.getElementById('at20mc').setAttribute("style", "display:block");
}

function ReplaceHoverLinks() {
    if (document.getElementById('at_hover') != null) {
		var addThisHover = document.getElementById('at_hover');
		var socialMediaOutlets = ["facebook", "google", "twitter", "yahoomail", "digg", "stumbleupon", "delicious", "blogger"];
		for (var i = 0; i < socialMediaOutlets.length; i++){
			addThisHover.innerHTML = addThisHover.innerHTML.replace("return addthis_sendto('" + socialMediaOutlets[i] + "');", "openWin('/bumper.aspx?addthis=" + socialMediaOutlets[i] + "&prevurl=" + stripAddThisParam(window.location.href) + "', 500, 400, false);")
		}
    }        
}

function SetTimer() {
    if (document.getElementById('at_hover') == null)
        addthis_wait = setInterval("ReplaceHoverLinks()", 1000);
    else
        clearInterval(addthis_wait);    
}

function ReplaceAddThisLinks() {
	var socialMediaOutlets = ["aim", "amazonwishlist", "amenme", "aolmail", "arto", "ask", "backflip", "baidu", "ballhype", "bebo", "bitly", "bizsugar", "bleetbox", "blinklist", "blogger", "bloggy", "blogmarks", "bobrdobr", "brainify", "bryderi", "buddymarks", "buzz", "care2", "citeulike", "connotea", "delicious", "designmoo", "digg", "diglog", "diigo", "doower", "dosti", "dotnetkicks", "dzone", "edelight", "ekudos", "email", "eucliquei", "evernote", "fabulously40", "facebook", "fark", "faves", "favorites", "favoritus", "fnews", "folkd", "forgetfoo", "fresqui", "friendfeed", "funp", "gabbr", "gacetilla", "globalgrind", "gmail", "google", "googletranslate", "gravee", "grumper", "habergentr", "hadashhot", "hatena", "hellotxt", "hemidemi", "hipstr", "hotklix", "hotmail", "hyves", "jumptags", "kaboodle", "kirtsy", "linkagogo", "linkedin", "live", "livejournal", "lunch", "lynki", "meneame", "mindbodygreen", "misterwong", "mixx", "multiply", "myaol", "mylinkvault", "myspace", "n4g", "netlog", "netvibes", "netvouz", "newstrust", "newsvine", "nujij", "oknotizie", "oneview", "oyyla", "phonefavs", "pingfm", "planypus", "plaxo", "plurk", "polladium", "posterous", "print", "printfriendly", "propeller", "pusha", "reddit", "segnalo", "shetoldme", "simpy", "slashdot", "smaknews", "sodahead", "sonico", "sphinn", "squidoo", "startaid", "strands", "studivz", "stumbleupon", "stylehive", "svejo", "symbaloo", "tagza", "technet", "technorati", "tellmypolitician", "thisnext", "tipd", "tumblr", "twitter", "typepad", "viadeo", "virb", "wordpress", "worio", "wovre", "wykop", "yahoobkm", "yahoomail", "yammer", "yardbarker", "yigg", "youmob"];
	for (var i = 0; i < socialMediaOutlets.length; i++){
		document.getElementById('ati_' + socialMediaOutlets[i]).setAttribute("onclick", "openWin('/bumper.aspx?addthis=' + socialMediaOutlets[i] + '&prevurl=" + stripAddThisParam(window.location.href) + "', 500, 400, false);")
	}
}

function stripAddThisParam(url){
    var atStart = url.indexOf("addthislink=");
    var atEnd;
    if (atStart != -1){
        atEnd = url.indexOf("&", atStart);
        if (atEnd == -1){
            return url.substring(0, atStart);
        } else {
            return (url.substring(0, atStart) + url.substring(atEnd + 1));
        }
    }
    return url;
}

function leaveNF(){
	var addThisDestination = getParamValue("addthis");
	if (addThisDestination != null){
		var prevURL = stripAddThisParam(getParamValue("prevurl"));
		var qsStart = prevURL.indexOf("?");
		if (qsStart == -1){
			prevURL += "?";
		} else {
			prevURL += "&";
		}
		prevURL += "addthislink=" + addThisDestination;
		location.href = prevURL;
	}
}

function leaveNFPopup(){
	var addThisDestination = getParamValue("addthis");
	if (addThisDestination != null){
		var prevURL = stripAddThisParam(getParamValue("prevurl"));
		var qsStart = prevURL.indexOf("?");
		if (qsStart == -1){
			prevURL += "?";
		} else {
			prevURL += "&";
		}
		prevURL += "addthislink=" + addThisDestination;
		window.opener.location.href = prevURL;
		window.close();
	}
}

function getDiggURL(shareURL){
    var diggURL = "http://digg.com/submit";
    diggURL += "?thumbnails=1&url=" + escape(stripAddThisParam(shareURL));
    return diggURL;
}

function checkAddThis(){
	var addThisAllowed = getParamValue("addthislink");
	if (addThisAllowed != null){
	    if (isMSIE && (addThisAllowed.toLowerCase() == "digg")){
	        openWin(getDiggURL(location.href), 780, 580, true);
	    } else {
    		addthis_sendto(addThisAllowed);
    	}
	}
}

function addLoadEvent(func) {   
	var oldonload = window.onload;   
	if (typeof window.onload != 'function') {   
		window.onload = func;   
	} else {   
		window.onload = function() {   
			if (oldonload) {   
				oldonload();   
			}   
			func();   
		}   
	}   
}   
  
addLoadEvent(checkAddThis);

// Coupons
function printCoupons() {
    openNewWindow("/CouponsBumper.aspx?o=" + GetAllCheckedBoxes(), "Loading Coupons");
}

function openNewWindow(url, name) {
    var winFeatures = "";//"menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
    
    window.open(url);
}

// Check enter key on text inputs
function checkEnter(e){ //e is event object passed from function invocation
    var characterCode; // literal character code will be stored in this variable

    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else{
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }

    if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
        document.forms[0].submit(); //submit the form
        return false;
    }
    else
        return true;
}

// Custom Tracking Page Name
var customPageName = ""; // set this variable for dynamic page view tracking

// detect browser
browser = {
    version: (/.+(?:rv|it|ra|IE)[\/: ]([\d.]+)/.exec(navigator.userAgent) || [0,'0'])[1],
    safari: /webkit/.test(navigator.userAgent),
    opera: /opera/.test(navigator.userAgent),
    MSIE: /MSIE/i.test(navigator.userAgent) && !/opera/.test(navigator.userAgent),
    mozilla: /mozilla/.test(navigator.userAgent) && !/(compatible|webkit)/.test(navigator.userAgent)
};

function buildAddThisLink() {
    //<a href="http://addthis.com/bookmark.php?v=250&amp;pub=nestlefamily" onmouseover="return AddThisFunction1(this, location.href);" onmouseout="addthis_close()" onclick="return AddThisFunction2();">
    if(!browser.MSIE || (browser.version >= 7)) {
        document.write('<a href=\"http://addthis.com/bookmark.php?v=250&amp;pub=nestlefamily\" onmouseover=\"return AddThisFunction1(this, location.href);\" onmouseout=\"addthis_close()\" onclick=\"return AddThisFunction2();\">');
    }
    else { // ie 6
        document.write('<a href=\"http://addthis.com/bookmark.php?v=250&amp;pub=nestlefamily\">');
    }
}