
// Javascript for header

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

// Hide the main menu.
function hideMainMenu() {
    var host = document.getElementById('subMenuDiv');

    // Make it invisible.
    host.style.display = "none";
}

// Hide the main menu.
//function showMainMenu() {
//    var host = document.getElementById('subMenuDiv');
//
//    // Make it visible.
//    host.style.display = "none";
//    alert('ss');
//}

var closeSubMenuTimerID = 0;



// Create a sub menu for the specified tab.
function showSubMenu(mainMenu) {
    var location = findPos(mainMenu);
    var text = trim(mainMenu.innerHTML.toLowerCase());
    var host = document.getElementById('subMenuDiv');

    // Make it visible.
    host.style.display = "";

    // Put it right under the parent menu.
    host.style.left = location[0] - 4;
    host.style.top = location[1] + 34;

    // Make it as wide as the parent.
    host.style.width = mainMenu.style.width;

    // Remove any existing nodes.
    while (host.childNodes.length > 0)
    {
        host.removeChild(host.childNodes[0]);
    }

    switch (text) {
        case "about lvac":
            addSubMenu('Home', relativePathToBase + 'index.shtml');
            addSubMenu('About LVAC', relativePathToBase + 'about.shtml');
            addSubMenu('LVAC History', relativePathToBase + 'history.shtml');
            addSubMenu('LVAC Board Members', relativePathToBase + 'boardmembers.shtml');
            break;
        case "news and events":
            addSubMenu('News', relativePathToBase + 'news.shtml');
            addSubMenu('Ambulance Call Statistics', relativePathToBase + 'news/1.shtml');
            addSubMenu('LVAC Newsletters', relativePathToBase + 'news/13.shtml');
            break;
        case "get involved":
            addSubMenu('Explorers', relativePathToBase + 'explorers.shtml');
            addSubMenu('Volunteers', relativePathToBase + 'volunteers.shtml');
            break;
        case "contact us":
            break;
        case "for members":
            addSubMenu('EMS Manager', 'https://secure.emsmanager.net/lancaster/index.php');
            addSubMenu('Important Links', relativePathToBase + 'members.shtml');
            break;
        default:
            alert('"' + text + '"');
    }

    // Cancel any existing close calls.
    clearTimeout(closeSubMenuTimerID);

    // Set the menu to auto close.
    closeSubMenuTimerID = setTimeout("hideMainMenu();", 3000);
}

// Adds a sub menu element to the menu.
function addSubMenu(menuName, menuLink) {

    // Create and add the submens
    var subMenu = document.createElement("div");

    // This works better in IE
    subMenu.className = "headingsubmenu";
    subMenu.innerHTML = menuName;

    subMenu.onmouseover = setMouseOver;
    subMenu.onmouseout = setMouseOut;
    subMenu.onclick = alertTest;
    subMenu.website = menuLink;

    // Get the parent and add the sub meny
    var host = document.getElementById('subMenuDiv');
    host.appendChild(subMenu);
}

function alertTest()
{
    window.location = this.website;
}

function setMouseOver()
{
    this.className = "headingsubmenuactive";    
}

function setMouseOut() {
    this.className = "headingsubmenu";
}


// http://www.quirksmode.org/js/findpos.html
//
function findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    //If the browser supports offsetParent we proceed.

    if (obj.offsetParent) {
        //Every time we find a new object, we add its offsetLeft and offsetTop to curleft and curtop.
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            // The tricky bit: return value of the = operator
            //Now we get to the tricky bit:
        }
        while (obj = obj.offsetParent);
        // No, this is not a syntax error. I don't want to use == to compare obj to obj.offsetParent (that doesn't make sense anyhow, since an element is never equal to its offsetParent).

        // Instead, I use the = assignment operator to change the value of obj to obj.offsetParent. I explain this trick in more detail in this blog post.

        //The simple bits
        //The loop continues until the object currently being investigated does not have an offsetParent any more. While the offsetParent is still there, it still adds the offsetLeft of the object to curleft, and the offsetTop to curtop.

        // Finally, when the while loop has quit, we return an array with the calculated coordinates to whichever script asked for it.

        return [curleft, curtop];
    }
}


// Adjust the header image so that is is relative to the current position
// of the page.
//
function adjustHeaderImage() {
    var image = document.getElementById('headerimage');
    image.src = "../images/LVAC-banner.jpg";
}

