/*
  Makkink-Bey
  Copyright (C) 2008 by Systemantics, Bureau for Informatics

  Lutz Issler
  Mauerstr. 10-12
  52064 Aachen
  GERMANY

  Web:    www.systemantics.net
  Email:  mail@systemantics.net

  Permission granted to use the files associated with this
  website only on your webserver.
*/



var zIndex = 0;
var loaded = false;
var throbberTimeout = false;

function resize() {
	if (!loaded) {
		return;
	}
	var img = $("#image");
	if (img.length) {
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var title = $("#slidebox h1");
		img.css({
			maxWidth: (windowWidth/2-(windowWidth-$("#text").offset().left)-46-20)*2,
			maxHeight: (windowHeight/2-((title.length ? title.offset().top+title.height() : 0)+20)-46)*2
		});
		var w = img.width()+46*2;
		var h = img.height()+46*2;
		$("#imageContainer").css({
			height: h,
			width: w,
			left: (windowWidth-w)/2,
			top: ($(window).height()-h)/2
		});
		img.css({
			marginLeft: -img.width()/2,
			marginTop: -img.height()/2
		});
		$("#slidebox")
			.height($(document).height())
			.width($(window).width());
		$("#prev, #next")
			.css("top", windowHeight/2)
			.show();
		$("#imageContainer")
			.css("visibility", "visible");
	}
	$("#keywords").css("top", $("#sections").offset().top-$("#list").offset().top+9);
	$("body.myscape").height($(window).height());
}

var linkClicked = false;

function add(imgId) {
	$("#add-link span span").replaceWith("Wait ...");
	$("#add-link").load("/my/add", {img: imgId}, function() {
		$("#add")
			.removeClass("button")
			.attr("onclick", "");
	});
	linkClicked = true;
	return false;
}

$(window).load(function() {
	loaded = true;
	window.clearTimeout(throbberTimeout);
	$("#throbber").remove();
	resize();
});

$(function() {
	$(document).pngFix({altbg: "/elements/back_ie6.png"});

	$("#slidebox")
		.height($(document).height())
		.click(function() {
			if (linkClicked) {
				linkClicked = false;
			} else {
				location.href = $("#close a").attr("href");
			}
	});
	if ($("#image").length && !loaded) {
		throbberTimeout = window.setTimeout(function() {
			$.throbberShow({
				ajax: false,
				image: '/elements/throbber.gif',
				parent: $('<div id="throbber"></div>').appendTo("body")
			});
		}, 100);
	}
	$("#prev img, #next img, #close img, #add img").hover(
		function() {
			this.src = this.src.replace(".gif", "_on.gif");
		},
		function() {
			this.src = this.src.replace("_on.gif", ".gif");
		}
	);
	$("#close img, #add img").hover(
		function() {
			$(this).siblings("span").show();
		},
		function() {
			$(this).siblings("span").hide();
		}
	);
	$("#info:not(.contact)").columnize({columns: 2});
	$(".editable").each(function() {
		zIndex = Math.max(parseInt($(this).css("zIndex")), zIndex);
	});
	$(".myimage.editable").draggable({
		start: function(event, ui) {
			ui.helper.css("zIndex", ++zIndex);
		},
		stop: function(event, ui) {
			var imgId = this.id.substr(3);
			if (ui.position) {
				$.post("/my/update", {
					img: imgId,
					x: ui.position.left,
					y: ui.position.top,
					z: ui.helper.css("zIndex")
				});
			}
		},
		drag: function(event, ui) {
			var width = String(Math.max(Math.min((ui.position.top-50)/500*100, 100), 5))+"%";
			ui.helper.width(width);
		}
	});
	$(".remove").click(function() {
		$(this).html("Wait ...");
		jImg = $(this).parent();
		var imgId = jImg.get(0).id.substr(3);
		$.post("/my/remove", {img: imgId}, function() {
			jImg.remove();
		});
	});
	$("a.external, #credits a").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});
	$("#image").load(resize);
});

$(window).resize(resize);

