jQuery(function($) {
	var tabs = $(".pageTopTab");
	var pages = $(".pageTopTabBody");
	var active = "pageTopTabActive";

	function switchTab() {
		tabs.removeClass(active);
		pages.removeClass(active + "Body");

		var name = this.name;
		$(tabs).filter("." + name + "Tab").addClass(active);
		$(pages).filter("." + name + "Body").addClass(active + "Body");

		return false;
	}

	tabs.children("a").click(switchTab);
});

jQuery(function($) {
	var ess_images = []; // [index] -> [[href, description],...]
	var ess_images_map = {}; // map[href] == index
	var ess_current = null;
	
	function ess_update(idx) {
		var i = ess_images[idx];
		var total = ess_images.length;
		var href = i[0];
		var desc = i[1];
	
		var img = document.createElement("img");
		img.src = "images/loader.gif";
		$("#essImageContainer").empty().append(img);
		$("#essDescription").text(desc);
		$("#essCounter").text("Imagem " + (idx + 1) + " de " + total + ": ");
		
		img = document.createElement("img");
		img.onload = function() {
			$("#essImageContainer").empty().append(img);
		} 
		img.src = href;
	}
	
	function ess_prev() {
		if (ess_images.length == 0) return;
		
		if (ess_current == null || ess_current <= 0 || ess_current > ess_images.length) {
			ess_current = ess_images.length;
		}
		ess_update(--ess_current);
	}
	function ess_next() {
		if (ess_images.length == 0) return;
		
		if (ess_current == null || ess_current < 0 || ess_current >= ess_images.length - 1) {
			ess_current = -1;
		}
		ess_update(++ess_current);
	}
	
	function ess_show(hash) {
		var left = (document.documentElement.scrollLeft || document.body.scrollLeft);
		var top = (document.documentElement.scrollTop || document.body.scrollTop);
		var width = (document.documentElement.offsetWidth || document.body.clientWidth);
		var height = (document.documentElement.offsetHeight || document.body.clientHeight);
		
		var wwidth = 700;
		var wheight = 560;
		
		var wtop = Math.round(top + (height - wheight) / 2);
		var wleft = Math.round(left + (width - wwidth) / 2);
		
		var trigger = $(hash.t);
		var href = trigger.attr("href");
		ess_current = ess_images_map[href];
		ess_update(ess_current);
		
		hash.o.hide();
		hash.w.css({
			top: wtop + "px",
			left: wleft + "px",
			width: wwidth + "px",
			height: wheight + "px",
			position: "absolute",
			display: "none"
		});
		trigger.TransferTo({
			to: hash.w[0],
			className: "essTransfer",
			duration: "normal",
			complete: function() {
				hash.o.show();
				hash.w.show();
				try { hash.f.focus(); } catch (e) {}
			}
		});
		
		return false;
	}
	function ess_hide(hash) {
		hash.o.remove();
		hash.w.fadeOut("normal");
		return false;
	}

	var display = $("#essDisplay");
	$("body").append(display);
	
	display.jqm({
		onShow: ess_show,
		onHide: ess_hide,
		closeClass: "essClose",
		overlayClass: "essOverlay",
		overlay: 20,
		trigger: "a.ess_thumb"
	});
	$("a.ess_thumb img")
		.bind("mouseover", function() {
			$(this).animate({ borderColor: "#666" }, "fast");
		})
		.bind("mouseout", function() {
			$(this).animate({ borderColor: "#eee" }, "fast");
		})
	;
	$("a.ess_thumb")
		.each(function() {
			var idx;
			var self = $(this);
			var href = self.attr("href");
			var desc = self.attr("title");
			
			if (ess_images_map[href] == null) {
				idx = ess_images.length;
				ess_images[idx] = [ href, desc ];
				ess_images_map[href] = idx;
			}
		});
	$("#essButtonPrev").bind("click", ess_prev);
	$("#essButtonNext").bind("click", ess_next);
})
