var FOTOGALERIAS = {};
FOTOGALERIAS.desplazamiento = 0; 
FOTOGALERIAS.posicion = 0;
FOTOGALERIAS.id_galeria = 0;
FOTOGALERIAS.fotos = new Array();
FOTOGALERIAS.videos = new Array();


FOTOGALERIAS.verFoto = function(id_foto) {

	/*
	var pathPaginacion = '/galerias-fotos/html/paginacion.' + id_foto + '.html';
	$.ajax({
		url: pathPaginacion,
		async: false,
		success:
			function(data) {
				$('.scroller').html(data);
			}
	});
	*/
	
	$('#left').html('<a class="pagina-izquierda" onclick="javascript:FOTOGALERIAS.verFoto(' + FOTOGALERIAS.fotos[id_foto].id_contenido_anterior + ');return false;" href="' + FOTOGALERIAS.fotos[id_foto].url_anterior + '" title="mostrar foto anterior"><span>anterior</span></a>');
	$('#right').html('<a class="pagina-derecha" onclick="javascript:FOTOGALERIAS.verFoto(' + FOTOGALERIAS.fotos[id_foto].id_contenido_siguiente + ');return false;" href="' + FOTOGALERIAS.fotos[id_foto].url_siguiente + '" title="mostrar foto siguiente"><span>siguiente</span></a>');

	$('#window .pagina-numero').html(FOTOGALERIAS.fotos[id_foto].numFotoActual);
	
	$('.foto-miniatura').removeClass('activa');
	$('#thumb_' + id_foto).addClass('activa');
	var titulo_galeria = $('.galerias h2 span').html();
	$('.galerias h2').html(FOTOGALERIAS.fotos[id_foto].titulo + '<span class="fecha">' + titulo_galeria + '</span>');
	$('.foto-datos .firma .fecha').html(FOTOGALERIAS.fotos[id_foto].fecha);
	$('.foto-datos p').html(FOTOGALERIAS.fotos[id_foto].texto);
	$('.fotoGallery .media img').attr("src", FOTOGALERIAS.fotos[id_foto].path_foto);
	$('.fotoGallery .media img').attr("alt", FOTOGALERIAS.fotos[id_foto].titulo);
	
};

FOTOGALERIAS.verVideo = function(id_video) {

	
	var pathObjVideo = FOTOGALERIAS.videos[id_video].object;
	$.ajax({
		url: pathObjVideo,
		async: false,
		success:
			function(data) {
				$('.media').html(data);
				$('.fecha').html(FOTOGALERIAS.videos[id_video].fecha);
			}
	});
		
	$('#left').html('<a class="pagina-izquierda" onclick="javascript:FOTOGALERIAS.verVideo(' + FOTOGALERIAS.videos[id_video].id_contenido_anterior + ');return false;" href="' + FOTOGALERIAS.videos[id_video].url_anterior + '" title="mostrar video anterior"><span>anterior</span></a>');
	$('#right').html('<a class="pagina-derecha" onclick="javascript:FOTOGALERIAS.verVideo(' + FOTOGALERIAS.videos[id_video].id_contenido_siguiente + ');return false;" href="' + FOTOGALERIAS.videos[id_video].url_siguiente + '" title="mostrar video siguiente"><span>siguiente</span></a>');
	
	$('.foto-miniatura').removeClass('activa');
	$('#thumb_' + id_video).addClass('activa');
	console.log(FOTOGALERIAS.videos[id_video].numVideoActual);
	$('#window .pagina-numero').html(FOTOGALERIAS.videos[id_video].numVideoActual);
};

/*
FOTOGALERIAS.slideThumbs = function(numero, capa) {
	if(capa == null) 
		obj = $(".scroll-conten");
	else
		obj = $('#fotos-thumbnail').find('.conten').find('.scroll-conten');

	if(numero == '-' && parseInt($(obj).css("left")) == 0) 
		return;	

	FOTOGALERIAS.desplazamiento = parseInt($(obj).parents(".conten").css("width"));

	var total = $(obj).find(".foto-miniatura").length;
	var total2 = total*parseInt($(obj).find(".foto-miniatura").css("width"));

	if(total > 0) {
		if(numero == '+') {
			total2 = total2 + parseInt($(obj).css("left"));
			// 4 es el borde del thumb
			desplaza = parseInt($(obj).css("left")) - FOTOGALERIAS.desplazamiento + 4;
		} else {
			total2 = total2 - parseInt($(obj).css("left"));
			desplaza = parseInt($(obj).css("left")) + FOTOGALERIAS.desplazamiento  - 4;
		}

		if(total2 > FOTOGALERIAS.desplazamiento) { // Desplazamos al Slide
			$(obj).animate({"left": desplaza + "px"}, 500);
		}
	}
};
*/

FOTOGALERIAS.slideThumbs = function(numero, capa) {
	if(capa == null) 
		obj = $(".scroll-conten");
	else
		obj = $('#' + capa).find('.conten').find('.scroll-conten');

	if(numero == '-' && parseInt($(obj).css("left")) == 0) 
		return;	

	FOTOGALERIAS.desplazamiento = parseInt($(obj).parents(".conten").css("width"));
	

	var total = $(obj).find(".foto-miniatura").length;
	
	
	var total2 = 0;
	$(obj).find(".foto-miniatura").each(function() {
		total2 = total2 + parseInt($(this).width());
	});
	
	if(total2 == 0) {
		var aux = 86;
	
		if($(".foto-miniatura").html() != null && ($(".foto-miniatura:first").find("img").attr("src").indexOf("/prensa/") >= 0 || $(".foto-miniatura:first").find("img").attr("src").indexOf("/audios/") >= 0)) {
			aux = 100;
		}
		
		if(isNaN($(obj).find(".foto-miniatura").width())) {
			var total2 = total*parseInt($(obj).find(".foto-miniatura").width());
		} else {
			var total2 = total*aux;
		}
		var total2 = total*aux;
	}
	
	if(total > 0) {
		if(numero == '+') {
			
			total2 = total2 + FOTOGALERIAS.posicion;
			desplaza = FOTOGALERIAS.posicion - FOTOGALERIAS.desplazamiento;
			
		} else {
			total2 = total2 - FOTOGALERIAS.posicion;
			desplaza = FOTOGALERIAS.posicion + FOTOGALERIAS.desplazamiento;
		}
		if(numero == '-' && desplaza == FOTOGALERIAS.desplazamiento) {
			return;
		}
		//console.log(total2 + "--" + FOTOGALERIAS.desplazamiento + "--" + desplaza);
		//if(total2 > FOTOGALERIAS.desplazamiento) { // Desplazamos al Slide
		if(total2 > 0) { // Desplazamos al Slide
			$(obj).animate({"left": desplaza + "px"}, 500);
			FOTOGALERIAS.posicion = desplaza;
		}
	}
};


FOTOGALERIAS.activaThumb = function(id_foto) {
	$("#div_paginacion_thumbs ul li").each(function(index) {
		if($(this).attr("id") == "thumb_" + id_foto) {
			var offset = $(this).offset();
			if(offset.left -50 > FOTOGALERIAS.desplazamiento) {
				//FOTOGALERIAS.slideThumbs('+');
			}
			$(this).addClass("activo");			
		} else {
			$(this).removeClass("activo");
		}
	});
};

FOTOGALERIAS.getJSON = function(pathJson, arrayDatos) {
	$.ajax(
		{
			url: pathJson,
			async: false,
			dataType: "json",
			success:
				function(datosJson) {
					if(pathJson.indexOf("/galerias-fotos/") >= 0)
						FOTOGALERIAS.fotos = datosJson;
					else
						FOTOGALERIAS.videos = datosJson;
				}
		}
	);
};

FOTOGALERIAS.fekoorTB = function(id_seccion) {
	$.ajax(
		{
			url: '/fekoor-tb/object/videos/ultimos-videos-' + id_seccion + '-portada-mm-utf8.html',
			async: false,
			success:
				function(datosVideos) {
					$(".activo").removeClass("activo");
					$(".galerias").html(datosVideos);	
					$("#fekoortb" + id_seccion).parents("a").addClass("activo");
				}
		}
	);
};

$(document).ready(function() {
	if($(".scroll-conten").html() != null) {		
		FOTOGALERIAS.id_galeria = $('#id_galeria').html();
		if(FOTOGALERIAS.id_galeria != null) {
			if(document.location.href.indexOf('/centro-arbolarte/') > 0) {
				FOTOGALERIAS.getJSON('/centro-arbolarte/galerias-fotos/json/' + FOTOGALERIAS.id_galeria + '.json');
			} else {
				FOTOGALERIAS.getJSON('/galerias-fotos/json/' + FOTOGALERIAS.id_galeria + '.json');
			}
		}

		if($(".nombre_seccion").html() == 'Vídeos') {
			if(document.location.href.indexOf('/centro-arbolarte/') > 0) {
				FOTOGALERIAS.getJSON('/centro-arbolarte/multimedia/videos/json/videos.json');
			} else {
				FOTOGALERIAS.getJSON('/multimedia/videos/json/videos.json');
			}
		}
	}
});

