﻿/* 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;
}

/* Product Page Tabs */
var prodTabNames = ["comments", "ingredients", "wheretobuy"];
var prodTabs;
var prodTabCurrent;

function initProdTabs(){
    if (location.pathname.toLowerCase().indexOf("products_") != -1){
        var tmpTab;
        prodTabs = new Array();
        for (var i = 0; i < prodTabNames.length; i++){
            tmpTab = new Object();
            tmpTab.tab = document.getElementById(prodTabNames[i] + "Link");
            tmpTab.content = document.getElementById(prodTabNames[i]);
            prodTabs.push(tmpTab);
        }
        prodTabCurrent = -1;
        showProdTab(0);
    }
}

function showProdTab(divID){
    pageTracker._trackPageview(getTrackPath(prodTabNames[divID]));
    if (prodTabCurrent != -1){
        toggleProdTab(prodTabCurrent, false);
    }
    toggleProdTab(divID, true);
    prodTabCurrent = divID;
    addCommentsToggle(false, true);
}

function toggleProdTab(divID, showIt){
    prodTabs[divID].tab.className = showIt ? "tabon" : "taboff";
    prodTabs[divID].content.style.display = showIt ? "block" : "none";
}

function addCommentsToggle(showIt, ignoreTrack) {
    var msgDIV = document.getElementById("commentsAdd");
    var formDIV = document.getElementById("commentsForm");
    msgDIV.style.display = showIt ? "none" : "block";
    formDIV.style.display = showIt ? "block" : "none";
    if (!ignoreTrack){
        pageTracker._trackPageview(getTrackPath((showIt ? "commentsform" : prodTabNames[0])));
    }
}

function getTrackPath(eventName){
    return (location.pathname.substring(0, location.pathname.indexOf(".")) + "_" + eventName + ".aspx");
}

function cookieDelete(name){
	document.cookie = name + "=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;";
}

function cookieRead(Name){
	var cookieValue = "";
	var search = Name + "=";
	if(document.cookie.length > 0){ 
		offset = document.cookie.indexOf(search);
		if (offset != -1){ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	return cookieValue;
}

function cookieWrite(Name, Value){
	document.cookie = Name + "=" + escape(Value) + "; path=/;";
}

/*Popup Window Functions*/
var popUpWin;
var popDefWidth = 500;
var popDefHeight = 650;
var lastWidth;
var lastHeight;

//Open URL in new window with fixed dimensions
function openWin(url, winName, pixWidth, pixHeight, canScroll) {
    var winFeatures = "width=" + pixWidth + ",height=" + pixHeight + "," + getCentered(pixWidth, pixHeight) + ",resizable=no,scrollbars=";
    if (canScroll) {
        winFeatures += "yes";
    } else {
        winFeatures += "no";
    }
    popUpWin = window.open(url, winName, winFeatures);
    popUpWin.focus();
}

//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);
    }
}

/* Preloading and Rollovers */
var rolloverOn;
var rolloverOff;
var rolloverImages;

function preloadRollovers() {
    if (document.images) {
        rolloverImages = getElementsByTagClass("img", "rollover").concat(getElementsByTagClass("input", "rollover"));
        rolloverOn = new Array(rolloverImages.length);
        rolloverOff = new Array(rolloverImages.length);
        for (var i = 0; i < rolloverImages.length; i++) {
            rolloverOff[i] = new Image();
            rolloverOn[i] = new Image();

            if (isMSIE6 && isPNG(rolloverImages[i])) {
                rolloverOff[i].src = getPNGSrc(rolloverImages[i].runtimeStyle.filter);
            } else {
                rolloverOff[i].src = rolloverImages[i].src;
            }

            rolloverOn[i].src = getRolloverSrc(rolloverOff[i].src);
            rolloverImages[i].navid = i;
            if (rolloverImages[i].name.indexOf("$") == -1) {
                rolloverImages[i].name = "rollover" + i;
                rolloverImages[i].id = "rollover" + i;
            }
            rolloverImages[i].onmouseover = function() {
                rolloverLight(this.id, this.navid);
            }
            rolloverImages[i].onmouseout = function() {
                rolloverDim(this.id, this.navid);
            }
        }
    }
}

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 == className) {
            matches.push(matchesByTag[i]);
        }
    }
    return matches;
}

function getRolloverSrc(offSrc) {
    var regEx = new RegExp("_off");
    return offSrc.replace(regEx, "_on");
}

function getRolloutSrc(offSrc) {
    var regEx = new RegExp("_on");
    return offSrc.replace(regEx, "_off");
}

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);
}

function removeQS() {
    if (location.href.indexOf("?") != -1) {
        return location.href.substring(0, location.href.indexOf("?"));
    } else {
        return location.href;
    }
}

/* AddThis Configuration */
var addthis_options = 'facebook, twitter, digg, delicious, stumbleupon, myspace, email';
var addthis_header_color = "#FFFFFF";
var addthis_header_background = "#e24912";
var addthis_offset_top = 1;
var addthis_offset_left = -156;
var addthis_config = {
   data_ga_tracker: "pageTracker"
} // AddThis Google Analytics Integration
var addthis_localize = {
    share_caption: "<div align='center'><img src='images/hdr_bookmark_share.gif'></div>"
};
var addthis_share = {
    url:removeQS() + "?utm_source=SocialMedia&utm_medium=socialmedia&utm_campaign=2010_q1_dove_mencare"
};

function clearOtherCheckBoxes(chkBox){
    var theForm = document.forms[0];  

    switch(chkBox){
        case theForm.ReceiveMoreInformation:
            if (chkBox.checked){
                if (theForm.ReceiveMoreInformation_DoveMenCare !=null){
                    theForm.ReceiveMoreInformation_DoveMenCare.checked = false;            
                }
                if (theForm.ReceiveMoreInformation_Dove !=null){
                    theForm.ReceiveMoreInformation_Dove.checked = false;            
                }
            }
            break;
        case theForm.ReceiveMoreInformation_DoveMenCare:
            if (chkBox.checked){
                theForm.ReceiveMoreInformation.checked = false;
            }
             break;
        case theForm.ReceiveMoreInformation_Dove:
            if (chkBox.checked){
                theForm.ReceiveMoreInformation.checked = false;
            }

            break;
    }
}

function pullCurtain(){
    showTakeover();
}

function hideCurtain(){
    showHomePage(false);
}

//Support function: checks to see if target
//element is an object or embed element
function isObject(targetID){
   var isFound = false;
   var el = document.getElementById(targetID);
   if(el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")){
      isFound = true;
   }
   return isFound;
}

//Support function: creates an empty
//element to replace embedded SWF object
function replaceSwfWithEmptyDiv(targetID){
   var el = document.getElementById(targetID);
   if(el){
      var div = document.createElement("div");
      el.parentNode.insertBefore(div, el);
      //Remove the SWF
      swfobject.removeSWF(targetID);
      //Give the new DIV the old element's ID
      div.setAttribute("id", targetID);
   }
}

function loadSWF(url, targetID, fv, par, att){
   //Check for existing SWF
   if(isObject(targetID)){
        //replace object/element with a new div
        replaceSwfWithEmptyDiv(targetID);
   }
     
   //Embed SWF
   if (swfobject.hasFlashPlayerVersion("9.0.115")) {
        var attributes = att;
        var params = par;
        params.flashvars = toNameValue(fv);
        att.data = url;
        var obj = swfobject.createSWF(attributes, params, targetID);
   }
}

function toNameValue(obj){
    var nameValues = "";
    for (var property in obj){
        nameValues += property + "=" + escape(obj[property]) + "&";
    }
    return nameValues.substring(0, nameValues.length - 1);
}

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;
	}
}

// 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();   
		}   
	}   
}   

addLoadEvent(initProdTabs);
addLoadEvent(preloadRollovers);


