if (typeof SSB == "undefined") var SSB = new Object();

SSB.init = function ()
{
	SSB.id = $("body").attr("id").match(/BODY_(\d+)/)[1];

	console.log("SSB.init()", SSB.id, typeof SSB["setup_" + SSB.id]);

	SSB.setup_search();
	SSB.setup_nav1();
	
	if (SSB.is_leaf()) {
		SSB.setup_leaf();
		SSB.hunt.setup();
	}

	if (typeof SSB["setup_" + SSB.id] == "function") {
		SSB["setup_" + SSB.id]();
	}

	//$("a[rel='external']").bind("click", function() { window.open($(this).attr("href")); return false; });
	$("a[rel='external']").attr("target", "_blank");
	
	if ($.browser.msie) SSB.ie_fixes();
	
		
	if (SSB.is_leaf()) {
		SSB.preload_large();
	}
}

SSB.setup_search = function ()
{
	console.log("SSB.setup_search()");

	$("#q").one("click", function () {
		$(this).attr("value", "");
	});
}

SSB.setup_leaf = function ()
{
	console.log("SSB.setup_leaf()");

	var n = $("#image-col img.thumbnail, #image-col2 img.thumbnail").size();

	if (n > 1) $("#the-thumbnails").show();

	$("#image-col img.thumbnail, #image-col2 img.thumbnail").bind("click", function() {
		var i = $(this).attr("src").match(/\d\d\d\d_(\d+)_th/)[1];

		if ($("ul#the-images li#image-" + i).attr("class").indexOf("selected") != -1) return;

		$("ul#the-images li.selected").fadeOut();
		$("ul#the-images li#image-" + i).fadeIn();
		
		$("ul#the-images li.selected").toggleClass("selected");
		$("ul#the-images li#image-" + i).toggleClass("selected");
		
		$("img.thumbnail").removeClass("selected");
		$(this).addClass("selected");
	}).css("cursor", "pointer");
	
	if (! $.browser.msie) {
		$("#image-col img.thumbnail, #image-col2 img.thumbnail").hover(function() {
			var alt = $(this).attr("alt");
			var pos = $(this).position();
			$("#the-tooltip").text(alt);
			$("#the-tooltip").css("top", pos.top + 66);
			$("#the-tooltip").css("left", pos.left + 10);
			$("#the-tooltip").show();
		}, function () {
			$("#the-tooltip").hide();
		});
	}

	$("#the-images img").bind("click", function() {
		$("#image-popup").css("height", $(document).height());
		$("#image-popup-holder img").remove();

		$("#image-popup").show(); // height/width of the images is 0 in IE if this popup isn't visible...

		var src = $(this).attr("src");
		var alt = $(this).attr("alt");
		
		src = src.slice(0,-4) + "_LG" + src.slice(-4);
		
		var $img = $("#image-popup-holder").createPrepend("img", { "src": src, "alt": alt });

		$img.bind("load", function () {
			var width = $(this).attr("width"); if (width == 0) width = $(this).width();
			var height = $(this).attr("height"); if (height == 0) height = $(this).height();
			var page_height = $(document).height();
			$("#image-popup").css("height", Math.max(page_height, height+26)); // 826 = 20+3+height+3
			$("#image-popup-holder").css("width", width+6+14);
		});

		if ($.browser.msie) {
			if ($img[0].complete) {
				var width = $($img[0]).attr("width"); if (width == 0) width = $($img[0]).width();
				var height = $($img[0]).attr("height"); if (height == 0) height = $($img[0]).height();
				var page_height = $(document).height();
				$("#image-popup").css("height", Math.max(page_height, height+26)); // 826 = 20+3+height+3
				$("#image-popup-holder").css("width", width+6+14);
			}

			$("#image-popup").css("width", $(document).width() + "px");
		}
	}).css("cursor", "pointer");

	$("#image-col").createAppend("a", { "id": "magnify", "href": "#", "title": "View Larger" }).bind("click", function () {
		$("#the-images li.selected img").trigger("click");
		return false;
	});

	$("#image-popup").bind("click", function() {
		$("#image-popup").hide();
		return false;
	});

	$("#image-popup-holder").bind("click", function() {
		return false;
	});

	$("#image-popup-holder a#close").bind("click", function() {
		$("#image-popup").hide();
		return false;
	});

	$("#the-thumbnails").createAppend("div", { "id": "the-tooltip" });
}

SSB.setup_3600 = function ()
{
	console.log("SSB.setup_3600()");

	$("#image-col2 img.thumbnail").bind("click", function() {
		var i = $(this).attr("src").match(/\d\d\d\d_(\d+)_th/)[1];
		$(".step").hide();
		$("#step-" + i).show();
	}).css("cursor", "pointer");
	
	$("a.goto").bind("click", function() {
		var i = $(this).attr("id").match(/goto-(\d+)/)[1];
		$("img[@src='images/3600_" + i + "_th.png']").trigger("click");
	}).css("cursor", "pointer");
	
	$("a.goto").show();	
	
	// bueller?
	if ($.browser.msie) {
		$("#image-col2 img.thumbnail").removeClass("selected");

		$("#image-col2 img.thumbnail").each(function (i) {
			if (i == 0) {
				$(this).addClass("selected");
			}
		});
	}
}

SSB.setup_10000 = function ()
{
	console.log("SSB.setup_10000()");
	
	$("#thename").attr("value", "");

	$("#cert-form").bind("submit", function () {
		var action = $("#cert-form").attr("action");
		var thename = $("#thename").attr("value");
		var url = action + "?thename=" + escape(thename);
		
		SSB.cert_w = window.open(url, "certificate","resizable=no,status=no,scrollbars=no,width=826,height=660");
		SSB.cert_w.focus();
		
		$("#thename").attr("value", "");
	//	$("#create").css("visibility", "hidden");

		return false;
	});
	
	$("#create").hover(function () {
		$(this).css("background-position", "left top");
	}, function () {
		$(this).css("background-position", "left bottom");
	});
}

SSB.setup_nav1 = function ()
{
	return;

	console.log("SSB.setup_nav1()");
	
	$("ul#nav1 > li").each(function (index) {
		$(this).hover(function () {
			$(this).animate({width: 300, height: 106}, "fast", function () { });
		}, function () {
			$(this).animate({width: 121, height: 69}, "fast", function () { });
		});
	});
}

SSB.preload_large = function ()
{
	var $preload = $("#image-col").createAppend("div", { "id": "preload" });
	
	$("#the-images img").each(function (i) {
		var src1 = $(this).attr("src");
		var src2 = src1.slice(0,-4) + "_LG" + src1.slice(-4);
		console.log("SSB.preload_large()", src2);
		$preload.createAppend("img", { "src": src2 });
	});
}

SSB.is_onecol = function ()
{
	return $("body").attr("class").indexOf("ONECOL") != -1;
}

SSB.is_twocol = function ()
{
	return $("body").attr("class").indexOf("TWOCOL") != -1;
}

SSB.is_leaf = function ()
{
	return SSB.is_onecol() || SSB.is_twocol() || SSB.id == "3600";
}

SSB.ie_fixes = function ()
{
	$(window).resize(function () {
		$("#image-popup").css("width", $(window).width() + "px");
	});
}

