// JavaScript Document
MenuColorao = function(){
	this.items = [];
}
MenuColorao.prototype.init = function(newLabel , newDescription , newAction , divName){
	this.label = newLabel;
	if(newDescription) this.description = newDescription;
	if(newAction) this.action = newAction; //Puede ser una URL o una funcion
	if(divName) this.childrenDiv = document.getElementById(divName);
	return this;
}
MenuColorao.prototype.addItem = function(oItem,selected){
		oItem.parentObject=this;
		if(selected) oItem.selected = selected;
		this.items.push(oItem);
}
MenuColorao.prototype.doClick = function(){
	
	this.selectItem();

	// Puto Explorer no pirula
	//if(this.action && this.action.__proto__ == String.prototype){self.location.href = this.action;}
	//if(this.action && this.action.__proto__ == Function.prototype){this.action();}
	
	//Cadenas
	if(this.action && (typeof(this.action)=="string") ){self.location.href = this.action;}
	//Funciones
	if(this.action && (typeof(this.action)=="function")){this.action();}
	
	//Si ncesitas algo mas por qui lo mplias
	
	//Muestro hijos
	this.showChildren();
}


MenuColorao.prototype.showChildren=function(){
	if(this.items.length==0) return;
	
	//Genero codigo
	this.strChildren = "<ul>";
	for(var i=0;i<this.items.length;i++){
	   	//this.strChildren += ('<li><a href="#" class="unselected"><span>'+this.items[i].label+'</span></a></li>');
	   	
	   	var oChild = this.items[i];
	   	
	   	this.strChildren += ('<li>');
	   	this.strChildren += ('<a href="#" class="unselected"><span>'+oChild.label+'</span></a>');
		if(oChild.description) this.strChildren += ('<p>'+oChild.description+'</p>');
	   	this.strChildren += ('</li>');
	}
	
	this.strChildren += "</ul>";
	this.childrenDiv.innerHTML = this.strChildren;
	
	//Programo hijos
	var arrLinks = this.childrenDiv.getElementsByTagName("a");
	for(var i=0;i<arrLinks.length;i++){
		var newLink=arrLinks[i];
		newLink.classRef=this.items[i];
		this.items[i].linkRef=newLink;
		newLink.onmousedown=function(){
			this.classRef.doClick();
			return false;
		}
	}
	
	if(this.getTopParent().autoClick == true) 
		this.getDefaultItem().doClick();
	else{
		this.getDefaultItem().selectItem();
		this.getDefaultItem().showChildren();
	}
	
	
}

//Retorna el elemento mas alto en la jerarquia
MenuColorao.prototype.getTopParent=function(){
	if(this.parentObject=="undefined" || this.parentObject==null) return this;
	return this.parentObject.getTopParent();
	
}

//Recorro mis hijos buscando el selected y lo disparo
MenuColorao.prototype.initMenu=function(){
	this.doClick();
}

//Recorro mis hijos buscando el selected y lo disparo
MenuColorao.prototype.getDefaultItem=function(){

	var defaultItem = this.items[0];
	for(var i=0;i<this.items.length;i++) {
		if(this.items[i].selected){
			defaultItem = this.items[i];
		}
	}
	return defaultItem;
}	


//Recorro mis hijos buscando el selected y lo disparo
MenuColorao.prototype.selectItem=function(){
	if(!this.parentObject) return false;
	
	for(var i=0;i<this.parentObject.items.length;i++){
		var it = this.parentObject.items[i];
		if(it==this){
			it.selected = true;
			it.linkRef.className = "selected";
		}else{
			it.selected = false;
			it.linkRef.className = "unselected";
		}
	}
}