//Constantes

menu.SubMenus = []
menu.duracion = 250
menu.retardo = 100
menu.intervalo = 10

// Metodos estaticos

menu.showMenu = function(id){
	var reg = menu.SubMenus
	var obj = menu.SubMenus[id]
	
	if (typeof(obj) != 'undefined'){
		if (typeof(obj.container) != 'undefined') {
			obj.over = true
			for (submenu in reg) if (id != submenu) menu.hide(submenu)
			if (obj.hideTimer) { reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer) }
			if (!obj.open && !obj.aniTimer) reg[id].Abrir(true)
		}
	}
}

menu.hideMenu = function(id){
	var obj = menu.SubMenus[id]
	if (typeof(obj) != 'undefined'){
		if (typeof(obj.container) != 'undefined') {
			if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
			obj.hideTimer = window.setTimeout("menu.hide('" + id + "')", menu.retardo);
		}
	}
}

menu.hide = function(id){
	var obj = menu.SubMenus[id]
	obj.over = false
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
	obj.hideTimer = 0
	if (obj.open && !obj.aniTimer)	obj.Abrir(false)
}

// Objeto Menu

function menu(id, dir){
	this.ie  = document.all ? 1 : 0
	this.ns4 = document.layers ? 1 : 0
	this.dom = document.getElementById ? 1 : 0

	if (this.ie || this.ns4 || this.dom) {
		this.id		 = id
		this.dir	 = dir
		this.orientation = dir == "left" || dir == "right" ? "h" : "v"
		this.dirType	 = dir == "right" || dir == "down" ? "-" : "+"
		
		this.hideTimer	 = false
		this.aniTimer	 = false
		this.open	 = false
		this.over	 = false
		this.startTime	 = 0

		
		this.globalRef = "menu_"+id
		eval(this.globalRef+"=this")
		
		
		menu.SubMenus[id] = this

		var d = document;
	
		var ancho =  eval("d.getElementById('" + this.id  + "T').clientWidth") + 22;
		var alto  = eval("d.getElementById('" + this.id  + "T').clientHeight")+2;
		var arriba = eval("d.getElementById('" + this.id  + "').offsetTop + d.getElementById('" + this.id  + "').clientHeight")+2;
		var derecha  = eval("d.getElementById('" + this.id  + "').offsetLeft");
		
		
		
		this.dim = this.orientation == "h" ? ancho : alto
		
                var strCSS = '<style type="text/css">';
                strCSS += '#' + this.id + 'C { visibility:hidden; '
		strCSS += 'left:' + derecha + 'px; '
		strCSS += 'top:' + arriba + 'px; '
		strCSS += 'width:' + ancho + 'px; '
		strCSS += 'height:' + alto + 'px; '		
		strCSS += 'overflow:hidden; z-index:10000; }'
		strCSS += '#' + this.id + 'C, #' + this.id + 'CC { position:absolute; '
		strCSS += 'width:' + ancho + 'px; '
		strCSS += 'height:' + alto + 'px; '
		strCSS += 'clip:rect(0 ' + ancho + ' ' + alto + ' 0); '
		strCSS += '}'
        strCSS += '</style>';
		
		d.getElementById('mw').innerHTML += strCSS;
               
		this.load();
	}
}

menu.prototype.load = function() {
	var d = document
	var lyrId1 = this.id + "C"
	var lyrId2 = this.id + "CC"
	var obj1 = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
	if (obj1) var obj2 = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
	var temp

	if (!obj1 || !obj2) window.setTimeout(this.globalRef + ".load()", 100);
	else {
		this.container	= obj1
		this.submenu	= obj2
		this.style	= this.ns4 ? this.submenu : this.submenu.style
		this.homePos	= eval("0" + this.dirType + this.dim)
		this.outPos	= 0
		this.accelConst	= (this.outPos - this.homePos) / menu.duracion / menu.duracion 

		if (this.ns4) this.submenu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.submenu.onmouseover = new Function("menu.showMenu('" + this.id + "')")
		this.submenu.onmouseout = new Function("menu.hideMenu('" + this.id + "')")

		this.Cerrar()
	}
}
	
menu.prototype.Abrir = function(open) {
		var arriba = eval("document.getElementById('" + this.id  + "').offsetTop + document.getElementById('" + this.id  + "').clientHeight")+2;
		var derecha  = eval("document.getElementById('" + this.id  + "').offsetLeft");
		eval(this.id + 'C.style.top=' + arriba);
		eval(this.id + 'C.style.left=' + derecha);
	this[open ? "onactivate" : "ondeactivate"]()
	this.open = open
	if (open){ 
		this.setVisibility(true)
		eval("document.getElementById('" + this.id  + "').className='mOver'");
	}
	this.startTime = (new Date()).getTime()	
	this.aniTimer = window.setInterval(this.globalRef + ".Deslizar()", menu.intervalo)
}

menu.prototype.Deslizar = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > menu.duracion) {
		this.Cerrar()
	}else {
		var d = Math.round(Math.pow(menu.duracion-elapsed, 2) * this.accelConst)
		if (this.open && this.dirType == "-")
			d = -d
		else if (this.open && this.dirType == "+")	
			d = -d
		else if (!this.open && this.dirType == "-")	
			d = -this.dim + d
		else						
			d = this.dim + d

		this.moveTo(d)
	}
}

menu.prototype.Cerrar = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer)
	this.moveTo(this.open ? this.outPos : this.homePos)
	if (!this.open){ 
		this.setVisibility(false);
		eval("document.getElementById('" + this.id  + "').className='mNormal'");
	}
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.Abrir(this.over)
	}
}

menu.prototype.setVisibility = function(bShow) { 
	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}
menu.prototype.moveTo = function(p) { 
	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}
menu.prototype.getPos = function(c) {
	return parseInt(this.style[c])
}

menu.prototype.onactivate = function() { }
menu.prototype.ondeactivate = function() { }
