/*
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   Intercambio de estilo alernativo.
   Namespace es.aquataller.utiles
   version:  d01-m02-a07
//  -- -- -- -- -- -- -- -- --
//  Mauricio F. Tolezano (www.acuataller.com)
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
// 
//
*/



es.aquataller.utiles.IntercambioEstilo = {
	
	// opciones por defecto.
	preferencias : {
						// atributo clase para identificar los estilos alternativos que se utilizaran.
						claseEstilos 	: null,
						// hoja de estilo alternativa por defecto.
						estiloInicial 	: null,
						// nombre de la cookie que guarda la seleccion de estilos alternativos.
						cookie 			: null
					},
					
	// atributo "title" del estilo activo.
	estiloActivo : null,		
					
	
	iniciar : function(opciones) 
	{	 				
		this.setOpciones(opciones);	
		this.activarEstiloPorDefecto();	
	},	
	
	
	getEstiloActivo : function() 
	{	 				
		return this.estiloActivo;	
	},
	
	
	activarEstiloPorDefecto : function(title) 
	{	 				
		var title = this.getCookie() || this.preferencias.estiloInicial || this.getPrimerEstilo();
		if(!title) return false;		
		this.activarEstilo(title);
	},	
	
	
	// retorna el primer estilo alternativo.
	getPrimerEstilo : function() 
	{	 				
		var i, l, estilos = this.getEstilos();
		
		if(this.preferencias.claseEstilos) {
			for(i=0;l=estilos[i];i++) {
				if(	l.rel.indexOf('style')!=-1 && l.title && l.className==this.preferencias.claseEstilos ) {
					return l.title;
				}
			}
		}else{
			for(i=0;l=estilos[i];i++) {
				if(	 l.rel.indexOf('style')!=-1 && l.title ) {
					return l.title;
				}
			}			
		}
		return null;
	},	
	
	
	activarEstilo : function(title) 
	{	 				
		var i, l;
		for(i=0;l=this.getEstilos()[i];i++) 
		{
			if(	 l.rel.indexOf('style')!=-1 && l.title && 
				 (l.className==this.preferencias.claseEstilos || !this.preferencias.claseEstilos)) 
			{
				l.disabled=true;
				if(l.title==title){
					l.disabled=false;
					this.estiloActivo = title;
					this.setCookie(title);
				}
			}
		}	
	},		
	
	
	getEstilos : function() 
	{	 				
		return document.getElementsByTagName('link');	
	},	
	
	
	// Aplicar opciones por defecto.
	setOpciones : function(opciones) 
	{
		opciones = opciones || {};
		var prop = {};
		for (prop in this.preferencias) this.preferencias[prop] = opciones[prop] || this.preferencias[prop];
	},
	
	
	setCookie : function (value) 
	{
		var days = 365, date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();	
		document.cookie = this.preferencias.cookie +'='+value+expires+'; path=/';
	},


	getCookie : function () 
	{
		var nameEQ = this.preferencias.cookie + '=';
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
}

