/*
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   Ajuste al area de la ventana del navegador.
   Activa una hoja estilo alternativa segun el area de la ventana.
   Namespace es.aquataller.ui
   version:  d014-m04-a07
//  -- -- -- -- -- -- -- -- --
//  Mauricio F. Tolezano (www.acuataller.com)
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
// 
//
*/



es.aquataller.ui.AjustarArea = {
	
	anchos 		: [],
	altos 		: [],
	claseAncho 	: 'ancho-',
	claseAlto 	: 'alto-',
	anchoActivo : false,
	altoActivo  : false,
	
	
	
	iniciar : function() 
	{	 		
		var ordenAsc = new Function('x','y','return ( x < y ) ? -1 : ( x > y ) ? 1 : 0;');
		
		// Anchos predefinidos.
		for(i=0; l=this.getLinks()[i]; i++)
		{
			var ancho = this.getAnchoDeEstilo(l);
			if(ancho) {
				this.anchos.push(ancho);
			}			
		}		
		this.anchos.sort(ordenAsc);
		
		// Alto predefinidos.
		for(i=0; l=this.getLinks()[i]; i++)
		{
			var alto = this.getAltoDeEstilo(l);
			if(alto) {
				this.altos.push(alto);
			}			
		}		
		this.altos.sort(ordenAsc);
		
		// Iniciar ajuste de area.
		this.actualizarArea();
	},
	
	
	
	// Acualiza el area segun el espacio disponible en la ventana.
	actualizarArea : function() 
	{	 		
		var area = this.getAreaVentana();
		if(!area.w || !area.h) return false;

		// Ancho
		// Selecciona el mayor ancho que sea menor al area de la ventana 
		// o el menor ancho disponible aunque sea mayor que la ventana.
		var nuevoAncho=false, i=0;
		do{	nuevoAncho = this.anchos[i];
			i++;
		} while( this.anchos[i] && this.anchos[i] < area.w )		
		
		// Alto
		// Selecciona de igal manera para al alto.		
		var nuevoAlto=false, i=0;
		do{	nuevoAlto = this.altos[i];
			i++;
		} while( this.altos[i] && this.altos[i] < area.h )
		
	
		if(	nuevoAncho==this.anchoActivo
		   	&&
		   	nuevoAlto ==this.altoActivo ) return false;
		
		//if(	nuevoAncho==this.anchoActivo ) return false;	

		
		for(i=0; l=this.getLinks()[i]; i++)
		{
			if(l.getAttribute("rel").indexOf("style") != -1)
			{			
				// Comprobando que el estilo sea para ajustar el ancho y 
				// retornando el valor del ancho al que corresponde.
				var anchoEstilo = this.getAnchoDeEstilo(l);
				// Lo mismo para el alto.				
				var altoEstilo  = this.getAltoDeEstilo (l);				
				
				if(anchoEstilo) 
				{					
					// Desactivando todos lo sestilos de ancho.
					l.disabled = true;
					// Activando el estilo correspondiente al nuevo ancho.
					if( anchoEstilo==nuevoAncho ) {
						l.disabled = false;
						this.anchoActivo = nuevoAncho;						
					}					
				}				
				if(altoEstilo) 
				{
					// Desactivando todos lo sestilos de alto.
					l.disabled = true;
					// Activando el estilo correspondiente al nuevo alto.
					if( altoEstilo==nuevoAlto ) {
						l.disabled = false;
						this.altoActivo = nuevoAlto;						
					}					
				}		
			}
		}		
	},
	
	
	
	// Retorna el ancho asociado a un estilo.
	getAnchoDeEstilo : function(link) 
	{	
		if(link.className && link.className.indexOf(this.claseAncho)!=-1) 
		{
			return parseInt(link.className.substring(this.claseAncho.length, link.className.length));
		}	
		return false;
	},
	
	
	
	// Retorna el alto asociado a un estilo.
	getAltoDeEstilo : function(link) 
	{	
		if(link.className && link.className.indexOf(this.claseAlto)!=-1) 
		{
			return parseInt(link.className.substring(this.claseAlto.length, link.className.length));
		}	
		return false;
	},
	
	
	
	getLinks : function() 
	{	
		return document.getElementsByTagName('link');
	},
	
	
	
	// Retorna el area de la ventana del navegador.
	getAreaVentana : function() 
	{
		var area={w:false,h:false};
		
		if (typeof window.innerWidth!='undefined') 
		{
			area.w = window.innerWidth;
			area.h = window.innerHeight;
		}
		else if (typeof document.documentElement != 'undefined'
				 && typeof document.documentElement.clientWidth != 'undefined'
				 && document.documentElement.clientWidth != 0 )
		{
			area.w = document.documentElement.clientWidth;
			area.h = document.documentElement.clientHeight;
		}
		else if ( document.body==null ) 
		{
			ELO.functionsToCallOnload.push('es.aquataller.ui.AjustarArea.iniciar()');
		}
		else {
			var body = document.getElementsByTagName('body')[0];
			area.w = body.clientWidth;
			area.h = body.clientHeight;
		}
			
		return area;
	}

}



function addResizeEvent(func) {
	var oldOnResize = window.onresize;		
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			oldOnResize();
			func();
		}
	}
}	
function acuataller_actualizarArea() {
	es.aquataller.ui.AjustarArea.actualizarArea();
}
addResizeEvent( acuataller_actualizarArea );


es.aquataller.ui.AjustarArea.iniciar();

