// Enlace en ventana nueva.
$(document).ready(function(){
	var i;
	var enlaces = $("a");
	var cuantos = enlaces.length;
	var j = 0;
	for (i = 0; i < cuantos; i++) {
		if (enlaces[i].getAttribute("href") && enlaces[i].getAttribute("rel") == "external") {
			enlaces[i].target="_blank";
		}
	}
});

// Limpiar inputs.
$(document).ready(function(){
	var i, cuantos;
	var los_inputs = $("input");
	var los_textareas = $("textarea");
	cuantos = los_inputs.length;
	for (i = 0; i < cuantos; i++) {
		if (los_inputs[i].getAttribute("type").toLowerCase() == "text" || los_inputs[i].getAttribute("type").toLowerCase() == "password") {
			los_inputs[i].onfocus = function() {
				if ((this.value.charCodeAt(0) == 32 || this.value.charCodeAt(0) == 160) && this.value.length == 1 || (this.value == this.defaultValue)) this.value = "";
			};
			los_inputs[i].onblur = function() {
				if (this.value == "") this.value = this.defaultValue;
			};
		}
	}
	cuantos = los_textareas.length;
	for (i = 0; i < cuantos; i++) {
		los_textareas[i].onfocus = function() {
			if (this.value.charCodeAt(0) == 32 || this.value.charCodeAt(0) == 160 && this.value.length == 1 || (this.value == this.defaultValue)) this.value = "";
		};
		los_textareas[i].onblur = function() {
			if (this.value == "") this.value = this.defaultValue;
		};
	}
});

// Menú horizontal.
$(document).ready(function(){
	$("li.opcionMenu").mouseover(function(){
		$(this).find("a:eq(0)").click(function(){
   			return false;
		});
		$(this).find("ul.opcionSubMenu").show();
	});
	$("li.opcionMenu").mouseout(function(){
		$(this).find("ul.opcionSubMenu").hide();
	});
	$("li.opcionMenu").focus(function(){
		$(this).find("a:eq(0)").click(function(){
   			return false;
		});
		$(this).find("ul.opcionSubMenu").show();
	});
	$("li.opcionMenu").blur(function(){
		$(this).find("ul.opcionSubMenu").hide();
	});
});


// Botones.
$(document).ready(function(){
	// Botón volver 1
	$("a#boton_volver_1").click(function(){
		history.back();
		return false;
	});
	// Botón volver 2
	$("a#boton_volver_2").click(function(){
		history.back();
		return false;
	});
	// Botón imprimir 1
	$("a#boton_imprimir_1").click(function(){
		window.print();
		return false;
	});
	// Botón imprimir 2
	$("a#boton_imprimir_2").click(function(){
		window.print();
		return false;
	});
});

// Contador de caracteres restantes.
$(document).ready(function(){
    var countdown = {
        init: function() {
            countdown.remaining = countdown.max - $(countdown.obj).val().length;
            if (countdown.remaining > countdown.max) {
                $(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
            }
			if (countdown.remaining > 0) {
				$(countdown.obj).siblings(".remaining").html("Restan " + countdown.remaining + " caracteres.");
			} else {
				$(countdown.obj).siblings(".remaining").html("<span class=\"jumi\">Restan " + countdown.remaining + " caracteres.</span>");
			}
        },
        max: null,
        remaining: null,
        obj: null
    };
    $(".countdown").each(function() {
        $(this).focus(function() {
            var c = $(this).attr("class");
            countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
            countdown.obj = this;
            iCount = setInterval(countdown.init,1000);
        }).blur(function() {
            countdown.init();
            clearInterval(iCount);
        });
    });
});


//$(document).ready(function() {
//	$("input").keyup(function(e){
//		//alert(e.keyCode);
//		if (e.keyCode == 13) {
//			alert('Enter key was pressed.');
//		}
//	});
//});