var total;
var actual;
var nactual;
var leyenda;
var anchol;
var INT_PASADOR = null;

$(document).ready(function () {
	// ****** DESTACADOS DE PROYECTOS
	total = $("div.obras > div.obra").size();
	actual = $("div.obras > div.obra:first");
	nactual = 1;
	leyenda = actual.next();
	anchol = leyenda.css("width");

	actual.fadeIn('slow');
	leyenda.children().each(function() {
		$(this).css("display","none");
	});
	leyenda.css("width","0").css("display","block");
	leyenda.animate({width: anchol}, 1000, function() {
		leyenda.children().each(function() {
			$(this).fadeIn();
		});
		setTimeout("cambiaObra()",3000);
	});
	// ****** FIN DESTACADOS DE PROYECTOS
				
/*	
	$("div.main_proyectos > div.proyectomarco").hide();
	$("div.main_proyectos > div.opciones").hide();
	$("div.main_proyectos").children(":first").show().next().show();
	$("div.menupro > div.menu > img.on").hide();
	$("div.menu_proyectos > div.carril").children(":first").children(":first").children(":first").next().next().show();
*/

	//$("div.proyectomarco > div.marco > div.foto").children().hide();
	//$("div.proyectomarco > div.marco > div.foto").children(":first").show();

/*	
	$("div.menupro > div.menu > a").click(function(e) {
		e.preventDefault();
		var marco = $(this).attr("class");		

		$("div.menupro > div.menu > img.on").hide();
		$(this).next().show();
		
		$("div." + marco + " > div.marco > div.foto").children().hide();
		$("div." + marco + " > div.marco > div.foto").children(":first").show();
		
		$("div.main_proyectos > div.opciones").hide();
		$("div.main_proyectos > div.proyectomarco").hide();
		$("div.main_proyectos > div." + marco).show();
	});
*/	
	// Llamamos a eventos Proyectos
	eventosProyecto();

	// ****** Click en un proyecto del menu lateral
	$("div.menu_proyectos > div.carril > div.menupro > div.menu > a").click(function (e) {
		e.preventDefault();
		var data = {};
		var self = this;
		data.ajax = 1;
		
		$.ajax({
			url: $(self).attr("href"),
			type: "get",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			data: data,
			beforeSend: function () {
				$("img.on").remove();
				var aux = $('<img class="on" src="/images/bgmenupro_on.png" />');
				aux.insertAfter($(self));
				//$(this).parent().append('<img class="on" src="/images/bgmenupro_on.png" />');
				//var auxref = $("img.on");
				//$(this).parent().append($("img.on"));
				//auxref.clone().appendTo($(this).parent());
				//auxref.remove();
				//$("img.on").remove();
				//$(this).parent().prepend($(auxref));
			},
			success: function (r) {
				var r = $('<div>'+r+'</div>');
				var cont = r.find("div.main_proyectos").html();				
				$("div.main_proyectos").html(cont);
				eventosProyecto();
			}
		});		
	});
});

$(window).load(function () {
	var alturafoto = $("div.main_proyectos > div.proyectomarco > div.marco > div.foto img").height();
	var anchurafoto = $("div.main_proyectos > div.proyectomarco > div.marco > div.foto img").width();

	//$("div.main_proyectos > div.proyectomarco > div.marco > div.foto").css("height", alturafoto + "px");
	//$("div.main_proyectos > div.proyectomarco > div.marco > div.foto").css("width", anchurafoto + "px");
	
	//var newaltura = parseInt($("div.main_proyectos > div.proyectomarco > div.marco > div.foto").css("margin-top")) + $("div.main_proyectos > div.opciones").height() + alturafoto + 1;
	//$("div.proyectos > div.nav_proyectos").css("height", newaltura + "px");	
	
	//var lataux = $("div.nav_proyectos > div.opciones_up").height() + $("div.nav_proyectos > div.opciones_down").height() + parseInt($("div.nav_proyectos > div.menu_proyectos").css("margin-top"));
	//lataux = newaltura - lataux;
	//$("div.nav_proyectos > div.menu_proyectos").css("height", lataux + "px");	

/*
	if ($("div.nav_proyectos div.carril").height() > $("div.menu_proyectos").height()) {
		$("div.nav_proyectos div.menupro img.on").each(function () {
			var top = $(this).closest("div.menupro").position().top;
			$("div.nav_proyectos div.carril").animate({top:"-=" + top + "px"});
		});
	}
*/
	if ($("div.nav_proyectos div.carril").height() > $("div.menu_proyectos").height()) {
		var auxpag = $("img.on").closest("div.menupro").prevAll().size();
		var auxtop = ($("div.menupro:first").height() + 8) * auxpag;
		$("div.menu_proyectos > div.carril").animate({top:'-'+auxtop+'px'});
	} else {
		$("div.proyectos > div.nav_proyectos a.up").remove();
		$("div.proyectos > div.nav_proyectos a.down").remove();
	}
	
	// ****** Funcionalidad del pasador de proyectos lateral
	$("div.proyectos > div.nav_proyectos").each(function () {
		var self = this;
		var ul = $(this).find("div.menu_proyectos");
		var carril = $(this).find("div.carril");
		var nitems = $(this).find("div.menupro").size();

		var itemsxpag = 1;
			
		var npages = Math.ceil(nitems / itemsxpag);

		var indice = $("div.nav_proyectos div.menupro").index($("div.nav_proyectos div.menupro img.on").closest("div.menupro")) + 1;
		var pagactual = indice;
		if (pagactual > 1)
			$(self).find("a.up").removeClass("nomore");
		if (pagactual == npages)
			$(self).find("a.down").addClass("nomore");
			
		var actual = $("div.nav_proyectos div.menupro img.on").closest("div.menupro");
		var alto = actual.height() + parseInt(actual.css("margin-bottom"));

		if (carril.height() <= ul.height() && pagactual==1) {
			$(this).find("a.up").remove();
			$(this).find("a.down").remove();
		}
		
		else {
			$(this).find("a.down").click(function (e) {
				e.preventDefault();

				if (pagactual == npages)
					return;
				$(self).find("a.up").removeClass("nomore");
				
				alto = actual.height() + parseInt(actual.css("margin-bottom"));				
				actual = actual.next();
				
				carril.animate({top:"-=" + alto + "px"});
				pagactual++;
				
				if (pagactual == npages)
					$(this).addClass("nomore");
			});
			$(this).find("a.up").click(function (e) {
				e.preventDefault();

				if (pagactual == 1)
					return;
				$(self).find("a.down").removeClass("nomore");

				actual = actual.prev();				
				alto = actual.height() + parseInt(actual.css("margin-bottom"));
				
				carril.animate({top:"+=" + alto + "px"});
				pagactual--;
				
				if (pagactual == 1)
					$(this).addClass("nomore");
			});
		}
	});	
		
});

function cambiaObra() {
	actual.next().fadeOut('slow');
	actual.fadeOut('slow');
	if (nactual < total) {
		actual = leyenda.next();
		leyenda = actual.next();
		actual.fadeIn('slow');
		leyenda.fadeIn('slow');
		nactual++;
	}
	else {
		actual = $("div.obras > div.obra:first");
		leyenda = actual.next();
		actual.fadeIn('slow');
		leyenda.fadeIn('slow');
		nactual = 1;
	}
	setTimeout("cambiaObra()",3000);
}		

function eventosProyecto() {

	// ****** Funcionalidad del pasador de la barra de mas fotos
	$("div.marco > div.navmarco").each(function () {
		var self = this;
		var ul = $(this).find("div.contenido");
		var carril = $(this).find("div.carril");
		var nitems = $(this).find("div.mini").size();

		var itemsxpag = 8;
			
		var npages = Math.ceil(nitems / itemsxpag);

		var pagactual = 1;

//		var pixdemas = Math.abs((ul.find("div.mini").position().left - ul.find("div.mini + div.mini").position().left)) - (parseInt(ul.find("div.mini").css("width")) + parseInt(ul.find("div.mini").css("margin-right")));
//		var ancho = (parseInt(ul.find("div.mini").width()) + parseInt(ul.find("div.mini").css("margin-right")) + pixdemas) * itemsxpag;
		var ancho = 696;
		
		if (nitems <= itemsxpag) {
			$(this).find("a.next").remove();
			$(this).find("a.prev").remove();
		}
		else {
			$(this).find("a.next").click(function (e) {
				e.preventDefault();

				if (pagactual == npages)
					return;
				$(self).find("a.prev").removeClass("nomore");
				
				carril.animate({left:"-=" + ancho + "px"});
				pagactual++;
				
				if (pagactual == npages)
					$(this).addClass("nomore");
			});
			$(this).find("a.prev").click(function (e) {
				e.preventDefault();

				if (pagactual == 1)
					return;
				$(self).find("a.next").removeClass("nomore");
				
				carril.animate({left:"+=" + ancho + "px"});
				pagactual--;
				if (pagactual == 1)
					$(this).addClass("nomore");
			});
		}
	});

	// ****** Click en una foto de la barra de mas fotos (Carga la imagen que tienen en el atributo data-img)
	$("div.mini > a").click(function(e, fuente) {
		if (fuente != "robot") {
			clearTimeout(INT_PASADOR);
		}
		var marco = $(this).attr("class");
		var foto = $("div." + marco + " > div.marco > div.foto > img");
		//var foto = $("div.marco > div.foto > img");	
		e.preventDefault();
		var img = new Image();
		img.src = $(this).attr("data-img");
		if (img.complete) {
				var fh = 444;
				var fw = 739;
				var auxw = img.width;
				var auxh = img.height;
				
				var nw = 0;
				var nh = 0;
				var resize = false;
				if (auxh > fh) {
					nh = fh;
					nw = (auxw * fh)/auxh;
					resize = true;
				}
				if ((auxw > fw) && (!resize)) {
					nw = fw;
					if (nh != 0)
						auxh = nh;
					nh = (auxh * fw)/auxw;
					resize = true;
				}
				
				if (resize) {
					img.width = nw;
					img.height = nh;
				}
				
				foto.fadeOut('slow',function() {
					$(this).remove();
				});
		}
		else {
			$(img).load(function() {				
				var auxw = $(img).get(0).width;
				var auxh = $(img).get(0).height;
				var fh = 444;
				var fw = 739;
				
				var nw = 0;
				var nh = 0;
				var resize = false;
				if (auxh > fh) {
					nh = fh;
					nw = (auxw * fh)/auxh;
					resize = true;
				}
				if ((auxw > fw) && (!resize)) {
					nw = fw;
					if (nh != 0)
						auxh = nh;
					nh = (auxh * fw)/auxw;
					resize = true;
				}
				
				if (resize) {
					img.width = nw;
					img.height = nh;
				}
				
				foto.fadeOut('slow',function() {
					$(this).remove();
				});
			});
		}
		foto.parent().prepend($(img));
	});
	
	// ****** Oculta barra de masfotos cuando solo hay una foto
	if ($("div.main_proyectos > div.proyectomarco > div.marco > div.navmarco div.mini").size() <= 1)
		$("div.main_proyectos > div.proyectomarco > div.marco > div.navmarco").hide();
	
	// ****** Click en enlace mas fotos (muestra o oculta barra de fotos del proyecto)
	$("div.main_proyectos > div.opciones > a").click(function(e) {
		var marco = $(this).attr("class");
		var navmarco = $("div." + marco + " > div.marco > div.navmarco");
		e.preventDefault();
		if (navmarco.css("display") == "block") {
			navmarco.fadeOut();
			$("div." + marco + " > a." + marco + " > img.up").hide();
			$("div." + marco + " > a." + marco + " > img.down").show();
		}
		else {
			navmarco.fadeIn();
			$("div." + marco + " > a." + marco + " > img.up").show();
			$("div." + marco + " > a." + marco + " > img.down").hide();					
		}
	});	
	
	if ($("div.navmarco div.carril div.mini").size() > 1) {
		//if (INT_PASADOR != null)
			//clearTimeout(INT_PASADOR);
			
		function nextMini() {
			function show_next() {
				clearTimeout(INT_PASADOR);
				INT_PASADOR = setTimeout(function () {
					nextMini();
				},5000);
			}
			
			if ($("div.navmarco div.carril div.mini.active").size() == 0 || !$("div.navmarco div.carril div.mini.active").next().is("div")) {
				var next = $("div.navmarco div.carril").find("div.mini:first");
			}
			else {
				var next = $("div.navmarco div.carril div.mini.active").next();
			}
			$("div.navmarco div.carril div.mini.active").removeClass("active");
			next.addClass("active");
			next.find("a").trigger("click", ["robot"]);
			var img = $("div.marco div.foto img");
			if (!img.get(0).complete) {
				img.load(function () {
					show_next();
				});
			}
			else {
				show_next();
			}
		}
		nextMini();
	}	
	
}
