$(document).ready(function()
{
	if($.browser.msie && parseInt($.browser.version) <= 6)
	{ return false; }
	
	initNavi();
	/* init Events in content and bubbles */
	var thisParent = thisParent || $("body");
	initEvents(thisParent);
	
});

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

jQuery.jQueryRandom = 0;

/** * jQuery.rand v1.0 * * Randomly filters any number of elements from a jQuery set. * * MIT License: @link http://www.afekenholm.se/license.txt * * @author: Alexander Wallin (http://www.afekenholm.se) * @version: 1.0 * @url: http://www.afekenholm.se/jquery-rand */

(function($){
	$.fn.rand = function(k){ var b = this, n = b.size(),k = k ? parseInt(k) : 1;
	if (k > n) return b.pushStack(b); else if (k == 1) return b.filter(":eq(" + Math.floor(Math.random()*n) + ")");
	r = b.get(); for (var i = 0; i < n - 1; i++) { var swap = Math.floor(Math.random() * (n - i)) + i; r[swap] = r.splice(i, 1, r[swap])[0]; } r = r.slice(0, k);
	return b.filter(function(i){ return $.inArray(b.get(i), r) > -1; }); }; })(jQuery);

/* Strings fuer selektoren parsen. */
function addShlashes()
{
	return this.replace(/(\[|\]|\.|\:|\+)/g, "\\$1");
}
String.prototype.addShlashes = addShlashes;
/* /Strings ... */

/* jQuery um regex-Filter erweitern. */
jQuery.extend(
	jQuery.expr[':'],
	{
		regex: function(a, i, m, r)
		{
			var r = new RegExp(m[3], 'i');
			return r.test(jQuery(a).text());
		}
	}
);

/* Basis Session für jQuery */
jQuery.session = function(thisVar, thisVal)
{
	if(typeof thisVal != 'undefined')
	{
		document.cookie = thisVar + "=" + encodeURIComponent(thisVal) + "; path=/";
	}
	else
	{ 
		if(document.cookie && document.cookie != "")
		{
			var cookies = document.cookie.split(";");
			for (var i = 0; i < cookies.length; i++)
			{
				var cookie = jQuery.trim(cookies[i]).split("=");
				if(cookie[0] == thisVar)
				{ return cookie[1]; }
			}
		}
		return "";
	}
};

/* /jQuery ... */


/* Init basic events and effects */
function initEvents(thisParent)
{
	initStage();
	initArchive();
	initButtons(thisParent);
	initSelects(thisParent);
	initLinks(thisParent);
//	initPlayer(thisParent);
	initSamples(thisParent);
	initImgScroller(thisParent);
	initVAlign(thisParent);
	initDetails(thisParent);	
	initSkipLists(thisParent);	
	initSlideshow(thisParent);
	initEcardSlider();
	initEcardRandomizer(thisParent);	
	initRegister(thisParent);
	initShader();
	initImageCaption();	
	initInputSample(thisParent);
	initExtLinks();
	initPrintView();
}

// Button Abstände und Größe anpassen
function initButtons(thisParent)
{
	thisParent.find("button").each(function()
	{
		var thisButton = $(this);
		var thisArea = thisButton.children("div,strong,img");
		if(thisArea.length == 1)
		{ 
			thisButton.css({
				width: thisArea.get()[0].offsetWidth,
				height: thisArea.get()[0].offsetHeight
			}); 
			thisArea.css({
				position: "absolute",
				left: 0,
				top: 0
			}); 
			thisArea.css({
				left: (thisButton.offset().left - thisArea.offset().left),
				top: (thisButton.offset().top - thisArea.offset().top)
			});
		}
		thisArea = thisButton.children("span");
		if(thisArea.length == 1)
		{ 
			thisArea.css({
				whiteSpace: "nowrap"
			}); 
			thisButton.css({
				width: thisArea.get()[0].offsetWidth
			}); 
			thisArea.css({
				position: "relative",
				left: (thisButton.offset().left - thisArea.offset().left)
			});
		}
	});
}

// Select Felder ersetzen
function initSelects(thisParent)
{
	if(!thisParent)
	{ var thisParent = $("body"); }
 
	thisParent.find("select").each(function()
	{
		var thisSelect = $(this);
		// Init Fakeselect 
		thisSelect.before("<div class=\"select\"><div class=\"value\"></div></div>");
		var thisFSelect = thisSelect.prev();
		
		// alert(thisSelect);
		
		thisFSelect.attr("unselectable","on");
		thisFSelect.css({
			width:							thisSelect.width(),
			position: 					"absolute",
			zIndex: 						"1",
			display:						"inline",
			marginTop:					"-1",
			backgroundRepeat: 	"no-repeat",
			backgroundPosition: "right top",
			cursor: 						"default",
			MozUserSelect:			"none",
			KhtmlUserSelect:		"none"
		});
		
		var thisValue = thisFSelect.children(".value");
		thisValue.css({
			width: 			thisValue.width() + "px", // Auto -> fixed
			height:			thisFSelect.css("height"),
			lineHeight:	thisFSelect.css("height"),
			position:		"relative",
			overflow:		"hidden",
			whiteSpace: "nowrap",
			textIndent:	"4px"
		});

		if(thisSelect.children("option[selected]").text())
		{ thisValue.html(thisSelect.children("option[selected]").text()); }
		else
		{ thisValue.html(thisSelect.children("option:first").text()); }
		thisValue.after("<div class=\"options\"></div>");
		thisSelect.css("visibility", "hidden");
		var thisOptions = thisValue.next();
		thisOptions.css({
			left: 			"-" + thisFSelect.css("border-left-width"),
			position: 	"absolute",
			zIndex: 		"1",
			overflow: 	"auto",
			whiteSpace: "nowrap",
			textIndent: "4px"
		});

		thisOptions.html( thisSelect.html().replace(/\n/gi, "").replace(/\r/gi, "").replace(/<option/gi, "<div class=\"option\"").replace(/<\/option>/gi, "</div>\n").replace(/ value="[^"]*"/gi, "").replace(/<!--.*-->/gi, ""));
		thisOptions.children(".option").css({
			position: "relative",
			float: "left",
			clear: "left",
			overflow: "visible"
		});

		var longestOption = 0;
		thisOptions.children(".option").each(function(){
			var thisOption = $(this);
			if(longestOption < thisOption.width())
			{ longestOption = thisOption.width(); }
		});

		thisOptions.children(".option").css("float", "none");
		
		var scrollWidth = 0;
		if(thisOptions.height() > 200)
		{ 
			scrollWidth = 25;
			thisOptions.height(200); 
		}
		thisOptions.width("500%");
		if(thisOptions.width() > longestOption)
		{ thisOptions.width(longestOption + scrollWidth); }
		if(thisOptions.width() < thisFSelect.width())
		{ thisOptions.width("100%"); }
		thisOptions.hide(0);
		
		thisFSelect.click(function(){ fakeSelect($(this)); return false; });
		thisOptions.children(".option").click(function(){ fakeSelectChoice($(this)); return false; });
		$(document).click(function(){ $(".options").slideUp(100); });
	});
}

// Select Feld Dropdowns
function fakeSelect(thisFSelect)
{
	var thisOptions = thisFSelect.children(".options");
	var thisValue = thisFSelect.children(".value");
	if(thisOptions.is(":hidden"))
	{
		$(".options").slideUp(100);
	
		$(".select").css("z-index", 1);
		thisFSelect.css("z-index", 10);
		thisOptions.slideDown(100);
	}
	else
	{ thisOptions.slideUp(100); }
		
}

// Select Feld Auswahl
function fakeSelectChoice(thisOption)
{
	var thisOptions = thisOption.parent(".options");
	var thisFSelect = thisOptions.parent(".select");
	var thisValue = thisOptions.siblings(".value");
	var thisSelect = thisFSelect.next();
	var thisOptionIndex = thisOptions.children(".option").index(thisOption);

	thisOptions.children(".option").removeClass("selected");
	thisOption.addClass("selected");
	thisValue.html(thisOption.html());
	thisSelect.get(0).selectedIndex = thisOptionIndex;
	thisSelect.trigger("change");
	thisSelect.trigger("onclick");
	thisOptions.slideUp(100);

}

// Linkgestaltung
function initLinks(thisParent)
{
	thisParent.find("a.more,button>span,#microsite ul.linklist li a").each(function()
	{
		var thisE = $(this);
		thisE.addClass("js");
		thisE.append("<span>&nbsp;»</span>");
	});
}

// Eingabefeld Hinweise parsen
function initSamples(thisParent)
{
	if(!thisParent)
	{ var thisParent = $("body"); }
 
	thisParent.find("label.sample,span.sample").each(function()
	{
		var thisSample = $(this);
		thisSample.hide();
		if(thisSample.attr("for") && thisSample.attr("for") != "")
		{ var thisInput = $("#" + thisSample.attr("for")); }
		else 
		{ var thisInput = thisSample.siblings("input,textarea"); }
		if(thisInput.val() == "")
		{ thisInput.val(thisSample.text()); }
		thisInput.focus(function()
		{
			if(thisInput.val() == thisSample.text())
			{ thisInput.val(""); }
		});
		thisInput.parents("form").submit(function()
		{
			if(thisInput.val() == thisSample.text())
			{ thisInput.val(""); }
		});
	});
	
}

/* Init Navi Layout and mouseover */
function initNavi(thisNavi)
{
	var thisNavi = thisNavi || $("#navi,#metanavi");

// Nach launch einkommentieren! 
//	$("#subnavi").hide();
 
	thisNavi.find("a").each(function()
	{
		var thisLink = $(this);
		var thisLink2 = $("#subnavi").find("a[href='" + thisLink.attr("href") +"']").filter("a:contains(" + thisLink.text() + ")");
		
		if(thisLink2.length == 1)
		{
			var thisParent = thisLink.parent("span,strong");
			var thisParent2 = thisLink2.parent("span,strong");
			var thisList = thisParent2.next("ul");
			
			if(thisList.length == 1)
			{
				thisParent.after("<div class=\"box\"></div>");
				var thisBox = thisParent.next(".box");
				thisBox.hide();
				thisBox.css({
					top: thisLink.height()
				});
				thisBox.append(thisList.clone());
				thisBox.find("ul li ul").remove();
				var thisTimer = false;
				
				// add sub-submenue
				initNavi(thisBox);
				if(thisLink.parents("div.box").length > 0) // add "»" as hint
				{ thisLink.append("<span class=\"haschilds\">»</span>"); }
				
				thisParent.add(thisBox).not(":animated").hover(
					function()
					{
						window.clearTimeout(thisTimer);
						thisBox.css("z-index", 2);
						thisTimer = window.setTimeout(function()
						{
							thisBox.slideDown(500);
						},
						500); 
					},
					function()
					{
						window.clearTimeout(thisTimer);
						thisBox.css("z-index", 1);
						thisTimer = window.setTimeout(function()
						{
							thisBox.slideUp(500);
						},
						500); 
					}
				);
			}
		}
	});
}

function initStage()
{
	$("#stage").addClass("js");
	var stageBackground = $("#stage").css("background-color");
	var contentWidth = $("#stage").width() - $("#stage h2:first").outerWidth() * $("#stage h2").length;
	
	$("#stage .stagecontent").css({
		display: "block",
		width: contentWidth + "px"
	});

	$("#stage h2").wrapInner("<span></span>").css({
		background: stageBackground
	});
	$("#stage h2:first").css("background-color", $("#stage .stagecontent:first").css("background"));
		
	

	/* placing pictures and texts */
	$("#stage").find(".textbox,.imgbox").css({
		marginTop: 0,
		marginBottom: 0,
		paddingTop: 0,
		paddingBottom: 0,
		top: "auto",
		bottom: "auto"
	});
	$("#stage").find(".textbox,.imgbox").each(function()
	{
		$(this).css({
			top: Math.round(($("#stage").height() - $(this).height()) / 2) + "px"
		});
	});


	$("#stage h2").each(function(i)
	{
		var thisTrigger = $(this);
		var thisContent = thisTrigger.next(".stagecontent");
		
		/* removing a href */
		thisTrigger.children("span").text(thisTrigger.text());
		/* switching places */
		$("#stage").prepend(thisContent); // reverse order of pages.
		thisContent.after(thisTrigger); // trigger after content.

		thisTrigger.click(function()
		{
			if(stageTimer)
			{ window.clearTimeout(stageTimer); }
			if(!thisContent.is(":visible") && $("#stage .stagecontent:animated").length == 0)
			{
				var openContent = $("#stage .stagecontent:visible");
				if(openContent.find("object").length > 0 && openContent.find("object").get(0).sendEvent)
				{
					openContent.find("object").get(0).sendEvent("STOP");
				}

				/* color */
				$("#stage h2").css("background-color", stageBackground); // reset all
				thisTrigger.css("background-color", thisContent.css("background-color")); 
				
				/* fixing panels on right side (float width problem) */
				if(thisContent.index("#stage .stagecontent") > openContent.index("#stage .stagecontent"))
				{
					var lastIndex = thisContent.index("#stage .stagecontent");
					$("#stage").css("background-color", thisContent.css("background-color"));
				}
				else
				{ 
					var lastIndex = openContent.index("#stage .stagecontent"); 
					$("#stage").css("background-color", openContent.css("background-color"));
				}
				$("#stage h2:gt(" + (lastIndex - 1) + ")").each(function(i)
				{
					var thisRight = $(this).outerWidth() * ($("#stage h2:gt(" + lastIndex + ")").length - i);
					$(this).css({ 
						position: "absolute", 
						right: thisRight
					});
				});
				
				openContent.animate({ width: 0 }, 1000, function(){ openContent.hide(); $("#stage h2").css({ position: "relative", right: "auto" }); });
				thisContent.show().animate({ width: contentWidth }, 1000);
			}
			
			thisTrigger.blur();

			/* Loop Autoanimation */
			stageAutoAnim(8000);

			return false;
		});
	});
	// fixing layouts
	$("#stage .deco *,#stage .teaser *").each(function()
	{
		$(this).css({
			width: $(this).width() 
		});
	});
	$("#stage .deco,#stage .teaser").css({
		overflow: "hidden"
	});
		
	/* At last: Closing all after first page */ 
	$("#stage h2:eq(0)").css("background-color", $("#stage .stagecontent:eq(0)").css("background-color"));
	$("#stage .stagecontent:gt(0)").hide().css({ width: 0	});
	$("#stageheader").remove();
	
	/* Start Autoanimation */
	stageAutoAnim(8000);
}

var stageTimer = false;
function stageAutoAnim(delay)
{
	if(stageTimer)
	{ window.clearTimeout(stageTimer); }
	stageTimer = window.setTimeout(
		function(){
			var openContent = $("#stage .stagecontent:visible");
			var thisTrigger = openContent.next("h2").next(".stagecontent").next("h2");
			if(thisTrigger.length == 0)
			{ thisTrigger = $("#stage h2:first"); }
			thisTrigger.click();
		},
		delay
	);
}

/* IMGSCROLLER - SCROLLABLE LIST */
function initImgScroller(thisParent)
{
	thisParent.find(".imgscroller").each(function()
	{
		var thisScroller = $(this);
		var thisImages = thisScroller.children("ul.images");
		thisScroller.addClass("js");
		
		var maxLoad = 20;
		thisImages.children("li").find("img").each(function(i)
		{
			var thisImg = $(this);
			thisImg.attr("alt", "");
			thisImg.attr("title", "");
			thisImg.attr("saveheight", thisImg.attr("height"));
			thisImg.attr("savesrc", thisImg.attr("src"));
			thisImg.removeAttr("src");
		});
		
		var imgCount = 5;

		while(thisImages.children("li").size() < imgCount + 1)
		{
			thisImages.append(thisImages.children("li").clone());
		}
		thisScroller.prepend("<div unselectable=\"on\" class=\"scroller left\">«</div>");
		thisScroller.append("<div unselectable=\"on\" class=\"scroller right\">»</div>");

		var baseImgCount = thisImages.children("li").length; 
		// move current to center
		if(baseImgCount > imgCount && thisImages.children("li.current").length > 0)
		{
			while(thisImages.children("li").index(thisImages.children("li.current")) < parseInt(imgCount / 2))
			{ thisImages.prepend(thisImages.children("li:last")); }
			while(thisImages.children("li").index(thisImages.children("li.current")) > parseInt(imgCount / 2))
			{ thisImages.append(thisImages.children("li:first")); }
		}
	 
		thisImages.find("img:lt(" + maxLoad + ")").each(function(i)
		{
			var loadImg = $(this);
			if(loadImg.attr("savesrc"))
			{ loadImg.attr("src", loadImg.attr("savesrc")); }
		});

		thisImages.find("img:gt(" + (thisImages.children("li").length - maxLoad) + ")").each(function(i)
		{
			var loadImg = $(this);
			if(loadImg.attr("savesrc"))
			{ loadImg.attr("src", loadImg.attr("savesrc")); }
		});

		// hide non-visible images, init scroller
		thisImages.children("li:gt(" + (imgCount - 1) + ")").css("width", "0").css("top", thisScroller.height() / 2).css("margin", "0").hide();

		var thisImgWidth = thisImages.children("li:eq(0)").width();
		if (imgCount == 5)
		{
			thisImages.children("li:eq(0)").css({width: thisImgWidth * 0.6, top: (thisScroller.height() - thisImages.children("li:eq(0)").find("img").attr("saveheight") * 0.6) / 2});
			thisImages.children("li:eq(1)").css({width: thisImgWidth * 0.8, top: (thisScroller.height() - thisImages.children("li:eq(1)").find("img").attr("saveheight") * 0.8) / 2});
			thisImages.children("li:eq(2)").css({width: thisImgWidth * 1.0, top: (thisScroller.height() - thisImages.children("li:eq(2)").find("img").attr("saveheight") * 1.0) / 2});
			thisImages.children("li:eq(3)").css({width: thisImgWidth * 0.8, top: (thisScroller.height() - thisImages.children("li:eq(3)").find("img").attr("saveheight") * 0.8) / 2});
			thisImages.children("li:eq(4)").css({width: thisImgWidth * 0.6, top: (thisScroller.height() - thisImages.children("li:eq(4)").find("img").attr("saveheight") * 0.6) / 2});
		}

		thisImages.css({
			left: (thisScroller.width() - thisImages.width() - parseInt(thisImages.children("li:eq(0)").css("margin-left"))) / 2
		});

		thisScroller.children(".scroller.left").css({
			left: ((thisScroller.width() - thisImages.width()) / 2 - thisScroller.children(".scroller.left").width()) / 2,
			top: (thisScroller.height() - thisScroller.children(".scroller.left").height()) / 2
		});
		
		thisScroller.children(".scroller.right").css({
			right: ((thisScroller.width() - thisImages.width()) / 2 - thisScroller.children(".scroller.left").width()) / 2,
			top: (thisScroller.height() - thisScroller.children(".scroller.left").height()) / 2
		});
		
		thisScroller.attr("unselectable","on");
		thisScroller.css({
			MozUserSelect: "none",
			KhtmlUserSelect: "none"
		});

		thisScroller.children(".scroller.left").click(function()
		{
			var loadImg = thisImages.children("li").children("img:eq(" + (thisImages.children("li").length - maxLoad) + ")");
			if(loadImg.attr("savesrc"))
			{ loadImg.attr("src", loadImg.attr("savesrc")); }
			scrollImgScroller($(this), "left", imgCount);
			return false;
		});
		thisScroller.children(".scroller.right").click(function()
		{
			var loadImg = thisImages.children("li").children("img:eq(" + maxLoad + ")");
			if(loadImg.attr("savesrc"))
			{ loadImg.attr("src", loadImg.attr("savesrc")); }
			scrollImgScroller($(this), "right", imgCount);
			return false;
		});		
	});
}

function scrollImgScroller(thisLink, direction, imgCount)
{
	var thisScroller = thisLink.parents(".imgscroller");
	var thisImages = thisScroller.children(".images");
	var thisImgWidth = thisImages.children("li:eq(2)").width();
	
	if(thisImages.has("li:animated").length == 0)
	{
		if(direction == "left")
		{
			thisImages.prepend(thisImages.children("li:last").show());
			thisImages.children("li:eq(" + imgCount + ")").animate({width:		"0", top: thisScroller.height() / 2, marginLeft:	"0px"}, 400, function(){ $(this).hide(); });
			if (imgCount == 5)
			{
				thisImages.children("li:eq(0)").animate({width: thisImgWidth * 0.6, top: (thisScroller.height() - thisImages.children("li:eq(0)").find("img").attr("saveheight") * 0.6) / 2, marginLeft: "20px"});
				thisImages.children("li:eq(1)").animate({width: thisImgWidth * 0.8, top: (thisScroller.height() - thisImages.children("li:eq(1)").find("img").attr("saveheight") * 0.8) / 2});
				thisImages.children("li:eq(2)").animate({width: thisImgWidth * 1.0, top: (thisScroller.height() - thisImages.children("li:eq(2)").find("img").attr("saveheight") * 1.0) / 2});
				thisImages.children("li:eq(3)").animate({width: thisImgWidth * 0.8, top: (thisScroller.height() - thisImages.children("li:eq(3)").find("img").attr("saveheight") * 0.8) / 2});
				thisImages.children("li:eq(4)").animate({width: thisImgWidth * 0.6, top: (thisScroller.height() - thisImages.children("li:eq(4)").find("img").attr("saveheight") * 0.6) / 2});
			}
				
		}
		else if(direction == "right")
		{
			thisImages.children("li:eq(" + imgCount + ")").show();
			thisImages.children("li:eq(0)").animate({width:		"0", top: thisScroller.height() / 2, marginLeft:	"0px"}, 400, function(){ $(this).hide(); });
			if (imgCount == 5)
			{
				thisImages.children("li:eq(1)").animate({width: thisImgWidth * 0.6, top: (thisScroller.height() - thisImages.children("li:eq(1)").find("img").attr("saveheight") * 0.6) / 2});
				thisImages.children("li:eq(2)").animate({width: thisImgWidth * 0.8, top: (thisScroller.height() - thisImages.children("li:eq(2)").find("img").attr("saveheight") * 0.8) / 2});
				thisImages.children("li:eq(3)").animate({width: thisImgWidth * 1.0, top: (thisScroller.height() - thisImages.children("li:eq(3)").find("img").attr("saveheight") * 1.0) / 2});
				thisImages.children("li:eq(4)").animate({width: thisImgWidth * 0.8, top: (thisScroller.height() - thisImages.children("li:eq(4)").find("img").attr("saveheight") * 0.8) / 2});
				thisImages.children("li:eq(5)").animate({width: thisImgWidth * 0.6, top: (thisScroller.height() - thisImages.children("li:eq(5)").find("img").attr("saveheight") * 0.6) / 2, marginLeft: "20px"}, function(){ thisImages.append(thisImages.children("li:first")); });
			}
		}
	}
}

function initRegister(thisParent)
{
	thisParent.find(".registerbox").each(function()
	{
		var thisRegisterBox = $(this);
		thisRegisterBox.addClass("js");
		if(thisRegisterBox.find(".listbox").length > 0)
		{ thisRegisterBox.addClass("listboxes"); }
		thisRegisterBox.children("h2:first-child,h3:first-child").addClass("on");
		thisRegisterBox.children("h2,h3").each(function(i)
		{
			var thisTrigger = $(this);
			if(thisTrigger.is("h2,h3"))
			{ 
				var thisRegister = thisTrigger.next(".registercontent"); 
				if(i > 0)
				{ thisRegisterBox.children(".registercontent:eq(0)").before(thisTrigger); }
				thisTrigger.wrapInner("<span></span>");
			}
			else
			{ var thisRegister = $(thisTrigger.attr("href")); }
			
			thisRegister.openHeight = thisRegister.height(); 
			if(i > 0)
			{ thisRegister.hide(); }

			thisTrigger.attr("unselectable","on");
			thisTrigger.css({
				MozUserSelect: "none",
				KhtmlUserSelect: "none",
				cursor: "default"
			});

			thisTrigger.click(function()
			{
				var openRegister = thisRegisterBox.children(".registercontent:visible");
				if(thisRegister.is(":hidden") && thisRegisterBox.children(".registercontent:animated").length == 0)
				{
					var openRegister = thisRegisterBox.children(".registercontent:visible");
					var newHeight = thisRegister.openHeight - openRegister.height() + thisRegisterBox.height();
	
					thisRegisterBox.find("h2.on,h3.on").removeClass("on");
					thisTrigger.addClass("on");
					if(openRegister.find("object").length > 0 && openRegister.find("object").get(0).sendEvent)
					{
						openRegister.find("object").get(0).sendEvent("STOP");
					}
					thisRegisterBox.css({
						height: thisRegisterBox.height()
					}); 
					openRegister.slideUp(500);
					thisRegister.slideDown(500);
					thisRegisterBox.animate({
						height: newHeight
					}, 500); 
				}
				thisTrigger.blur();
				return false;
			});
		});
	});
}

function initVAlign(thisParent)
{
	thisParent.find("blockquote").each(function()
	{
		var thisQuote = $(this);
//		if(thisQuote.height() < 102)
		{
			thisQuote.css({
				paddingTop: (thisQuote.parent().height() - thisQuote.height() - thisQuote.next("cite").height()) / 2 
//				marginBottom: (102 - thisQuote.height()) / 2 * 0.7 
			});
		}
	});
	thisParent.find(".mediabox").each(function()
	{
		var thisDiff = parseInt($(this).css("height")) - parseInt($(this).children("object").height());
		if(thisDiff > 0 && $(this).children("object").siblings().length == 0)
		{
			$(this).children("object").css("margin-top", thisDiff / 2);	
		} 
	});
}

function initDetails(thisParent)
{
	$("div.folderlinks").css("visibility", "visible");
	thisParent.find("div.folderlinks>a").each(function(i)
	{
		var thisLink = $(this);
		var thisInfo = $(thisLink.attr("href")); 
		var moreInfo = thisInfo.children("div").addClass("wrapper");
		
		thisInfo.children(":not(div)").remove();
		moreInfo.after("<div class='bottom'></div>");
			
		var maxHeight = 0;
		thisInfo.find(".teaser").each(function()
		{
			var thisTeaser = $(this);
			if(thisTeaser.height() > maxHeight)
			{ maxHeight = thisTeaser.height(); }
		});
		thisInfo.find(".teaser").css({
			height: maxHeight + "px"
		});
		
		thisInfo.addClass("js").hide();
		thisLink.parents(".folderlinks").append(thisInfo);
		
		thisInfo.find(".teaser:gt(" + (thisInfo.find(".teaser").length - 1 - parseInt(thisInfo.find(".teaser").length / 2)) + ")").css("background", "none");
		thisLink.click(function(e)
		{
			$(thisParent).click(function(e)
			{
				thisLink.removeClass("on");
				thisInfo.hide();
			});
				
			if(thisLink.hasClass("on"))
			{
				thisLink.removeClass("on");
				thisInfo.hide();
			}
			else
			{
				$("div.folderlinks a").removeClass("on");
				$("div.moreinfo").hide();
				
				thisLink.addClass("on");
					thisLink.css({
					"z-index": 101
				});				 
				
				thisInfo.show();
				thisInfo.css({
					position: "absolute",
					marginTop: thisLink.height() - 14,
					left: -20,
					"z-index": 100
				});
			}
			thisLink.blur();			 
			return false;
		});
	});
}

function initSkipLists(thisParent)
{
	makeSkipLists(thisParent.find("div.listbox .wrapper"), ".teaser", 3);
	makeSkipLists(thisParent.find("div.archivelist"), ".teaser", 1, "prepend");
}

function makeSkipLists(thisLists, listChilds, showMax, position)
{
	var listChilds = listChilds;
	var showMax = showMax;
	thisLists.each(function()
	{
		var thisList = $(this);
		
		if(thisList.find(listChilds).length > showMax)
		{
			var maxHeight = 0;
			thisList.find(listChilds).each(function()
			{
				if(maxHeight < $(this).height())
				{ maxHeight = $(this).height(); }
			});
			thisList.find(listChilds).css({
				height: maxHeight
			});
			thisList.find(listChilds + ":gt(" + (showMax - 1) + ")").hide();
			thisList.append("<div class=\"skiplist\"><strong><a href=\"#-\">«</a></strong><span></span><strong><a href=\"#+\">»</a></strong></div>");
			if(position=="prepend")
			{ thisList.prepend(thisList.children(".skiplist")); }
			//thisList.append(thisList.children(".skiplist").clone());
			
			thisList.find(listChilds).wrapAll("<div class=\"teaserbox\"></div>");
			
			var thisNumbers = thisList.children(".skiplist").children("span");
			var thisTeaserBox = thisList.children(".teaserbox");
			var thisCtrl = thisList.children(".skiplist").children("strong").children("a");

			for(i = 1; i <= Math.ceil(thisList.find(listChilds).length / showMax); i++)
			{ thisNumbers.append("<a href=\"#" + i + "\">" + i + "</a>"); }
			thisList.children(".skiplist").find("a").css({
				cursor: "default"
			});

			thisTeaserBox.css({
				position: "relative",
				width: "100%",
				height: thisTeaserBox.height(),
				marginLeft: "-10px",
				paddingLeft: "10px",
				overflow: "hidden"
			});
			thisTeaserBox.find(listChilds).css({
				position: "relative",
				top: 0
			}).show();
			
			var fullWidth = thisNumbers.find("a:first").addClass("on").width() * thisNumbers.find("a").length; 
			if(thisNumbers.width() >= fullWidth)
			{
				thisNumbers.addClass("small");
			}
			else
			{
				thisNumbers.find("a:gt(0):not(:last)").wrapAll("<span></span>");
				thisNumbers.find("a:first").css({
					position: "absolute",
					left: 0,
					top: 0
				});
				thisNumbers.find("a:last").css({
					position: "absolute",
					right: 0,
					top: 0
				});
				thisNumbers.children("span").css({
					position: "absolute",
					display: "block",
					width: thisNumbers.width() - thisNumbers.find("a:first").width() * 2, 
					left: thisNumbers.find("a:first").width(),
					top: 0,
					overflow: "hidden"
				});	
			}
		
			thisNumbers.find("a").click(function()
			{
				var thisLink = $(this);
				var firstTeaser = thisTeaserBox.find(listChilds + ":eq(" + (showMax * (parseInt(thisLink.text()) - 1)) + ")"); 
				var newTop = ((isNaN(parseInt(firstTeaser.css("top")))) ? 0 : parseInt(firstTeaser.css("top"))) + parseInt(thisTeaserBox.offset().top - firstTeaser.offset().top);

				thisNumbers.find("a").removeClass("on");
				thisLink.addClass("on");
				thisTeaserBox.find(listChilds).animate({
					top: newTop
				}, 500, "swing");

				if(thisNumbers.width() < fullWidth)
				{
					var newLeft = ((isNaN(parseInt(thisLink.css("left")))) ? 0 : parseInt(thisLink.css("left"))) + parseInt((thisNumbers.width() - thisLink.width()) / 2 + thisNumbers.offset().left - thisLink.offset().left);  
					var maxLeft = parseInt(thisNumbers.width() - thisLink.width() * thisNumbers.find("a").length);

					if(thisLink.next().length == 0 || newLeft < maxLeft)
					{ newLeft = maxLeft; }
					else if(newLeft > 0)
					{ newLeft = 0; }

					thisNumbers.find("a:gt(0):not(:last)").animate({
						left: newLeft
					}, 500, "swing");
				}

				thisLink.blur();
				return false;
			});
				
			thisCtrl.click(function(i)
			{
				var thisLink = $(this);
				var newLink = false 
				switch(thisCtrl.index(thisLink))
				{
					case 0: // prev 
						newLink = thisNumbers.find("a:eq(" + (thisNumbers.find("a").index(thisNumbers.find("a.on")) - 1) + ")");
						break;
					case 1: // next
						newLink = thisNumbers.find("a:eq(" + (thisNumbers.find("a").index(thisNumbers.find("a.on")) + 1) + ")");
						break;
				}
				if(newLink.length == 1)
				{ newLink.triggerHandler("click"); }

				thisLink.blur();
				return false;
			});
			
		}
		
	});
}

function initArchive()
{
	$("ul#archive>li>div").addClass("box").hide();
	
	if($("ul#archive>li").length > 0) {
		$("h2>a:first").addClass("on");
		var thisShown = $("ul#archive>li div.box:first").show();
		thisShown.css("z-index", $("ul#archive>li").length + 1);
	}
	
	$("ul#archive>li").hover(
		function()
		{
			var thisItem = $(this);
			var activeItem = $("ul#archive>li:has(div.box:visible)");

			$("ul#archive>li").not(thisItem).removeClass("over");
			thisItem.addClass("over");
			if($("ul#archive>li").index(thisItem) != $("ul#archive>li").index(activeItem))
			{
				if(activeItem.length > 0)
				{
					activeItem.find("h2>a").addClass("on");
					activeItem.find("div.box:visible").slideUp(
						500,
						function()
						{
							activeItem.find("h2>a").removeClass("on");
	
							if(thisItem.is(".over"))
							{
								thisItem.css("z-index", $("ul#archive>li").length + 1);
								thisItem.find("h2>a").addClass("on");
								thisItem.children("div.box").slideDown(500);
							}
						}
					);
				}
				else
				{
					thisItem.css("z-index", $("ul#archive>li").length + 1);
					thisItem.find("h2>a").addClass("on");
					thisItem.children("div.box").slideDown(500);
				}
			}
		},
		function()
		{
			var thisItem = $(this); 
			thisItem.css("z-index", $("ul#archive>li").length - $("ul#archive>li").index(thisItem));
		}
	);
}

function playerReady(obj) // ready-call JWPlayer
{
	var domPlayer = document.getElementById(obj["id"]);
	domPlayer.addControllerListener("PLAY", "stopOtherStuff");
	domPlayer.addControllerListener("STOP", "startOtherStuff");
}

function stopOtherStuff(obj)
{
	if(stageTimer && $("object#" + obj["id"]).parents("#stage").length > 0) // stop Stage Animation
	{
		window.clearTimeout(stageTimer);
	} 
	$("object:not(#" + obj["id"] + ")").each(function() // stop other Player
	{
		$(this).get(0).sendEvent("STOP");
	});
	return true;
}

function startOtherStuff(obj)
{
	if($("object#" + obj["id"]).parents("#stage").length > 0) // stop Stage Animation
	{
		stageAutoAnim(2000);
	}
	return true; 
}

function testEvent(obj) // just debug
{
	var eventText = "";
	$.each(obj, function(key, val)
	{
		eventText += key + ": " + val + "\n";
	});
	alert(eventText);
	return false;
}

function initSlideshow()
{
	var scrollContentInnerWidth = 0;
	$(".slideshowcontent li").each(function()
	{
		scrollContentInnerWidth += $(this).width() + parseInt($(this).css("margin-left")) + parseInt($(this).css("margin-right")) + parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right")); 
	});
	
	if(scrollContentInnerWidth > $(".slideshowcontent").width())
	{
		$(".slideshow").prepend("<div class=\"prev\">«</div><div class=\"forward\">»</div>");
		if(scrollContentInnerWidth < $(".slideshowcontent").width() * 2) 
		{
			scrollContentInnerWidth *= 2;
			$(".slideshowcontent ul").css({
				width: scrollContentInnerWidth
			});
			$(".slideshowcontent ul").append($(".slideshowcontent ul").html());
		}
	}	
	$(".slideshowcontent").css({
		width: "360px",
		height: "160px",
		overflow: "hidden"
	});
	
	
	$(".slideshowcontent ul").css({
		position: "relative",
		width: scrollContentInnerWidth
	});
	
	$(".slideshowcontent li").each(function()
	{
		var thisList = $(this);
		
		thisList.css({
			left: thisList.offset().left - $(".slideshowcontent").offset().left 
		});
				
		var thisBubble = thisList.find("div.bubble");
		thisBubble.hide();
		thisBubble.css ({
			top: (thisList.height() - 40)+ "px"
		});
		$(".slideshow").prepend(thisBubble);
		var bubbleTimeout = false;
		thisList.hover(
			function()
			{
				if(bubbleTimeout)
				{ window.clearTimeout(bubbleTimeout); }
				thisBubble.show();
			},
			function()
			{
				bubbleTimeout = window.setTimeout(function(){ thisBubble.hide(); }, 50);
			}
		);
		thisBubble.hover(
			function(){ thisList.trigger("mouseover"); },
			function(){ thisList.trigger("mouseout"); }
		);
		
	});
	
	$(".slideshowcontent li").css({
		position: "absolute"
	});
	
	$(".prev,.forward").click(function()
	{
		if(!$(".slideshowcontent li").is(":animated"))
		{
			if($(this).is(".prev"))
			{ var thisDirection = +1; }
			else
			{ var thisDirection = -1; }

			var firstBox = $(".slideshowcontent li:first");
			$(".slideshowcontent li").each(function()
			{
				if( (thisDirection == -1 && $(this).offset().left + $(this).width() - $(".slideshowcontent ul").offset().left < 0) || (thisDirection == +1 && parseInt($(this).css("left")) > $(".slideshowcontent").width()) )
				{
					$(this).css({
						left: (parseInt($(this).css("left")) - thisDirection * scrollContentInnerWidth) + "px" 
					});
				}
			});
		
			$(".slideshowcontent li").each(function()
			{
				$(this).animate(
				{
					left: (parseInt($(this).css("left")) + thisDirection * 360) + "px" 
				}, 700, "swing");
			});
		}
	});
}

function initShader()
{
	$("a[href$='.jpg'],.videoplayer a, a.popup").click(function()
	{
		
		var thisElement = $(this).parent().find("a[href$='.jpg']");

		if (thisElement.html() == null) {
			var clickImg = false; 
		}
		
		$("body").find("object").each(function()
		{
			if($(this).get(0).sendEvent)
			{
				$(this).get(0).sendEvent("STOP");
			}
		});
		
		$("body").append("<div id=\"shader\"></div><div id=\"box\"></div>");
		$("#shader,#box").fadeTo(0, 0.01);
		
		if (clickImg == false) {
			$("#box").append("<iframe frameborder=\"0\" id=\"ifrm\" src=\"" + $(this).attr("href") + "\" />");
			autoResize();
		}
		else {
			$("#box").append("<div id=\"imgcontainer\"><img src=\"" + $(this).attr("href") + "\" /></div>");
			autoResizeImg();
		}
			
		$("#box").append("<div id=\"boxcloser\">X</div>");
		
	
		$("#shader").fadeTo(300, 0.4);
		$("#shader,#boxcloser").click(function()
		{
			$("#shader,#box").fadeOut(300, function(){ $("#shader,#box").remove(); });
		});

		return false;
	});
}

function autoResize(){
 
	$("#ifrm").load(function() {
			
		var thisContent = $("#ifrm").contents();
		thisContent.css({
			float: "left"
		});
		
		var thisHeight = thisContent.height() + 20;
		var thisWidth = thisContent.width();
				
		if(thisWidth > $(window).width() * 0.9)
		{ thisWidth = $(window).width() * 0.9; } 
		if(thisHeight > $(window).height() * 0.9)
		{ thisHeight = $(window).height() * 0.9; } 
		$("#ifrm").css({		
			height: thisHeight + "px",
			width: thisWidth + "px"
		});
		
		$("#box").center();		
		$("#box").fadeTo(500, 1);
		
			$(window).scroll(function() {
				$("#box").center();		
			});
			
			$(window).resize(function() {
				$("#box").center();		
			});						

	});
}

function autoResizeImg(){

	$("#imgcontainer img").load(function() {
	
	var maxHeight = ($(window).height() - 100);
	if($("#imgcontainer img").height() > maxHeight)
	{ $("#imgcontainer img").height(maxHeight); }

	$("#box").center();		
	$("#box").fadeTo(500, 1);
	
		$(window).scroll(function() {
			$("#box").center();		
		});
		
		$(window).resize(function() {
			$("#box").center();		
		});	
		
	});						

}

function initEcardSlider()
{
	var scrollContentInnerWidth = 0;
	$(".ecardcontent li").each(function()
	{
		scrollContentInnerWidth += $(this).width() + parseInt($(this).css("margin-left")) + parseInt($(this).css("margin-right")) + parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right")); 
	});
	
	if(scrollContentInnerWidth > $(".ecardcontent").width())
	{
		$(".ecardslider").prepend("<div class=\"prev\">«</div><div class=\"forward\">»</div>");
		if(scrollContentInnerWidth < $(".ecardcontent").width() * 2) 
		{
			scrollContentInnerWidth *= 2;
			$(".ecardcontent ul").css({
				width: scrollContentInnerWidth + "px"
			});
			$(".ecardcontent ul").append($(".ecardcontent ul").html());
		}
	}	
	$(".ecardcontent").css({
		width: "830px",
		height: "200px",
		overflow: "hidden"
	});
		
	$(".ecardcontent ul").css({
		position: "relative",
		width: scrollContentInnerWidth + "px"
	});
		
	// var activePosition = $(".ecardcontent li.active").offset().left;

	$(".ecardcontent li").each(function()
	{
		var thisList = $(this);

		thisList.css({
			left: thisList.offset().left - $(".ecardcontent").offset().left + 20,
			width: $(this).width() + "px",
			cursor: "pointer"
		});
		
		thisList.click(function(e){
		
			$(".ecardcontent li.active").removeClass("active");	
		  $(this).addClass("active");
		  var thisId = $(this).find(".textbox").attr("id");
		  var thisText = $(this).find("blockquote span").html();
		  var thisAutor = $(this).find("cite span").html();
		  	  
		  getEcardInfos(thisId, thisText, thisAutor);
					  
			this.blur();
			return false;
		});
						
	});

	$(".ecardcontent li").css({
		position: "absolute"
	});
	
	$(".prev,.forward").click(function()
	{
		if(!$(".ecardcontent li").is(":animated"))
		{
			if($(this).is(".prev"))
			{ 
				var thisDirection = +1; 
			}
			else
			{ 
				var thisDirection = -1; 
			}

			var firstBox = $(".ecardcontent li:first");
			$(".ecardcontent li").each(function()
			{
				if( (thisDirection == -1 && $(this).offset().left + $(this).width() - $(".ecardcontent ul").offset().left < 0) || (thisDirection == +1 && parseInt($(this).css("left")) > $(".ecardcontent").width()) )
				{
					$(this).css({
						left: (parseInt($(this).css("left")) - thisDirection * scrollContentInnerWidth) + "px" 
					});
				}
			});
		
			$(".ecardcontent li").each(function()
			{
				
				$(this).animate(
				{
					left: (parseInt($(this).css("left")) + thisDirection * 350) + "px" 
				}, 700, "swing");
			});
		}
	});
}

function getEcardInfos(thisId, thisText, thisAutor) {
		
		$("form [name=id]").val(thisId);
		$(".zitat_text").html(thisText);
		$(".zitat_autor").html(thisAutor);
	
		initVAlign($(".textbox"));
	
}

function initEcardRandomizer(thisParent) {
 	
	$("#randomize").click(function()
	{
	
		var thisSearchContent = $(".ecardcontent");
		
		var thisId = thisSearchContent.find(".textbox").rand(1).attr("id");
		
	  var thisText = thisSearchContent.find("#" + thisId + " blockquote span").html();
	  var thisAutor = thisSearchContent.find("#" + thisId + " cite span").html();
	  
		thisSearchContent.find(".active").removeClass("active");
		thisSearchContent.find("#" + thisId).parent().addClass("active");
		  
		$("form [name=id]").val(thisId);
		$(".zitat_text").html(thisText);
		$(".zitat_autor").html(thisAutor);
	
		initVAlign($(".textbox"));  
				
		this.blur();
		return false;
	});
	
}

function initImageCaption() {

	$("div.smallbubble").each(function()
		{
			var thisBubble = $(this);
			var thisParent = thisBubble.parents(".bigimage,.imagebox,.imgbox,.deco");
			var thisImage = thisParent.children("img");
			
			$("body").append(thisBubble);
			
			thisBubble.hide();
			
			var bubbleTimeout = false;
			thisParent.hover(
				function()
				{
					if(thisParent.is(".deco"))
					{
						thisBubble.css ({
							top: (thisParent.offset().top + thisParent.height() - 40)+ "px",
							left: (thisParent.offset().left + 20)+ "px"
						});
					}
					else if(thisParent.is(".imgbox"))
					{
						thisBubble.css ({
							top: (thisParent.offset().top + thisParent.height() + 4)+ "px",
							left: (thisParent.offset().left - 48)+ "px"
						});
					}
					else 
					{
						thisBubble.css ({
							top: (thisParent.offset().top + thisParent.height() - 8)+ "px",
							left: (thisParent.offset().left)+ "px"
						});
					}
					if(bubbleTimeout)
					{ window.clearTimeout(bubbleTimeout); }
					thisBubble.show();
				},
				function()
				{
					bubbleTimeout = window.setTimeout(function(){ thisBubble.hide(); }, 50);
				}
			);
			thisBubble.hover(
				function(){ thisList.trigger("mouseover"); },
				function(){ thisList.trigger("mouseout"); }
			);
			
		});

}

function initInputSample(thisParent)
{
	var thisParent = thisParent || $("body");
	thisParent.find("form:has(label span.sample)").each(function()
	{ 
		var thisForm = $(this);
		thisForm.find("label span.sample").each(function()
		{
			var thisSample = $(this);
			var thisInput = thisSample.siblings("input");
			if(thisInput.val() == "" || thisInput.val() == thisSample.text())
			{ 
				thisInput.val(thisSample.text()); 
			} 
			thisSample.hide();
	
			var thisContainer = thisInput.parents("form").parent("div[id]");
	
			thisInput.focus(function()
			{
				thisInput.css({
					color: $("body").css("color")
				});
				if(thisInput.val() == thisSample.text())
				{ thisInput.val(""); } 
			});
		});

		thisForm.submit(function()
		{
			thisForm.find("label span.sample").each(function()
			{
				var thisSample = $(this);
				var thisInput = thisSample.siblings("input");
				if(thisInput.val() == thisSample.text())
				{ thisInput.val(""); } 
			});
		});
	});
}

function initExtLinks()
{
	$("a[href^='http://'],a[href$='.pdf'],a[href$='.doc'],a[href$='.mp3']").each(function()
	{
		$(this).attr("target", "_blank");
	});
}

function initPrintView()
{
	$("a.printpage").click(function()
	{
	 var thisHref = this;
	 window.open(thisHref, "printwindow","location=0,status=0,scrollbars=1, width=970,height=800");
	 return false;
	});
}
