(function($) {
	$.fn.center = function(w) {
		e = this;
		var ew = e.width();
		var eh = e.height();

		if (!w)
			w = $(window);
		var ww = w.width();
		var wh = w.height();

		var left = (ww - ew) / 2;
		var top = (wh - eh) / 2;

		return e.css("left", ((left > 0 ? left : 0) + "px"))
			.css("top", ((top > 0 ? top : 0) + "px"));
	};

	$.fn.loadimg = function(callback) {
		var interval = 300;
		var maxwait = 5000;

		var imgleft = $("img", this);
		// alert("IMGLEFT[" + imgleft.length + "] AT START");

		var checker = function() {
			imgleft = $.grep(imgleft, function(e) { return !$(e).attr("complete"); });
			// alert("IMGLEFT[" + imgleft.length + "]");
			if (maxwait >= 0 && imgleft.length > 0) {
				maxwait -= interval;
				setTimeout(function() { checker(callback); }, interval);
			} else {
				if ($.isFunction(callback))
					callback();
			}
		};
		checker();
		return this;
	};

	$.fn.rebind = function(e, h) {
		return this.unbind(e)
			.bind(e, h);
	};

	$.fn.reclick = function(h) {
		return this.unbind("click")
			.bind("click", h);
	};
})(jQuery);

function softlink(href) {
	var prev = $("#main").children();
	hideinfo();
	showwait();
	var temp = $("#temp");

	temp.load(href, function() {
		temp.loadimg(function() {
			hidewait();
			temp.children().eq(0)
				.detach()
				.appendTo($("#main"))
				.css("opacity", "0.0")
				.fadeTo(3500, 1.0, function() {
					temp.empty();
					prev.remove(); });
			setup();
		});
	});
}

function showwait() {
	hidewait();
	$('<div id="wait"><img src="wait.gif" /></div>')
		.appendTo($("body"))
		.css("position", "absolute")
		.center()
		.show();
}

function hidewait() {
	$("#wait").hide().remove();
}

function showinfo(inner) {
	hideinfo();
	$("#info").empty()
		.load(inner, function(h) {
			$("#info div.info")
				.detach()
				.appendTo("body")
				.css("opacity", "0.0")
				.center()
				.fadeTo(2000, 0.9, function() { });
			$("a.mailto").reclick(function() { this.blur() });
			$("div.xinfo").show();
			$("a.xinfo").reclick(function(e) {
				e.preventDefault();
				this.blur();
				hideinfo(); }); });
}

function hideinfo() {
	$("div.info")
		.stop(true)
		.fadeTo(500, 0.0, function() {
			$("div.info").remove(); });
}

function setup() {
	$("div.centerme").center();
	$(window).resize(function() {
		$("div.centerme").center();
		$("div.info").center();
	});

	$("div.autofade").each(function (i, d) {
		$(d).css("opacity", 0.4)
			.hover(
				function() {
					$(this).stop(true).fadeTo(500, 1.0) },
				function() {
					$(this).stop(true).fadeTo(1000, 0.4) });
	});

	$("a.soft").reclick(function(e) {
		e.preventDefault();
		this.blur();
		softlink(this.href + " div.image");
	});

	$("a.showinfo").reclick(function(e) {
		e.preventDefault();
		this.blur();
		if ($("#info").children().length > 0) {
			hideinfo();
			return;
		}
		showinfo(this.href + " div.info");
	});
}

$(function() { setup(); });


