/*
**********************************************************************************************************************

Script:     default.js
Created:    David Hood, 2008.11.14
Function:   Main script library for the MRHOOD.COM Website Template
Changes:    <YYYY.MM.DD>    <Author ID>     <Change>
            2009.10.24      D.Hood          Standardized to template
            2009.10.25      D.Hood          Added new menu style

=======================================================================================================================

Copyright (C) 2008-2009 by David Hood, Pitt Meadows, BC
This work is a private literary publication and is protected under Canadian copyright laws.

**********************************************************************************************************************
*/

var MENU_OPEN = 1;
var MENU_CLOSE = -1;
var MENU_SPEED = 5;
var MENU_TIMER = 15;
var bCombo=false;
var oAJAX=null;

function iAbsTop(oObject)
{
    var iTotal = 0, iInc = 0;

    while (oObject != null)
    {
        if (iInc++ > 0) iTotal += oObject.offsetTop;  // Assume the first object is at position 0 (Safari compatibility);
        oObject = oObject.offsetParent;
    }
    return iTotal-1;
}

function iAbsLeft(oObject)
{
    var iTotal = 0;

    while (oObject != null)
    {
        iTotal += oObject.offsetLeft;
        oObject = oObject.offsetParent;
    }

    return iTotal;
}

function fHighlight(oTemp, iMode)
{
    switch (iMode)
    {
        case 2:
            oTemp.className = oTemp.className.indexOf("on2") >= 0 ? oTemp.className : oTemp.className + "on2";
            break;
        case 1:
            oTemp.className = oTemp.className.indexOf("on1") >= 0 ? oTemp.className : oTemp.className + "on1";
            break;
        default:
            oTemp.className = oTemp.className.replace(/on1/g, "");
            oTemp.className = oTemp.className.replace(/on2/g, "");
            break;
    }
    // if (iMode == 1) oTemp.className = oTemp.className.indexOf("on") >= 0 ? oTemp.className : oTemp.className + "on";
    // if (iMode == 0) oTemp.className = oTemp.className.replace(/on/g, "");
}

// This is the main menu function
function fMenu(sID, iDirection)
{
    var oMenu = document.getElementById(sID);
    var oMenuSub = document.getElementById(sID + "sub");
    if (oMenuSub) clearInterval(oMenuSub.timer);
    
    if (iDirection == MENU_OPEN)
    {
        clearTimeout(oMenu.timer);
        oMenu.className = oMenu.className.indexOf("on1") >= 0 ? oMenu.className : oMenu.className + "on1";
        
        if (oMenuSub)
        {
            if (oMenuSub.style.display == "none") oMenuSub.style.display = "block";
            if (oMenuSub.iHeightMax && oMenuSub.iHeightMax <= oMenuSub.offsetHeight)
            { 
                return;
            }
            else if (!oMenuSub.iHeightMax)
            {
                oMenuSub.style.left = (iAbsLeft(oMenu) - 2) + "px";
                oMenuSub.style.top = (iAbsTop(oMenu) + oMenu.offsetHeight + 1) + "px";
                oMenuSub.style.height = "auto";
                oMenuSub.iHeightMax = oMenuSub.offsetHeight;
                oMenuSub.style.height = "0px";
                oMenuSub.iHeightPad = oMenuSub.offsetHeight;
            }
            oMenuSub.timer = setInterval(function() { fMenu_change(oMenuSub, MENU_OPEN) }, MENU_TIMER);
        }
    }
    else
    {
        oMenu.timer = setTimeout(function() { fMenu_close(oMenu, oMenuSub) }, 50);
    }
}

// Transforms the submenu (makes it slowly appear and scroll down)
function fMenu_change(oMenuSub, iDirection)
{
    var iHeightCurrent = oMenuSub.offsetHeight;
    var iDistance = iDirection == MENU_OPEN ? Math.round((oMenuSub.iHeightMax - iHeightCurrent) / MENU_SPEED) : Math.round(iHeightCurrent / MENU_SPEED);
    
    if (iDistance < 1) iDistance = 1;
    
    if ((iHeightCurrent - oMenuSub.iHeightPad - iDistance < 0 && iDirection == MENU_CLOSE) || (iHeightCurrent + oMenuSub.iHeightPad + iDistance > oMenuSub.iHeightMax) && iDirection == MENU_OPEN)
    {
        clearInterval(oMenuSub.timer);
        oMenuSub.style.height = iDirection == MENU_OPEN ? (oMenuSub.iHeightMax - oMenuSub.iHeightPad) + "px" : "0px";
        if (navigator.appName.indexOf("Microsoft") < 0) { oMenuSub.style.opacity = iDirection == MENU_OPEN ? 1 : 0; } // else { oMenuSub.style.filter = "alpha(opacity=" + (iDirection == MENU_OPEN ? "100" : "0") + ")"; }
        if (iDirection == MENU_CLOSE) oMenuSub.style.display = "none";
    }
    else
    {
        oMenuSub.style.height = (iHeightCurrent + (iDistance * iDirection) - oMenuSub.iHeightPad) + "px";
        if (navigator.appName.indexOf("Microsoft") < 0) { oMenuSub.style.opacity = (iHeightCurrent - oMenuSub.iHeightPad) / oMenuSub.iHeightMax; } // else { oMenuSub.style.filter = "alpha(opacity=" + ((iHeightCurrent - oMenuSub.iHeightPad) * 100 / oMenuSub.iHeightMax) + ")"; }
    }
    // document.getElementById("debug").innerHTML += "X ";
    // document.getElementById("debug").innerHTML += "iHeightCurrent = " + iHeightCurrent + ", iDistance = " + iDistance + ", height = " + oMenuSub.style.height + ", offsetHeight = " + oMenuSub.offsetHeight + ", iHeightMax = " + oMenuSub.iHeightMax + ", iDirection = " + iDirection + "<br />";
}

// If the mouse is still hovering over the menu, this will reset the timers and ensure the menu is visible
function fMenu_check(sID)
{
    var oMenu = document.getElementById(sID);
    var oMenuSub = document.getElementById(sID + "sub");
    
    clearTimeout(oMenu.timer);
    clearInterval(oMenuSub.timer);
    
    if (oMenuSub.offsetHeight < oMenuSub.iHeightMax)
    {
        oMenuSub.timer = setInterval(function() { fMenu_change(oMenuSub, MENU_OPEN) }, MENU_TIMER);
    }
}

// Closes the menu and unhighlights
function fMenu_close(oMenu, oMenuSub)
{
    oMenu.className = oMenu.className.replace(/on1/g, "");
    if (oMenuSub) oMenuSub.timer = setInterval(function() { fMenu_change(oMenuSub, MENU_CLOSE) }, MENU_TIMER);
}

// Determines whether the target browser is IE or not (used for overlay determination)
function fIsIE()
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        var iPos = navigator.appVersion.indexOf("MSIE");
        var iVer = navigator.appVersion.substr(iPos+5, 1).charCodeAt(0)-48;
        
        return iVer >= 7 ? false : true;
    }
    else
    {
        return false;
    }
}

// Preloads all files into a picture array
function fSlideShow_Preload(aInput, aOutput)
{
    var iCou;

    for (iCou=0; iCou < aInput.length; iCou++)
    {
        aOutput[iCou] = new Image();
        aOutput[iCou].src = aInput[iCou];
    }
}

// For this to work, each page must declare iPic (current pic), oPic (picture object) and aPic (array of pictures)
function fSlideShow()
{
    if (document.all)
    {
        oPic.style.filter = "blendTrans(duration=2)";
        oPic.style.filter = "blendTrans(duration=3)";
        oPic.filters.blendTrans.Apply();
        oPic.filters.blendTrans.Play();
    }

    oPic.src = aPic[iPic++].src;
    if (document.all) oPic.filters.blendTrans.Play();
    if (iPic >= aPic.length) iPic = 0;
    setTimeout("fSlideShow();", 10000);
}

function fPopupOpenTimer(sItem, iPos)
{
    oTimeOut = setTimeout("fPopupOpen(\"" + sItem + "\", " + iPos + ");", 500);
}

// Standard Popup Menus
function fPopupClose(sMenu)
{
    var oSubmenu = document.getElementById(sMenu);

    if (oSubmenu) oSubmenu.style.display="none";
}

function fPopupOpen(oLink, sMenu, iPos, iID)
{
    var oPopup = document.getElementById("bInfo");

    oAJAX = fAJAX();
    if (oAJAX == null)
    {
        alert("Your browser doesn't support AJAX.");
    }
    else
    {
        oAJAX.onreadystatechange=fAJAXout;
        oAJAX.open("GET", "./calendarpop.php?id=" + iID, true);
        oAJAX.send(null);
        
        if (oLink && oPopup)
        {
            switch (iPos)
            {
                case 2: // Center
                    oPopup.style.left = iAbsLeft(oLink) + (oLink.offsetWidth / 2) - 150 + "px";
                    break;

                case 3: // Right justify
                    oPopup.style.left = iAbsLeft(oLink) + oLink.offsetWidth - 325 + "px";
                    break;

                default: // Left justify
                    oPopup.style.left = iAbsLeft(oLink) + 10 + "px";
                    break;
            }
            // oPopup.style.top = (iAbsTop(oLink) + (oLink.offsetHeight / 2)) + "px";
            oPopup.style.top = iAbsTop(oLink) + "px";

            if (oTimeOut) clearTimeout (oTimeOut);
        }
    }
}

function fPopupReset()
{
    if (oTimeOut) clearTimeout (oTimeOut);
}

function fPopupCloseTimer(sMenu)
{
    oTimeOut = setTimeout("fPopupClose(\"" + sMenu + "\");", 250);
}


// Loads the XML HTTP Request module
function fAJAX()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


// Returns the result of the AJAX call
function fAJAXout() 
{
    var oPopup = document.getElementById("bInfo");
    
    if (oAJAX.readyState==4) 
    {
        oPopup.innerHTML = oAJAX.responseText;
        oPopup.style.display="";
    }
}

// Global select / deselect
function fToggle_selectAll(iState, sFilter)
{
    aTemp = document.getElementsByTagName("input");
    for (var iItem in aTemp) if (aTemp[iItem].type == "checkbox") aTemp[iItem].checked = iState;
}

// Generic window opener; centers screen
function fView(sURL, iWidth, iHeight)
{
    var sSize, sCentre;
    var iCentreX, iCentreY;

    iCentreX = (screen.availWidth - iWidth) / 2;
    iCentreY = (screen.availHeight - iHeight) / 2;

    sSize = "width=" + iWidth + ",height=" + iHeight + ",";
    sCentre = "top=" + iCentreY + ",left=" + iCentreX + ",";

    window.open (sURL, "_blank", sCentre + sSize + "fullscreen=no,resizable=yes,titlebar=no,scrollbars=no,menubar=no,status=no,location=no");
}


// Generic window opener; centers screen
function fViewOption(sURL, iWidth, iHeight, sOption)
{
    var sSize, sCentre;
    var iCentreX, iCentreY;

    iCentreX = (screen.availWidth - iWidth) / 2;
    iCentreY = (screen.availHeight - iHeight) / 2;

    sSize = "width=" + iWidth + ",height=" + iHeight + ",";
    sCentre = "top=" + iCentreY + ",left=" + iCentreX + ",";

    window.open (sURL, "_blank", sCentre + sSize + sOption);
}

// Sets the view
function fChangeView()
{
    document.getElementById("txtAction").value = 1;
    document.frmEdit.submit();
}

function fEditMenu(iIndex)
{
    fView("./admin.php?doc=secadminmenu_edit&id=" + iIndex, 550, 180);
}

function fDelMenu(iIndex)
{
    fView("./admin.php?doc=secadminmenu_del&id=" + iIndex, 550, 240);
}

function fRefreshMenu()
{
    document.location="./index.php?doc=adminmenu";
}

function fEditDocument(iIndex)
{
    fViewOption("./admin.php?doc=secadmindocedit&id=" + iIndex, 1024, 768, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditMenuDoc(iIndex, iMenu)
{
    fView("./admin.php?doc=secadminmenudoc_edit&id=" + iIndex + "&idmenu=" + iMenu, 550, 500);
}

function fDelMenuDoc(iIndex, iMenu)
{
    fView("./admin.php?doc=secadminmenudoc_del&id=" + iIndex + "&idmenu=" + iMenu, 550, 240);
}

function fRefreshMenuDoc(iIndex)
{
    document.location="./index.php?doc=adminmenudoc&id=" + iIndex;
}

function fListCategory()
{
    fViewOption("./admin.php?doc=secadmincategory", 550, 400, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fListCategoryIcal()
{
    fView("./secadmincategoryical.php", 100, 100);
}

function fEditUser(iIndex)
{
    fView("./admin.php?doc=secadminuser_edit&id=" + iIndex, 600, 355);
}

function fEditUserGroup(iIndex)
{
    fViewOption("./admin.php?doc=secadminusergroup&id=" + iIndex, 500, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fDelUser(iIndex)
{
    fView("./admin.php?doc=secadminuser_del&id=" + iIndex, 550, 240);
}

function fRefreshUser()
{
    fChangeView();
}

function fEditStudy(iIndex)
{
    fView("./admin.php?doc=secadminstudy_edit&id=" + iIndex, 800, 600);
}

function fRefreshStudy()
{
    document.location="./index.php?doc=adminstudy";
}

function fViewStudy(iIndex)
{
    fViewOption("./admin.php?doc=secstudyresponse&id=" + iIndex, 800, 600, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fDelStudy(iIndex)
{
    fView("./admin.php?doc=secadminstudy_del&id=" + iIndex, 550, 240);
}

function fEditGroup(iIndex)
{
    fView("./admin.php?doc=secadmingroup_edit&id=" + iIndex, 610, 250);
}

function fDelGroup(iIndex)
{
    fView("./admin.php?doc=secadmingroup_del&id=" + iIndex, 550, 240);
}

function fRefreshGroup()
{
    fChangeView();
}

function fEditGroup_user(iIndex)
{
    fViewOption("./admin.php?doc=secadmingroupuser&id=" + iIndex, 500, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditGroup_group(iIndex)
{
    fViewOption("./admin.php?doc=secadmingroupgroup&id=" + iIndex, 500, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fPrintDocument(iIndex)
{
    fViewOption("./print.php?doc=" + iIndex, 800, 600, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditArticle(iIndex, iArt)
{
    fViewOption("./admin.php?doc=secadminblogedit&id=" + iIndex + "&idart=" + iArt, 1024, 768, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditBlog(iIndex, iArt)
{
    fViewOption("./admin.php?doc=secadminblog_edit&id=" + iIndex + "&idart=" + iArt, 550, 350, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fDelBlog(iIndex, iArt)
{
    fView("./admin.php?doc=secadminblog_del&id=" + iIndex + "&idart=" + iArt, 550, 240);
}

function fRefreshBlog(iDoc)
{
    document.location="./index.php?doc=adminblog&id=" + iDoc;
}

function fToggle_blogComments()
{
    var oDisplay = document.getElementById("blogComments");
    oDisplay.style.display = oDisplay.style.display == "" ? "none" : "";
}

function fToggle_blogArticles()
{
    var oDisplay = document.getElementById("blogArticles");
    oDisplay.style.display = oDisplay.style.display == "" ? "none" : "";
}

function fView_blogComments(iIndex)
{
    fViewOption("./admin.php?doc=secblogcomment&id=" + iIndex, 800, 600, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fTransfer(sCode)
{
    fView("./media.php?id=" + sCode, 200, 200);
}
