/*
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 Menu de anclas
 version:  d14-m06-a06.
 -- -- -- -- -- -- -- -- --
 Mauricio F. Tolezano - Acuataller (www.acuataller.com)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
//
// Requiere:	Behaviour
//
*/

com.acuataller.MenuAnclas = function( idma, nc, idsel, ep ) 
{
	this.idMenuAnclas		= idma;
	this.nombreClase		= nc;
	this.estiloAPadres		= (ep)? ep : 1;	
	this.seleccionActivaId	= false;
	this.elementoActivo	= false;
	
	this._asignarEventos();
	this.seleccionInicio(idsel);
}


com.acuataller.MenuAnclas.prototype.seleccionInicio = function(idsel) 
{	
	var selIncio = this._getSeleccionPorURL();
	if( selIncio && this.seleccionarPorId(selIncio) ){
		// Selección por URL (en el if)
	}
	else if( idsel && this.seleccionarPorId(idsel) ){
		// Selección enviada al constructor.
		selIncio = idsel;
	}else{
		// Seleccionando el primer elemento del menu.
		selIncio = this.seleccionarPrimerElemento();
	}
	this.seleccionActivaId = selIncio;
}


com.acuataller.MenuAnclas.prototype._getSeleccionPorURL = function() 
{
	var aURL =  this._getAnclaURL( window.location.toString() );
	if( aURL && document.getElementById(aURL) )
	{
		var as = document.getElementById(this.idMenuAnclas).getElementsByTagName('a');
		for(var i=0; i < as.length; i++) {
			if( this._getAnclaURL(as[i].href)==aURL ) return aURL;			
		}
	};
	return false;
}


com.acuataller.MenuAnclas.prototype._getElementoPorAncla = function( aURL ) 
{
	var as = document.getElementById(this.idMenuAnclas).getElementsByTagName('a');
	for(var i=0; i < as.length; i++) {
		if( this._getAnclaURL(as[i].href)==aURL ) return as[i];			
	}
	return false;	
}


com.acuataller.MenuAnclas.prototype._getAnclaURL = function(url) 
{
	if( url.indexOf("#") != -1 ){		
		return url.substring( url.indexOf("#") + 1, url.length);				
	}
	return false;
}


com.acuataller.MenuAnclas.prototype.seleccionarPrimerElemento = function() 
{
	var primerElementoMenu = document.getElementById(this.idMenuAnclas).getElementsByTagName('a')[0];
	var ancla = this._getAnclaURL( primerElementoMenu.href );

	if( this.seleccionarPorId(ancla) ) return ancla;
	
	return false;
}


com.acuataller.MenuAnclas.prototype.seleccionarPorId = function(idE, elemento) 
{	
	var destino = document.getElementById(idE);	

	elemento = (elemento)? elemento : this._getElementoPorAncla(idE);
	if(elemento)
	{
		if(this.seleccionActivaId) document.getElementById( this.seleccionActivaId ).className = '';
		if(this.elementoActivo) this._getElementoPadre( this.elementoActivo ).className = '';
		
		destino.className 	= this.nombreClase;
		this._getElementoPadre( elemento ).className = this.nombreClase;
		
		this.seleccionActivaId 	= idE;
		this.elementoActivo  	= elemento;
		return true;
	}
	return false;
}


com.acuataller.MenuAnclas.prototype._getElementoPadre = function(elemento) 
{
	var padre;
	for(var i=0; i < this.estiloAPadres; i++) {
		padre = elemento.parentNode;
	}
	return padre;
}


com.acuataller.MenuAnclas.prototype.seleccionar = function( elemento ) 
{	
	var ancla = this._getAnclaURL(elemento.href);	
	this.seleccionarPorId(ancla, elemento);
}


com.acuataller.MenuAnclas.prototype._asignarEventos = function() 
{
	var objRef = this;
	eval(
		"var myrules = {										"+	
		
		"	'#"+ this.idMenuAnclas +" a' : function(element){	"+
		"		element.onmousedown  = function(){				"+
		"			objRef.seleccionar(this); 					"+
		"			return false;								"+
		"		}												"+
		"	},													"+
		
		"	'#"+ this.idMenuAnclas +" a' : function(element){	"+
		"		element.onfocus  = function(){					"+
		"			objRef.seleccionar(this); 					"+
		"			return false;								"+
		"		}												"+
		"	}													"+		
		
		"};"					
	);
		
	Behaviour.register(myrules);
	Behaviour.apply(); 
}

