/*
 *	----------------------------------------------------------------------------
 *	TextEngine.js
 *	Inclusión no intrusiva de iconos de accesibilidad.
 *	Aumento y disminución de texto, alto contraste, impresión.
 *
 *	Autor:		Pablo Suárez León <psuarez@technosite.es>
 *	Fecha:		25/06/2008
 *	Versión:	1.1
 *  Comentario: Modificada para que la Cookie se destruya al cerrar el navegador
 *	----------------------------------------------------------------------------
 */
 
var TextEngine = {
	
	increase     : null,
	decrease     : null,
	normal       : null,
	text         : null,
	bodyObj      : null,
	
	init: function(parentId, beforeId){

//		document.getElementsByTagName('body')[0].style.overflow = '-moz-scrollbars-vertical';

		/* Contenedor de los iconos */
		teDiv    = document.getElementById('TextEngineDiv');
		/*teDiv    = document.createElement('span');
		teDiv.id = 'TextEngineDiv';*/
		
		/* Elemento padre */
		parentObj = document.getElementById(parentId);
		
		/* Elemento siguiente a la inserción */
		if(beforeId){
			beforeObj = document.getElementById(beforeId);
		}
		
		/* Hoja de estilos */
		style  = document.styleSheets[0];

		/* Elemento body */
		this.bodyObj = document.getElementsByTagName('BODY')[0];
		
		/* Texto */
		this.text         = document.createElement('SPAN');
		/* Creaciación de los iconos */
		this.increase     = document.createElement('IMG');
		this.normal       = document.createElement('IMG');
		this.decrease     = document.createElement('IMG');
		
		/* Texto */
		this.text.appendChild(document.createTextNode('Cambiar tama' + "\u00F1" + 'o del texto'));
		/* Imagenes de los iconos */
		this.increase.src     = 'img/bigText.jpg';
		this.increase.alt     = "Aumentar el tama" + "\u00F1" + "o del texto";
		this.decrease.src     = 'img/smallText.jpg';
		this.decrease.alt     = "Reducir el tama" + "\u00F1" + "o del texto";
		this.normal.src       = 'img/resetText.jpg';
		this.normal.alt       = "Tama"+ "\u00F1" +"o original";
		/* Asignación de eventos */
		this.decrease.onclick    = this.decreaseText;
		this.normal.onclick      = this.resetText;
		this.increase.onclick    = this.increaseText;
		
		
		/* Añadir iconos al contenedor */
		teDiv.appendChild(this.text);
		teDiv.appendChild(this.increase);
		teDiv.appendChild(this.normal);
		teDiv.appendChild(this.decrease);

		
		/* Inserción del contenedor */
		if(beforeId){
			parentObj.insertBefore(teDiv, beforeObj);
		}
		else{
			parentObj.appendChild(teDiv);
		}
		
		/* Creación del estilo */
		if(style.insertRule){
			style.insertRule('#TextEngineDiv{float:left;margin:0px;}', style.cssRules.length);
			style.insertRule('#TextEngineDiv img{margin-left:1px;cursor:pointer;}', style.cssRules.length);
		}
		else if(style.addRule){
			style.addRule('#TextEngineDiv', 'float:left;margin:0px;');
			style.addRule('#TextEngineDiv img', 'margin-left:1px;cursor:pointer;');
		}
		
		/* Comprobar si existe la cookie y redimensionar */
		if(act = CookieEngine.GetCookie('textEngine')){
			TextEngine.bodyObj.style.fontSize = act;
		}
	},

	decreaseText: function(){
		var size = TextEngine.getTextSize() / 1.05 + 'em';
		TextEngine.bodyObj.style.fontSize = size;
		/* Guardar cookie */
		CookieEngine.SaveCookie('textEngine', size);
	},

	increaseText: function(){
		var size = TextEngine.getTextSize() * 1.05 + 'em';
		TextEngine.bodyObj.style.fontSize = size;
		/* Guardar cookie */
		CookieEngine.SaveCookie('textEngine', size);
	},

	resetText: function(){
		TextEngine.bodyObj.style.fontSize = '.65em';
		/* Eliminar cookie */
		CookieEngine.SaveCookie('textEngine', '.65em');
	},




getTextSize: function(){
		if(this.bodyObj.style.fontSize){
			return parseFloat(this.bodyObj.style.fontSize);
		}
		else{
			return 1;
		}
	}
}


CookieEngine = {
	
	GetCookieValue: function(index){  
		var cookie = document.cookie;
		var endStr = cookie.indexOf(";", index);
		if(endStr == -1){ 
			endStr = cookie.length;
		}
		
		return unescape(cookie.substring(index, endStr));
	},
   
	GetCookie: function(name) {  
		var cookie = document.cookie;
		var arg = name + "=";
		var alen = arg.length;
		var glen = cookie.length;
		
		var i = 0;
		while(i < glen){  
			var j = i + alen;
			if(cookie.substring(i, j) == arg){
				return this.GetCookieValue(j);
			}
			i = cookie.indexOf(" ", i) + 1;
			if(!i){  
				break;
			}
		}  
		return null;
	},

	SaveCookie: function(name, value){  
		document.cookie = name + "=" + escape(value) + "; path=/";
	}
}

function autoComplete(){
		if(document.getElementById('USER')){
			document.getElementById("USER").setAttribute("autocomplete", "off");
		}
}

window.onload = function(){
		TextEngine.init('sizetext');
		autoComplete();
}
