/**
 * This javascript is used to control menu events and contents.
 *
 * $Rayge: rayge/js/menu.js,v 1.1 2005/12/14 16:52:23 fhelly Exp $
 * $Id: Menu.js 67 2006-03-21 01:31:44Z François Helly $
 *
 * $Revision: 54 $
 * $Date: 2006-04-07 15:24:11 +0200 (Fri, 07 Apr 2006) $
 * $Author: $
 *
 * Copyright 2005-2006 Francois Helly <fhelly@bebop-design.net>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @version $Revision: 54 $
 * @package rayge
 */

var menus = new Array();

/**
 * Constructs the menu object and sets its attributes.
 *
 * @param string id   The menu container ID
 * @param string tag  The menu items container html tag
 */
function Menu(id, tag)
{

    if (!document.getElementById && !getElementsByTagName) {
        this.container = false;
        return this;
    }
      this.container   = document.getElementById(id)||false;
    if (this.container) {
        this.entries = this.container.getElementsByTagName(tag);
    }
    this.tag         = tag;

    this.toggle = function(mode)
    {
        if (!this.container) { return; }
        // Initialize the events
        for (var i=0;i<this.entries.length;i++) {
            if (this.entries[i].hasChildNodes()) {

                this.entries[i].onmouseover = mouseOverMenu;
                this.entries[i].onclick     = clickOnMenu;
                this.entries[i].mode        = mode;
                this.entries[i].tag         = this.tag;
                this.inittoggle(this.entries[i]);
            }

        }
    }

    this.inittoggle = function(entry)
    {
        childs = entry.getElementsByTagName(entry.tag);
        for (var i = 0; i<childs.length; i++) {
            childs[i].style.display = (childs[i].style.display == 'none') ? entry.mode : 'none';
        }
    }

    this.additem = function(id, title, pos, href, onclick, img, textnode, classname)
    {
        if (!this.container) { return; }
        /* Defines if a text node is required
           or not, default to true */
        if (typeof(textnode) == 'undefined') {
            textnode = new Boolean(true);
        }
        var item  = document.createElement(this.tag);
        var link  = document.createElement('a');
        var index = pos;
        link.title = title;
        if (typeof(onclick) != 'undefined' && onclick) {
            link.onclick = onclick;
        }
        if (typeof(href) != 'undefined') {
            link.href = href;
            if (typeof(classname) != 'undefined') {
                link.className = classname;
            }
        } else {
            link.className = 'inactive';
        }
        link.setAttribute('id', id);
        if (typeof(img) != 'undefined' && img) {
            var icon = document.createElement('img');
            icon.setAttribute('class', 'icon');
            icon.setAttribute('title', title);
            icon.setAttribute('alt', title);
            icon.src = img;
            link.appendChild(icon);
        }
        if (textnode) {
            var text  = document.createTextNode(title);
            link.appendChild(text);
        }
        item.appendChild(link);
        if (pos < -1) {
            index = this.entries.length +pos+1;
        }
        if (pos == -1 || index > this.entries.length) {
            this.container.appendChild(item);
        } else {
            this.container.insertBefore(item, this.entries[index]);
        }
    }

}
function mouseOverMenu(e)
{
    if (!e) {
        var e = window.event;
    }
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    if (document.all) {
        this.style.cursor = 'hand';
    } else {
        this.style.cursor = 'pointer';
    }
}
function clickOnMenu(e)
{
    if (!e) {
        var e = window.event;
    }
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    childs = this.getElementsByTagName(this.tag);
    for (var i = 0; i<childs.length; i++) {
        childs[i].style.display = (childs[i].style.display == 'none') ? this.mode : 'none';
    }
}