/////////////////////////////////
// Cookies:
/////////////////////////////////
function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


/////////////////////////////////
// Contraste:
/////////////////////////////////
function cambiarContraste(){

	if(!document.styleSheets) return false;

	var estilo = getCookie('estilo');

	var expira = new Date ();
	expira.setTime(expira.getTime() + (24 * 60 * 60 * 10000)); // 1 mes

	if(estilo == null){
		estilo = 1;

		setCookie('estilo','1',expira,'/');
	}else{
		estilo = (parseInt(estilo) + 1) % 3;
		delCookie('estilo');
		setCookie('estilo',estilo,expira,'/');
	}
	
	if(estilo == 0){
		var links = document.getElementsByTagName('link');
		for(var i=0; i<links.length; i++){
			if(links[i].type == 'text/css'){
				links[i].href = '';
				links[i].href = 'http://www.hvn.es/estilo/basicoHVN.css';
			}
		}
	}else{
		var color = '#000000';
		var backColor = '#FFFFFF';
		var borderColor = '#666666';
		if(estilo == 2){
			color = '#FFFFFF';
			backColor = '#000000';
			borderColor = '#666666';
		}

		var css = document.styleSheets; 
	
		// Recorremos cada hoja de estilos
		for(var i=0; i<css.length; i++){
			var reglas;
			if(document.styleSheets[i].cssRules)   //W3C
				reglas = document.styleSheets[i].cssRules;
			else if(document.styleSheets[i].rules) // Microsoft
				reglas = document.styleSheets[i].rules;
			
			// Recorremos cada regla
			for(var j=0; j<reglas.length; j++){ 
				reglas[j].style.backgroundColor = backColor;
				reglas[j].style.color = color;
				reglas[j].style.borderColor = borderColor;
			} 
		}
	}
}

function iniciarContraste(){
	var estilo = getCookie('estilo');
	if(estilo == null || parseInt(estilo) == 0) return false;
	
	var color = '#000000';
	var backColor = '#FFFFFF';
	var borderColor = '#666666';
	if(estilo == 2){
		color = '#FFFFFF';
		backColor = '#000000';
		borderColor = '#666666';
	}

	if(!document.styleSheets) return false;
	var css = document.styleSheets; 

	// Recorremos cada hoja de estilos
	for(var i=0; i<css.length; i++){
		var reglas;
		if(document.styleSheets[i].cssRules)   //W3C
			reglas = document.styleSheets[i].cssRules;
		else if(document.styleSheets[i].rules) // Microsoft
			reglas = document.styleSheets[i].rules;
		
		// Recorremos cada regla
		for(var j=0; j<reglas.length; j++){ 
			reglas[j].style.backgroundColor = backColor;
			reglas[j].style.color = color;
			reglas[j].style.borderColor = borderColor;
		} 
	}
}


/////////////////////////////////
// Fuentes:
/////////////////////////////////
var tamInicial = 1;

function cambiarFuente(nuevoTam) {
	var expira = new Date ();
	expira.setTime(expira.getTime() + (24 * 60 * 60 * 10000)); // 1 mes

	setCookie('tamFuente', nuevoTam, expira, '/');

	document.body.style.fontSize=nuevoTam+'em';
}

function aumentarFuente(){
	// Tomamos el valor actual de la cookie
	var tamAct = getCookie('tamFuente');
	
	if(tamAct == null)
		tamAct = tamInicial;

	// Tamaño máximo: 2
	var tamN = eval(tamAct) + 0.1;
	if(tamN >= 1.5)
		tamN = 1.5;

	cambiarFuente(tamN);
}

function disminuirFuente() {

	// Tomamos el valor actual de la cookie
	var tamAct = getCookie('tamFuente');

	if (tamAct == null)
		tamAct = tamInicial;

	// Tamaño mínimo: 0.5	
	var tamN = eval(tamAct) - 0.1;
	if(tamN <= 0.5)
		tamN = 0.5;
	
	cambiarFuente(tamN);
}

function reiniciarFuente(){
	cambiarFuente(tamInicial);
}

function iniciarFuente(){
	// Tomamos el valor actual de la cookie
	var tamAct = getCookie('tamFuente');

	if(tamAct == null)
		tamAct = tamInicial;

	cambiarFuente(tamAct);
}
