function displayMenuPrincipal(){
	
	/*   INSTRUCCIONES PARA REPRESENTAR LOS ESTADOS OVER/OUT   */
	
	var primerNivel = $("#MenuPrincipal_MenuArbol_UL > li");

	var representarHover = function(item, index){
		item.children("a").css({"background" : "#27a1c9"});
	}
	
	var representarOut = function(item, index){
		item.children("a").css({"background" : "#8B206B"});
	}
	
	primerNivel.map(function(index){
		$(this).hover(
			function(){
				representarHover($(this), index);
			},
			function(){
				representarOut($(this), index);
			}
		);
		
	});
	
	/*   INSTRUCCIONES PARA DESPLEGAR EL 2º NIVEL   */
	
	// Comportamiento para el ratón
	$("#MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Root").mouseover( function() {	
		if($(this).hasClass("lastChild") == false){
			$(this).find("ul:first").css("left","0px");
		}else{
			var lista = $(this).find("ul:first");
			lista.css("left",-(lista.outerWidth() - lista.parent("li").outerWidth()) + "px");
		}
	});
	$("#MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Parent").mouseover( function() {
		if($(this).parents("#MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Root.lastChild").length == 0){
			$(this).find("ul:first").css("left","0px");
		}else{
			var lista = $(this).find("ul:first");
			lista.css("left",-(lista.outerWidth() + $(this).outerWidth()) + "px");
		}
	});
	$("#MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Root, #MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Parent").mouseout( function() {
		$(this).find("ul").css("left","-10000px");
	});
	// Comportamiento para el teclado
	$("#menuPrincipal ul.menu li a").focus( function() {
		$("#MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Root ul").css("left","-10000px");
	});
	$("#MenuPrincipal_MenuArbol_UL li.AspNet-TreeView-Root a").focus( function() {

		if($(this).parents("li:first").hasClass("lastChild") == false){
			$(this).parents("li:first").find("ul").css("left","0px");
		}else{				
			var lista = $(this).parents("li:first").find("ul");
			lista.css("left",-(lista.outerWidth() - lista.parent("li").outerWidth()) + "px");			
		}

	});
	$("#menuPrincipal ul.menu li.parent li a").focus( function() {
		if($(this).parents("li.parent:first").hasClass("ultimo") == false){
			$(this).parents("li.parent:first").find("ul").css("left","0px");
		}else{
			var lista = $(this).parents("li.parent:first").find("ul");
			lista.css("left",-(lista.width() - lista.parent("li").width() + 19) + "px");
		}
	});
	$("#enlace_anterior_mp, #enlace_posterior_mp").focus( function() {
		$("#menuPrincipal ul.menu li.parent ul").css("left","-10000px");
	});
}

/// LIMPIA LOS ESPACIOS EN BLANCO DE LOS CAMPOS DE TEXTO ///
/// Quitar el &nbsp; de inputs tipo texto, password y textareas.

function reset_form() {	
	$("input[type='text'], input[type='password'], textarea").focus( function() {
		if ($(this).val() == " ") {
			$(this).val("");
		}
	});
}
function AlinearVertical(containerId, contentId) {
	
        var containerHeight = $("#"+containerId).height();
        var contentHeight = $("#"+contentId).height();
        if(contentHeight < containerHeight) {
            var newTop= (containerHeight - contentHeight)/2;
            $("#"+contentId).css("marginTop", newTop+"px");
        };
}
function fototecaAlinearVertical() {
    AlinearVertical('imgScroll','imgPreview');
}
function target_blank() {
	// Enlace en ventana nueva.
	var elementos = $("a[rel='external']");
	$.each(elementos, function() {
		$(this).attr("target","_blank");
		if (!$(this).attr("title")){
			$(this).attr("title", mensaje);
		} else {
			mensaje2=$(this).attr("title") & " (" & mensaje & ")";
			$(this).attr("title", mensaje2);	
		}
		
	});	
}
function target_blank_visor() {
	//Añadir un nuevo parámetro al href que señaliza que hay JavaScript activado
	var enlace = "";
	var elementos = $("a[href*='DocumentosVisor']");
	$.each(elementos, function() {
		if ($(this).attr("href").indexOf('?')>=0){
			enlace = $(this).attr("href") + "&js=1";
		} else{
			enlace = $(this).attr("href") + "?js=1";	
		}
		$(this).attr("href",enlace);
    });
}

function isset(variable_name) {
	try {
			if (typeof(eval(variable_name)) != 'undefined')
			if (eval(variable_name) != null)
			return true;
		}
	catch(e) { }
	return false;
}

function roll_resumenes_item() {
	var elementos = $(".contenido .vistaCompleta .resumenesItem div h3 a, .contenido .vistaReducida .resumenesItem div h3 a");
	$.each(elementos, function() {	
		$(this).parents("div:first").wrapInner("<a></a>");
		$(this).parents("a:first").attr("href",$(this).attr("href"));
	});
}
function analytics() {
	$("a[rel*='external']").click(function(){
		var url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
		pageTracker._trackPageview('/external/'+ url);
	});
	$("a[href*='mailto']").click(function(){
		var url = $(this).attr("href").replace(/^mailto\:*/i, "");										  
		pageTracker._trackPageview('/mailto/'+ url);
	});
	$("a[href*='DocumentosVisor.aspx']").click(function(){
		var url = $(this).attr("href").split("DocumentosVisor.aspx?")[1].split("&")[0];		
		pageTracker._trackPageview('/downloads/'+ url);
	});	
}

function menuGipuzkoaBerritzen() {
	var gBerri=$("#MenuContextual_MenuArbol_UL li.AspNet-TreeView-Selected>a[href='home.aspx?tabid=205'],#MenuContextual_MenuArbol_UL li.AspNet-TreeView-Selected>a[href=home.aspx?tabid=428],#MenuContextual_MenuArbol_UL li.AspNet-TreeView-Selected>a[href=home.aspx?tabid=639]").parent().html();
	if(gBerri){
	$("#MenuContextual_MenuArbol_UL").html("<li class='AspNet-TreeView-Root firstChild AspNet-TreeView-Selected'>"+gBerri+"</li>");
	}
	var gBerri2=$("#MenuContextual_MenuArbol_UL li.AspNet-TreeView-ChildSelected>a[href='home.aspx?tabid=205'],#MenuContextual_MenuArbol_UL li.AspNet-TreeView-ChildSelected>a[href=home.aspx?tabid=428],#MenuContextual_MenuArbol_UL li.AspNet-TreeView-ChildSelected>a[href=home.aspx?tabid=639]").parent().html();
	if(gBerri2){
	$("#MenuContextual_MenuArbol_UL").html("<li class='AspNet-TreeView-Root firstChild AspNet-TreeView-ChildSelected'>"+gBerri2+"</li>");
	}
}


function ocultarMartesInnobasqueEnMenuHorizontal() {//Para ocultar la opción Agenda>Martes en Innobasque solo en el menú horizontal
	$("div#MenuPrincipal_MenuArbol ul li a[href=home.aspx?tabid=1312]").parent().parent().remove();
	$("div#MenuPrincipal_MenuArbol ul li a[href=home.aspx?tabid=791]").parent().parent().remove();
}
/// LLAMADA A TODAS LAS FUNCIONES  ///

$(document).ready(function(){
	menuGipuzkoaBerritzen();
	displayMenuPrincipal();
	reset_form();
	target_blank();
	target_blank_visor();
	fototecaAlinearVertical();
	analytics();
	ocultarMartesInnobasqueEnMenuHorizontal();
});
