/**
 * Esta funci�n deshabilita todas las pulsaciones del teclado
 * para el elemento proporcionado excepto la tecla tab
 * @param event Evento
 * @param element Elemento sobre el cual se deshabilitan las pulsaciones del teclado
 * @returns {Boolean} 
 */
function keyDownReadOnly(event, element) {
	if (element.readOnly) {	
		if (event.keyCode != 9) {
			return false;
		}
	}
	return true;
}
/**
 * El seiguiente metodo permite cargar el los datos para el banner de la cabezera
*/
function bannerLB(){
	if(banderaBannerLb == true){
		 $(".iframeHB").attr('src', '/InovoPublicidad/bannerGeneral1000x100.html');		 
	}
}



function replicarCampos() {
	$(".pp_content_container input")
			.each(
					function(index) {
						if ($(this).attr('type') != 'submit') {
							if ($(this).attr("id") != null
									&& $(this).attr("id") != 'javax.faces.ViewState') {
								var idInput = $(this).attr("id").replace(/:/g,
										"\\:");
								var contenidoInput;
								if ($(this).attr('type') == 'checkbox') {
									contenidoInput = $(this).attr("checked");
									contenidoInput = contenidoInput == null ? false
											: contenidoInput;
									$("input[id*=" + idInput + "]").attr(
											"checked", contenidoInput);
								} else {
									contenidoInput = $(this).val();
									$("input[id*=" + idInput + "]").val(
											contenidoInput);
								}
							}
						}

					});
	$(".pp_content_container select").each(function(index) {
		var xid = $(this).attr("id");
		xid = xid.replace(/:/g, "\\:");
		var campoUsuarioAuth = $(this).val();
		$("select[id*=" + xid + "]").val(campoUsuarioAuth);
	});
	$(".pp_content_container textarea").each(function(index) {
		if ($(this).attr('type') != 'submit') {
			var xid = $(this).attr("id");
			xid = xid.replace(/:/g, "\\:");
			campoUsuarioAuth = $(this).val();
			$("textarea[id*=" + xid + "]").val(campoUsuarioAuth);
		}
	});
	return true;
}

function uuid() {
	var uuid = "", i, random;
	for (i = 0; i < 32; i++) {
		random = Math.random() * 16 | 0;
		if (i == 8 || i == 12 || i == 16 || i == 20) {
			uuid += "-";
		}
		uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random))
				.toString(16);
	}
	return uuid;
}

$(document)
		.ready(
				function() {			
					
					var ubicacion = location.pathname.split('/')[1];
					var is404 = document.getElementById("pagina404");
					var bool = is404 != null;
					
					if (ubicacion == null || ubicacion == "") {
						ubicacion = "inicio";
					}
					if (bool) {
						ubicacion = "404";
					}
					$("#ulMenuHorizontalInicial > li")
							.each(
									function(index) {
										var li = $(this);
										var link = li.children("a");
										if (link != null) {
											var href = link.attr("href");
											var id = li.attr("id");
											if ((href != null && href != "" && href != "#")
													|| (id != null && id != "")) {
												if (href.indexOf(ubicacion) >= 0
														|| ((ubicacion == "articulos" || ubicacion == "areaTematica") && li
																.attr("id") == "liAreaTematicaDropDown")) {
													li
															.addClass("menuUbicacionActual");
												}
											}
										}
									});
					colorTablas();
					
					removerClaseAtive();
					
				});
function removerClaseAtive(){
	$('#linkIngrese, #linkRegistro').removeClass('active');
	
}

// Metodo que realiza Scroll a la paginacion de Documentos
// Se requiere incluir la libreria JqueryScrollTo
function scrollTo() {
	$.scrollTo(0, 2500, {
		queue : true
	});
}

//Funcion que permite hacer clic en un anchor
function clicPopUp(valor) {
	$("a[class='" + valor + "']").click();
}

//Funcion que permite hacer clic en un anchor
function clicPopUpClase(valor) {
	$("." + valor).click();
}

/**
 * Permite las pulsaciones del teclado correspondientes 
 * unicamente a numeros o keyCodes menores o iguales a 13
 * @param evt Evento
 * @returns {Boolean}
 */
function numbersOnly(evt) {
	var key = null;
	if (evt.which != null) {
		key = evt.which;
	}
	else {
		key = evt.keyCode;
	}
	return (key <= 13 || key == 35 || key == 36 || key == 37 || key == 39 || key == 46 || (key >= 48 && key <= 57)  || (key >= 96 && key <= 105));
}

/**
 * Funcion que combina las funciones numbersOnly y keyDownReadOnly 
 * @param event Evento que provoca el evento (normalmente es keyDown)
 * @param element Elemento que provoca el evento (normalmente un input text)
 */
function keyDownIndTel(event, element) {
	return keyDownReadOnly(event, element) && numbersOnly(event);
} 


var idNotificaciones = new Array();

// Mutex
var mutex = 0;
var exito;
var m;

function crearNotificacionNoty(notificaciones) {
	if (notificaciones.length > 0) {
		cerrarNotificaciones();
		while (notificaciones.length > 0) {
			var mensaje = notificaciones.pop();
			if (!existeNotificacion(mensaje.value)) {
				var notificacion = noty({
					text : mensaje.value,
					type : mensaje.tipo,
					dismissQueue : true,
					layout : 'topRight',
					theme : 'defaultTheme',
					timeout : mensaje.tiempo,
					template : '<div class="noty_message noty_contenedor" style="text-align:center;"><span class="noty_text noty_abierto"></span><div class="noty_close"></div></div>',
					closeWith : ['click']
				});
				idNotificaciones.push(notificacion);
			}
		}
	}
}

function existeNotificacion(mensaje) {
	var esta = false;
	$(".noty_abierto").each(function(index) {
		if ($(this).text() == mensaje) {
			esta = true;
		}
	});
	return esta;
}

function cerrarNotificaciones() {

	$(".noty_contenedor").each(function(index) {
		$(this).parent().parent().remove();
	});

	while (idNotificaciones.length > 0) {
		idNotificaciones.pop().close();
	}
}

function cerrarErroresNoty() {
	for ( var i = 0; i < idNotificaciones.length; i++) {
		var notificacionActiva = idNotificaciones[i];
		if (notificacionActiva.tipo = 'warning') {
			notificacionActiva.close();
		}
	}
}

/**
 * Esta funci�n deshabilita el espacio en el input en el que se use
 */
function inputSinEspacios(event) {
	if (event.keyCode == 32) {
		return false;
	}
	return true;
}


/*
 * Metodo que pinta de forma alternada filas de una tabla y que tengan como class="tablaembebida"
 */
function colorTablas(){
	$(".tablaembebida tr:even").css("background-color", "#CCCCCC"); // aplica color gris oscuro a las filas pares. 
	$(".tablaembebida tr:even td").css("border", "solid 1px"); // aplica borde 
    $(".tablaembebida tr:odd").css("background-color", "#F2F2F2"); // aplica color gris suave a las filas pares.
    $(".tablaembebida tr:odd td").css("border", "solid 1px"); // aplica borde
}

/*
 * Metodo para imprimir la pagina en la que se encuentra hubicado
*/
function imprimirPagina(){
		 window.print();	 
}

/*** Metodos para cargar la imagen de Vimeo ***/
function vimeoLoadingThumb(idVimeo){    
    var url = "http://vimeo.com/api/v2/video/" + idVimeo + ".json?callback=showThumb";
      
    var id_img = "#video-" + idVimeo;
    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = url;

    $(id_img).before(script);
}

function showThumb(data){
    var id_img = "#video-" + data[0].id;
    $(id_img).css("background","transparent url('"+data[0].thumbnail_medium+"') no-repeat center");
    $(id_img).css("background-size","130px 130px");
}

function tipoVideo(tipo){
	if(tipo.indexOf("vimeo") > 0){
		var result = tipo.lastIndexOf("/") + 1;
		idVimeo = tipo.substring(result, tipo.length);
		vimeoLoadingThumb(idVimeo);
	}else{
		var result = tipo.lastIndexOf("v=") + 2;
		idYoutube = tipo.substring(result,result + 11);
		$("#video-" + idYoutube).css("background","transparent url('http://img.youtube.com/vi/"+idYoutube+"/1.jpg') no-repeat center");
		$("#video-" + idYoutube).css("background-size","130px 130px");
	}
}

function vimeoLoadingThumbMini(idVimeo){    
    var url = "http://vimeo.com/api/v2/video/" + idVimeo + ".json?callback=showThumbMini";
      
    var id_img = "#mini-" + idVimeo;
    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = url;

    $(id_img).before(script);
}

function showThumbMini(data){
    var id_img = "#mini-" + data[0].id;
    $(id_img).css("background","transparent url('"+data[0].thumbnail_medium+"') no-repeat center");
}

function tipoVideoMini(tipo){
	if(tipo.indexOf("vimeo") > 0){
		var result = tipo.lastIndexOf("/") + 1;
		idVimeo = tipo.substring(result, tipo.length);
		vimeoLoadingThumbMini(idVimeo);
	}else{
		var result = tipo.lastIndexOf("v=") + 2;
		idYoutube = tipo.substring(result,result + 11);
		$("#mini-" + idYoutube).css("background","transparent url('http://img.youtube.com/vi/"+idYoutube+"/1.jpg') no-repeat center");
	}
}

/*** Fin Metodos para cargar la imagen de Vimeo ***/