$(document).ready(function(){
	var w = $(window).height();
	var above = $("#above").height();
	var slide = $("#slideshow").height();

	$(".promo").draggable();

	// catch!
	$("#navigation-content ul li a").click(function(){});

	if ($("#content-wrap #navigation-content").size()){
		$("#navigation-content ul li:last-child").addClass("last");
	
		$("#content-wrap").tabs({
			/*
			cookie: { expires: 30 },
			fx: [{ opacity: 'toggle', duration: 400 }, // hide option
		 			 { opacity: 'toggle', duration: 200 }], // show option
			*/
			show: function(event, ui){
				contentResize();
			}
		});
	}

	var w = ($(window).width()-960)/2;
	$("#leftlayer, #rightlayer").css({ width: w + "px" });
	
	$("#slideshow #slideshow-wrapper").css({ left: w + "px" });
	
	$("#slideshow #slideshow-wrapper .item:not('.movie')").live("mouseover", function(){
		$(this).stop().animate({ paddingTop: "40px" }, 150).find(".enlarge").css({ backgroundPosition: "center center" });
	}).live("mouseout", function(){
		$(this).stop().animate({ paddingTop: 0 }, 150).find(".enlarge").css({ backgroundPosition: "center -400px" });
	});

	$("table span").live("mouseover", function(){
		$(this).stop().animate({ opacity: 1 });
	}).live("mouseout", function(){
		$(this).stop().animate({ opacity: ".6" });
	});	

	$("#foot a:not('.colophon')").live("mouseover", function(){
		$(this).stop().animate({ opacity: ".8" }, 200);
	}).live("mouseout", function(){
		$(this).stop().animate({ opacity: ".5" }, 200);
	});

	if ($("#home-teaser").size()){
		homeResize();

		$("#home-teaser").cycle({ 
			timeout:	4000,
			before:		onBefore,
			after:		onAfter,
			speed: 		700,
			pager: 		".control",
			pagerAnchorBuilder: function(idx, slide){
      	return '<a href="#">&nbsp;&nbsp;</a>';
			}
		});
	}
	function onBefore(curr, next, opts, fwdFlag){
		$(this).css({ marginTop: "4000px" });
	}
	function onAfter(curr, next, opts, fwdFlag){
		$("#home-teaser .image").removeClass("current");
		$(this).css({ marginTop: 0 }).addClass("current");

		homeResize();
	}

	$("#home-teaser .image a img").live("mouseover", function(){
		$(this).stop().animate({ paddingTop: "40px" }, 200);
	}).live("mouseout", function(){
		$(this).stop().animate({ paddingTop: 0 }, 200);
	});

	$("#slideshow:not('.large') #slideshow-wrapper .item:not('.movie')").click(function(){
		var ndx = ($(this).prevAll().hasClass("movie")) ? $(this).index()-1 : $(this).index();

		$("#navigation, #slideshow, #below").animate({ opacity: 0 }, 100, function(){
			$("body").addClass("dark");
		});
		
		if ($.browser.msie)
			$("#navigation, #slideshow, #below").css({ visibility: "hidden" });

		$("#slideshow #slideshow-wrapper .item").each(function(){
			var imgSrc = $(this).find("img").attr("src");
			var imgTitle = $(this).find("p.title").html();

			if (imgSrc){
				imgSrc = imgSrc.substring(0, imgSrc.length-12);
				$("#gallery").append('<img class="image" src="' + imgSrc + '.jpg" alt="' + imgTitle + '" />');
			}
		});

		detailviewResize();
		$("#gallery").cycle({ 
			timeout:				0,
			before:					onBefore,
			after:					onAfter,
			speed: 					400,
			prev:						"#detail-view .prev",
			next:		 				"#detail-view .next, #detail-view #gallery img",
			startingSlide:	ndx
		});

		function onBefore(curr, next, opts, fwdFlag){
			$(this).css({ marginTop: "4000px" });
		}

		function onAfter(curr, next, opts, fwdFlag){
			$("#gallery img").removeClass("current");
			$(this).css({ marginTop: 0 });

			var index = opts.currSlide;
			$("#detail-view .prev")[index == 0 ? "hide" : "show"]();
			$("#detail-view .next")[index == opts.slideCount - 1 ? "hide" : "show"]();
			
			$(".title").html(next.alt);

			$(this).addClass("current");
			detailviewResize();
		}

		$(document).keypress(function(event) {
		  if (event.keyCode == 37) $("#gallery").cycle("prev");
		  if (event.keyCode == 39) $("#gallery").cycle("next");		  	
		});

		$("#detail-view #gallery").animate({ opacity: 1 }, 700);
		var left = $("#above").offset();		
		$("#detail-view").css({ left: left.left + "px" }).fadeIn(400);

	});

	$("#detail-view a.off").live("click", function(){
		return false;
	});

	$("#detail-view .close").click(function(){
		$("#detail-view #gallery").animate({ opacity: 0 }, 200, function(){
		
			$("#navigation, #slideshow, #below").animate({ opacity: 1 }, 200);
			
			if ($.browser.msie)
				$("#navigation, #slideshow, #slideshow, #below").css({ visibility: "visible" });

			$("#detail-view").removeAttr("style").fadeOut(400);

			$("#gallery").removeAttr("style").empty();
			$("body").removeClass("dark");
		});
		return false;
	});

	$(".your-subject input").attr("value", $("#navigation .sub li.current_page_item a").attr("title"));

	$("#connectors img, #style img, .connectors #overview img").tooltip({
		showURL: false,
		track: true
	});
});
// end $(document).ready(function(){ ... });

$(function(){
	$(window).hashchange(function(){
		var hash = parseInt(location.hash.replace( /^#/, "" ));
		var w = ($(window).width()-960)/2;

		if (!hash)
			hash = 0;

		// Iterate over all nav links, setting the "selected" class as-appropriate.
		var newprevhash = hash-1;
		if (newprevhash == "-1")
			$(".prev").hide();
		else
			$(".prev").show().attr("href", "#" + newprevhash);

		var newnexthash = hash+1;
		if (newnexthash >= $("#slideshow #slideshow-wrapper").children().size()-1)
			$(".next").hide();
		else
			$(".next").show().attr("href", "#" + newnexthash);

		// large gallery
		if ($("#slideshow.large #slideshow-wrapper").children().size())
			if (newnexthash >= $("#slideshow.large #slideshow-wrapper").children().size())
				$(".next").hide();
			else
				$(".next").show().attr("href", "#" + newnexthash);
		
		var ww = 0;
		$("#slideshow #slideshow-wrapper .item").each(function(){
			ww = ww + $(this).find("img, iframe").width() + 1;
			
			if ($(this).index()+1 == hash)
				return false;
		});

		if (!hash || hash == 0)
			ww = 0;

		$("#slideshow #slideshow-wrapper").animate({ left: w-ww + "px" }, { duration: 700 } );

	});

	// Since the event is only triggered when the hash changes, we need to trigger
	// the event now, to handle the hash the page may have loaded with.
	$(window).hashchange();
});

$(window).load(function(){
	// dynamische breite für titel und enlarge p element, 
	// bei unterschiedlich breiten bildern in der slideshow
	$("#slideshow #slideshow-wrapper .item, #home-teaser .image").each(function(){
		var w = $(this).find("img.image").width();

		$(this).find("p.title").css({ width: w-30 + "px" });
		$(this).find("p.enlarge").css({ width: w + "px" });
	});
	
	homeResize();
	galleryResize();
});

$(window).resize(function(){
	homeResize();
	detailviewResize();
	contentResize();
	galleryResize();
});

function homeResize(){
	var h = $(window).height();
	var nh = (h <= 845) ? h-220 : 630;
	nh = (nh < 315) ? 315 : nh;

	$("#home-teaser, #home-teaser .image, #home-teaser .image img").css({ height: nh +"px" });

	var w = $("#home-teaser .image.current img").width();

	if (w != $("#home-teaser .image.current").width())
		var nw = $("#home-teaser .image.current").width()-w;

	var h = $("#home-teaser .image.current img").height();

	if (h != $("#home-teaser .image.current").height())
		var nh = $("#home-teaser .image.current").height()-h;	

	if (w && nw && h && nw){
		$("#home-teaser .image").css({ left: 480-(w/2) + "px", width: nw +"px", height: nh + "px" });
		$("#home-teaser .image div.teasertext h2").css({ width: w + "px" });
	}
	
	if ($("#home-teaser .image.current img").offset())
		$(".promo").stop().animate({ left: $("#home-teaser .image.current img").offset().left-75 + "px", top: "150px" }, 300);
}

function contentResize(){
	var w = $(window).height();
	var above = $("#above").height();
	var slide = $("#slideshow").height();
	
	if ($("#above").offset()){
		var left = $("#above").offset();
		$("#detail-view").css({ left: left.left + "px" });
	}

	//
	var w = ($(window).width()-960)/2;
	$("#leftlayer, #rightlayer").css({ width: w + "px" });
	$("#slideshow #slideshow-wrapper").css({ left: w + "px" });

		var hash = parseInt(location.hash.replace(/^#/, ""));
		var ww = 0;
		$("#slideshow #slideshow-wrapper .item").each(function(){
			ww = ww + $(this).find("img, iframe").width() + 1;
			
			if ($(this).index()+1 == hash)
				return false;
		});

		if (!hash || hash == 0)
			ww = 0;

	$("#slideshow #slideshow-wrapper").animate({ left: w-ww + "px" }, { duration: 0 } );
}

function galleryResize(){
	var h = $(window).height();
	var nh = (h <= 800) ? h-200 : 600;
	nh = (nh < 315) ? 315 : nh;
	
	$("#slideshow.large, #slideshow.large .item, #slideshow.large .item img").css({ height: nh +"px", width: "auto" });

	var w = $("#slideshow.large .item").width();
	$("#slideshow.large .item p").css({ width: w -30 +"px" });
}

function detailviewResize(){
	var h = $(window).height();
	var nh = (h <= 845) ? h-220 : 630;
	nh = (nh < 315) ? 315 : nh;
	
	$("#gallery").css({ height: nh +"px" });
	$("#gallery img, #gallery img.current").css({ height: nh +"px", width: "auto" });

	$("#gallery img.current").livequery(function(){
		var w = $("#gallery img.current").width();
		if (w>0)
			$("#gallery img.current").css({ left: 480-(w/2) + "px" });
	});
}

function UnCryptMailto(s){
	var n = 0;
	var r = "";
	for (var i = 0; i < s.length; i++){
		n = s.charCodeAt(i);
		if (n >= 8364)
			n = 128;

			r += String.fromCharCode(n-1);
	}
	return r;
}

function linkTo_UnCryptMailto(s){
	location.href=UnCryptMailto(s);
}
