/**
 * MOD Fran: funcionamiento de algunas funciones basado en mootools (parcialmente)
 */

function setActiveStyleSheet(title) {
  var i, a, main;
  var listo = 0;
  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){
                listo = 1;
                a.disabled = false;
		//setActiveLogoImage(img);
          }
      }
  }
  if(listo == 0) {
     document.getElementsByTagName("link")[2].disabled = false;
     //setActiveLogoImage('http://www2.unavarra.es/estructura/imgs/logo.upna.800.gif');
  }

setActiveLogoImage();
createCookie("style", title, 365);
}


function setActiveLogoImage() {
	if($('estructura')) {
		var tam = $('estructura').clientWidth;

		var imagen = null;

		if(tam < 800)
			imagen = 'http://www2.unavarra.es/estructura/imgs/logo.upna.640.gif';
		else if(tam >= 800 && tam < 1024)
			imagen = 'http://www2.unavarra.es/estructura/imgs/logo.upna.800.gif';
		else if(tam >= 1024 && tam < 1280)
			imagen = 'http://www2.unavarra.es/estructura/imgs/logo.upna.1024.gif';
		else if(tam >= 1280)
			imagen = 'http://www2.unavarra.es/estructura/imgs/logo.upna.1280.gif';
	
		//$('accesorioResolucion').src = imagen;
		/*if (img != null) {
			var imagen = document.getElementById("accesorioResolucion");
		  	imagen.src = img;
		}*/
	}
}

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;
}

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

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;
}

/*window.onload = function(e) {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	console.log('aa');
	setActiveStyleSheet(title);
}
*/

window.addEvent('domready', function () {
	setActiveLogoImage();
});
//window.addEvent('load', function () {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	window.addEvent('keypress', checkKeyboardChange);
	setActiveStyleSheet(title);
	createCookie("style", title, 365);
//});

/*
window.addEvent('unload', function () {
    var title = getActiveStyleSheet();
    createCookie("style", title, 365);
});
*/

function checkKeyboardChange(e) {
	var e = new Event(e);
	
	if(e.control && (e.key == '+' || e.key == '-')) {
		setActiveLogoImage.delay(200);
		//console.log(' si');
	}
}

function changeStyle(styleSwap,language) {
	
	estiloActual = getActiveStyleSheet();
       

	if (!estiloActual) {
		estiloActual = "1.- Tamano de letra estandar";
	}

	numEstiloActual = parseInt(estiloActual.substring(0,1));
	numEstilo = numEstiloActual + parseInt(styleSwap);
	
	if (numEstilo >-1 && numEstilo < 4) {
		if (numEstilo == 0) { 
			setActiveStyleSheet("0.- Tamano de letra pequeno");
		}
		else if (numEstilo == 1) {		
			setActiveStyleSheet("1.- Tamano de letra estandar");
		}
		else if (numEstilo == 2) {
			setActiveStyleSheet("2.- Tamano de letra mediano");
		}
		else if (numEstilo == 3) {
			setActiveStyleSheet("3.- Tamano de letra grande");
		}
  }
}
