// IE6 se encarga de cambiar esto si no es el caso
var ie6 = false;

//
// Evento onload
// 
$(document).ready(function() {

	resize_handler();

	$(".js_hide").css({"display" : "none"});
	$(".js_show").css({"visibility" : "visible"});
	$("#deco").css("display", "none");

	// Gestión de pestañas
	//
	// Ocultar divs de contenido no seleccionado
	$("#niv2cen .modulo01 ul ~ .med").css({"display" : "none"});
	$("#niv2cen .modulo01 ul ~ .med:first").css({"display" : "block"});
	// Evento onclick
	$("#niv2cen .tabs li a").click(function(e) {
		e.preventDefault();
		jQuery(this).parent().siblings().removeClass("activo");
		jQuery(this).parent().addClass("activo");
		var destino = jQuery(this).attr("href");
		jQuery(this).parent().parent().siblings().filter(".med").hide();
		jQuery(destino).show();
	});

	if($("#login").length) {
		// Posicionar capa de login
		var top = $("#ref_login").position().top + $("#menu").position().top + 27;
		var left = $("#ref_login").position().left + 8;
		$("#login").css("top", top+"px");
		$("#login").css("left", left+"px");

		// Mostrar login
		//$("#show_login").mouseenter(function(e) {
		//	e.preventDefault();
		//	$("#login").show();
		//});
		
		//Ir a login privada
		$("#show_login").mouseup(function(e) {
			window.location.href="https://clientes.tressis.com";
		});

		// Ocultar login
		$("#login").mouseleave(function(e) {
			$("#login").hide();
		});
	}

	if(!ie6) {
		// El código de rollover no funciona bien con IE6, prescindimos de él
		$("a.boton_rojo, a.boton_gris").mouseenter(function(e) {
			$(this).addClass("rollover");
			$(this).prev().addClass("rollover");
			$(this).next().addClass("rollover");
		}).mouseleave(function(e) {
			$(this).removeClass("rollover");
			$(this).prev().removeClass("rollover");
			$(this).next().removeClass("rollover");
		}).focus(function(e) {
			$(this).addClass("rollover");
			$(this).prev().addClass("rollover");
			$(this).next().addClass("rollover");
		}).blur(function(e) {;
			$(this).removeClass("rollover");
			$(this).prev().removeClass("rollover");
			$(this).next().removeClass("rollover");
		});
	}

	// Rollovers de la home
	$("#niv1izq .bloque_default").mouseenter(function(e) {
		$(this).css("display", "none");
		$(this).next().css("display", "block");
	});
	$("#niv1izq .bloque_rollover").mouseleave(function(e) {
		$(this).css("display", "none");
		$(this).prev().css("display", "block");
	});

	// Pestañas de home
	$("#niv1izq .opaco>div").css({"display" : "none"});
	$("#niv1izq .opaco>div:first").css({"display" : "block"});
	$("#niv1izq .pest a").click(function(e) {
		e.preventDefault();
		$(this).parent().parent().children().removeClass("activa");
		$(this).parent().parent().children().removeClass("top");
		$(this).parent().parent().children().removeClass("bot");
		$(this).parent().addClass("activa");
		var cual = $(this).parent().attr("id");
		if(cual=="litop")
			$("#libot").addClass("top");
		if(cual=="libot")
			$("#litop").addClass("bot");
		var destino = $(this).attr("href");
		$(this).parent().parent().next().children().hide();
		$(destino).show();
	});

	// Pestañas de privada
	$(".ptabs a").click(function(e) {
		e.preventDefault();
		$(this).parent().parent().children().removeClass("activo");
		$(this).parent().addClass("activo");
		var destino = $(this).attr("href");
		$(this).parent().parent().parent().next().next().children().hide();
		$(destino).show();
	});

	// Cabeceras necesitan asignar estilo por JS en IE6
	if(ie6) {
		$("#header.niv3").css("height", "220px");
		$("#header.niv2").css("height", "325px");
	}

	// Obtenemos la altura del documento y se lo asignamos a la capa de decoración
	$("#deco").css({"height" : $(document).height()});
	$("#deco").css("display", "block");

	// Esto no hace nada, pero si no IE7+ no pinta bien los espacios alrededor de algunos botones
	$("a.boton_rojo, a.boton_gris").removeClass("rollover");
	$(window.parent).children().filter('iframe').attr('frameborder', '0');
	
	// Tablas privada
	$(".mod675 tr.subtotal, .mod675 tr.total").prev().children().css("border-bottom", "1px solid #eceeef");

	// Capa acciones
	$(".acciones").hide();
	$(".show_context").mouseenter(function(e) {
		$(this).attr("src", "img/pvd_tabla_acciones_rollover.gif");
		$(this).next().css("top", $(this).position().top+10+"px");
		$(this).next().css("left", $(this).position().left-155+"px");
		$(".acciones").hide();
		$(this).next().show();
	}).mouseleave(function(e) {
		$(this).attr("src", "img/pvd_tabla_acciones.gif");
	});
	$(".mod675 table").mouseleave(function(e) {
		$(".acciones").hide();
	})
	$(".acciones").mouseleave(function(e) {
		$(this).hide();
	});
	
	// Rollover icono calendario
	$(".ico_calendar").mouseenter(function(e) {
		$(this).attr("src", "img/ico_calendar_rollover.gif");
	}).mouseleave(function(e) {
		$(this).attr("src", "img/ico_calendar.gif");
	});

	// Hack z-index
	$(".bringtofront").css("position", "relative");
	$(".bringtofront").css("z-index", "50");

});

//
// Control del evento resize para ajustar bien el fondo
//

function resize_handler() {
	var scrWidth = $(window).width();
	var offset = "-260px";

	
	//document.getElementById("contenedor_menu")
	if($("#contenedor_menu.area_privada").length)
		$("#contenedor_menu").css('width',(scrWidth > 1003 ? scrWidth/2 + 179 : 683) + "px");
	else
		$("#contenedor_menu").css('width',(scrWidth > 1003 ? scrWidth/2 + 9 : 513) + "px");

	// Modificamos dimensiones del div deco para que se comporte bien para tamaños de ventana inferiores 1003px
	var anchoDeco = Math.max(1003, $(window).width());
	var offsetDeco = (anchoDeco - 1520) / 2;
	$("#deco").css("width", anchoDeco);
	$(document).css("width", anchoDeco);
}

window.onresize = resize_handler;

