//constants
var ARRAY_LENGTH = 5;
var MENU_NAME = 'aMenu';
var SUB_MENU_NAME = 'tblSubMenu';
var DEFAULT_BG_COLOR = '#D2C694';

//objects
var aNameList = new Array('Home','PrimaryCare','SpecialtyCare','Immunization','HealthPromotion');
var aIntervalList = new Array(ARRAY_LENGTH);
var aHover = new Array(ARRAY_LENGTH);
var aOpacity = new Array(ARRAY_LENGTH);
var tResetPreview;

//variables
var idx;
var isExternal = false;
var isFirefox = false;
var isIE = false;
var isInternal = false;
var isNetscape = false;
var isOpera = false;
var isPopup = false;
var isSafari = false;
var isQLHover = false;

var sBrowserName = '';
var iBrowserVersion = -1;

//initialize arrays
for (var x=1; x<ARRAY_LENGTH; x++)
{
    aIntervalList[x] = null;
    aHover[x] = false;
    aOpacity[x] = 100;
}

sBrowserName = navigator.appName;
switch (sBrowserName.toLowerCase())
{
    case 'microsoft internet explorer':
        isIE = true;
        break;
    case 'netscape':
        isNetscape = true;
        if (navigator.userAgent.indexOf('Safari')>=0)
            isSafari = true;
        else if (navigator.userAgent.indexOf('Firefox')>=0)
            isFirefox = true;
        break;
    case 'opera':
        isOpera = true;
        break;
}

if (isIE)
{
    idx = navigator.userAgent.indexOf('MSIE');
    iBrowserVersion = navigator.userAgent.substring(idx+5,navigator.userAgent.indexOf('.',idx));
}
else if (isSafari)
{
    idx = navigator.userAgent.indexOf('Safari');
    idx = navigator.userAgent.lastIndexOf('/',idx);
    iBrowserVersion = navigator.userAgent.substring(idx+1,navigator.userAgent.indexOf('.',idx));
}
else if (isFirefox)
{
    idx = navigator.userAgent.indexOf('Firefox');
    iBrowserVersion = navigator.userAgent.substring(idx+8,navigator.userAgent.indexOf('.',idx));
}
else
    iBrowserVersion = navigator.appVersion.substring(0,navigator.appVersion.indexOf('.',0));

//alert('MSIE='+isIE+'\nSafari='+isSafari+'\nFirefox='+isFirefox+'\nNetscape='+isNetscape+'\nOpera='+isOpera);
//alert(sBrowserName+' '+iBrowserVersion);

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

function checkClickable()
{
    //alert('pop='+isPopup+'\ninternal='+isInternal+'\nexternal='+isExternal);
    var oInput = document.getElementById('PreviewImageTarget');
    if (isPopup)
        openPopup(oInput.value);
    else if (isInternal)
        openWindow(oInput.value);
    else if (isExternal)
        window.open(oInput.value);
    else
        return false;
}
///////////////////////////////////////////////////////////////////////////////

function clearPreviewTimer()
{
    clearTimeout(tResetPreview);
}
///////////////////////////////////////////////////////////////////////////////

function expandable(obj)
{
    //alert('clicked');
    var oTog = obj.parentNode.getElementsByTagName("input")[0];
    var oDiv = obj.parentNode.getElementsByTagName("div")[1];
    var sClass   = "Expand Head";
    var sDisplay = "none";
    var sVisible = "hidden";
    var sExpand  = "[+]";
    oTog.value=Math.abs(oTog.value-1);
    if (oTog.value=="1") {
        sClass   += " On";
        sDisplay = "block";
        sVisible = "visible";
        sExpand  = "[-]";
    }
    //obj.style["color"]=sColor;
    oDiv.style["display"]=sDisplay;
    oDiv.style["visibility"]=sVisible;
    obj.getElementsByTagName("label")[0].innerHTML=sExpand;
    obj.className=sClass;
    //obj.getElementsByTagName("span")[0].style["color"]=sColor;
    //obj.getElementsByTagName("span")[1].style["color"]=sColor;
}
///////////////////////////////////////////////////////////////////////////////

function fadeSubMenu(i)
{
    aOpacity[i]-=5;
    if ((aOpacity[i]>=0) && (! aHover[i]))
        fadeObj(getObject(SUB_MENU_NAME+aNameList[i]),aOpacity[i]);
    else
    {
        clearInterval(aIntervalList[i]);
        aIntervalList[i] = null;
        aOpacity[i]=100;
    }
}
///////////////////////////////////////////////////////////////////////////////

function fadeObj(obj,val)
{
    if (val>0)
        setOpacity(obj,val);
    else
    {
        obj.style['visibility'] = 'hidden';
        if (isSafari)
            setDivLayerLevel(30);
        else
            obj.style['display'] = 'none';
    }
}
///////////////////////////////////////////////////////////////////////////////

function getObject(id)
{
    return document.getElementById(id);
} // end getObject()
///////////////////////////////////////////////////////////////////////////////

function hideMenu(obj)
{
    obj.className = 'Menu';
} // end hideMenu()
///////////////////////////////////////////////////////////////////////////////

function highLightPreview(sFrameName,sColor)
{
    //document.getElementById('inputTest').value = sFrameName+' '+sColor;
    highLightPreviewFrame(sFrameName,sColor);
    highLightTable(sColor);
}
///////////////////////////////////////////////////////////////////////////////

function highLightPreviewFrame(sFrameName,sColor)
{
    document.getElementById('PreviewFrame'+sFrameName).style['borderColor'] = sColor;
}
///////////////////////////////////////////////////////////////////////////////

function highLightTable(sColor)
{
    document.getElementById('PreviewTable').style['backgroundColor'] = sColor;
}
///////////////////////////////////////////////////////////////////////////////

function hover(val)
{
    aHover[val]=true;
    aOpacity[val]=100;
    try
    {
        clearInterval(aIntervalList[val]);
    }
    catch(e) {}
    showMenu(getObject(MENU_NAME+aNameList[val]));
    showSubMenu(getObject(SUB_MENU_NAME+aNameList[val]));
} // end hoverMenu()
///////////////////////////////////////////////////////////////////////////////

function hoverOut(val)
{
    aHover[val]=false;
    hideMenu(getObject(MENU_NAME+aNameList[val]));
    setFadeInterval(val);
} // end hoverOut()
///////////////////////////////////////////////////////////////////////////////

function openPopup(sImg)
{
    var oInput = document.getElementById('PreviewImageTarget');
    var oWin = window.createPopup();
    var oBdy = oWin.document.body;
    //oBdy.style['backgroundImage'] = 'url("Images/PreviewDefault.01.jpg")';
    oBdy.style['backgroundImage'] = 'url("'+sImg+'")';
    oBdy.style['backgroundRepeat'] = 'no-repeat';
    oBdy.style['backgroundPosition'] = 'center center';
    oBdy.style['border'] = '0.13em maroon solid';
    oWin.show(150,150,375,525,document.body);
}
///////////////////////////////////////////////////////////////////////////////

function openWindow(sURL)
{
    var oWin = window.open(sURL, 'addlInfo',
                            'height=600, width=800, location=no, menubar=no, '
                          + 'resizable=no, scrollbars, status=no, titlebar=no, '
                          + 'toolbar=no');
}
///////////////////////////////////////////////////////////////////////////////

function resetPreviewImage()
{
    if (isQLHover)
        setPreviewTimer();
    else
        setPreviewImage(0);
        //setPreviewImage("Images/PreviewDefault.01.jpg");
}
///////////////////////////////////////////////////////////////////////////////

function roundTo(val,dec)
{
    var iTmp = 0;
    if (isNaN(val) || isNaN(dec))
        return null;
    else
    {
        iTmp = Math.floor(val*Math.pow(10,dec)+0.5);
        return (iTmp/Math.pow(10,dec));
    }
}
///////////////////////////////////////////////////////////////////////////////

function setBrowserBestView()
{
    if (isIE) {
        if (Number(iBrowserVersion)<7) {
            showObject(document.getElementById("divBrowserBestView"),true,"inline");
            //document.getElementById("divBrowserBestView").style["display"]="inline";
            //document.getElementById("divBrowserBestView").style["visibility"]="visible";
            showObject(document.getElementById("divBBVie"),true,"inline");
            //document.getElementById("divBBVie").style["display"]="inline";
            //document.getElementById("divBBVie").style["visibility"]="visible";
            document.getElementById("divBBVie").getElementsByTagName("img")[0].setAttribute("alt","IE 7+");
            document.getElementById("divBBVie").getElementsByTagName("img")[0].setAttribute("title","IE 7+");
        }
        else if (Number(iBrowserVersion)==8) {
            showObject(document.getElementById("divBrowserBestView"),true,"inline");
            //document.getElementById("divBrowserBestView").style["display"]="inline";
            //document.getElementById("divBrowserBestView").style["visibility"]="visible";
            showObject(document.getElementById("divBBVie"),true,"inline");
            //document.getElementById("divBBVie").style["display"]="inline";
            //document.getElementById("divBBVie").style["visibility"]="visible";
            document.getElementById("divBBVie").getElementsByTagName("img")[0].setAttribute("alt","IE 8 w/Compatibility View");
            document.getElementById("divBBVie").getElementsByTagName("img")[0].setAttribute("title","IE 8 w/Compatibility View");
            showObject(document.getElementById("lblBBViecv"),true,"inline");
            //document.getElementById("divBBViecv").style["display"]="inline";
            //document.getElementById("divBBViecv").style["visibility"]="visible";
        }
    }
    else if (!isFirefox) {
        showObject(document.getElementById("divBrowserBestView"),true,"inline");
        //document.getElementById("divBrowserBestView").style["display"]="inline";
        //document.getElementById("divBrowserBestView").style["visibility"]="visible";
        showObject(document.getElementById("divBBVff"),true,"inline");
        //document.getElementById("divBBVff").style["display"]="inline";
        //document.getElementById("divBBVff").style["visibility"]="visible";
        document.getElementById("divBBVff").getElementsByTagName("img")[0].setAttribute("alt","FF 3+");
        document.getElementById("divBBVff").setAttribute("title","FF 3+");
        showObject(document.getElementById("divBBVie"),true,"inline");
        //document.getElementById("divBBVie").style["display"]="inline";
        //document.getElementById("divBBVie").style["visibility"]="visible";
        document.getElementById("divBBVie").getElementsByTagName("img")[0].setAttribute("alt","IE 7+");
        document.getElementById("divBBVie").setAttribute("title","IE 7+");
    }
}
///////////////////////////////////////////////////////////////////////////////

function setDivLayerLevel(val)
{
    for (var i=0; i<5; i++)
        document.getElementById('divLayer'+i).style['z-index'] = val;
}
///////////////////////////////////////////////////////////////////////////////

function setFadeInterval(i)
{
    if (! aHover[i])
        aIntervalList[i] = setInterval('fadeSubMenu('+i+')',25);
}
///////////////////////////////////////////////////////////////////////////////

function setOpacity(obj,val)
{
    if (obj.filters)
        obj.style['filter'] = 'alpha(opacity='+val+')';
    else if (obj.style['-moz-opacity'])
        obj.style['-moz-opacity'] = (val/100);
    else if (obj.style['-khtml-opacity'])
        obj.style['-khtml-opacity'] = (val/100);
    else
        obj.style['opacity'] = (val/100);
} // end setOpacity()
///////////////////////////////////////////////////////////////////////////////

function setPreviewImageOLD(sSrc)
{
    clearPreviewTimer();
    var oImg = document.getElementById('PreviewImage');
    var oInput = document.getElementById('PreviewImageTarget');
    if ((oImg != null) && (oInput != null))
    {
        oImg.src = sSrc;
        oImg.style['cursor'] = 'auto';
        oInput.value = '';
        isExternal = false;
        isInternal = false;
        isPopup = false;
        isQLHover = true;
    }
    setPreviewTimer();
}
///////////////////////////////////////////////////////////////////////////////

function setPreviewImage(yPos)
{
    clearPreviewTimer();
    var oImg = document.getElementById('PreviewImage');
    var oInput = document.getElementById('PreviewImageTarget');
    var yAdj = 5*yPos;
    if ((oImg != null) && (oInput != null))
    {
        //oImg.src = sSrc;
        //oImg.style['clip'] = 'rect('+yAdj+'px 250px '+(yAdj+350)+'px 0px)';
        //oImg.style['clip'] = 'rect('+yAdj+'% 100% '+(yAdj+5)+'% 0%)';
        oImg.style['top'] = toEM(yPos*-350)+'em';
        oImg.style['cursor'] = 'auto';
        oInput.value = '';
        isExternal = false;
        isInternal = false;
        isPopup = false;
        isQLHover = true;
    }
    setPreviewTimer();
}
///////////////////////////////////////////////////////////////////////////////

function setPreviewImageLinkOLD(sSrc,sURL,sType)
{
    clearPreviewTimer();
    var oImg = document.getElementById('PreviewImage');
    var oInput = document.getElementById('PreviewImageTarget');
    if ((oImg != null) && (oInput != null))
    {
        oImg.src = sSrc;
        oImg.style['cursor'] = 'pointer';
        oInput.value = sURL;
        isExternal = false;
        isInternal = false;
        isPopup = false;
        isQLHover = true;
        switch (sType.toLowerCase())
        {
            case 'external':
                isExternal = true;
                break;
            case 'internal':
                isInternal = true;
                break;
            case 'popup':
                isPopup = true;
                break;
            default:
                //setPreviewImage(sSrc);
                setPreviewImageTest(0);
                break;
        }
    }
    setPreviewTimer();
}
///////////////////////////////////////////////////////////////////////////////

function setPreviewImageLink(yPos,sURL,sType)
{
    clearPreviewTimer();
    var oImg = document.getElementById('PreviewImage');
    var oInput = document.getElementById('PreviewImageTarget');
    //var yAdj = 350*yPos;
    var yAdj = 5*yPos;
    if ((oImg != null) && (oInput != null))
    {
        //oImg.src = sSrc;
        //oImg.style['clip'] = 'rect('+yAdj+'px 250px '+(yAdj+350)+'px 0px)';
        //oImg.style['clip'] = 'rect('+yAdj+'% 100% '+(yAdj+5)+'% 0%)';
        oImg.style['top'] = toEM(yPos*-350)+'em';
        oImg.style['cursor'] = 'pointer';
        oInput.value = sURL;
        isExternal = false;
        isInternal = false;
        isPopup = false;
        isQLHover = true;
        switch (sType.toLowerCase())
        {
            case 'external':
                isExternal = true;
                break;
            case 'internal':
                isInternal = true;
                break;
            case 'popup':
                isPopup = true;
                break;
            default:
                //setPreviewImage(sSrc);
                setPreviewImageTest(0);
                break;
        }
    }
    setPreviewTimer();
}
///////////////////////////////////////////////////////////////////////////////

function setPreviewTimer()
{
    tResetPreview = setTimeout('resetPreviewImage()',5000);
}
///////////////////////////////////////////////////////////////////////////////

function showObject(obj,show,display)
{
    var visibility = "hidden";
    if (show)
        visibility = "visible";
    obj.style["display"]=display;
    obj.style["visibility"]=visibility;
}
///////////////////////////////////////////////////////////////////////////////

function showMenu(obj)
{
    obj.className = 'MenuHover';
} // end showMenu()
///////////////////////////////////////////////////////////////////////////////

function showSubMenu(obj)
{
    if (isSafari)
        setDivLayerLevel(10);
    obj.style['display'] = 'inline';
    if (! isIE)
        obj.style['display'] = 'table';
    obj.style['visibility'] = 'visible';
    setOpacity(obj,100);
} // end showSubMenu()
///////////////////////////////////////////////////////////////////////////////

function toEM(pixel)
{
    if (isNaN(pixel))
        return 0;
    return roundTo((pixel*1.38/22),2);
}
///////////////////////////////////////////////////////////////////////////////

