
function MenuDesplegableItem() {
	var a=arguments
    this.name = a[0];
    this.submenus = [];
    if (a[1]) {
        this.url = a[1];
    } else {
        this.url = null;
    }
}
MenuDesplegableItem.prototype.addSubmenu = function() {
	var a=arguments
    this.submenus[this.submenus.length] = a[0];
}

function MenuDesplegable() {
	var a=arguments
	if (a[0]) this.name = a[0];
	else this.name = null;

	this.numMenus = 0;
    this.menus = [];
    this.document = null;

    this.relDivs = [];
    this.layers = [];
    this.buttonLayers = [];

    /** button html es un string con marcas ||link|| y ||label|| **/
    this.buttonHtml = "";
	}

MenuDesplegable.prototype.onLoad = function() {
	l = new EventListener();
	l.onmouseout = function(e) {
        i = e.getSource().index;
        p = e.getSource().menu;
        p.layers[i].over = false;
		//p.hideMenu(i);
        setTimeout(p.name + 'menus.hideMenu(' + i + ')', 10);
	}
	l.onmouseover = function(e) {
        i = e.getSource().index;
        p = e.getSource().menu;
        p.layers[i].over = true;
		//p.hideMenu(i);
	}
    for (var i=0; i<this.numMenus; i++) {
        this.relDivs[i] = DynAPI.document.all[this.getName() + 'relDiv' + i];
        this.layers[i] = {};
        this.layers[i].link = new DynLayer ();
        this.layers[i].link.index = i;
        this.layers[i].link.menu = this;
        this.layers[i].over = false;
        this.layers[i].submenu=[];
        this.layers[i].lastblink_id=null;
        this.layers[i].lastopen_submenu=-1;
        this.layers[i].link.setVisible(false);
        this.layers[i].link.setZIndex(10000);
        this.layers[i].link.addEventListener(l);
        this.layers[i].link.setHTML(this.getHTML(i));
        DynAPI.document.addChild(this.layers[i].link);

        //botones
        this.buttonLayers[i] = {};
        this.buttonLayers[i].link = new DynLayer ();
        this.buttonLayers[i].link.index = i;
        this.buttonLayers[i].link.menu = this;
        this.buttonLayers[i].link.setVisible(false);
        this.buttonLayers[i].link.setZIndex(10000);
        this.buttonLayers[i].link.addEventListener(l);
        this.buttonLayers[i].link.setHTML(this.menus[i].botonActivo);
        DynAPI.document.addChild(this.buttonLayers[i].link);
	}

    this.onResize();
}

MenuDesplegable.prototype.onResize = function() {
    for (var i=0; i<this.numMenus; i++) {
        this.relDivs[i].updateValues();
        x = this.relDivs[i].getX() + this.relDivs[i].getWidth() - this.layers[i].link.getWidth();
        y = this.relDivs[i].getY() + this.relDivs[i].getHeight();
        this.layers[i].link.moveTo(x,y);

        xB = this.relDivs[i].getX();
        yB = this.relDivs[i].getY();
        this.buttonLayers[i].link.moveTo(xB, yB);
    }
}

MenuDesplegable.prototype.getName = function() {
    return this.name;
}

MenuDesplegable.prototype.setMenus = function () {
	var a=arguments;
	if (!a[0]) return;
	this.menus = a[0];
    this.numMenus = this.menus.length;
}

MenuDesplegable.prototype.showMenu = function (i) {
    //ComboHide.hide(this.layers[i].link);
    //alert('ComboHide.hide(' + this.name + 'menus.layers[' + i + '].link)');
    setTimeout('ComboHide.hide(' + this.name + 'menus.layers[' + i + '].link)', 20);

	for (var j=0; j<this.numMenus; j++) {
		if (i==j) {
            this.layers[j].link.setVisible(true)
            this.buttonLayers[j].link.setVisible(true);
        } else { 
            this.layers[j].link.setVisible(false);
            this.buttonLayers[j].link.setVisible(false);
        }
    }
}

MenuDesplegable.prototype.hideMenu = function (i) {
    if (!this.layers[i].over) {
        this.layers[i].link.setVisible(false);
        this.buttonLayers[i].link.setVisible(false);
        ComboHide.show();
    }
}


MenuDesplegable.prototype.getHeight = function () {
	var height = this.html.getHeight();
	if( this.expanded ) {
		for (var i=0; i<this.numMenus; i++) {
			height += this.content[i].getHeight();
		}
	}
	return height;
}

MenuDesplegable.prototype.getWidth = function () {
	var width = this.html.getWidth();
	if( this.expanded ) {
		for (var i=0; i<this.numMenus; i++) {
			width = Math.max(width, this.content[i].getWidth());
		}
	}
	return width;
	}

MenuDesplegable.prototype.getHTML = function (i) {
    var txt = '';
    var menu = this.menus[i];
    var submenus = this.menus[i].submenus;

    //txt += menu.botonActivo;
    txt += '<table border="0" cellspacing="0" cellpadding="0">';
    //txt += '<tr>';
    //txt += '    <td align="right">' + menu.botonActivo + '</td>';
    //txt += '</tr>';
    txt += '<tr>';
    txt += '    <td>';
    txt += this.extBoxStart;
    txt += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';

    for (var j=0; j<submenus.length; j++) {
        txt += '<tr>';
        txt += '    <td id="tid_' + this.getName() + '_' + i + '_' + j + '" onmouseover="' + this.getName() + 'menus.optionOver(' + i + ',' + j + ')" onmouseout="' + this.getName() + 'menus.optionOut(' + i + ',' + j + ')">';
        txt += this.optionBoxStart;
        if (submenus[j].url != null) {
            //txt += '<a href="' + submenus[j].url + '">';
            txt += submenus[j].url; //url contiene el href completo
        }
        txt += this.optionTextStart;
        txt += '<font id="fid_' + this.getName() + '_' + i + '_' + j + '">';
        txt += submenus[j].name;
        txt += '</font>';
        txt += this.optionTextEnd;
        if (submenus[j].url != null) {
            txt += '</a>';
        }
        txt += this.optionBoxEnd;
        txt += '</td>';
        txt += '</tr>';
    }

    txt += '</table>';
    txt += this.extBoxEnd;
    txt += '</td>';
    txt += '</tr>';
    txt += '</table>';
	return txt;
	}

MenuDesplegable.prototype.optionOver = function (i, j) {
    var tid = 'tid_' + this.getName() + '_' + i + '_' + j;
    var fid = 'fid_' + this.getName() + '_' + i + '_' + j;
    document.getElementById(tid).style.backgroundColor = this.bgColorOn;
    document.getElementById(fid).style.color = this.txtColorOn;
}

MenuDesplegable.prototype.optionOut = function (i, j) {
    var tid = 'tid_' + this.getName() + '_' + i + '_' + j;
    var fid = 'fid_' + this.getName() + '_' + i + '_' + j;
    document.getElementById(tid).style.backgroundColor='';
    document.getElementById(fid).style.color='';
}

