/* ----- generaldropdown.js ----- */
/*
* This is the code for the dropdown menus. It uses the following markup:
*
* <dl class="actionMenu" id="uniqueIdForThisMenu">
* <dt class="actionMenuHeader">
* <!-- The following a-tag needs to be clicked to dropdown the menu -->
* <a href="some_destination">A Title</a>
* </dt>
* <dd class="actionMenuContent">
* <!-- Here can be any content you want -->
* </dd>
* </dl>
*
* When the menu is toggled, then the dl with the class actionMenu will get an
* additional class which switches between 'activated' and 'deactivated'.
* You can use this to style it accordingly, for example:
*
* .actionMenu.activated {
* display: block;
* }
*
* .actionMenu.deactivated {
* display: none;
* }
*
* When you click somewhere else than the menu, then all open menus will be
* deactivated. When you move your mouse over the a-tag of another menu, then
* that one will be activated and all others deactivated. When you click on a
* link inside the actionMenuContent element, then the menu will be closed and
* the link followed.
*
* This file uses functions from register_function.js, cssQuery.js and
* nodeutils.js.
*
*/

function isDropDownMenu(node) {
if (hasClassName(node, 'display-element')) {
return true;
}
return false;
};

function hideAllDropDownMenus() {
var menus = cssQuery('div.display-element');
for (var i=0; i < menus.length; i++) {
replaceClassName(menus[i], 'on', 'off', true);
}
};

function toggleDropDownMenuHandler(event) {
if (!event) var event = window.event; // IE compatibility

// terminate if we hit a non-compliant DOM implementation
// returning true, so the link is still followed
if (!W3CDOM){return true;}

var container = findContainer(this, isDropDownMenu);
if (!container) {
return true;
}

// check if the menu is visible
if (hasClassName(container, 'activated')) {
// it's visible - hide it
replaceClassName(container, 'on', 'off', true);
} else {
// it's invisible - make it visible
replaceClassName(container, 'off', 'on', true);
}

return true;
};

function hideDropDownMenusHandler(event) {
if (!event) var event = window.event; // IE compatibility

hideAllDropDownMenus();

// we want to follow this link
return true;
};

function actionDropDownMenuDocumentMouseDown(event) {
if (!event) var event = window.event; // IE compatibility

if (event.target)
targ = event.target;
else if (event.srcElement)
targ = event.srcElement;

var container = findContainer(targ, isDropDownMenu);
if (container) {
// targ is part of the menu, so just return and do the default
return true;
}

hideAllDropDownMenus();

return true;
};

function actionDropDownMenuMouseOver(event) {
if (!event) var event = window.event; // IE compatibility

if (!this.tagName && (this.tagName == 'A' || this.tagName == 'a')) {
return true;
}

var container = findContainer(this, isDropDownMenu);
if (!container) {
return true;
}
var menu_id = container.id;

var switch_menu = false;
// hide all menus
var menus = cssQuery('div.display-element');
for (var i=0; i < menus.length; i++) {
var menu = menus[i]
// check if the menu is visible

// turn off menu when it's not the current one
if (menu.id != menu_id) {
replaceClassName(menu, 'on', 'off', true);
}
}

var menu = cssQuery('#'+menu_id)[0];
if (menu) {
if (hasClassName(menu, 'off')) {
replaceClassName(menu, 'off', 'on', true);
}
}
return true;
};

function initializeDropDownMenus() {
// terminate if we hit a non-compliant DOM implementation
if (!W3CDOM) {return false;}

document.onmousedown = actionDropDownMenuDocumentMouseDown;

hideAllDropDownMenus();

// add toggle function to header links
var menu_headers = cssQuery('div.display-element > div.display-header > a');
for (var i=0; i < menu_headers.length; i++) {
var menu_header = menu_headers[i];

menu_header.onclick = toggleDropDownMenuHandler;
menu_header.onmouseover = actionDropDownMenuMouseOver;
}

// add hide function to all links in the dropdown, so the dropdown closes
// when any link is clicked
var menu_contents = cssQuery('div.display-element > div.display-content');
for (var i=0; i < menu_contents.length; i++) {
menu_contents[i].onclick = hideDropDownMenusHandler;
}

// uncomment to enable sorting of elements
//var nodes = cssQuery('#objectMenu > dd.actionMenuContent li');
//sortNodes(nodes, getInnerTextFast);
};

registerEventListener(window, "load", initializeDropDownMenus);

function displayByClick(id) {
    node=document.getElementById(id);
    changed=0
    if (hasClassName(node, 'off')) {
        replaceClassName(node, 'off', 'on', true);
        changed=1
    }
    if (!changed && hasClassName(node, 'on')) {
        replaceClassName(node, 'on', 'off', true);
    }
}
