function inicio(iIdioma){
	
	var protocol;	
	protocol = window.parent.document.location.protocol;	
	if (protocol == 'http:') {		
		extrasJquery();	
	}
	
	externalLinks();
	imgAdjuntas(iIdioma);
	CargarUtilidades();
	imgGaleria();
}

function CargarUtilidades(){
	if(document.getElementById("avolver")){
		oServicioVolver = document.getElementById("avolver");
	 	oServicioVolver.style.display = "inline";
	 	oServicioVolver.onclick = volver;
	}
}

function VerImagen(iIdImagen){
	var windowImagen;
	windowImagen = window.open("/popup/popupimagen.asp?idimagen="+iIdImagen,"Imagen","width=100,height=100,top=20,left=20,scrollbars=no,resizable=no");
}

function externalLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.title = aArrayAlertas[iIdioma][3];
    		anchor.target = "_blank";
    	}
 	}
}

function imgAdjuntas(iIdioma){
	 var imgs = document.getElementsByTagName("img");
	 for (var i=0; i<imgs.length; i++) {
		var img = imgs[i];
		var sClass = img.className
		if (sClass.indexOf("cursorAdjunto") != -1){
			img.alt = img.alt + " " + aArrayAlertas[iIdioma][3];
	   }
	}
}

function vaciar(esto){esto.value=""}

function addLoadEvent(func) {
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
        window.onload = func;

    } else {
        window.onload = function() {

            if (oldonload) {
                oldonload();
            }
           	func();
        }
    }
}

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function extrasJquery() {
	$(document).ready(function() {
		// plantillas con acordeon vertical
		if ($(".acordeon")) {
	   		$(".acordeon .datos").hide();
	   		$(".acordeon .titulo").css("cursor", "pointer");
			$(".acordeon .titulo").click(function(){
				if($(this).parent().hasClass("bloqueactivo")) {
					$(this).parent().removeClass("bloqueactivo").find('.datos:first').slideToggle();
				}
				else {
					$(".acordeon .bloque").removeClass("bloqueactivo").find('.datos').hide();
					$(this).parent().addClass("bloqueactivo").find('.datos:first').slideToggle();
				}
				return false;
			});
		}
		
		// control del alto
		$("object, img").ready(function(){
			equalHeight($(".accesos .item .previo"));
			equalHeight($(".contenidotd69, .contenidotd30"));
		});				
		
		$("#n1 li.nivel1").each(function(){
			var id = $(this).attr("id");			
			$.ajax({
			   type: "POST",
			   url: window.parent.document.location.protocol+"//www.ferrovial.com/inc/programas/peticionesAjax/menuDesplegable.asp",
			   data: "identificador=" + id,
			   success: function(html){
			     $(html).insertAfter($('#' + id + ' a'));
			   }
			});
		});
		
		// muestra capa con menu horizontal
		$("#n1 li.nivel1").hover(function(){
			
			$(this).find(".capamenuover").hide();
			
			if(($(this).attr("id") == "n1_1") || ($(this).attr("id") == "n1_2")){
				$(this).find(".capamenuover").show();
			}
			
			if(($(this).attr("id") == "n1_3") || ($(this).attr("id") == "n1_4") || ($(this).attr("id") == "n1_5") || ($(this).attr("id") == "n1_6")){
				if(($(this).attr("id") == "n1_3")){
					 if ( $.browser.msie ) {					 
						 $(this).find(".capamenuover").css({"background-position": "-1732px 0"});
					 } else {
						 $(this).find(".capamenuover").css({"background-position": "-1730px 0"}); 
					 }
				}
				if(($(this).attr("id") == "n1_4")){
					 if ( $.browser.msie ) {					 
						 $(this).find(".capamenuover").css({"background-position": "-1592px 0"});
					 } else {
						 $(this).find(".capamenuover").css({"background-position": "-1590px 0"}); 
					 }
				}
				if(($(this).attr("id") == "n1_5")){
					 if ( $.browser.msie ) {					 
						 $(this).find(".capamenuover").css({"background-position": "-1451px 0"});
					 } else {
						 $(this).find(".capamenuover").css({"background-position": "-1449px 0"}); 
					 }
				}
				if(($(this).attr("id") == "n1_6")){
					 if ( $.browser.msie ) {					 
						 $(this).find(".capamenuover").css({"background-position": "-1311px 0"});
					 } else {
						 $(this).find(".capamenuover").css({"background-position": "-1308px 0"}); 
					 }
				}
				$(this).find(".capamenuover").css({"left": "80px"}).show();
			}
			
			
			if(($(this).attr("id") == "n1_7")){
				if ( $.browser.msie ) {
					$(this).find(".capamenuover").css({"right": "0", "background-position": "-1240px 0"}).show();
				 } else {
					$(this).find(".capamenuover").css({"right": "0", "background-position": "-1240px 0"}).show();
				 }
			}
			
			return false;
			
		}, function(){			
			$(this).find(".capamenuover").hide();
		});
		
		
	});
}

// Recorrer las imagenes de una galeria y reemplazar las funciones verimagen
function imgGaleria() {
	$('div.modulogal .imagenes').addClass('imagenesJS');
	$("div.modulogal .imagenesJS ul li img").hover(function(){								
		if (!$(this).hasClass("activo")) {		
			$(this).stop().animate({opacity: 1}, 200);
		}  			
		}, function(){			
			if (!$(this).hasClass("activo")) {		
				$(this).stop().animate({opacity: 0.5}, 200);
			}  
	});		
	$('div.modulogal .imagenesJS').before("<div class='ampliacion'><div></div></div>");
	$("div.modulogal .imagenesJS ul li img").each(function(){
		if (($(this).attr("onclick") != null) && ($(this).attr("onclick") != undefined)) {
			if (this.onclick.toString().indexOf("VerImagen") != -1) {
				// Quitar el texto de nueva ventana del alt
				$(this).attr("alt","");
				$(this).css({opacity: 0.5});
				$(this).removeAttr("onclick");
				$(this).removeAttr("onkeypress");
				$(this).click(function(){
					$("div.modulogal .imagenesJS ul li img.activo").stop().animate({opacity: 0.5}, 200);						
					$("div.modulogal .imagenesJS ul li img").removeClass("activo");
					verImagenGaleria($(this));					
					return false;
				});	
			}	
		}
			
	});
	var primeraImagen = $("div.modulogal .imagenes ul li img").get(0);
	if ((primeraImagen != null) && (primeraImagen != undefined)) {			
		verImagenGaleria($("#" + primeraImagen.id));
	}
}

// Accion de ver imagen ampliada en la galeria
function verImagenGaleria(img) {
	if (typeof(img) != 'undefined') {
		ampliarAdjuntaGaleria(img);
	}
}

function ampliarAdjuntaGaleria(img) {
	var idImagen = img[0].id.split("_")[1];
	img.stop().animate({opacity: 1});
	img.addClass('activo');
	$.post("/inc/aplicacionesPerso/galerias/cargarImagen.asp",
		{ idimagen: idImagen },
			function(xml){
				$('.ampliacion').hide();	
				$('.ampliacion div').html(xml);
				var descripcion = unescape($('.ampliacion div p').text());
				$('#imagenAdjunta').attr("alt", descripcion);
				$('.ampliacion div p').text(descripcion);
				$('.ampliacion').fadeIn();
				
				// volver a ajustar el alto de la zona derecha
				$("object, img").ready(function(){
					equalHeight($(".contenidotd69, .contenidotd30"));
					equalHeight($(".accesos .item"));
				});
			}
	);
}
