
/* PARA jQuery
-------------------------------------------------------------------------------------------------- */

jQuery(document).ready(function(){
    /* foto-noticia */
	jQuery('.anythingSlider').anythingSlider({
		easing: "swing",        // Anything other than "linear" or "swing" requires the easing plugin
		autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
		delay: 3000,                    // How long between slide transitions in AutoPlay mode
		startStopped: false,            // If autoPlay is on, this can force it to start stopped
		animationTime: 600,             // How long the slide transition takes
		hashTags: true,                 // Should links change the hashtag in the URL?
		buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Go",             // Start text
		stopText: "Stop"               // Stop text
		//navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
	});
	/* paginación */
	jQuery('#thumbNav a').click(function(){
		actualizaVisitaOJD('');
	});
		
	jQuery(".lstBloqueCarousel-rollover .jcarousel-item").hover(
		function(){
			// Mostramos el rollover
			jQuery('#'+this.id).stop().show().animate({
			opacity: 1
			}, 300);
		},
	    function(){
	        // Ocultamos el rollover
	        jQuery('#'+this.id).stop().animate({
	          opacity: 0
	        }, 300, function() { jQuery(this).hide(); });
		}
    );
	
    jQuery(".lstBloqueCarousel-rollover .rollover-mosaico").hover(
		function(){
			jQuery("#"+this.id).stop().show().animate({ opacity: 1 }, 300);
		},
		function(){
			jQuery("#"+this.id).stop().show().animate({ opacity: 0 }, 300);
		}
	);
	
	/* COMENTARIOS - PULGARES */
	jQuery(".pulgarArriba").click(function(){
		grabaComentarioVoto(this.id, 1);
	});
	
	jQuery(".pulgarAbajo").click(function(){
		grabaComentarioVoto(this.id, 0);
	});
	
	jQuery(".muestraComentario").click(function(){
		jQuery(this).parent().next().show().animate({ opacity: 1 }, 600);
		jQuery(this).parent().hide().animate({ opacity: 0 }, 600);
		actualizaVisitaOJD('');
	});
});


/* Cambiar el botón de anterior del carousel, para que está activo o no
------------------------------------------------------------------------------------------------ */
function cambia_btn_ant(carousel, item, i, state, tipo){
	if(i == 1){
		jQuery('#mosaico-ant-'+tipo).stop().animate({ opacity: 0.3 }, 600);
	}else{
		jQuery('#mosaico-ant-'+tipo).stop().animate({ opacity: 1}, 300);
	}
}

/* Cambiar el botón de siguiente del carousel, para que está activo o no
------------------------------------------------------------------------------------------------ */
function cambia_btn_sig(carousel, item, i, state, tipo){
	if(i == carousel.options.size){
		jQuery('#mosaico-sig-'+tipo).stop().animate({ opacity: 0.3 }, 600);
	}else{
		jQuery('#mosaico-sig-'+tipo).stop().animate({ opacity: 1}, 300);
	}
}

/* usamos initCallback callback para asignar controles de siguiente y anterior al carousel
------------------------------------------------------------------------------------------------ */
function funciones_carousel(carousel, tipo) {
	jQuery('#mosaico-ant-'+tipo).bind('click', function() {
		carousel.prev();
		actualizaVisitaOJD('');
		return false;
	});
   jQuery('#mosaico-sig-'+tipo).bind('click', function() {
		carousel.next();
		actualizaVisitaOJD('');
		return false;
	});
}

/* abre una ventana modal - jquery.simplemodal-1.3.5.min.js
------------------------------------------------------------------------------------------------ */
function abrePopModal(ruta, alto, ancho, scroll){
	
	if(ruta.indexOf("#") == -1){
		jQuery.modal('<iframe src="'+ ruta +'" height="'+ alto +'" width="'+ ancho +'" scrolling="'+ scroll +'" style="border:0">', {
			closeHTML: '',
			closeClass: "modalClose",
			overlayClose: true,
			onOpen: function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.data.hide();
				dialog.container.fadeIn('fast', function () {
					dialog.data.slideDown('fast');
					});
				});
			},
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.hide('fast', function () {
						dialog.overlay.slideUp('fast', function () {
							jQuery.modal.close();
						});
					});
				});
			}
		});
	}else{
		jQuery(ruta).modal( {
			minHeight: alto,
			minWidth: ancho,
			closeHTML: '',
			closeClass: "modalClose",
			overlayClose: true,
			onOpen: function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.data.hide();
				dialog.container.fadeIn('fast', function () {
					dialog.data.slideDown('fast');
					});
				});
			},
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.hide('fast', function () {
						dialog.overlay.slideUp('fast', function () {
							jQuery.modal.close();
						});
					});
				});
			}
		});
	}
}

/* graba el voto del comentario
------------------------------------------------------------------------------------------------ */
function grabaComentarioVoto(idPulgar, voto){	
	var idComentario = idPulgar.split("-")[1];
	dataStringComentariosVoto = "cual=comentarioVota&voto="+ voto +"&idComentario=" + idComentario;
	jQuery.ajax({
		type: "GET",
		url: "consultaAjax.php",
		data: dataStringComentariosVoto,
		success: function(cadena){
			if(cadena == "ok"){
				jQuery("#pulgar_1-"+ idComentario).animate({className: ''}, 500);
				jQuery("#pulgar_0-"+ idComentario).animate({className: ''}, 500);
				if(voto == 1){
					txt = "#txtVotosPos-"+idComentario;
				}else{
					txt = "#txtVotosNeg-"+idComentario;
				}
				jQuery(txt).html(parseInt(jQuery(txt).html()) + 1);
			}else{
				var aCadena = cadena.split("|||");
				if(aCadena[0] == 1){
					alert(aCadena[1]);
				}else{
					if(confirm(aCadena[1])){
						window.location.href  = aCadena[2];
					}
				}
			}
		}
	});
}


/* contador de caracteres #count_char
------------------------------------------------------------------------------------------------ */		
function muestraCaracteresDisponibles(totales, nCharWrite){
	var nChar = totales;
	if(nCharWrite > 0){
		nChar = (totales - nCharWrite) - 1;
	}
	if(nChar < 0){ nChar = 0; }
	jQuery('#count_char').html(nChar);
}


// Popups
//------------------------------------------------------------------------------------------------
var ventana;
function abrePop(cual, nombre, alto, ancho, ajustable, scroll) {
	if(alto == ""){ alto = 650; }
	if(ancho == ""){ ancho = 750; }
//	abrePopModal(cual, alto, ancho, 'auto');
	
	var donde_x, donde_y;
	donde_x = (screen.width - ancho) / 2;
	donde_y = (screen.height - alto) / 2;
	if (ie) {
		ventana = window.open(cual, nombre, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars='+scroll+', resizable='+ajustable+', copyhistory=no, width='+ancho+', height='+alto+', top='+donde_y+', left='+donde_x+'');
	} else if (ns) {
		ventana = window.open(cual, nombre, 'width='+ ancho +', height='+ alto +', location=no,toolbar=no, directories=no, menubar=no, resizable='+ajustable+', scrollbars='+ scroll +', status=no, top='+donde_y+', left='+donde_x+'');
	} else {
		ventana = window.open(cual, nombre, 'left='+ donde_x +', top='+ donde_y +', width='+ ancho +', height='+ (alto+5) +', location=no, toolbar=no, directories=no, resizable='+ ajustable +', scrollbars='+ scroll +', status=no');
	}
	ventana.focus();
}


// AJAX para cada blog del bloque de WP de portada
//------------------------------------------------------------------------------------------------
function lstBloqueCarousel_WP_itemLoadCallback(carousel, state){

	if (carousel.has(carousel.first, carousel.last)) {
        return;
    }

	jQuery.get(
		"consultaAjax.php",
		{
			cual: "devuelveInfoBlogWP",
            first: carousel.first,
            last: carousel.last
        },
		function(xml) {
            lstBloqueCarousel_WP_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        },
        'xml'
	);
}
function lstBloqueCarousel_WP_itemAddCallback(carousel, first, last, xml){
    // Set the size of the carousel
    carousel.size(parseInt(jQuery('total', xml).text()));

    jQuery('blog', xml).each(function(i) {
        carousel.add(first + i, jQuery(this).text());
    });
}


// AJUSTA el tamaño de un elemento
//------------------------------------------------------------------------------------------------
function ajustaTamanoBloque(cual, alto, ancho){
    if(alto != ""){
        jQuery(cual).height(alto);
    }
    if(ancho != ""){
        jQuery(cual).width(ancho);
    }
}


/**
 * Parse a JSON response.
 *
 * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message.
 */
function ecg_parseJson(data) {
  if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
    return { status: 0, data: data.length ? data : 'Unspecified error' };
  }
  return eval('(' + data + ');');
};

/* FIN jQuery
-------------------------------------------------------------------------------------------------- */
