﻿/* Browser Detection */
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;
}

/* Preloading and Rollovers */
var rolloverOn;
var rolloverOff;
var rolloverImages;
var topNav;
var topNavImages;
var topNavSection = -1;

function tnavSelect(currSection){
    topNavSection = currSection - 1;
    addLoadEvent(tnavSet);
}

function tnavSet(){
    topNav = document.getElementById("upper_nav");
    topNavImages = topNav.getElementsByTagName("img");
    if(topNavImages.length != 0){
        if (topNavSection != -1){
            currNavImage = topNavImages[topNavSection];
            rolloverLight(currNavImage.id, currNavImage.navid);
            removeMouseListeners(currNavImage);
        }
    }
}

function preloadRollovers(roImages) {
    if (document.images) {
        var currMax = 0;
        if (roImages == null){
            rolloverImages = getElementsByTagClass("img", "rollover").concat(getElementsByTagClass("input", "rollover"));
            rolloverOn = new Array(rolloverImages.length);
            rolloverOff = new Array(rolloverImages.length);
        } else {
            rolloverImages = roImages;
            currMax = rolloverOff.length;
        }
        for (var i = 0; i < rolloverImages.length; i++) {
            rolloverOff[i + currMax] = new Image();
            rolloverOn[i + currMax] = new Image();

            if (isMSIE6 && isPNG(rolloverImages[i])) {
                rolloverOff[i + currMax].src = getPNGSrc(rolloverImages[i].runtimeStyle.filter);
            } else {
                rolloverOff[i + currMax].src = rolloverImages[i].src;
            }

            rolloverOn[i + currMax].src = getRolloverSrc(rolloverOff[i + currMax].src);
            rolloverImages[i].navid = i + currMax;
            if (rolloverImages[i].name.indexOf("$") == -1) {
                rolloverImages[i].name = "rollover" + (i + currMax);
                rolloverImages[i].id = "rollover" + (i + currMax);
            }
            
            setMouseListeners(rolloverImages[i]);
        }
    }
}

function setMouseListeners(roImage){
    roImage.onmouseover = function() {
        rolloverLight(this.id, this.navid);
    };
    roImage.onmouseout = function() {
        rolloverDim(this.id, this.navid);
    };
}

function removeMouseListeners(roImage){
    roImage.onmouseover = function() {};
    roImage.onmouseout = function() {};
}

function getElementsByTagClass(tagName, className) {
    var matches = new Array();
    var matchesByTag = document.getElementsByTagName(tagName);
    for (var i = 0; i < matchesByTag.length; i++) {
        if (matchesByTag[i].className.indexOf(className) != -1) {
            matches.push(matchesByTag[i]);
        }
    }
    return matches;
}

function getSelectedSrc(offSrc) {
    var regEx;
    if (offSrc.indexOf("_on.png") == -1){
        regEx = new RegExp("_off.png");
    } else {
        regEx = new RegExp("_on.png");
    }
    return offSrc.replace(regEx, "_down.png");
}

function getRolloverSrc(offSrc) {
    var regEx = new RegExp("_off.png");
    return offSrc.replace(regEx, "_on.png");
}

function getRolloutSrc(offSrc) {
    var regEx = new RegExp("_on.png");
    return offSrc.replace(regEx, "_off.png");
}

function rolloverLight(imgID, navID) {
    if (document.images) {
		if (isMSIE6 && isPNG(document.getElementById(imgID))){
			document.getElementById(imgID).runtimeStyle.filter = getRolloverSrc(document.getElementById(imgID).runtimeStyle.filter);
		} else {
            document.getElementById(imgID).src = rolloverOn[navID].src;
		}
    }
}

function rolloverDim(imgID, navID) {
    if (document.images) {
		if (isMSIE6 && isPNG(document.getElementById(imgID))){
			document.getElementById(imgID).runtimeStyle.filter = getRolloutSrc(document.getElementById(imgID).runtimeStyle.filter);
		} else {
            document.getElementById(imgID).src = rolloverOff[navID].src;
        }
    }
}

function isPNG(img){
    return ((img.src.toLowerCase().indexOf(".png") != -1) || (img.runtimeStyle.filter.toLowerCase().indexOf(".png") != -1));
}

function getPNGSrc(pngFilter) {
    var startSearch = "AlphaImageLoader(src='";
    var start = pngFilter.indexOf(startSearch) + startSearch.length;
    var end = pngFilter.indexOf("'", start);
    return pngFilter.substring(start, end);
}

//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;
	}
}

/* AddThis Configuration */
//var addthis_offset_top = -150;
var addthis_offset_left = 50;
var addthis_options = 'facebook, twitter, myspace, delicious, digg, email'; // List of Social Media Outlets
var addthis_header_color = "#FFFFFF"; // AddThis Popup Header Text Color
var addthis_header_background = "#7da1d6"; // AddThis Popup Header Background Color
var addthis_localize = {
    share_caption: "<strong>Share</strong>"
} // AddThis Popup Header Text
var addthis_share = {
} // AddThis URL to Share
var addthis_config = {
   data_ga_tracker: "pageTracker"
} // AddThis Google Analytics Integration

// Enables Assignment of Multiple window.onload Functions
function addLoadEvent(func) {   
	var oldonload = window.onload;   
	if (typeof window.onload != 'function') {   
		window.onload = func;   
	} else {   
		window.onload = function() {   
			if (oldonload) {   
				oldonload();   
			}   
			func();   
		}   
	}   
}   

/* Begin Google Tracking Code */

function onclickFix(theLink, trackName) {
    pageTracker._trackPageview(trackName);
    if (theLink.target.toLowerCase() == "_blank") {
        return true;
    } else {
        setTimeout("location.href = '" + theLink.href + "'", 500);
        return false;
    }
}

function getRecipeID() {

    var param = getParamValue('RecipeID');
    if (param == null) {
        param = "";
    }

    return param;
}

var homePodNames = ["recipes", "videos", "discussion", "experts", "prizes"];

function initPodsNav(){
    topNav = document.getElementById("upper_nav");
    topNavImages = topNav.getElementsByTagName("img");

    for (var i = 0; i < topNavImages.length; i++){
        topNavImages[i].podid = i;
        topNavImages[i].onmouseover = function(){
            showPods(homePodNames[this.podid]);
            rolloverLight(this.id, this.navid);
        };
        topNavImages[i].onmouseout = function(){};
    }
}

function showPods(podToShow) {
    var recipesPodDiv = document.getElementById("recipe_pods");
    var videosPodDiv = document.getElementById("video_pods");
    var discussionPodDiv = document.getElementById("discussion_pods");
    var expertsPodDiv = document.getElementById("expert_pods");
    var prizesPodDiv = document.getElementById("prizes_pods");

    if (podToShow == "recipes") {
        recipesPodDiv.style.display = "block";
        videosPodDiv.style.display = "none";
        discussionPodDiv.style.display = "none";
        expertsPodDiv.style.display = "none";
        prizesPodDiv.style.display = "none";
    }
    else if (podToShow == "videos") {
        recipesPodDiv.style.display = "none";
        videosPodDiv.style.display = "block";
        discussionPodDiv.style.display = "none";
        expertsPodDiv.style.display = "none";
        prizesPodDiv.style.display = "none";
    }
    else if (podToShow == "discussion") {
        recipesPodDiv.style.display = "none";
        videosPodDiv.style.display = "none";
        discussionPodDiv.style.display = "block";
        expertsPodDiv.style.display = "none";
        prizesPodDiv.style.display = "none";
    }
    else if (podToShow == "experts") {
        recipesPodDiv.style.display = "none";
        videosPodDiv.style.display = "none";
        discussionPodDiv.style.display = "none";
        expertsPodDiv.style.display = "block";
        prizesPodDiv.style.display = "none";
    }
    else if (podToShow == "prizes") {
        recipesPodDiv.style.display = "none";
        videosPodDiv.style.display = "none";
        discussionPodDiv.style.display = "none";
        expertsPodDiv.style.display = "none";
        prizesPodDiv.style.display = "block";
    }

    for (var i = 0; i < topNavImages.length; i++) {
        rolloverDim(topNavImages[i].id, topNavImages[i].navid);
    }
}


function toggleDiscussionTabs(idSelected) {
    var facebookDiv = document.getElementById("facebook_block");
    var twitterDiv = document.getElementById("twitter_block");

    if (facebookDiv != null && twitterDiv != null) {
        if (idSelected == "facebook" && facebookDiv.style.display != "block") {
            facebookDiv.style.display = "block";
            twitterDiv.style.display = "none";
        }
        else if (idSelected == "twitter" && twitterDiv.style.display != "block") {
            facebookDiv.style.display = "none";
            twitterDiv.style.display = "block";
        }
    }
}

addLoadEvent(preloadRollovers);

function showSTAF() {
    var stafDiv = document.getElementById("staf_popup");
    if (stafDiv != null) {
        if (stafDiv.style.display != "block") {
            stafDiv.style.display = "block";
        }
    }

}

function hideSTAF() {
  
    var stafDiv = document.getElementById("staf_popup");
    if (stafDiv != null) {
        stafDiv.style.display = "none";
    }

}
function showSTAFSent() {
    var stafSentDiv = document.getElementById("staf_sent_panel");
    var stafPanelDiv = document.getElementById("staf_panel");
    var stafDiv = document.getElementById("staf_popup");
    
    if (stafSentDiv != null) 
    {
        if (stafSentDiv.style.display != "block") 
        {
            stafSentDiv.style.display = "block";
        }
    }
    
    if (stafDiv != null) {
        if (stafDiv.style.display != "block") {
            stafDiv.style.display = "block";
        }
    }

    if (stafPanelDiv != null) {
        if (stafPanelDiv.style.display == "block") {
            stafPanelDiv.style.display = "none";
        }
    }

}


function popRules() {
    openWin("rules.aspx", 670, 520, false);
}

/*Popup Window Functions*/
var popUpWin;
var popDefWidth = 500;
var popDefHeight = 500;
var lastWidth;
var lastHeight;

//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);
        if (!popUpWin) {
            var swfMovie = getObject("lufthansa");
            swfMovie.openRules();
        } else {
            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);
    }
}

function isAutoTrackPage() {
    var excludedPages = ["sign_up.aspx"];
    var pageURL = location.pathname.toLowerCase();
    for (var i = 0; i < excludedPages.length; i++) {
        if (pageURL.indexOf(excludedPages[i].toLowerCase()) != -1) {
            return false;
        }
    }
    return true;
}

function toggleDiscussionContent(brandToShow) {
    var hellmannsFBDiv = document.getElementById("hellmanns_fb");
    var bestfoodsFBDiv = document.getElementById("bestfoods_fb");

    if (brandToShow == "hellmanns") {
        hellmannsFBDiv.style.display = "block";
        bestfoodsFBDiv.style.display = "none";
    }
    else {
        hellmannsFBDiv.style.display = "none";
        bestfoodsFBDiv.style.display = "block";
    }
}

function setRecipeNavColor() {
    /*set all to default color */
    recipeNavLinks = getElementsByTagClass("a", "recipe_nav");
    for (var i = 0; i < recipeNavLinks.length; i++) {
        recipeNavLinks[i].style.color = "#003366";
    }

    /*set selected one to selected color */
    var param = getParamValue('Category');
    if (param != null) {

        selectedDiv = document.getElementById(param);
        selectedDiv.style.color = "#0099CC";
    }
}
