$(document).ready( function () {
				
	$("#timeMachine").timeMachine({
		change: function (el) {
			el.parent().find(".hotspot, .info").hide();
			el.parent().children(".hotspot.play").click();
			el.parent().find(".gallery.flash").children("img").show();
			el.parent().find(".gallery.flash").children("object").hide();
			el.find(".hotspot").fadeIn();
		}
	});
	
	$("#timeMachine .hotspot.footer").hover(function () {
		if ($(this).find(".info").html() == "") return;
		$(this).parent().parent().find(".info").stop(true, true).slideDown(500);
	}, function () {
		$(this).parent().parent().find(".info").stop(true, true).slideUp(500);
	});
	
	function tmGallery(gallery) {
		
		gallery.parent().find(".hotspot.play").click( function () {
				$(this).toggle();
				gallery.children("img").hide();
				gallery.children("object").css("display", "block");
				return false;
			});
			
			if (gallery.is(".flash"))
				return;
		
			gallery.cycle({
				fx: 'fade',
				timeout: 0,
				width: "100%", 
				height: "100%",
				fit: 1,
				prev: gallery.parent().find('.hotspot.prev'),
				next: gallery.parent().find('.hotspot.next'),
				prevNextClick: function (isNext, index, el) {
					var info = $(el).find(".maisinfo").html();
			
					gallery.closest(".panel").find(".hotspot.footer").find(".info").html(info);
					if (info != "") {
						gallery.closest(".panel").find(".hotspot.footer").find(".maisinfo").show();
					} else {
						gallery.closest(".panel").find(".hotspot.footer").find(".maisinfo").hide();
					}
					gallery.parent().find(".galleryCur").html(index + 1);
					gallery.closest(".panel").find(".flipv").attr("src", $(el).find("img").attr("src"));
				}
			});
		
	}
	
	$(window).load( function () {
		$("#timeMachine .gallery").each( function (i) {

			tmGallery($(this));
			
		});
		
		$("#timeMachine .hotspot.header").hover(function () {
			$(this).children(".menu").stop(true, true).slideDown(500);
		}, function () {
			$(this).children(".menu").stop(true, true).slideUp(500);
		});
		
		$("#timeMachine .menu a").click(function () {
			
			$(this).closest(".panel").find(".menu a").removeClass("activo");
			$(this).addClass("activo");
			
			var menu = $(this);
			
			$.ajax({
				url: menu.attr("href"),
				success: function(data) {
					menu.closest(".panel").find(".gallery").html(data);
					tmGallery(menu.closest(".panel").find(".gallery"));
					
					var el = menu.closest(".panel").find(".gallery span:first");
					var info = el.find(".maisinfo").html();
					var total = menu.closest(".panel").find(".gallery > span").length;
		
					menu.closest(".panel").find(".hotspot.footer").find(".info").html(info);
					if (info != "") {
						menu.closest(".panel").find(".hotspot.footer").find(".maisinfo").show();
					} else {
						menu.closest(".panel").find(".hotspot.footer").find(".maisinfo").hide();
					}
					if (total > 1) {
						menu.closest(".panel").find(".galleryCur").html(1);
						menu.closest(".panel").find(".galleryTotal").html(total);
						menu.closest(".panel").find(".galleryCounter").show();
					} else {
						menu.closest(".panel").find(".galleryCounter").hide();
					}
					menu.closest(".panel").find(".flipv").attr("src", $(el).find("img:first").attr("src"));
				}
			});
			
			return false;
		});
		
	});
	
});


