var _menuElements = new Array();
var _menuItems = new Array();
var _menuInf = null;
var _menuLayout = null;
var _menuTimeout = 500;
var ie = (document.all);
var _menuTransparency = false;


// xb.js required!

function MenuItem(name, idItem, idSubBlock, layout)
{
	this.name = name;
	this.idItem = idItem;
	this.idSubBlock = idSubBlock;
	this.cn = "";

	this.obj = this.name + '_obj';
	eval(this.obj + ' = this;');
	this.timerID = false;

	if (getEbyID(this.idSubBlock))
	{
	    if( layout == "dropdown" )
		    getEbyID(this.idSubBlock).style.left = getOffsetLeft( getEbyID(this.idItem) )+'px';
	    if( layout == "dropright" )
		    getEbyID(this.idSubBlock).style.top = getOffsetTop( getEbyID(this.idItem) )+'px';
	}
}

MenuItem.prototype =
{
    className: "MenuItem",

    mouseOver: function()
    {
        if (this.timerID)
        {
	        clearTimeout(this.timerID);
	        this.timerID = false;
	    }

    },

    mouseOut: function()
    {
        if(!this.timerID)
        {
	        this.timerID = setTimeout(this.obj + '.hide()', _menuTimeout);
	    }

    },
    
    addClassName: function(presentcn,postfix)
    {
        var temp = presentcn.split(" ");
        var classnames = new Array();
        for(i=0;i < temp.length;i++)
            if( temp[i] != "" )
                classnames[classnames.length] = temp[i];
                
        if(classnames.length > 0)
            classnames[classnames.length] = classnames[classnames.length-1] + postfix;
            
        return classnames.join(" ");
    },

    show: function()
    {
        for(var i=0; i<_menuItems.length; i++)
            if (this.idSubBlock != _menuItems[i].idSubBlock)
                _menuItems[i].hide();

        showBlock(getEbyID(this.idSubBlock),true);
        
        this.showIFrame();
        this.mouseOver();

        if( this.cn == "" )
        {
            this.cn = getEbyID(this.idItem).className;
            getEbyID(this.idItem).className = this.addClassName(this.cn,'_over');
        }
    },

    hide: function()
    {
    	if (getEbyID(this.idSubBlock))
    	{
    	    showBlock(getEbyID(this.idSubBlock),false);
            this.hideIFrame();
    	}

        if( this.cn != "" )
        {
            getEbyID(this.idItem).className = this.cn;
            this.cn = "";
        }
    },

    showIFrame: function()
    {
        if( ie )
        {
            var ifNode = getEbyID("menuif_" + this.name);
            var nodeBlock = getEbyID(this.idSubBlock);
            var rect = getBlockRect(nodeBlock);

            showBlock(ifNode,true);
            ifNode.style.left    = (rect.left) + "px";
            ifNode.style.top     = (rect.top) + "px";
            ifNode.style.width   = (rect.right-rect.left) + "px";
            ifNode.style.height  = (rect.bottom-rect.top) + "px";
            
            if( _menuTransparency )
                if( ifNode.contentWindow )
                    if( ifNode.contentWindow.document )
                        if( ifNode.contentWindow.document.body )
        		            ifNode.contentWindow.document.body.style.backgroundColor = "transparent";
        }
    },

    hideIFrame: function()
    {
        if( ie )
        {
            var ifNode = getEbyID("menuif_" + this.name);
            showBlock(ifNode,false);
        }
    }
}

function menuEvents(idSubBlock, obj)
{
    if (!getEbyID(idSubBlock))
        return;

    var navItems = getEbyID(idSubBlock).getElementsByTagName('div');
    for (var i=0; i<navItems.length; i++)
    {
        if (navItems[i].className.indexOf('menuparent') > -1)
        {
            navItems[i].onmouseover = function() { this.cn = this.className; this.className += ' ' + this.className + ' _over'; }
            navItems[i].onmouseout = function() { this.className = this.cn; }
        }
    }
    var navItems = getEbyID(idSubBlock).getElementsByTagName('a');
    for (var i=0; i<navItems.length; i++)
    {
        addEvent(navItems[i], 'mouseover', obj,'mouseOver');
        addEvent(navItems[i], 'mouseout', obj,'mouseOut');
    }
}

function menuIframe(name, idSubBlock)
{
    if( ie )
    {
        nodeBlock = getEbyID(idSubBlock);

		var iframe = document.createElement("IFRAME");
        iframe.id = "menuif_" + name;
        
        if( _menuTransparency )
            iframe.allowTransparency = true;
            
        iframe.border = "0";
        iframe.frameBorder = "0";
        iframe.style.position = "absolute";
        iframe.style.zIndex = '0';
        iframe.style.left = '0px';
        iframe.style.top = '0px';
        showBlock(iframe,false);

		document.body.appendChild(iframe);

    }

}

// layout: dropdown, dropright

function initMenu(aMenu,layout)
{
    if( layout == undefined )
        layout = "dropdown";

    _menuElements = new Array();
    _menuItems = new Array();


    for(var i=0;i < aMenu.length;i++)
    {
        _menuElements[_menuElements.length] = getEbyID(aMenu[i].idItem);

        if( ! _menuElements[i] )
            throw('Invalid menu element : ' + aMenu[i].idItem);

        _menuElements[i]._menuIdx = i;
        if( !_menuElements[i].href )
            _menuElements[i].onclick = function() { return false; };
    }

    window.onresize = function() { initMenu(_menuInf,_menuLayout); }

    initMenuItems(aMenu,layout);

    _menuInf = aMenu;
    _menuLayout = layout;
}



function initMenuItems(aMenu,layout)
{
    for(var i=0;i < aMenu.length;i++)
    {
        _menuItems[_menuItems.length] = new MenuItem(aMenu[i].name, aMenu[i].idItem, aMenu[i].idSubBlock,layout);
        menuEvents(aMenu[i].idSubBlock,_menuItems[i]);
        menuIframe(aMenu[i].name, aMenu[i].idSubBlock);

        addEvent(_menuElements[i], 'mouseover', _menuItems[i], 'show' );
        addEvent(_menuElements[i], 'mouseout', _menuItems[i], 'mouseOut' );

    }
}


