
var ie=navigator.appName=="Microsoft Internet Explorer";
var mac=navigator.platform=="MacPPC";

var shown = {};
//-------------------------------------------------------------------------
function getAncestorByTagName(elt,tagName,level)
{
        level = level || 1;
        var i = 0;
        while(elt && i< level) {
                elt = elt.parentElement;
                if (elt && elt.nodeName == tagName)
                        i++;
        }
        return elt;
}
//------------------------------------------------------------------------------------------
function getX(img) {
  var X=0;
  do {
        X+=img.offsetLeft;
  } while (img=img.offsetParent);
  return X;
}
//------------------------------------------------------------------------------------------
function getY(img) {
  var Y=0;
  do {
        Y+=img.offsetTop;
  } while (img=img.offsetParent);
  return(Y);
}
//------------------------------------------------------------------------------------------
// item = {text:,tooltip:,link:,submenu:,id:,image:,hiimage:} 
function rollOver(img,image){
//      alert(img.src + " | " + img.__old_one)
        if(!image)
                img.src = img.__old_one || img.src;
        else if (image == img.src.substr(img.src.length - image.length)) 
                return;
        else{
                img.__old_one = img.src;
                img.src=image;
        }
}
//----------------------------------------------------------------------------------------
function getElement(id){
        if (document.getElementById)
                return document.getElementById(id);
        else if (document.all)
                return document.all[id];
        else if (document.layers)
                return document.layers[id];
        return null;
}

__menus = {}

function _get_menu_id(){
        var id =""
        do{
                id = Math.round(Math.random()*10.0E9);
        }while (__menus[id])
        return id;
}

//mac ie does not support array.push
Array.prototype.push = function(item){
	this[this.length]=item;
}

function Menu (items,opts){
        this.$items=[];
        opts = opts || {};
        if (items){
                for (var i=0;i<items.length;i++)
                        this.addItem(items[i])
        }
        this.id = opts.id || _get_menu_id(); 
        this.direction = opts.dir || 'v';
        this.visible = opts.visible || false;
        this.border=opts.border == null ? 1 : opts.border;
        this.width = opts.width || 0;
        __menus[this.id] = this;
}

Menu.prototype.addItem = function (item){
        if (!item)
                return;
        item.id =  item.id || _get_menu_id();
        item.parent = this;
        if (item.menu && item.menu.$items)
//      if (item.menu instanceof Menu)
                item.menu.parent = item;
        __menus[item.id] = item;
        this.$items[this.$items.length]= item;
}

//mac ie can not calculate current <td> Y
Menu.prototype.itemIndex = function (item){
	var i = 0;
	var items = this.$items;
	while(i<items.length && item!=items[i])i++;
	if(i==items.length) i = 0;
	return i;
}

Menu.prototype.show = function(){
        for (var i in __menus){
                if (!(__menus[i] && __menus[i].$items))
                        continue;
                __menus[i].__show = __menus[i].visible || false;
        }

        var x,elm = getElement(this.id);
        
        if (this.parent)
                x =getElement(this.parent.id)
        if (x){
				if(mac && ie){
					var index = this.parent.parent.itemIndex(this.parent);
					elm.style.top = getY(x)-getY(document.getElementById("__MenuSpan"))+
					(this.parent.parent.direction == "h"? x.offsetHeight : index*(x.offsetHeight + 1) );
				}else
	                elm.style.top = getY(x) + (this.parent.parent.direction == "h"? x.offsetHeight : - 1);
                elm.style.left = getX(x) + (this.parent.parent.direction == "h"? -1 : x.offsetWidth);
                elm.style.zIndex = x.style.zIndex + 1;
                if (this.parent.parent.direction == "h"){
                        //alert(elm.offsetWidth + " | " + x.offsetWidth);
                        elm.style.width = elm.offsetWidth > x.offsetWidth ? elm.offsetWidth :x.offsetWidth;
                        //alert(elm.style.width);
                }
        }
                
        x = this;
        while (x.parent){
                x.__show = true;
                x = x.parent;
        }
        Redraw();
}


Menu.prototype.hide = function(){
        if (this.visible)
                return;
        for (var i=0;i<this.$items.length;i++){
                if (this.$items[i].menu)
                        this.$items[i].menu.hide();
        }
        this.__show = false;
        Redraw();
}

function hideAll(){
        for (var i in __menus){
                if (!__menus[i] || !__menus[i].$items)
//              if (!(__menus[i] instanceof Menu))
                        continue;
                __menus[i].__show = __menus[i].visible;
        }
        Redraw();
}

Redraw = function (){
        var elm;
        for (var i in __menus){
                if (!__menus[i] || !__menus[i].$items)
                //if (!(__menus[i] instanceof Menu))
                        continue;
                elm = getElement(i)
                pelm = null;
                par = __menus[i].parent;
                if(par)
                        pelm = getElement(par.id);
                if ( __menus[i].visible  || __menus[i].__show ){
                        elm.style.visibility = "visible";
                        if (pelm){
                                
                                if(!par.parent.parent)
                                        pelm.className = 'rootmenuitem_high';
                                else
                                        pelm.className = 'menuitem_high';
                                        
                                pimg = getElement('__image' + par.id);
                                if (pimg && par.hiimage)
                                        rollOver(pimg,par.hiimage);
                        }
                } else {
                        elm.style.visibility = "hidden";
                        if (pelm){
                                if(!par.parent.parent)
                                        pelm.className = 'rootmenuitem';
                                else
                                        pelm.className = 'menuitem';
                                pimg = getElement('__image' + par.id);
                                if (pimg)
                                        rollOver(pimg);
                        }
                }
        }
}

function onMenuItemOver(div,subid){
        var this_menu = __menus[div.id].parent;

        if(!this_menu.parent)
                div.className = 'rootmenuitem_high';
        else
                div.className = 'menuitem_high';
        if (subid)
                __menus[subid].show();
        else if (this_menu){
                this_menu.show();
        }
        for (var i =0;i<this_menu.$items.length;i++){
                elm = getElement(this_menu.$items[i].id);
                if (div != elm){
                        if(!this_menu.parent)
                                elm.className = 'rootmenuitem';
                        else
                                elm.className = 'menuitem';
                }
        }
}

function onMenuItemOut(div,subid){
        var this_menu = __menus[div.id].parent;
        if(!this_menu.parent)
                div.className = 'rootmenuitem';
        else
                div.className = 'menuitem';
/*      if (subid)
                __menus[subid].hide()*/
}

function onMenuOut(div){
/*      menu = __menus[div.id];
        if(menu.visible) return;
        x = window.event.clientX;
        y = window.event.clientX;
        var _top = String(div.style.top).replace(/[^0-9] * /g,"");
        var _left = String(div.style.left).replace(/[^0-9]* /g,"");
        if (_top < y && y < _top + div.offsetHeight &&
                _left < x && x < _left + div.offsetWidth)
                return;
        Redraw(event);
//      __menus[div.id].hide(div.id);*/
}


function onMenuOver(div){
}

Menu.prototype.toString = function() {
        var txt = [];
        var subs = [];
        // generating menu items
        for (var i=0;i<this.$items.length;i++){
                obj = this.$items[i];
                var par = obj;
        
                //if (obj.menu instanceof Menu)
                if (obj.menu  && obj.menu.$items)
                        subs.push(obj.menu.toString());
                obj = this.$items[i];
                tmp = [];
                
                tmp.push('<td class='+(!par.parent.parent?"rootmenuitem":"menuitem")+' style="height: 100%" id="');
                tmp.push(obj.id);
                tmp.push('" onmouseover="javascript: onMenuItemOver(this')
                tmp.push((obj.menu && obj.menu.$items) ? ",'" + obj.menu.id  + "'": "");
                tmp.push(')"');
                tmp.push('" onmouseout="javascript: onMenuItemOut(this')
                tmp.push((obj.menu && obj.menu.$items) ? ",'" + obj.menu.id  + "'": "");
                tmp.push(')"');
                tmp.push(obj.tooltip ? 'title="' + obj.tooltip + '"' : "");
                if (obj.link){
                        tmp.push ('onclick="javascript: document.location.href=\'' + obj.link+ '\'"')
                }
                if (ie){
                        tmp.push('style="width: 100%"')
                }
                tmp.push('>');
                if (document.layers && obj.link){
                        tmp.push('<a href="' + obj.link + '">')
                }
                if (obj.image){
                        tmp.push('<img border=0 id="__image' + obj.id + '" onmouseover="javascript: rollOver(this,\'') 
                        tmp.push(obj.hiimage || '');
                        tmp.push('\')" onmouseout="javascript: rollOver(this)" alt= "') 
                        tmp.push(obj.text ||'')
                        tmp.push('"src="' + obj.image + '">')
                }else{
                        tmp.push('<span style="' +  (obj.width ? ('width: ' + obj.width): '') + '">' +
obj.text + "</span>")
                }
                if (document.layers && obj.link){
                        tmp.push('</a>')
                }
                tmp.push("</td>")
                txt.push(tmp.join(""));
        }
        return    (document.layers ? "" :  subs.join("")) + '<div id=' + this.id + ' onmouseout="javascript:onMenuOut(this)"' + 
        ' onmouseover="javascript: onMenuOver(this)" class=menu style="display: inline; ' + 
        (this.width ? 'width: ' + this.width + 'px;' : '')+ 'visibility: ' +
        (this.visible ? "visible; padding-left: 0px" : "hidden")  + 
         '; position: '  + (this.visible ? "relative" : "absolute") + ';margin:0px;padding: 0px">' + 
         '<table border=0 ' + (this.width ? 'width=' + this.width: '') + ' cellpadding=0 cellspacing=0><tr><td class=border>' + 
         '<table border=0 ' + (this.width ? 'width=' + this.width: '') + ' cellpadding=0 cellspacing=' + this.border +'><tr>' + 
         txt.join( this.direction == "h" ? "" : "</tr><tr>") + 
         '</tr></table>' + '<td></tr></table></div>'; 
}
//-------------------------------------------------------------------------
function cancelBubble(evt){
    if(evt){
         if (evt.stopPropagation)
             evt.stopPropagation()
         else
         if (evt.cancelBubble != null)
             evt.cancelBubble = true;
    }else
    if(event){
    	//mac ie
	    if (event.stopPropagation)
             event.stopPropagation()
        else
        if (event.cancelBubble != null)
            event.cancelBubble = true;      
    
    }
}
//-------------------------------------------------------------------------


