// www.brainjar.com (Mike Hall)

var ActiveBrowser = new Browser();
var ActiveMenu = null;

//window.defaultStatus = 'Fleadh 2007':

function Browser() {
    var u, s, i;
    this.isexplorer   = false;
    this.isopera      = false;
    this.isnetscape   = false;
    this.version    = null;
    u = navigator.userAgent;
    //alert(u);
    s = "Opera";
    if ((i = u.indexOf(s)) >= 0) {
        this.isopera = true;
        this.version = parseFloat(u.substr(i + s.length));
        return;
    }
    s = "Netscape6/";
    if ((i = u.indexOf(s)) >= 0) {
        this.isnetscape = true;
        this.version = parseFloat(u.substr(i + s.length));
        return;
    }
    // Treat any other "Gecko" ActiveBrowser as Netscape 6.1.
    s = "Gecko";
    if ((i = u.indexOf(s)) >= 0) {
        this.isnetscape = true;
        this.version = 6.1;
        return;
    }

    s = "MSIE";
    if ((i = u.indexOf(s))) {
        this.isexplorer = true;
        this.version = parseFloat(u.substr(i + s.length));
        return;
    }
}
function OnMenuButtonClick(event, id) {
    var button;
    // Get the target button element.
    if (ActiveBrowser.isexplorer)
        button = window.event.srcElement;
    else
        button = event.currentTarget;
    // Blur focus from the link to remove that annoying outline.
    button.blur();
    // Associate the named menu to this button if not already done.
    // Additionally, initialize menu display.

    if (button.menu == null) {
        if (id) {
            button.menu = document.getElementById(id);
            if (button.menu.isInitialized == null) StartMenuPopup(button.menu);
        }
    }
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Set mouseout event handler for the button, if not already done.
    if (button.onmouseout == null) button.onmouseout = OnMenuMouseOut;
    // Exit if this button is the currently active one.
    if (button == ActiveMenu) return false;
    // [END MODIFIED]
    // Reset the currently active button, if any.
    if (ActiveMenu != null) SetMenuButtonNormal(ActiveMenu);
    // Activate this button, unless it was the currently active one.
    if (button != ActiveMenu) {
        SetMenuButtonActive(button);
        ActiveMenu = button;
    }
    else
        ActiveMenu = null;
    return false;
}

function OnMenuButtonOver(event, id) {
    //alert(id);
    var button;
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Activates this button's menu if no other is currently active.
    if (ActiveMenu == null) {
        OnMenuButtonClick(event, id);
        return;
    }
    // [END MODIFIED]
    // Find the target button element.
    if (ActiveBrowser.isexplorer)
        button = window.event.srcElement;
    else
        button = event.currentTarget;
    // If any other button menu is active, make this one active instead.
    if (ActiveMenu != null && ActiveMenu != button) OnMenuButtonClick(event, id);
}

function SetMenuButtonActive(button) {
    //alert(99);
    var x, y;
    // Update the button's style class to make it look like it's
    // depressed.
    button.className += " MenuButtonActive";
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Set mouseout event handler for the button, if not already done.
    if (button.onmouseout == null) button.onmouseout = OnMenuMouseOut;
    if (button.menu.onmouseout == null) button.menu.onmouseout = OnMenuMouseOut;
    // [END MODIFIED]
    // Position the associated drop down menu under the button and
    // show it.
    x = GetPageOffsetLeft(button);
    y = GetPageOffsetTop(button) + button.offsetHeight;
    //document.title = button.offsetHeight;
    // For IE, adjust position.
    if (ActiveBrowser.isexplorer) {
        //x += button.offsetParent.clientLeft - 2;// 2;
        //y = 204;
        x += button.offsetParent.clientLeft + 1; // - 1;
        y += button.offsetParent.clientTop - 2; // - 7;
    } else {
        y -= 1;
    }

    button.menu.style.left = x + "px";
    button.menu.style.top  = y + "px";
    button.menu.style.visibility = "visible";

  // For IE; size, position and show the menu's IFRAME as well.

    if (button.menu.iframeEl != null) {
        button.menu.iframeEl.style.left = button.menu.style.left;
        button.menu.iframeEl.style.top  = button.menu.style.top;
        button.menu.iframeEl.style.width  = button.menu.offsetWidth + "px";
        button.menu.iframeEl.style.height = button.menu.offsetHeight + "px";
        button.menu.iframeEl.style.display = "";
    }
}

function SetMenuButtonNormal(button) {
    // Restore the button's style class.
    RemoveClassName(button, "MenuButtonActive");
    // Hide the button's menu, first closing any sub menus.
    if (button.menu != null) {
        CloseMenuPopup(button.menu);
        button.menu.style.visibility = "hidden";
        // For IE, hide menu's IFRAME as well.

    if (button.menu.iframeEl != null)
        button.menu.iframeEl.style.display = "none";
    }
}

function OnMenuPopupOver(event) {
    var menu;
    // Find the target menu element.
    if (ActiveBrowser.isexplorer)
        menu = GetContainerWith(window.event.srcElement, "DIV", "MenuPopup");
    else
        menu = event.currentTarget;
    // Close any active sub menu.
    if (menu.activeItem != null) CloseMenuPopup(menu);
}

function OnMenuItemOver(event, id) {
    //alert(161);
    var item, menu, x, y;
    // Find the target item element and its parent menu element.
    if (ActiveBrowser.isexplorer)
        item = GetContainerWith(window.event.srcElement, "A", "MenuItem");
    else
        item = event.currentTarget;

    menu = GetContainerWith(item, "DIV", "MenuPopup");
    // Close any active sub menu and mark this one as active.
    if (menu.activeItem != null) CloseMenuPopup(menu);
    menu.activeItem = item;
    // Highlight the item element.
    item.className += " MenuItemActive";
    // Initialize the sub menu, if not already done.
    if (item.subMenu == null) {
        item.subMenu = document.getElementById(id);
        if (item.subMenu.isInitialized == null) StartMenuPopup(item.subMenu);
    }
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Set mouseout event handler for the sub menu, if not already done.
    if (item.subMenu.onmouseout == null)
        item.subMenu.onmouseout = OnMenuMouseOut;
    // [END MODIFIED]
    // Get position for submenu based on the menu item.
    x = GetPageOffsetLeft(item) + item.offsetWidth;
    y = GetPageOffsetTop(item);
    // Adjust position to fit in view.
    var maxX, maxY;
    if (ActiveBrowser.isexplorer) {
        maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + (document.documentElement.clientWidth  != 0 ? document.documentElement.clientWidth  : document.body.clientWidth);
        maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop)   + (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
    }
    if (ActiveBrowser.isopera) {
        maxX = document.documentElement.scrollLeft + window.innerWidth;
        maxY = document.documentElement.scrollTop  + window.innerHeight;
    }
    if (ActiveBrowser.isnetscape) {
        maxX = window.scrollX + window.innerWidth;
        maxY = window.scrollY + window.innerHeight;
    }
    maxX -= item.subMenu.offsetWidth;
    maxY -= item.subMenu.offsetHeight;
    if (x > maxX) x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
    y = Math.max(0, Math.min(y, maxY));
    // Position and show the sub menu.

    if (isexplorer) {
        y -= 1;
        x -= 2;
    } else {
        y -= 1;
        x -= 2;
    }

    item.subMenu.style.left       = x + "px";
    item.subMenu.style.top        = y + "px";
    item.subMenu.style.visibility = "visible";
    // For IE; size, position and display the menu's IFRAME as well.
    if (item.subMenu.iframeEl != null) {
        item.subMenu.iframeEl.style.left    = item.subMenu.style.left;
        item.subMenu.iframeEl.style.top     = item.subMenu.style.top;
        item.subMenu.iframeEl.style.width   = item.subMenu.offsetWidth  + "px";
        item.subMenu.iframeEl.style.height  = item.subMenu.offsetHeight + "px";
        item.subMenu.iframeEl.style.display = "";
    }
    // Stop the event from bubbling.
    if (ActiveBrowser.isexplorer)
        window.event.cancelBubble = true;
    else
        event.stopPropagation();
}

function CloseMenuPopup(menu) {
    if (menu == null || menu.activeItem == null) return;
    // Recursively close any sub menus.
    if (menu.activeItem.subMenu != null) {
        CloseMenuPopup(menu.activeItem.subMenu);
        menu.activeItem.subMenu.style.visibility = "hidden";
        // For IE, hide the sub menu's IFRAME as well.
        if (menu.activeItem.subMenu.iframeEl != null)
            menu.activeItem.subMenu.iframeEl.style.display = "none";
        menu.activeItem.subMenu = null;
    }
    // Deactivate the active menu item.
    RemoveClassName(menu.activeItem, "MenuItemActive");
    menu.activeItem = null;
}

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.

function OnMenuMouseOut(event) {
    var e;
  // If there is no active button, exit.
    if (ActiveMenu == null) return;
    // Find the element the mouse is moving to.
    if (ActiveBrowser.isexplorer)
        e = window.event.toElement;
    else if (event.relatedTarget != null)
        e = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
    // If the element is not part of a menu, reset the active button.
    if (GetContainerWith(e, "DIV", "MenuPopup") == null) {
        SetMenuButtonNormal(ActiveMenu);
        ActiveMenu = null;
    }
}

// [END MODIFIED]
function StartMenuPopup(menu) {
    var itemList, spanList;
    var textEl, arrowEl;
    var itemWidth;
    var w, dw;
    var i, j;
    // For IE, replace arrow characters.
    if (ActiveBrowser.isexplorer) {
        menu.style.lineHeight = "2.5ex";
        spanList = menu.getElementsByTagName("SPAN");
        for (i = 0; i < spanList.length; i++)
            if (HasClassName(spanList[i], "MenuItemArrow")) {
                spanList[i].style.fontFamily = "Webdings";
                spanList[i].firstChild.nodeValue = "4";
        }
    }
    // Find the width of a menu item.
    itemList = menu.getElementsByTagName("A");
    if (itemList.length > 0)
        itemWidth = itemList[0].offsetWidth;
    else
        return;
    // For items with arrows, add padding to item text to make the
    // arrows flush right.

    // alert(itemWidth);
    for (i = 0; i < itemList.length; i++) {
        spanList = itemList[i].getElementsByTagName("SPAN");
        textEl  = null;
        arrowEl = null;
        for (j = 0; j < spanList.length; j++) {
            if (HasClassName(spanList[j], "MenuItemText"))  textEl  = spanList[j];
            if (HasClassName(spanList[j], "MenuItemArrow")) arrowEl = spanList[j];
        }
        if (textEl != null && arrowEl != null) {
            textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
            // For Opera, remove the negative right margin to fix a display bug.
            if (ActiveBrowser.isopera) arrowEl.style.marginRight = "0px";
        }
    }
    // Fix IE hover problem by setting an explicit width on first item of
    // the menu.
    if (ActiveBrowser.isexplorer) {
        w = itemList[0].offsetWidth;
        itemList[0].style.width = w + "px";
        dw = itemList[0].offsetWidth - w;
        w -= dw;
        itemList[0].style.width = w + "px";
    }
    // Fix the IE display problem (SELECT elements and other windowed controls
    // overlaying the menu) by adding an IFRAME under the menu.
    if (ActiveBrowser.isexplorer) {
        var iframeEl = document.createElement("IFRAME");
        iframeEl.frameBorder = 0;
        iframeEl.src = "javascript:;";
        iframeEl.style.display = "none";
        iframeEl.style.position = "absolute";
        iframeEl.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        menu.iframeEl = menu.parentNode.insertBefore(iframeEl, menu);
    }
    // Mark menu as initialized.
    menu.isInitialized = true;
}

function GetContainerWith(node, tagName, className) {
    // Starting with the given node, find the nearest containing element
    // with the specified tag name and style class.
    while (node != null) {
        if (node.tagName != null && node.tagName == tagName && HasClassName(node, className)) return node;
        node = node.parentNode;
    }
    return node;
}

function HasClassName(e, name) {
    var i, list;
    list = e.className.split(" ");
    for (i = 0; i < list.length; i++)
        if (list[i] == name) return true;
    return false;
}
function RemoveClassName(e, name) {
    var i, curList, newList;
    if (e.className == null) return;
    newList = new Array();
    curList = e.className.split(" ");
    for (i = 0; i < curList.length; i++)
        if (curList[i] != name) newList.push(curList[i]);
    e.className = newList.join(" ");
}

function GetPageOffsetLeft(e) {
    var x;
    // Return the x coordinate of an element relative to the page.
    x = e.offsetLeft;
    if (e.offsetParent != null) x += GetPageOffsetLeft(e.offsetParent);
    return x;
}

function GetPageOffsetTop(e) {
    var y;
    y = e.offsetTop;
    s = e.id + '=' + e.offsetTop + ': ' + e.offsetParent;
    //alert(s);
    if (e.offsetParent != null) {
        y += GetPageOffsetTop(e.offsetParent);
    }
    return y;
}
function aaa() {
    var e = document.getElementById('top5');
    alert(e.offsetParent.clientTop);
}