// JavaScript Document

/*
	Versión 1.0
*/

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	document.writeln('<style type="text/css">img.png, input.image { visibility:hidden; } </style>');
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
		
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			fnFixPng(img);
			img.attachEvent("onpropertychange", fnPropertyChanged);
		}
		img.style.visibility = "visible";
	}

	var nl = document.getElementsByTagName("INPUT");
	for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
		if (e.className && e.className.match(/\bimage\b/i) != null) {
			if (e.src.match(/\.png$/i) != null) {
				fnFixPng(e);
				e.attachEvent("onpropertychange", fnPropertyChanged);
			}
			e.style.visibility = "visible";
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "src") {
		var el = window.event.srcElement;
		if (!el.src.match(/x\.gif$/i)) {
			el.filters.item(0).src = el.src;
			el.src = "x.gif";
		}
	}
}

function dbg(o) {
	var s = "";
	var i = 0;
	for (var p in o) {
		s += p + ": " + o[p] + "\n";
		if (++i % 10 == 0) {
			alert(s);
			s = "";
		}
	}
	alert(s);
}

function fnFixPng(img) {
	var src = img.src;
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
	img.src = "x.gif";
}


function pasar_pagina(paginacion, siguiente){
	document.getElementById("paginacion").value= paginacion;
	document.getElementById("siguiente").value= siguiente;	
	document.getElementById("form1").submit();
	return false;
}
	
function irPagina(){
	if (document.getElementById("pag")){
		if(document.form1.pag.value!=""){
			document.form1.pag.value = document.form1.pag.value - 1;
		}
	}
	if (document.getElementById("pagUp")){
		if(document.form1.pagUp.value!=""){
			document.form1.pagUp.value = document.form1.pagUp.value - 1;
			document.form1.pag.value= document.form1.pagUp.value;
		} 
	}
	document.form1.submit();

}
//Para vaciar el texto del buscador
$(document).ready( function(){
	var clearMePrevious = "";
	$("#buscar").focus(function(){
		if($("#buscar").val()==$("#buscar").attr("title")){
			clearMePrevious = $("#buscar").val();
			$("#buscar").val("");
		}
	});
	// Si vuele a estar vacío añadimos el texto de nuevo
	$("#buscar").blur(function(){
		if($("#buscar").val()==""){
			$("#buscar").val(clearMePrevious);
		}
	});
});

/*
	Versión 1.0
*/
/* ELEMENTOS COMUNES */

$(document).ready( function(){
	// Enlace en ventana nueva.
	$("a[rel='external']").attr("target","_blank");

	// Quitar el &nbsp; de inputs tipo texto, password y textareas.
	$("input[type='text'], input[type='password'], textarea").focus( function() {
		if (($(this).attr("value")) && ($(this).attr("value").charCodeAt(0) == 32 || $(this).attr("value").charCodeAt(0) == 160) && ($(this).attr("value").length == 1)) {
			this.value = "";
			return false;
		}
	});
});

// Resaltar input(text-password)/textarea seleccionado
$(document).ready(function(){
	$("input[type='text'], input[type='password'], textarea, select").focus( function() {
		if ($(this).attr("readonly")) {
			// Nada
		} else {
			$(this).addClass("enfocado");
		}
		return false;
	});
	$("input[type='text'], input[type='password'], textarea, select").blur( function() {
		$(this).removeClass("enfocado");
	});
	$("input[type='text'], input[type='password'], textarea, select").each( function() {
		if ($(this).attr("readonly")) {
			$(this).addClass("solo_lectura");
		}
	});
});


// Imprimir página
$(document).ready(function(){
	$("a.imprimir").click( function() {
		window.print();
		return false;
	});
});


//BOTONES REDONDOS

		var hasEstiloInput = function(str){
		
			var tmpArr = str.split(" ");
			var tmpArr2;
		
			for(var i = 0; i < tmpArr.length; i++){
				tmpArr2 = tmpArr[i].split("_");
				
				for(var j = 0; j < tmpArr2.length; j++){
					if(tmpArr2[j] == "estiloInput"){
						return tmpArr[i];
					}
				}
				
			}
			
			return null;
		}
		
		$(document).ready(function(){
			// por cada elemento <a> que encuentre con un class 'href_boton'...
			var enlacesEsquinasRedondeadas = $("a.href_boton").map(function(index){
				// ... hacer que su contenido sea envuelto por cuatro <span>...
				$(this).html("<span><span><span><span>" + $(this).text() + "<\/span\><\/span\><\/span\><\/span\>");
				// ... añadir un <div> con un class 'clearall' para limpiar
				if($(this).hasClass("addLimpiar") == false){
					$(this).after("\<div class='clearall'\>\<\/div\>");
				}
			});
			// por cada elemento <input> que encuentre con un class 'input_boton'...
			var inputEsquinasRedondeadas = $("input.input_boton").map(function(index){
				// ... envolverlo dentro de cuatro <span>, el primero de los cuales tiene un class 'b02'...
				$(this).wrap("<span class='input_boton'><span><span><span><\/span><\/span><\/span><\/span>");
				// ... añadir estilos personales al span que contiene todo el conjunto...
				var tmpEstiloInput = hasEstiloInput($(this).attr("class"));
				if(tmpEstiloInput != null){
					$(this).parents("span.input_boton").addClass(tmpEstiloInput);
				}
				// ... añadir un <div> con un class 'clearall' para limpiar
				if($(this).hasClass("addLimpiar") == false){	
					$(this).parents("span.input_boton").after("<div class='clearall'><\/div\>");
				}
			});
		});



//Menú horizontal multicapa
$(document).ready(function(){
	$("#menu_ppal li.materias ul").css("display","block");
	

	// Creamos dinamicamente "enlace_anterior" y "enlace_posterior"
	$("#menu_ppal > ul").before('<a href="#" id="enlace_anterior">x</a>');
	$("#menu_ppal > ul").after('<a href="#" id="enlace_posterior">x</a>');
	// Comportamiento para el ratón
	$("#menu_ppal > ul > li.parent").mouseover( function() {
		$(this).find("ul").css("left","0px");
	});
	$("#menu_ppal > ul > li.parent").mouseout( function() {
		$(this).find("ul").css("left","-10000px");
	});

	// Eliminamos todos los parent de segundo nivel
	// Producian que no funcionase el click
	$("#menu_ppal > ul li.parent li.parent").each( function() {
		$(this).removeClass("parent");
	});

	// Comportamiento para el teclado
	$("#menu_ppal > ul li a").focus( function() {
		$("#menu_ppal > ul li.parent ul").css("left","-10000px");
	});
	$("#menu_ppal > ul li.parent a").focus( function() {
		$(this).parents("li.parent:first").find("ul").css("left","0px");
	});
	$("#menu_ppal > ul li.parent li a").focus( function() {
		$(this).parents("li.parent:first").find("ul").css("left","0px");
	});
	$("#enlace_anterior, #enlace_posterior").focus( function() {
		$("#menu_ppal > ul li.parent ul").css("left","-10000px");
	});
	$("#menu_ppal > ul > li").hover(
		function(){
			$(this).children("a").addClass("representarHover");
		},
		function(){
			$(this).children("a").removeClass("representarHover");
		}
	);	
});
									



function ayudas(){ 
        $('.bubbleInfo').each(function () {
            var distance = 0;
			var mostrar_ayuda = $('.mostrar_ayuda', this);
            var info = $('.popup', this);
            info.css('opacity', 0)
			mostrar_ayuda.mouseover(function () {
				info.css({
                        top: 18,
                        left: -8,
                        display: 'block'
                    },0,'swing')
				.animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
				 });
				return false;
			
            })
           
        }); 
        $('body').mouseover(function () {
        	$('.popup').css({
                        top: '-= 0px',
                        opacity: 0					
                    },0,'swing');
                return false;
        });
				
 }  
 
 
function comprar(){ 
	$(".comprar_libro").click(function(){
		$.get($(this).attr("href") + "?" + new Date().getTime(),
		function(data) {
			$.blockUI({
				fadeIn: 700, 
				fadeOut: 700, 
				showOverlay: true, 
				centerY: false, 
				message: data,
				css: { 
				top:  ($(window).height() - 500) /2 + 'px', 
				left: ($(window).width() - 620) /2 + 'px', 
				width: '620px', 
				border: 'none', 
				padding: '10px', 
				backgroundColor: '#ffffff', 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: 1		
			} }); 
		});
		return false;
	});
}

function banner(){
	$.get("nuevo.php",
			function(data) {
				$.blockUI({
					fadeIn: 700, 
					fadeOut: 700, 
					showOverlay: true, 
					centerY: false, 
					message: data,
					css: { 
					top:  ($(window).height() - 500) /2 + 'px', 
					left: ($(window).width() - 400) /2 + 'px', 
					width: '400px', 
					border: 'none', 
					padding: '10px', 
					backgroundColor: '#ffffff', 
					'-webkit-border-radius': '5px', 
					'-moz-border-radius': '5px', 
					opacity: 1		
				} }); 
  			});
			/* setTimeout(function() { 
	            $.unblockUI({ 
	                onUnblock: function(){ } 
	            }); 
	        }, 160000); */
	
  			return false;
  		}






function mostrar_ocultar_pasos_resumen() { 
	$("a.mostrar_categorias").click(function () {
		$(this).blur();
		if ($("a.mostrar_categorias").hasClass("coleccion_abierto")) {
			$("a.mostrar_categorias").removeClass("coleccion_abierto");
			$("a.mostrar_categorias").addClass("coleccion_cerrado");
			$("#ocultar").removeClass("novisible");
			$("#mostrar").addClass("novisible");
			$(this).next().fadeOut("fast");
		} else {
			$("a.mostrar_categorias").removeClass("coleccion_cerrado");
			$("a.mostrar_categorias").addClass("coleccion_abierto");
			$("#mostrar").removeClass("novisible");
			$("#ocultar").addClass("novisible");
			$(this).next().fadeIn("fast");
		}
		return false;
	});
}



	
	



function mostrar_ocultar_bloque(){ 
	$("h4.mostrar_info_bloque").click(
		function () { 
			if ($(this).hasClass("abierto")) {
				$(this).next().hide();
				$(this).removeClass("abierto");
				
			}else {
				$(this).next().show();
				$(this).addClass("abierto");
				
			}
			//$(this).next().slideToggle("fast");
		}
	);
}








/* ELEMENTOS QUE SE CARGAN AL INICIO DE TODAS LAS PÁGINAS */
$(document).ready(function() {
	ayudas();
	comprar();
	mostrar_ocultar_pasos_resumen();
	mostrar_ocultar_bloque();
});


/* DOCUMENTOS PRENSA */
	$(document).ready(function(){
		$(".recortes_prensa").click(function(){
				$.blockUI({ 
					message: $('.ventana_prensa'), 
					css: { padding: '0 0 0 0', margin: '0 0 0 -250px', width: '500px', height: '450px', top: '15%', left: '50%', textAlign: 'left', color: '#000', border: 'none', backgroundColor:'#FFF', cursor: 'default' }, 
					overlayCSS: { backgroundColor:'#000', opacity: '0.6', cursor: 'default'}, 
					baseZ: 10000, 
					centerX: true, 
					centerY: true, 
					allowBodyStretch: true, 
					constrainTabKey: true, 
					fadeOut: 400, 
					applyPlatformOpacityRules: true
				 });
  			
  			return false;
  		});		
		
	});
