﻿var globalScroll = 0;
var globalWidth = 0;
var articlePop = "";
var step = 0;
var zIndex = 0;

function headerSelection(header)
{
    var obj = getObject("MI_" + header);
    
    if (!obj) return;
    
    obj.className = "selected";
    obj.setAttribute("class", "selected");
}


function getObject(obj)
{
    if (typeof obj == "object")
    {
        return obj;
    }
    else if (document.getElementById)
    {
        return document.getElementById(obj);
    }
    else if (document.layers)
    {
        return document.layers[obj];
    }
    else if (document.all)
    {
        return document.all.obj;
    }
    else
    {
        return false;
    }
}


function setupArrows()
{
    var leftArrow = getObject("leftArrow");
    var rightArrow = getObject("rightArrow");
    
    leftArrow.onmouseover = function () { scrollInterval = setInterval("scroll(-2);", 1); };
    leftArrow.onmouseout = function () { clearInterval(scrollInterval); };
    rightArrow.onmouseover = function () { scrollInterval = setInterval("scroll(2);", 1); };
    rightArrow.onmouseout = function () { clearInterval(scrollInterval); };
}


function scroll(x)
{
    var scrollerDiv = getObject("scrollerDiv");
    
    globalScroll += x;
    
    if (globalWidth == 0)
    {
        scrollerDiv.style.overflow = "auto";
        globalWidth = scrollerDiv.scrollWidth - 911;
        scrollerDiv.style.overflow = "hidden";
    }
    
    if (globalScroll < 0)
    {
        globalScroll = 0;
    }
    else if (globalScroll > globalWidth)
    {
        globalScroll = globalWidth;
    }
    
    scrollerDiv.scrollLeft = globalScroll;
}


function showLayover(elm)
{
    var div = getObject("imgLayover");
    var img = getObject("imgLayoverImg");
    var extraPX = 0;
    var href = document.location.href;
    var layover = (elm) ? (elm.getAttribute) ? elm.getAttribute("layover") : elm.layover : null;

    
    if (!layover || layover == "" || layover == null)
    {
        if (div)
        {
            doAction(div, "hide");
            hideAllOver();
        }
        
        return false;
    }
    
    if (!div)
    {
        div = document.createElement("div");
        div.id = "imgLayover";
        div.onmouseout = showLayover;
        div.style.zIndex = "999";
        
        getObject("scrollerDiv").appendChild(div);
        
        div = getObject("imgLayover");
    }
    else
    {
        doAction(div, "show");
        div.style.height = null;
        div.style.width = null;
    }
    
    if (!img)
    {
        img = document.createElement("img");
        img.alt = "";
        img.id = "imgLayoverImg";
        img.style.position = "absolute";
        img.style.left = 0;
        img.style.top = 0;
        
        div.appendChild(img);
        img = getObject("imgLayoverImg");
    }
    
    img.src = eval(layover + ".src");
    
    try {
        if (typeof eval(elm.src.substring(elm.src.lastIndexOf("/") + 1, elm.src.lastIndexOf(".")) + "Over") == "object")
        {
            hideAllOver();
            elm.src = eval(elm.src.substring(elm.src.lastIndexOf("/") + 1, elm.src.lastIndexOf(".")) + "Over.src");
        }
    } catch(e) {}
    
    div.style.width = img.offsetWidth;
    div.style.height = img.offsetHeight;
    
    switch(href.substring(href.lastIndexOf("/") + 1, href.lastIndexOf(".")))
    {
        case "quivertinis" : extraPX = 35; break;
        case "quivers" : extraPX = 75; break;
    }
    
    img.style.left = getLeft(div, elm) + (elm.offsetWidth * 0.5) - (img.offsetWidth * 0.5) + "px";
    img.style.top = elm.height - img.offsetHeight + extraPX + "px";
}


function hideAllOver()
{
    var div = getObject("scrollerDiv");
    
    if (!div) return;
    
    for (var x = 0; x < div.childNodes.length; x++)
    {
        try {
            if (div.childNodes[x].tagName == "IMG")
            {
                if (div.childNodes[x].src.toString().indexOf("Over"))
                {
                    div.childNodes[x].src = div.childNodes[x].src.toString().replace(/Over/, "");
                }
            }
        } catch(e) {}
    }
}


function getLeft(div, elm)
{
    if (!div || !elm) return;
    if (typeof elm == "string") elm = getObject(elm);
    if (!elm) return;
    
    var position = 0;
    
    if (elm.offsetParent)
    {
        do
        {
            position += (parseInt(elm.offsetLeft) > 0) ? elm.offsetLeft : 0;
        } while (elm = elm.offsetParent && elm != div);
    }
    
    return position;
}


function doAction(obj, type)
{
    if (!obj || !type) return;
    if (typeof obj == "string") obj = getObject(obj);
    if (!obj) return;
    
    if (type == "show" || type == "hide")
    {
        type = (type == "show") ? "block" : "none";
        
        if (document.getElementById || document.all)
        {
            obj.style.display = type;
        }
        else if (document.layers)
        {
            obj.display = type;
        }
        else
        {
            try {
                obj.style.display = type;
            } catch(e) {}
        }
    }
}


function preloadLayovers()
{
    preload("mangolayover");
    preload("mangotinilayover");
    preload("strawlayover");
    preload("strawtinilayover");
    preload("pinelayover");
    preload("kiwilayover");
    preload("kiwitinilayover");
    preload("lemonlayover");
    preload("lemontinilayover");
}


function preloadResizers()
{
    preload("quiverStrawOver.jpg");
    preload("quiverMangoOver.jpg");
    preload("quiverCoconutOver.jpg");
    preload("quiverKiwiOver.jpg");
    preload("quiverLemonOver.jpg");
}


function preloadImage(img)
{
    img = img.replace(/\.jpg/gi, "");
    
    eval(img + " = new Image()");
    eval(img + ".src = 'images/" + img + ".jpg'");
}


function preload(layover)
{
    var tag = (layover.indexOf(".") != -1) ? "." + layover.split(".")[1] : ".png";
    
    layover = layover.split(".")[0];
    
    eval(layover + " = new Image(265, 325)");
    eval(layover + ".src = 'images/" + layover + tag + "'");
}


function shuffle(arr)
{
    for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
}


function popupArticle(articlePath, name)
{
    var url = window.location.href;
    var popUrl = url.substr(0, url.lastIndexOf("/") + 1) + "article.php";
    
    popUp(popUrl);
    articlePop.articleUrl = articlePath;
	
	return false;
}


function popupMachineView()
{
    var url = window.location.href;
    var popUrl = url.substr(0, url.lastIndexOf("/") + 1) + "machineViews.php";
    
    popUp(popUrl, 450, 600);
}


function showPETE()
{
    var url = window.location.href;
    var popUrl = url.substr(0, url.lastIndexOf("/") + 1) + "pete.php";

    popUp(popUrl, 800, 600);
}


function showQTimeMoment()
{
    var url = window.location.href;
    var popUrl = url.substr(0, url.lastIndexOf("/") + 1) + "qtimemoment.php";

    popUp(popUrl, 680, 495);
}


function popUp(url, w, h)
{
    if (!w) w = 980;
    if (!h) h = 600;
    
	if (!articlePop.closed && articlePop.location)
	{
	    try {
		    articlePop.close();
		} catch(e) {
		    top.close();
		}
		
		popUp(url, w, h);
		return false;
	}
	
	articlePop = window.open(url, "", "width=" + w + ",height=" + h + ",scrollbars=1");
		
	if (!articlePop.opener) articlePop.opener = self;
}


function nutritionPop(nut)
{
    var url = window.location.href;
    var popUrl = url.substr(0, url.lastIndexOf("/") + 1);
    
    if (nut == "all")
    {
        popUp(popUrl + "article.php");
        articlePop.articleUrl = "nutritionInfo.jpg";
    }
    else
    {
        popUp(popUrl + "nutrition.php");
        articlePop.articleUrl = getNutricianJPG(nut);
    }
    
    
    function getNutricianJPG(nut)
    {
        switch(nut)
        {
            case "straw" : return "nut_straw.jpg";
            case "mango" : return "nut_mango.jpg";
            case "lemon" : return "nut_lemon.jpg";
            case "kiwi" : return "nut_kiwi.jpg";
            case "coconut" : return "nut_coconut.jpg";
            default : return nut;
        }
    }
}