function target_blank() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	var i;
	var enlace;

	for (i = 0; i < anchors.length; i++) {
		var enlace = anchors[i];
		if (enlace.getAttribute("href") && enlace.getAttribute("rel") == "external") {
			enlace.target = "_blank";
		}
	}

}

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

function setActiveStyleSheetVisual(titulo) {
	if (titulo == "tam_1") {
		if (document.getElementById("letra_menos")) {
			document.getElementById("letra_menos").src = document.getElementById("letra_menos").src.replace(/1/gi, "2");
		}
	}
	else {
		if (document.getElementById("letra_menos")) {
			document.getElementById("letra_menos").src = document.getElementById("letra_menos").src.replace(/2/gi, "1");
		}
	}
	if (titulo == "tam_5") {
		if (document.getElementById("letra_mas")) {
			document.getElementById("letra_mas").src = document.getElementById("letra_mas").src.replace(/1/gi, "2");
		}
	}
	else {
		if (document.getElementById("letra_mas")) {
			document.getElementById("letra_mas").src = document.getElementById("letra_mas").src.replace(/2/gi, "1");
		}
	}
}

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) {
			if (a.getAttribute("title").indexOf("tam_") != -1) {
				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")) {
			if (a.getAttribute("title").indexOf("tam_") != -1) {
				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;
}

function tamanoLinks() {
	var a, b;
	a = document.getElementById("tamMenos");
	b = document.getElementById("tamMas");
	if (a) {
		a.onclick = function() {
			var titulo = getActiveStyleSheet();
			if (titulo) {
				var tamanio = titulo.charAt(4);
				var tamanio_menos = parseInt(tamanio) - 1;			
				if (tamanio_menos == 0) tamanio_menos = 1;
			}
			else {
				var tamanio_menos = 2;
			}
			setActiveStyleSheet("tam_" + tamanio_menos);
			return false;
		}
	}
	if (b) {
		b.onclick = function() {
			var titulo = getActiveStyleSheet();
			if (titulo) {
				var tamanio = titulo.charAt(4);
				var tamanio_mas = parseInt(tamanio) + 1;
				if (tamanio_mas == 6) tamanio_mas = 5;
			}
			else {
				var tamanio_mas = 2;
			}
			setActiveStyleSheet("tam_" + tamanio_mas);
			return false;
		}
	}
}

//CAMBIO DE ANCHO SEGÚN RESOLUCIÓN
/*	-------------------------------------------------------------
	function getBrowserWidth()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	As odd as it may seem, this function returns
					the browser's width in pixels.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on Particle Tree
					http://www.particletree.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	
		<!--
		function getBrowserWidth(){
			if (window.innerWidth){
				return window.innerWidth;}	
			else if (document.documentElement && document.documentElement.clientWidth != 0){
				return document.documentElement.clientWidth;	}
			else if (document.body){return document.body.clientWidth;}		
				return 0;
		}
		//-->

/*	-------------------------------------------------------------
	function determineStyle()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	With this here function we can swap styles.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	
		<!--
		function determineStyle(){

			var browserWidth = getBrowserWidth();
				
			var i, a, main;
			
			if (browserWidth <= 939){
				for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
					if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
						if (a.getAttribute("title").indexOf("res_") != -1) {
							a.disabled = true;
							if(a.getAttribute("title") == "res_narrow") a.disabled = false;
							//if (document.getElementById("cab_mensaje")) {
							//	document.getElementById("cab_mensaje").src = document.getElementById("cab_mensaje").src.replace(/1024/gi, "800");
							//}
						}
					}
				}
			}
			
			if (browserWidth > 939){
				for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
					if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
						if (a.getAttribute("title").indexOf("res_") != -1) {
							a.disabled = true;
							if(a.getAttribute("title") == "res_wide") a.disabled = false;
							//if (document.getElementById("cab_mensaje")) {
							//	document.getElementById("cab_mensaje").src = document.getElementById("cab_mensaje").src.replace(/800/gi, "1024");
							//}
						}
					}
				}
			}
		}
	


// AQUI EMPIEZA LO DEL MENU
function menu() {
	var ul_menu = document.getElementById("menuNav");
	if (ul_menu) {
		var ul_menu_hijos = ul_menu.childNodes;
		for (var i=0;i<ul_menu_hijos.length;i++) {
			if (ul_menu_hijos[i].id) {
				if (ul_menu_hijos[i].id.indexOf("menu_") >= 0) {
					var link_menu = ul_menu_hijos[i].getElementsByTagName("A");
					var ul_submenu = ul_menu_hijos[i].getElementsByTagName("UL");
					if (link_menu[0]) {
						/*
						if (link_menu[0].href.indexOf("#") != -1) {
							link_menu[0].onclick = function() {
								link_menu[0].blur();
								return procesar_menu(this, "mostrar");
							}
						}
						*/
						var ocultar_submenu = true;
						if (link_menu[0].className == "seleccionado") {
							ocultar_submenu = false;
						}
						if (ocultar_submenu) {
							for (var m=0;m<ul_submenu.length;m++) {
								ul_submenu[m].style.display = "none";
							}
						}
					}
				}
			}
		}
	}
}

function procesar_menu(elemento, accion) {
	var li_menu = elemento.parentNode;
	var ul_submenus = li_menu.getElementsByTagName("UL");
	if (elemento.className == "seleccionado") {
		if (ul_submenus.length > 0) {
			return false;
		}
	}
	for (var i=0;i<ul_submenus.length;i++) {
		if (accion == "mostrar") {
			ul_submenus[i].style.display = "block";
			elemento.onclick = function() {
				elemento.blur();
				return procesar_menu(this, "ocultar");
			}
		}
		else if (accion == "ocultar") {
			ul_submenus[i].style.display = "none";
			elemento.onclick = function() {
				elemento.blur();
				return procesar_menu(this, "mostrar");
			}
		}
	}
	if (ul_submenus.length > 0) {
		return false;
	}
	else {
		return true;
	}
}

function attachEventListener(target, eventType, functionRef, capture) {
    if (typeof target.addEventListener != "undefined") {
        target.addEventListener(eventType, functionRef, capture);
    } else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    } else {
        return false;
    }

    return true;
};
// HAY QUE METER EL MENU EN EL ONLOAD DE ABAJO
// AQUI ACABA LO DEL MENU

function iniciar_tamano(){
	var cookie = readCookie("dipuinnovastyle");
	var titulo = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(titulo);
}

var TourVirtualJavascript = 0;
function arrancarTourVirtual() {
	if (TourVirtualJavascript == 1) {
		arrancar();
	}
}

attachEventListener(window, "load", iniciar_tamano, false);
attachEventListener(window, "load", tamanoLinks, false);
attachEventListener(window, "load", target_blank, false);
attachEventListener(window, "load", menu, false);
//attachEventListener(window, "load", determineStyle, false);
//attachEventListener(window, "resize", determineStyle, false);
attachEventListener(window, "load", arrancarTourVirtual, false);