/*
  City Visions Europe
  Copyright (C) 2009 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.

  Changes to these files are PROHIBITED due to license restrictions.
*/



$(function() {
	$("#left h2, #header h1, .articlepreview h1, .article h1 a").css("visibility", "hidden")
	$("#languages li:last").addClass("last");
	$(".text.layout_intro > p:first").addClass("home");
	$(".text.layout_text > *, .text.layout_intro > p:not(:first)")
		.wrapAll("<div></div>").parent()
		.columnize({columns: 2}).children(".column:last").addClass("last");
	$("#comments .column:last, #addcomment .column:last").addClass("last");
	$("a[href^='http://'], .downloads a").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});
	$("#left h2, #header h1, .articlepreview h1, .article h1 a").css("visibility", "");
	$("#left h2").click(function() {
		$(this).parent().toggleClass("closed");
	});
	$(".videos img").click(function() {
		document.video.SetURL($(this).attr("class"));
	});
	$("#comment-form").validate();
});

$(window).load(function() {
	$(".images a, a.preview").click(function() {
		var $a = $(this);
		var $overlay = $('<div id="overlay"></div>')
			.height($(document).height());
		var $lightbox = $('<div id="image"></div>');
		if ($(".images a").length>1) {
			// Setup arrows for clicking left/right through the images
			$lightbox
				.append(
					$('<div id="previous"><img src="/elements/left.gif" alt="" /></div>')
						.click(function() {
							$overlay.click();
							var prev = $a.prev();
							if (prev.length) {
								prev.click();
							} else {
								$(".images a:last").click();
							}
						})
				)
				.append(
					$('<div id="next"><img src="/elements/arrow.gif" alt="" /></div>')
						.click(function() {
							$overlay.click();
							var next = $a.next();
							if (next.length) {
								next.click();
							} else if ($a.hasClass("preview")) {
								$(".images a:eq(1)").click();
							} else {
								$(".images a:first").click();
							}
						})
				)
				.hover(
					function() {
						$("div", this).css("display", "block");
					},
					function() {
						$("div", this).css("display", "");
					}
				);
		}
		$lightbox
			.add($overlay)
			.click(function() {
				$overlay.remove();
				$lightbox.remove();
			});
		$("body")
			.append($overlay)
			.append($lightbox);
		var bigImg = new Image();
		$(bigImg)
			.addClass("full")
			.load(function() {
				// Show lightbox
				var ratio = bigImg.width/bigImg.height;
				if (bigImg.width>$(window).width()-200) {
					bigImg.width = $(window).width()-200;
					bigImg.height = bigImg.width/ratio;
				}
				if (bigImg.height>$(window).height()-100) {
					bigImg.height = $(window).height()-100;
					bigImg.width = bigImg.height*ratio;
				}
				$lightbox
					.width(bigImg.width)
					.height(bigImg.height)
					.css({
						left: ($(window).width()-bigImg.width)/2,
						top: ($(window).height()-bigImg.height)/2+$(window).scrollTop(),
					})
					.prepend(bigImg);
				if ($a.attr("title")) {
					$lightbox.append('<div id="caption"><span>'+$a.attr("title")+'</span></div>');
				}

				// Show zoombox
				if ($a.hasClass("zoomable")) {
					var $img = $(this);
					var $zoombox = $img
						.wrap('<div class="zoombox"></div>')
						.parent()
						.width($img.width())
						.height($img.height())
					$img.css("position", "absolute");
					var zoomedImage = new Image();
					zoomedImage.onload = function() {
						$pos = $zoombox.offset();
						$(zoomedImage)
							.addClass("zoomed")
							.css({
								left: ($zoombox.width()-zoomedImage.width)/2,
								top: ($zoombox.height()-zoomedImage.height)/2
							})
							.draggable({
								containment: [$pos.left+$zoombox.width()-zoomedImage.width, $pos.top+$zoombox.height()-zoomedImage.height, $pos.left, $pos.top]
							});
						$zoombox.append(zoomedImage);
						var $zoomButton = $('<div class="zoomButton">+</div>')
							.click(function() {
								$img.toggle();
								$zoomButton.html(
									$img.filter(":visible").length>0
										? "+"
										: "&ndash;"
								);
								return false;
							});
						$zoombox
							.prepend($zoomButton);
					};
					zoomedImage.src = $a.attr("href").replace("image.", "fullsize.");
				}
			});
		bigImg.src = $a.attr("href");
		return false;
	});
});
