(function($) {

	$.fn.rebind = function(e, h) {
		this.unbind(e)
			.bind(e, h);
	};

	$.fn.reclick = function(h) {
		this.rebind("click", h);
	};

	$.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;
	};

})(jQuery);

function updthumbs(here) {
	var thumbs = $("div.thumbs").first();
	$(".thumb-current", thumbs)
		.removeClass("thumb-current")
		.addClass("thumb");
	here = unescape(here.replace(/.*\//, ""));
	var all_a = $("a", thumbs);
	all_a.each(function(i, a) {
		a = $(a);
		var thishref = unescape(a.attr("href").replace(/.*\//, ""));
		// alert(here + "\n" + thishref);
		if (here === thishref) {
			thumbs.data("current", i);
			a.closest("span").removeClass("thumb").addClass("thumb-current");
			var fi = $("img", all_a.first());
			var li = $("img", all_a.last());
			var wg = li.position().left - fi.position().left + 2 * li.width();
			var ws = thumbs.width();
			var newleft = i * (wg - ws) / all_a.length;
			thumbs.stop().animate({ scrollLeft: newleft }, 2000);
			// alert("SCROLL " + newleft + "/" + wg + " " + ws + "-" + wg);
		}
	});
}

function softlink(href) {
	href = href.replace(/ /g, "%20");
	// alert("SOFT " + href);
	updthumbs(href);
	$("#softlinktemp").remove();
	var targetsel = "div.middle-single";
	var target = $(targetsel).first().css("position", "relative");
	target.css("height", target.height());
	var temp = $("<div id='softlinktemp' style='display: none'></div>");
	temp.appendTo($("body"));
	var prevcontent = target.children();
	prevcontent.css({ position: "absolute", left: 0, top: 0 });
	temp.load(href + " " + targetsel, function() {
		var tgt = temp.children().eq(0).children().eq(0);
		tgt.detach()
			.appendTo(target)
			.css({ opacity: 0.0, left: 0, top: 0 })
			.fadeTo(700, 1.0, function() {
				temp.remove();
				// tgt.css("position", "static");
				prevcontent.remove();
				var currslideid = $("div.thumbs").data("sliding");
				if (currslideid) {
					setTimeout(function() { shownext(currslideid); }, 5000);
					// $(".slideshow").html("#"); // STOP symbol
				}
			});
		setup();
	});
}

function nexthref() {
	var thumbs = $("div.thumbs").first();
	var all_a = $("div.thumbs a");
	var next = thumbs.data("current");
	if (next === undefined) {
		next = 1;
	} else {
		next = (next + 1) % all_a.length;
	}
	// alert("NEXT " + next + " " + all_a.eq(next).attr("href"));
	var ret = all_a.eq(next).attr("href");
	if (ret) {
		ret = ret.replace(/ /g, "%20");
	}
	return ret;
}

function preloadnext() {
	$("#preloadtemp").remove();
	var href = nexthref();
	if (!href) {
		return;
	}
	$("<div id='preloadtemp' style='display: none'></div>")
		.appendTo($("body"))
		.load(href + " img.main");
}

function shownext(slideid) {
	// alert("SHOWNEXT(" + slideid + ")");
	var thumbs = $("div.thumbs").first();
	var currslideid = thumbs.data("sliding");
	if (!currslideid || currslideid != slideid) {
		// alert("CANCEL");
		return;
	}
	softlink(nexthref());
}

function toggleslideshow() {
	var thumbs = $("div.thumbs").first();
	var slide = $(".slideshow").first();
	if (thumbs.data("sliding")) {
		// alert("TOGGLE FROM TRUE");
		thumbs.data("sliding", false);
		slide.html("&#x25b6;"); // PLAY symbol
	} else {
		// alert("TOGGLE FROM FALSE");
		var newslideid = $.now();
		thumbs.data("sliding", newslideid);
		slide.html("&#x25a0;"); // STOP symbol
		shownext(newslideid);
	}
}

function adjustmainsize() {
	return; // disabled, doesn't work yet
	var main = $("table.maintable");
	if (main.length < 1)
		return;
	setTimeout(function() {
		var bh = $("body").height();
		var wh = $(window).height() - 20;
		var mh = main.data("adjheight") || main.height();
		var newmh = mh + wh - bh;
		if (newmh < 100)
			newmh = 100;
		console.log("HEIGHT: body=" + bh + " window=" + wh + " main=" + mh + " new=" + newmh);
		main.data("adjheight", newmh).height(newmh);
	}, 1);
}

function setup() {
	$("a.soft").reclick(function(e) {
		e.preventDefault();
		$(this).blur();
		// alert($(this).attr("href"));
		var thumbs = $("div.thumbs").first();
		if (thumbs.data("sliding"))
			toggleslideshow();
		var href = $(this).attr("href");
		softlink(href);
	});
	$(".slideshow").reclick(toggleslideshow);
	if ($("div.thumbs").data("sliding")) {
		$(".slideshow").html("&#x25a0;"); // STOP symbol
	}
	$(window).rebind("resize", function() { adjustmainsize() });
	adjustmainsize();
	preloadnext();
}

$(function() { setup(); });


