////////////////////////////
/// PRODUCT DIV FUNCTIONS
////////////////////////////

dd=document;NS=(dd.layers)?1:0;IE=(dd.all)?1:0;DOM=(dd.getElementById)?1:0
function showLayer(L){ 
if(NS)dd.layers[L].display='show'; 
else if (IE)obg=dd.all[L]; 
else obg=dd.getElementById(L); 
if(!NS)obg.style.display='block' 
}

function hideLayer(L){ 
if(NS)dd.layers[L].display='hide'; 
else if (IE)obg=dd.all[L]; 
else obg=dd.getElementById(L); 
if(!NS)obg.style.display='none' 
}

function div_overview()
{
showLayer('overview')
hideLayer('availability')
hideLayer('directions')
hideLayer('ingredients')
hideLayer('warnings')
hideLayer('storage')
hideLayer('benefits')
}

function div_availability()
{
showLayer('availability')
hideLayer('overview')
hideLayer('directions')
hideLayer('ingredients')
hideLayer('warnings')
hideLayer('storage')
hideLayer('benefits')
}

function div_directions()
{
showLayer('directions')
hideLayer('overview')
hideLayer('availability')
hideLayer('ingredients')
hideLayer('warnings')
hideLayer('storage')
hideLayer('benefits')
}

function div_ingredients()
{
showLayer('ingredients')
hideLayer('overview')
hideLayer('availability')
hideLayer('directions')
hideLayer('warnings')
hideLayer('storage')
hideLayer('benefits')
}

function div_warnings()
{
showLayer('warnings')
hideLayer('overview')
hideLayer('availability')
hideLayer('directions')
hideLayer('ingredients')
hideLayer('storage')
hideLayer('benefits')
}

function div_storage()
{
showLayer('storage')
hideLayer('overview')
hideLayer('availability')
hideLayer('directions')
hideLayer('ingredients')
hideLayer('warnings')
hideLayer('benefits')
}

function div_benefits()
{
showLayer('benefits')
hideLayer('overview')
hideLayer('availability')
hideLayer('directions')
hideLayer('ingredients')
hideLayer('warnings')
hideLayer('storage')
}

////////////////////////////
/// GLOBAL FUNCTIONS
////////////////////////////

var baseFooterFontSize = 10;
var baseCopyFontSize = 11;
var baseCopyFontSize12 = 12;

function adjustFontSize(whichDirection) {
	//Standard 'gray fontsize11' body copy
	arr = document.getElementById("container").getElementsByTagName("div");
	copyblocks = new Array();
	
	for (var i = 0; i < arr.length; i++) {
		if (arr[i].className == "copy") {
			copyblocks.push(arr[i]);
		}
	}

	//Blue subheaders 'blue fontsize12 bold'
	arr = document.getElementById("container").getElementsByTagName("span");
	copyblocks12 = new Array();
	
	for (var i = 0; i < arr.length; i++) {
		if (arr[i].className == "blue fontsize12 bold") {
			copyblocks12.push(arr[i]);
		}
	}

	if (whichDirection == "+") {
		baseCopyFontSize = (parseInt(baseCopyFontSize)+1) +'px';
		baseCopyFontSize12 = (parseInt(baseCopyFontSize12)+1) +'px';
		baseFooterFontSize = (parseInt(baseFooterFontSize)+1) +'px';
	} else {
		if (parseInt(baseCopyFontSize) >= 9) {
			baseCopyFontSize = (parseInt(baseCopyFontSize)-1) +'px';
			baseCopyFontSize12 = (parseInt(baseCopyFontSize12)-1) +'px';
			baseFooterFontSize = (parseInt(baseFooterFontSize)-1) +'px';
		} else {
			return;
		}
	}
	
	for (var i = 0; i < copyblocks.length; i++) {
		copyblocks[i].style.fontSize = baseCopyFontSize;
	}
	
	for (var i = 0; i < copyblocks12.length; i++) {
		copyblocks12[i].style.fontSize = baseCopyFontSize12;
	}

	document.getElementById("footer").style.fontSize = baseFooterFontSize;
}

function newWindow(url,name,w,h,t,m,s,r,st,l,d,c) {
	var windowOptions = "width=" + w + "," +
	"height=" + h + "," +
	"toolbar=" + t + "," +
	"menubar=" + m +"," +
	"scrollbars=" + s + "," +
	"resizable=" + r + "," +
	"status=" + st + "," +
	"location=" + l + "," +
	"directories=" + d + "," +
	"copyhistory=" + c;
	window.open(url, name, windowOptions);
}

function printBathroomResults(city, state, zipCode) {
	newWindow('/generic.jhtml?id=/imodium/include/3_5_1.inc&city=' + city + '&state=' + state + '&zipCode=' + zipCode,'bathroomresults',630,625,0,0,1,0,0,0,0,0);
}

function printPage() {
    var agt = navigator.userAgent.toLowerCase(); 
    var is_win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
    var is_mac    = (agt.indexOf("mac")!=-1);
    var macos  = (is_mac && ((agt.indexOf("mac os") != -1) || (agt.indexOf("macos") != -1) || this.ie));
    var macosx = (macos && ((agt.indexOf("os x") != -1) || (agt.indexOf("osx") != -1)));

	if(macosx || is_win ){
		window.print();
	} else {
		alert("To print this page, press 'command+P'.")
	}
}

////////////////////////////
/// NAVIGATION FUNCTIONS
////////////////////////////

function topNavigation(id) {
    // Browser detection
    this.browser = {
        "ie": Boolean(document.body.currentStyle)
    };

    /* Initialize the menu */
    this.init = function() {
        fixSections();
        parse(document.getElementById(this.id).childNodes, this.tree, this.id);
    }

    /* Search for .section elements and set width for them */
    function fixSections() {
        var arr = document.getElementById(self.id).getElementsByTagName("div");
        var sections = new Array();
        var widths = new Array();
        
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].className == "section") {
                sections.push(arr[i]);
            }
        }
        for (var i = 0; i < sections.length; i++) {
            widths.push(getMaxWidth(sections[i].childNodes));
        }
        for (var i = 0; i < sections.length; i++) {
            sections[i].style.width = (widths[i]) + "px";
        }
        if (self.browser.ie) {
            for (var i = 0; i < sections.length; i++) {
                setMaxWidth(sections[i].childNodes, widths[i]);
            }
        }
    }

    /* Search for an element with highest width among given nodes, return that width */
    function getMaxWidth(nodes) {
        var maxWidth = 0;
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) { continue; }
            if (nodes[i].offsetWidth > maxWidth) { maxWidth = nodes[i].offsetWidth; }
        }
        return maxWidth;
    }

    /* Set width for item2 elements */
    function setMaxWidth(nodes, maxWidth) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType == 1 && /item2/.test(nodes[i].className) && nodes[i].currentStyle) {
				nodes[i].style.width = (maxWidth - parseInt(nodes[i].currentStyle.paddingLeft) - parseInt(nodes[i].currentStyle.paddingRight)) + "px";
            }
        }
    }

    /* Parse nodes, create events, position elements */
    function parse(nodes, tree, id) {
        for (var i = 0; i < nodes.length; i++) {
            if (1 != nodes[i].nodeType) {
                continue;
            }
            switch (true) {
                // .item1
                case /\bitem1\b/.test(nodes[i].className):
                    nodes[i].id = id + "-" + tree.length;
                    tree.push(new Array());
                    nodes[i].onmouseover = item1over;
                    nodes[i].onmouseout = item1out;
                    break;
                // .item2
                case /\bitem2\b/.test(nodes[i].className):
                    nodes[i].id = id + "-" + tree.length;
                    tree.push(new Array());
                    break;
                // .section
                case /\bsection\b/.test(nodes[i].className):
                    // id, events
                    nodes[i].id = id + "-" + (tree.length - 1) + "-section";
                    nodes[i].onmouseover = sectionOver;
                    nodes[i].onmouseout = sectionOut;
                    // position
                    var box1 = document.getElementById(id + "-" + (tree.length - 1));
                    var box2 = document.getElementById(nodes[i].id);
                    box2.style.top = box1.offsetTop + box1.offsetHeight + "px";
					box2.style.left = box1.offsetLeft - 12 + "px";
                    // sections, sectionsShowCnt, sectionsHideCnt
                    self.sections.push(nodes[i].id);
                    self.sectionsShowCnt.push(0);
                    self.sectionsHideCnt.push(0);
                    break;
            }
            if (nodes[i].childNodes) {
                if (/\bsection\b/.test(nodes[i].className)) {
                    parse(nodes[i].childNodes, tree[tree.length - 1], id + "-" + (tree.length - 1));
                } else {
                    parse(nodes[i].childNodes, tree, id);
                }
            }
        }
    }

    /* event, item1:onmouseover */
    function item1over() {
        var id_section = this.id + "-section";
        if (self.visible) {
            var el = new Element(self.visible);
            el = document.getElementById(el.getParent().id);
            if (/item1-active/.test(el.className)) {
                el.className = el.className.replace(/item1-active/, "item1");
            }
        }
        if (self.sections.contains(id_section)) {
            self.sectionsHideCnt[self.sections.indexOf(id_section)]++;
            var cnt = self.sectionsShowCnt[self.sections.indexOf(id_section)];
            setTimeout(function(a, b) { return function() { self.showSection(a, b); } } (id_section, cnt), 0);
        } else {
            if (self.visible) {
                var cnt = self.sectionsHideCnt[self.sections.indexOf(self.visible)];
                setTimeout(function(a, b) { return function() { self.hideSection(a, b); } } (self.visible, cnt), 0);
            }
        }
    }

    /* event, item1:onmouseout */
    function item1out() {
        var id_section = this.id + "-section";
        if (self.sections.contains(id_section)) {
            self.sectionsShowCnt[self.sections.indexOf(id_section)]++;
            if (id_section == self.visible) {
                var cnt = self.sectionsHideCnt[self.sections.indexOf(id_section)];
                setTimeout(function(a, b) { return function() { self.hideSection(a, b); } }(id_section, cnt), 0);
            }
        }
    }

    /* event, section:onmouseover */
    function sectionOver() {
        self.sectionsHideCnt[self.sections.indexOf(this.id)]++;
        var el = new Element(this.id);
        el = document.getElementById(el.getParent().id);
        if (!/item1-active/.test(el.className)) {
            el.className = el.className.replace(/item1/, "item1-active");
        }
    }

    /* event, section:onmouseout */
    function sectionOut() {
        self.sectionsShowCnt[self.sections.indexOf(this.id)]++;
        var cnt = self.sectionsHideCnt[self.sections.indexOf(this.id)];
        setTimeout(function(a, b) { return function() { self.hideSection(a, b); } }(this.id, cnt), 0);
    }

    /* Show section (1 argument passed)
     * Try to show section (2 arguments passed) - check cnt with sectionShowCnt */
    this.showSection = function(id, cnt) {
        if (typeof cnt != "undefined") {
            if (cnt != this.sectionsShowCnt[this.sections.indexOf(id)]) { return; }
        }
        this.sectionsShowCnt[this.sections.indexOf(id)]++;
        var el = new Element(id);
        var parent = document.getElementById(el.getParent().id);
        if (!/item1-active/.test(parent.className)) {
            parent.className = parent.className.replace(/item1/, "item1-active");
        }
        if (this.visible) {
            if (id == this.visible) { return; }
            this.hideSection(this.visible);
        }
        document.getElementById(id).style.visibility = "visible";
        document.getElementById(id).style.zIndex = "1";
        this.visible = id;
    }

    /* Hide section (1 argument passed)
     * Try to hide section (2 arguments passed) - check cnt with sectionHideCnt */
    this.hideSection = function(id, cnt) {
        if (typeof cnt != "undefined") {
            if (cnt != this.sectionsHideCnt[this.sections.indexOf(id)]) { return; }
        }
        var el = new Element(id);
        var parent = document.getElementById(el.getParent().id);
        parent.className = parent.className.replace(/item1-active/, "item1");
        document.getElementById(id).style.zIndex = "-1";
        document.getElementById(id).style.visibility = "hidden";
        if (id == this.visible) { this.visible = ""; }
        else {
            return;
        }
        this.sectionsHideCnt[this.sections.indexOf(id)]++;
    }

    /* Necessary when showing section that doesn't exist - hide currently visible section. See: item1over() */
    this.hideSelf = function(cnt) {
        if (this.visible && cnt == this.sectionsHideCnt[this.sections.indexOf(this.visible)]) {
            this.hideSection(this.visible);
        }
    }

    /* Element (.section, .item2 etc) */
    function Element(id) {
        /* Get parent element */
        this.getParent = function() {
            var s = this.id.substr(this.menu.id.length);
            var a = s.split("-");
            a.pop();
            return new Element(this.menu.id + a.join("-"));
        }
        this.menu = self;
        this.id = id;
    }

    var self = this;
    this.id = id; /* menu id */
    this.tree = []; /* tree structure of menu */
    this.sections = []; /* all sections, required for timeout */
    this.sectionsShowCnt = [];
    this.sectionsHideCnt = [];
    this.visible = ""; /* visible section, ex. menu-0-section */
}

/* Finds the index of the first occurence of item in the array, or -1 if not found */
if (typeof Array.prototype.indexOf == "undefined") {
    Array.prototype.indexOf = function(item) {
        for (var i = 0; i < this.length; i++) {
            if ((typeof this[i] == typeof item) && (this[i] == item)) {
                return i;
            }
        }
        return -1;
    }
}

/* Check whether array contains given string */
if (typeof Array.prototype.contains == "undefined") {
    Array.prototype.contains = function(s) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === s) {
                return true;
            }
        }
        return false;
    }
}


////////////////////////////
/// EXIT POPUP FUNCTION
////////////////////////////

function newWin(WinURL, WinNAME, WinWIDTH, WinHEIGHT, WinRESIZE, WinSCROLL, WinTOOLBAR)
{	
	/*	variables passed by the link calling the function	*/
	var URL = WinURL;
	var WINDOW_NAME = WinNAME;
	var POPUP_WIDTH = WinWIDTH;
	var POPUP_HEIGHT = WinHEIGHT;
	var POPUP_RESIZE = WinRESIZE;
	var POPUP_SCROLL = WinSCROLL;
	var POPUP_TOOLBAR = WinTOOLBAR;

	/*	window properties that determine the window attributes depending on what values are passed by the link calling the function	*/
	var window_toolbar = POPUP_TOOLBAR;
	var window_resize = POPUP_RESIZE;
	var window_scroll = POPUP_SCROLL;
	var window_width = POPUP_WIDTH;
	var window_height = POPUP_HEIGHT;
	var screen_balance_w = ((screen.width)-(window_width))/2;
	var screen_balance_h = ((screen.height)-(window_height))/2;
	var set_top = screen_balance_h;
	var set_left = screen_balance_w;	

	win = window.open(URL,WINDOW_NAME,'resizable=' + window_resize + ',toolbar=' + window_toolbar + ',scrollbars=' + window_scroll + ',width=' + window_width + ',height=' + window_height + ',top=' +  set_top + ',left=' + set_left + '');
	win.focus();
}

/*window.onload = function(e){
	document.getElementById('espanol').style.left = parseInt((document.documentElement.scrollWidth - 999)/2) + 802 + "px";
}

window.onresize = function(e){
	document.getElementById('espanol').style.left = parseInt((document.documentElement.scrollWidth - 999)/2) + 802 + "px";
}*/

var baseFooterFontSize = 10;
var baseCopyFontSize = 11;
var baseCopyFontSize12 = 12;

function adjustFontSize(whichDirection) {
	//Standard 'gray fontsize11' body copy
	arr = document.getElementById("container").getElementsByTagName("div");
	copyblocks = new Array();
	
	for (var i = 0; i < arr.length; i++) {
		if (arr[i].className == "copy") {
			copyblocks.push(arr[i]);
		}
	}

	//Blue subheaders 'blue fontsize12 bold'
	arr = document.getElementById("container").getElementsByTagName("span");
	copyblocks12 = new Array();
	
	for (var i = 0; i < arr.length; i++) {
		if (arr[i].className == "blue fontsize12 bold") {
			copyblocks12.push(arr[i]);
		}
	}

	if (whichDirection == "+") {
		baseCopyFontSize = (parseInt(baseCopyFontSize)+1) +'px';
		baseCopyFontSize12 = (parseInt(baseCopyFontSize12)+1) +'px';
		baseFooterFontSize = (parseInt(baseFooterFontSize)+1) +'px';
	} else {
		if (parseInt(baseCopyFontSize) >= 9) {
			baseCopyFontSize = (parseInt(baseCopyFontSize)-1) +'px';
			baseCopyFontSize12 = (parseInt(baseCopyFontSize12)-1) +'px';
			baseFooterFontSize = (parseInt(baseFooterFontSize)-1) +'px';
		} else {
			return;
		}
	}
	
	for (var i = 0; i < copyblocks.length; i++) {
		copyblocks[i].style.fontSize = baseCopyFontSize;
	}
	
	for (var i = 0; i < copyblocks12.length; i++) {
		copyblocks12[i].style.fontSize = baseCopyFontSize12;
	}

	document.getElementById("footer").style.fontSize = baseFooterFontSize;
}

function newWindow(url,name,w,h,t,m,s,r,st,l,d,c) {
	var windowOptions = "width=" + w + "," +
	"height=" + h + "," +
	"toolbar=" + t + "," +
	"menubar=" + m +"," +
	"scrollbars=" + s + "," +
	"resizable=" + r + "," +
	"status=" + st + "," +
	"location=" + l + "," +
	"directories=" + d + "," +
	"copyhistory=" + c;
	window.open(url, name, windowOptions);
}

function printPage() {
    var agt = navigator.userAgent.toLowerCase(); 
    var is_win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
    var is_mac    = (agt.indexOf("mac")!=-1);
    var macos  = (is_mac && ((agt.indexOf("mac os") != -1) || (agt.indexOf("macos") != -1) || this.ie));
    var macosx = (macos && ((agt.indexOf("os x") != -1) || (agt.indexOf("osx") != -1)));

	if(macosx || is_win ){
		window.print();
	} else {
		alert("To print this page, press 'command+P'.")
	}
}

function rollover(which, state) {
        if (document.images) {
			ext = which.src.substring(which.src.lastIndexOf("."));
			if (state == "ro") {
				which.src = which.src.substring(0,which.src.lastIndexOf('.')) + "_ro" + ext;
			} else {
				which.src = which.src.substring(0,which.src.lastIndexOf('_')) + ext;
			}
        }
}


