//Accesibilidad Cambio de tamaño de letras y Cambio de tamaño de pantalla

////////////////////////
//Crear y leer cookies//
////////////////////////

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  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;
}


//////////////////////////////
//Cambio de tamaño de letras//
//////////////////////////////

var zoom=0;

function zoomText(Accion,Elemento){
	//inicializacion de variables y parámetros 
	var obj=document.getElementById(Elemento);
	var max = 135 //tamaño máximo del fontSize
	var min = 120 //tamaño mínimo del fontSize
	
	if (obj.style.fontSize==""){
		 obj.style.fontSize="100%";
		}
	
	actual=parseInt(obj.style.fontSize); //valor actual del tamaño del texto 
	incremento=20;// el valor del incremento o decremento en el tamaño 

	//accion sobre el texto 
	if( Accion=="reestablecer" ){
		 obj.style.fontSize="100%"
		 zoom=0;
		}

	if( Accion=="aumentar" && ((actual+incremento) <= max )){
		 valor=actual+incremento;
		 obj.style.fontSize=valor+"%"
		 zoom=zoom+1;
		}

	if( Accion=="disminuir" && ((actual+incremento) >= min )){
		 valor=actual-incremento;
		 obj.style.fontSize=valor+"%"
		 zoom=zoom-1;
		}
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

var col='nor';

function setActiveStyleSheetcolor(title) {
  var i, a, main;
  col=title;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

//////////////////////////////////////
//Accesibilidad->Cambio de pantallas//
//////////////////////////////////////

function changeToolHilite(oldtool, newtool) {//cambio de nombre de *.gif a *-hilite.gif de newtool y viceversa con  oldtool
	if (oldtool != newtool) {
		if (oldtool) {
			oldtool.src = oldtool.src.replace(/-hilite/,'');
		}
		newtool.src = newtool.src.replace(/.gif$/,'-hilite.gif');
	}
}

function getRuleByName(ruleName){
	for (i=0; i<document.styleSheets.length; i++){
		var style = document.styleSheets[i];
		var rules = style.cssRules?style.cssRules:style.rules;
		if (rules){
			for (j = 0; j<rules.length; j++){
				if (rules[j].selectorText.trim().toUpperCase() == ruleName.trim().toUpperCase()){
					return rules[j];
				}
			}
		}
	}
	return null;
}

var tipopant='wide'

function setScreenType(screentype){
	tipopant=screentype;
	//alert(tipopant);
	bclass = document.body.className.trim();
	if (bclass.indexOf(' ') > 0){
		bclass = bclass.replace(/^\w+/,screentype);
	}else{
		bclass = screentype + ' ' + bclass;
	}

	document.body.className = bclass;
	equalHeightInit();
	createCookie("ScreenType", screentype, 365);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

//addEvent - attach a function to an event
function jaAddEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

function equalHeight (elems){
	if (!elems) return;
	var maxh = 0;
	for (var i=0; i<elems.length; i++)
	{
		if (elems[i] && elems[i].scrollHeight > maxh) maxh = elems[i].scrollHeight;
	}

	for (i=0; i<elems.length; i++){
		if (elems[i]) elems[i].parentNode.style.height = maxh + "px";
	}
}

function getElem (id) {
	var obj = document.getElementById (id);
	if (!obj) return null;
	var divs = obj.getElementsByTagName ('div');
	if (divs && divs.length >= 1) return divs[divs.length - 1];
	return null;
}

function getFirstDiv (id) {
	var obj = document.getElementById (id);
	if (!obj) return null;
	var divs = obj.getElementsByTagName ('div');
	if (divs && divs.length >= 1) return divs[0];
	return obj;
}

function getDivElemsByClass (parent, className) {
	var objs = parent.getElementsByTagName ('div');
	var elems = new Array();
	var j = 0;
	for (var i=0; i<objs.length; i++)
	{
		if (instr(objs[i].className, className) )
		{
			elems[j++] = objs[i];
		}
	}
	return elems;
}

function instr(str, item){
	var arr = str.split(" ");
	for (var i = 0; i < arr.length; i++){
		if (arr[i] == item) return true;
	}
	return false;
}

function equalHeightInit (){
	var objs = new Array();
	objs[0] = getFirstDiv ("ja-topsl1");
	objs[1] = getFirstDiv ("ja-topsl2");
	objs[2] = getFirstDiv ("ja-topsl3");
	equalHeight (objs);
	
	var janewsblocks = getDivElemsByClass (document, "ja-newsblock");
	for (var i=0; i<janewsblocks.length; i++)
	{
		var janewsitems = getDivElemsByClass (janewsblocks[i], "ja-newsitem-inner");
		if (janewsitems.length > 1)
			equalHeight (janewsitems);
	}
}

/*jaAddEvent (window, 'load', equalHeightInit);*/

jaToolsHover = function() {
	var jautw = document.getElementById("ja-usertoolswrap");	
	if (!jautw) return;

	jautw.onmouseover=function() {
		this.className="ja-toolswraphover";
	}
	jautw.onmouseout=function() {
		this.className="";
	}
}

/*jaAddEvent (window, 'load', jaToolsHover);*/

/////////////////////////////////////////
//Acciones al cargar y descargar la web//
/////////////////////////////////////////

window.onload = function(e) {
  //alert("onload");
  
  //pone la pantalla como lo diga la cookie, sino usara la wide, dado que la variable esta con ese valor
  var cookie2 = readCookie("screentype");
  //alert(cookie2);
  var title2 = cookie2 ? cookie2 : tipopant;
  //alert('title2'+title2)
  setScreenType('narrow');
  setScreenType('wide');
  setScreenType('auto');
  setScreenType(title2);
  switch(title2)
	{
	 case 'narrow':changeToolHilite(olt,curtool1);olt=curtool1;break;
	 case 'wide':changeToolHilite(olt,curtool2);olt=curtool2;break;
	 case 'auto':changeToolHilite(olt,curtool3);olt=curtool3;break;
	}
  
  //pone la pantalla del contraste que arque la cookie; sino hay, la pondra normal
  var cookie3 = readCookie("color");
  var title3 = cookie3 ? cookie3 : col;
  setActiveStyleSheetcolor(title3);

  var cookie = readCookie("letra");
  var title = cookie ? cookie : zoom;
  //alert(title);
  if(title==1) zoomText('aumentar','ja-wrapper');
  else if(title==-1)zoomText('disminuir','ja-wrapper');
  

  //Lee y pone el tamaño de letra anteriormente usada, sino usara la A
  /*var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);*/

}

window.onunload = function(e) {
  //alert("onunload");
  //var title = getActiveStyleSheet();
  var title2 = tipopant;
  var title3 = col;
  //var tipopant='narrow'; 
  createCookie("letra", zoom, 1);
  createCookie("screentype", title2, 1);
  createCookie("color", title3, 1);
}