/*
**********************************************************************************************************************

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 AJAX_RETURN="";     // Used to control output of an AJAX call
var bCombo=false;
var oAJAX=null;

function fTest(sObject)
{
    var oObject = document.getElementById(sObject);
    var sLeft = "Left...";
    var sTop = "Top...";

    while (oObject != null)
    {
        sLeft += oObject.tagName + "=" + oObject.offsetLeft + ", ";
        sTop += oObject.tagName + "=" + oObject.offsetTop + ", ";
        oObject = oObject.offsetParent;
    }
    alert(sLeft + "\r\n" + sTop);
}

function iAbsTop(oObject)
{
    var iTotal = 1;

    while (oObject != null)
    {
        // alert (oObject.tagName + " = " + oObject.offsetTop);
        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)
            {
                var oTest = oMenuSub.getBoundingClientRect();
                var iAlignRight = (oMenu.style.textAlign == "right" ? (oTest.right - oTest.left - oMenu.offsetWidth) : 0);

                oMenuSub.style.left = (iAbsLeft(oMenu) - iAlignRight) + "px";
                oMenuSub.style.top = (iAbsTop(oMenu) + oMenu.offsetHeight) + "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 (!fIsIE()) oMenuSub.style.opacity = iDirection == MENU_OPEN ? 1 : 0;
        if (iDirection == MENU_CLOSE) oMenuSub.style.display = "none";
    }
    else
    {
        oMenuSub.style.height = (iHeightCurrent + (iDistance * iDirection) - oMenuSub.iHeightPad) + "px";
        if (!fIsIE()) oMenuSub.style.opacity = (iHeightCurrent - oMenuSub.iHeightPad) / 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 < 9 ? true : false;
    }
    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 resulting list into a pop-up menu
function fSelect(oInput, sList)
{
    var oPopup = document.getElementById("b" + sList);
    AJAX_RETURN = "b" + sList;
    oPopup.style.left = iAbsLeft(oInput) + "px";
    oPopup.style.top = (iAbsTop(oInput) + oInput.offsetHeight) + "px";
    oPopup.style.height = "auto";
    oPopup.style.width = (oInput.style.textAlign == "justify" ? oInput.offsetWidth + "px" : "auto");

    if (oInput.value.length > 0)
    {
        oAJAX = fAJAX();
        if (oAJAX == null)
        {
            alert("Your browser doesn't support AJAX.");
        }
        else
        {
            oAJAX.onreadystatechange=fSelectUpdate;
            oAJAX.open("GET", "./" + sList.toLowerCase() + ".php?text=" + oInput.value, true);
            oAJAX.send();
        }
    }
    else oPopup.style.display="none";
}

function fSelectUpdate()
{
    var oPopup = document.getElementById(AJAX_RETURN);
    
    if (oAJAX.readyState==4)
    {
        if (oAJAX.responseText == "")
        {
            oPopup.style.display="none";
        }
        else
        {
            oPopup.innerHTML = oAJAX.responseText;
            oPopup.style.display="";
        }
        
    }
}

// Loads the resulting media into a DIV
function fMedia(iMedia)
{
    var oPopup = document.getElementById("bMedia");
    var iWindowHeight = document.documentElement.clientHeight, iWindowWidth = document.documentElement.clientWidth, iHeightOld;
    var iIndex = aAlbumID[iMedia];
    var aTemp = aAlbum[iIndex].split(",");
    var iWidth = parseInt(aTemp[1]);
    var iHeight = parseInt(aTemp[2]);
    iHeightOld = iHeight;
    
    // Calculate the next picture
    if (iIndex == 0)
    {
        iPrev = 0;
    }
    else
    {
        aTemp = aAlbum[iIndex - 1].split(",");
        iPrev = parseInt(aTemp[0]);
    }
    
    // Calculate the previos picture
    if (aAlbum[iIndex + 1] == undefined) 
    {
        iNext = 0;
    }
    else
    {
        aTemp = aAlbum[iIndex + 1].split(",");
        iNext = parseInt(aTemp[0]);
    }
    
    if (iHeight + 130 > iWindowHeight) 
    {
        iHeight = iWindowHeight - 130;
        iWidth = (iHeight / iHeightOld) * iWidth;
    }
    
    oPopup.style.left = ((iWindowWidth - (iWidth + 30)) / 2) + "px";
    oPopup.style.top = ((iWindowHeight - (iHeight + 130)) / 2) + "px";
    oPopup.style.width = iWidth + "px";
//    oPopup.style.height = iHeight + "px";
    oPopup.style.display="";
    
    if (iMedia > 0)
    {
        oAJAX = fAJAX();
        if (oAJAX == null)
        {
            alert("Your browser doesn't support AJAX.");
        }
        else
        {
            oAJAX.onreadystatechange=fMediaUpdate;
            oAJAX.open("GET", "./mediapop.php?mid=" + iMedia + "&midn=" + iNext + "&midp=" + iPrev + "&h=" + iHeight, true);
            oAJAX.send();
        }
    }
    else oPopup.style.display="none";
}

function fMediaUpdate()
{
    var oPopup = document.getElementById("bMedia");
    
    if (oAJAX.readyState==4)
    {
        if (oAJAX.responseText == "")
        {
            oPopup.style.display="none";
        }
        else
        {
            oPopup.innerHTML = oAJAX.responseText;
            oPopup.style.display="";
        }
    }
}

function fMediaClose()
{
    var oPopup = document.getElementById("bMedia");
    oPopup.style.display="none";
}


// 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);
}

function fAdminAlbum(iDoc)
{
    fViewOption("./admin.php?doc=secadminalbum&id=" + iDoc, 500, 500, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fAdminBlog(iDoc)
{
    fViewOption("./admin.php?doc=secadminblog&id=" + iDoc, 500, 500, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fAdminEvent(iDoc)
{
    fViewOption("./admin.php?doc=secadminevent&id=" + iDoc, 650, 500, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fAdminEventReg(iEvent)
{
    fViewOption("./admin.php?doc=secadmineventreg&event=" + iEvent, 650, 550, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fAdminEventRegMove(iEvent, iEventReg)
{
    fViewOption("./admin.php?doc=secadmineventregmove&event=" + iEvent + "&eventreg=" + iEventReg, 500, 350, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

// Sets the view
function fChangeView()
{
    document.getElementById("txtAction").value = 1;
    document.frmEdit.submit();
}

function fDelAlbum(iAlbum)
{
    fView("./admin.php?doc=secadminalbum_del&album=" + iAlbum, 550, 240);
}

function fDelBlog(iBlog)
{
    fView("./admin.php?doc=secadminblog_del&blog=" + iBlog, 550, 240);
}

function fDelEvent(iEvent)
{
    fView("./admin.php?doc=secadminevent_del&event=" + iEvent, 550, 240);
}

function fDelGroup(iIndex)
{
    fView("./admin.php?doc=secadmingroup_del&group=" + iIndex, 550, 240);
}

function fDelLocation(iIndex)
{
    fView("./admin.php?doc=secadminloc_del&loc=" + iIndex, 550, 240);
}

function fDelMedia(iIndex)
{
    fView("./admin.php?doc=secadminmedia_del&media=" + iIndex, 550, 240);
}

function fDelMenu(iIndex)
{
    fView("./admin.php?doc=secadminmenu_del&id=" + iIndex, 550, 260);
}

function fDelMenuDoc(iIndex, iMenu)
{
    fView("./admin.php?doc=secadminmenudoc_del&id=" + iIndex + "&menu=" + iMenu, 550, 260);
}

function fDelUser(iIndex)
{
    fView("./admin.php?doc=secadminuser_del&user=" + iIndex, 550, 240);
}

function fEditAlbum(iIndex, iAlbum)
{
    fViewOption("./admin.php?doc=secadminalbum_edit&id=" + iIndex + "&album=" + iAlbum, 680, 750, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditArticle(iIndex, iBlog)
{
    fViewOption("./admin.php?doc=secadminblogedit&id=" + iIndex + "&blog=" + iBlog, 1024, 768, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditBlog(iIndex, iBlog)
{
    fViewOption("./admin.php?doc=secadminblog_edit&id=" + iIndex + "&blog=" + iBlog, 550, 350, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

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 fEditEvent(iIndex, iEvent)
{
    fViewOption("./admin.php?doc=secadminevent_edit&id=" + iIndex + "&event=" + iEvent, 550, 470, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditEventReg(iEvent)
{
    fViewOption("./admin.php?doc=eventreg&event=" + iEvent, 775, 420, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditGroup(iIndex)
{
    fView("./admin.php?doc=secadmingroup_edit&group=" + iIndex, 610, 250);
}

function fEditGroup_group(iIndex)
{
    fViewOption("./admin.php?doc=secadmingroupgroup&group=" + iIndex, 510, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditGroup_location(iIndex)
{
    fViewOption("./admin.php?doc=secadmingrouplocation&group=" + iIndex, 510, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditGroup_user(iIndex)
{
    fViewOption("./admin.php?doc=secadmingroupuser&group=" + iIndex, 510, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditLocation(iIndex, iParent)
{
    fView("./admin.php?doc=secadminloc_edit&loc=" + iIndex + "&parent=" + iParent, 600, 300);
}

function fEditLocationGroup(iIndex)
{
    fViewOption("./admin.php?doc=secadminlocationgroup&loc=" + iIndex, 500, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditLocationParent(iIndex)
{
    fViewOption("./admin.php?doc=secadminloc_editparent&loc=" + iIndex, 300, 500, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditMedia(iIndex)
{
    fViewOption("./admin.php?doc=secadminmedia_edit&media=" + iIndex, 500, 500, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fEditMenu(iIndex)
{
    fView("./admin.php?doc=secadminmenu_edit&id=" + iIndex, 550, 180);
}

function fEditMenuDoc(iIndex, iMenu)
{
    fView("./admin.php?doc=secadminmenudoc_edit&id=" + iIndex + "&menu=" + iMenu, 550, 500);
}

function fEditUser(iIndex)
{
    fView("./admin.php?doc=secadminuser_edit&user=" + iIndex, 600, 400);
}

function fEditUserGroup(iIndex)
{
    fViewOption("./admin.php?doc=secadminusergroup&user=" + iIndex, 500, 650, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

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 fMap(ll, z)
{
    fViewOption("./admin.php?doc=secadminloc_editmap" + (ll!=undefined ? "&ll=" + ll : "") + (z!=undefined ? "&z=" + z : ""), 1024, 768, "fullscreen=no,resizable=yes,titlebar=yes,scrollbars=yes,menubar=no,status=no,location=no");
}

function fPrintDocument(sLink)
{
    fViewOption("./print.php" + sLink, 800, 600, "fullscreen=no,resizable=yes,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

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 fTransfer(sCode)
{
    fView("./media.php?id=" + sCode, 200, 200);
}

function fView_blogComments(iIndex, iBlog)
{
    fViewOption("./admin.php?doc=blogcomment&id=" + iIndex + "&blog=" + iBlog, 800, 600, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fViewEventReg(iEvent)
{
    fViewOption("./admin.php?doc=eventreglist&event=" + iEvent, 600, 550, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

function fViewNotify(iPage)
{
    fViewOption("./admin.php?doc=notify&id=" + iPage, 600, 350, "resizable=yes,fullscreen=no,titlebar=no,scrollbars=yes,menubar=no,status=no,location=no");
}

