var FOTOGRAFIA = {};

FOTOGRAFIA.preview = function(elemento) {
	$("img[imagesrc]",elemento).each(
		function() {
			$(this).attr("src",$(this).attr("imagesrc"));
			$(this).removeAttr("imagesrc");
		}
	);
}

FOTOGRAFIA.columnas = function(elemento) {
	$("#AC .mod_cat_destacadas .mcgd_foto > a:eq(2)").show();
	$("#CA .mod_cat_destacadas .mcgd_foto > a:eq(2)").show();
}

FOTOGRAFIA.inicializar = function() {
	$(".foto_paginacion li a[indice]").each(
		function() {
			var indice = $(this).attr("indice");
			$(this).text(indice);
		}
	);
}

FOTOGRAFIA.modulo_inicializar = function() {
	$(".galeria_contento").each(
		function() {
			FOTOGRAFIA.preview(this);
		}
	);
}

FOTOGRAFIA.modulo_navegacion = function(galeria,indice) {
	galeria = $(galeria);
	fotos = $(".div_galeria_contento[indice]",galeria);
	num_fotos = fotos.length;
	if(num_fotos > 0) {
		if((indice == "+") || (indice == "-")) {
			foto_visible_indice = ($(".div_galeria_contento:visible[indice]",galeria).length == 1) ? parseInt($($(".div_galeria_contento:visible[indice]",galeria).get(0)).attr("indice")) : 1;
			indice = (indice == "+") ? (foto_visible_indice + 1) : (foto_visible_indice - 1);
		}
		foto_indice = ((indice == null) || isNaN(indice) || (indice > num_fotos)) ? 1 : ((indice < 1) ? num_fotos : indice);
		$(fotos).fadeOut();
		$(fotos.get(foto_indice - 1)).each(
			function() {
				var foto_obj = this;
				var foto_src = $("img",this).attr("src");
				var foto_img = new Image();
				foto_img.onload = function() {
					$("img",foto_obj).attr("src",foto_src);
					$(foto_obj).fadeIn();
					// Ajusto la posición
					var margenAlto = $(".not_foto",foto_obj).height() - foto_img.height;
					var margenAncho = $("a",foto_obj).width() - foto_img.width;
					if(margenAlto > 0) {
						$("img",foto_obj).css("margin-top",margenAlto/2 + "px");
						$(".not_autor",foto_obj).css("bottom",(margenAlto/2 + 10) + "px");
					}
					if(margenAncho > 0) $(".not_autor",foto_obj).css("right",margenAncho/2 + "px");
					foto_img.onload=function() {};
				};
				foto_img.src = foto_src;
			}
		);
		pag_indice_inicio = (foto_indice > 2) ? (foto_indice - (((num_fotos - foto_indice) < 2) ? (4 - (num_fotos - foto_indice)) : 2)) : 1;
		if(pag_indice_inicio < 1) pag_indice_inicio = 1;
		pag_indice_fin = ((pag_indice_inicio + 4) < num_fotos) ? (pag_indice_inicio + 4) : num_fotos;
		$(".foto_paginacion li.fpag a[indice]",galeria).each(
			function() {
				$(this).removeClass("activo");
				pag_indice = parseInt($(this).attr("indice"));
				if(pag_indice == foto_indice) $(this).addClass("activo");
				if((pag_indice >= pag_indice_inicio) && (pag_indice <= pag_indice_fin)) $(this).parent("li.fpag").show(); else $(this).parent("li.fpag").hide();
			}
		);
		$(".foto_paginacion li #fnum",galeria).text(foto_indice);
	 }
}

FOTOGRAFIA.navegador_inicializar = function() {
	$(".navegador_contento").each(
		function() {
			var galeria = this;
			FOTOGRAFIA.preview(galeria);
		}
	);
}

FOTOGRAFIA.navegador_navegacion = function(galeria,accion) {
	galeria = $(galeria);
	accion =((accion == null) || (accion != "-")) ? "+" : "-";
	alto = $($(".fau_thumb",galeria).get(0)).height();
	limite = -(($(".fau_thumb",galeria).length * alto) / 2);
	posicionInicial = parseInt($(".thumbs_navagable",galeria).css("top"));
	posicion = (accion == "-") ? (posicionInicial + (alto*4)) : (posicionInicial - (alto*4));
	if(posicion > 0) posicion = 0;
	if(posicion < limite) posicion = posicionInicial;
	$(".thumbs_navagable",galeria).animate({top: posicion + "px"},500);
}

FOTOGRAFIA.masInfo = function() {
	if($("#foto-masinformacion").length == 1) $("#foto-masinformacion").slideToggle("normal");
}

FOTOGRAFIA.fotos = function(id_contenido,id_tipo,usuario) {
	var fotos = [];
	$.ajax(
		{
			type: 'POST', url: GENERAL.path + '/api/ajax.php', async: false, data: {adm_target: "fotografia", adm_accion: "fotos", id_contenido: id_contenido, id_tipo: id_tipo},
			success: function(data) {
				var respuesta = GENERAL.ajaxDecode(data);
				fotos = $.isArray(respuesta) ? respuesta : [respuesta];
			}
		}
	);
	return fotos;
}

FOTOGRAFIA.buscar = function() {
	frm = window.document.frmBuscador;
	var q = frm.q.value;
	var que = $("input[name='que']:radio:checked",frm).val();
	var donde = '';
	if(frm.donde_texto.checked) donde += ((donde == "") ? "texto" : ",texto");
	if(frm.donde_etiquetas.checked) donde += ((donde == "") ? "etiquetas" : ",etiquetas");
	if(q == "") {
		alert("No has introducico el término de busqueda");
		frm.q.focus();
		return false;
	}
	if(q.length < 3) {
		alert("El término de busqueda tiene que tener al menos tres caracteres");
		frm.q.focus();
		return false;
	}
	window.location = GENERAL.path + "/buscador.html?que=" + que + "&donde=" + donde + "&q=" + escape(q);
}

FOTOGRAFIA.xml = function(id_contenido,id_tipo) {
	var xml = "";
	$.ajax(
		{
			type: 'POST', url: GENERAL.path + '/api/ajax.php', async: false, data: {adm_target: "general", adm_accion: "xml", id_contenido: id_contenido, id_tipo: id_tipo},
			success: function(data) {xml = data}
		}
	);
	return xml;
}

FOTOGRAFIA.calendario = function(elemento,accion) {
	accion = (accion == null) ? "" : accion;
	if((elemento != "") && ($(elemento).length == 1)) {
		var moduloObj = $(elemento);
		var mes_select = $("select[name='mes_agenda']",moduloObj).get(0);
		var ano_select = $("select[name='anio_agenda']",moduloObj).get(0);
		if(accion == "+") {
			if($(mes_select).val() == "12") {
				if(ano_select.selectedIndex < (ano_select.length - 1)) {
					mes_select.selectedIndex = 0;
					ano_select.selectedIndex = ano_select.selectedIndex + 1;
				}
			}
			else mes_select.selectedIndex = mes_select.selectedIndex + 1;
		}
		if(accion == "-") {
			if($(mes_select).val() == "01") {
				if(ano_select.selectedIndex > "2009") {
					mes_select.selectedIndex = 11;
					ano_select.selectedIndex = ano_select.selectedIndex - 1;
				}
			}
			else mes_select.selectedIndex = mes_select.selectedIndex - 1;
		}
		var mes = $(mes_select).val();
		var ano = $(ano_select).val();
		$.get(GENERAL.path + '/api/ajax.php',{adm_target: "fotografia", adm_accion: "calendario", mes: ano + mes},
			function(data) {
				$(".cont_calendario",moduloObj).html(data);
			}
		);
	}
}

FOTOGRAFIA.presentacion = function(id_galeria) {
	$.get(GENERAL.path + "/includes/galerias/" + id_galeria + ".presentacion.xml",
		function(data) {
			var xml = data;
			$(xml).presentacion();
		}
		,"xml"
	);
}

$(document).ready(
	function() {
		FOTOGRAFIA.inicializar();
		FOTOGRAFIA.modulo_inicializar();
		FOTOGRAFIA.navegador_inicializar();
	}
);
