﻿//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 urlParam(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) {
        return null;
    }
    return unescape(results[1]);
};

var FBO = {};
var isHellmanns = (location.host.toLowerCase().indexOf("bestfoods") == -1);
if (isHellmanns){
    FBO.brandName = "Hellmann's";
    FBO.brandStripped = "hellmanns";
} else {
    FBO.brandName = "Best Foods";
    FBO.brandStripped = "bestfoods";
}

function getSharePath() {
    var sharePath = location.href;
    if (sharePath.indexOf("?") != -1) {
        sharePath = sharePath.substring(0, sharePath.indexOf("?"));
    }
    sharePath = sharePath.substring(0, sharePath.lastIndexOf("/") + 1);
    return sharePath;
}

function getShareProxy(config) { 
    var sp = getSharePath() + "shareproxy.aspx";
    sp += "?title=" + escape(config.title);
    sp += "&desc=" + escape(config.description);
    sp += "&imgsrc=" + escape(getSharePath() + config.imgsrc);
    sp += "&page=" + escape(getSharePath() + config.url);
    return sp;
}



// Bring Homepage Facebook or Twtitter Tab to the Foreground When Selected
function toggleDiscussionTabs(idSelected) {
    var facebookDiv = document.getElementById("facebook_feed");
    var twitterDiv = document.getElementById("twitter_feed");

    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";
            /*
            $('.scrollpane').jScrollPane({
                showArrows: true,
                verticalDragMinHeight: 19,
                verticalDragMaxHeight: 19,
                verticalGutter: 7
            });
            */
        }
    }
}

/* AddThis Configuration */
//var addthis_offset_top = -150;
var addthis_offset_left = 0;
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",
        data_track_clickback: true
} // AddThis Google Analytics Integration

function newRecipePrint(id, version) {
    window.open('/printrecipes.aspx?RecipeID=' + id + '&Version=' + version + '&trackHits=false', 'printwin', 'width=700,height=550,toolbar=yes, scrollbars=yes,resizable=yes,menubar=yes')
         }

 function newRecipePrintLocal() {
             window.open('printrecipe.aspx')

         }



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 onclickFix(theLink, category, action, label) {
    if ((label != null) && (label != '')) {
        pageTracker._trackEvent(category, action, label);
    } else {
        pageTracker._trackEvent(category, action);
    }
    if (theLink.target.toLowerCase() == "_blank") {
        return true;
    } else {
        setTimeout("location.href = '" + theLink.href + "'", 500);
        return false;
    }
}

function isAutoTrackPage() {
    var excludedPages = [];
    var pageURL = location.pathname.toLowerCase();
    for (var i = 0; i < excludedPages.length; i++) {
        if (pageURL.indexOf(excludedPages[i].toLowerCase()) != -1) {
            return false;
        }
    }
    return true;
}

// 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();
        }
    }
}

function clearField(textbox, isfocus) {

    if (isfocus) {

        if (textbox.value == "ENTER EMAIL ADDRESS") {

            textbox.value = "";

        }
    }
    else if (textbox.value == "") {
    textbox.value = "ENTER EMAIL ADDRESS";
    }

}

function FacebookShare(recipeURL) {
    var recipeImage = recipeURL.substring(0, recipeURL.indexOf('.')) + ".jpg";
    var options = {
        method: "feed",
        name: "Take meals to the next level with " + FBO.brandName,
        link: location.protocol + "//" + location.hostname + "/promotions/chickenchallenge/" + recipeURL,
        picture: location.protocol + "//" + location.hostname + "/promotions/chickenchallenge/media/images/web/" + recipeImage,
        description: "Make chicken juicier, casseroles creamier with delicious, easy recipes from " + FBO.brandName + "®."
    }
    FB.ui(options, function(response) {
        if (response && response.post_id) {
            pageTracker._trackEvent('Social', 'Facebook Share');
        } else {
        }
    });

}

var isFacebook = (location.pathname.toLowerCase().indexOf("/facebook") != -1);
window.fbAsyncInit = function() {
    FB.init({ appId: isHellmanns ? "207521609310854" : "189468604457843", status: true, cookie: true, xfbml: true });
    if (isFacebook) {
        if (location.href.toLowerCase().indexOf("registration") != -1) {
            FB.Canvas.setSize({ width: 760, height: 1500 });
        } else
        if (location.href.toLowerCase().indexOf("tab") != -1) {
            FB.Canvas.setSize({ width: 520, height: 800 });
        } else {
            if (location.href.toLowerCase().indexOf("entersweeps") != -1) {
                $('body').css('overflow', 'hidden');
            }
            FB.Canvas.setAutoResize();
        }
        FB.Canvas.scrollTo(0, 0);
    }
    FB.Event.subscribe('edge.create', function(href, widget) {
        pageTracker._trackEvent('Social', 'FB Like', href);
    });
};

