/**
 * Bibliothek erstellt von Lutz Hoffarth.
 * Voraussetzung sind die Bibliotheken: jQuere-1.4.4, jQuere-ui-1.8.10 und lutzensClasses.js
 * @author Lutz Hoffarth
 * @version 15.03.2011 Einbindung der lutzensClasses.js Bibliothek.
 * @version 26.02.2011 Erstellt
 */
var contentExpand = false;
var runC = false;
var runS = false;
var animation = new Animation(Animation.EASING_EFFECT_EXPO, Animation.EASING_LOCATION_OUT, 1000);
var oldIdCenter = "Home";
var oldIdSouth = "1_1";
var centerImagePath = "images/center/";
function setContentSize(centerMinWidth, centerMinHeight, minSouthWidth, southMinHeight, southMaxHeight, southContentImgBtnWidth, southUeber, centerMinimizedHeight) {
	var wiw = getBrowserWidth();
	var wih = getBrowserHeight();
	var scrollLeft = $(window).scrollLeft();
	if (wiw < minSouthWidth) wiw = minSouthWidth;
	if (wih < southMaxHeight + centerMinimizedHeight - southUeber) wih = southMaxHeight + centerMinimizedHeight - southUeber;
	var centerInnerWidth = wiw;
	var centerInnerHight = wih - (southMinHeight - southUeber);
	var imgWidth = wiw;
	if (imgWidth < minSouthWidth) imgWidth = minSouthWidth; // nicht kleiner als Contentbereich. Absprache vom 11.03.2011
	if (imgWidth < centerMinWidth) imgWidth = centerMinWidth;
	var imgHight = centerInnerHight;
	if (imgHight < centerMinHeight) imgHight = centerMinHeight;
	var imgOrigVerh = centerMinWidth / centerMinHeight;
	var imgVerh = imgWidth / imgHight;
	if (imgVerh > imgOrigVerh) { // Wenn breiter als Original
		imgHight = Math.floor(imgWidth / imgOrigVerh); // Höhe an die Breite anpassen
	} else if (imgVerh < imgOrigVerh) { // Wenn schmaler als Original
		imgWidth = Math.floor(imgHight * imgOrigVerh); // Breite an die Höhe anpassen
	}
	var imgLeft = Math.floor((centerInnerWidth - imgWidth) / 2);
	var imgTop = Math.floor((centerInnerHight - imgHight) / 2);
	if (scrollLeft > 0 && scrollLeft + wiw > minSouthWidth) scrollLeft = minSouthWidth - wiw; // Verhindert, dass der Bereich nicht weiter scrollbar ist als nötig
	$("#contentSouthButton").css("left", scrollLeft + Math.floor((wiw - southContentImgBtnWidth) / 2)); // Position des SouthButtons
	$("#contentCenter").css("width", wiw).css("height", wih - southMinHeight + southUeber).css("left", scrollLeft);
	$("#contentCenterImage").css("width", imgWidth).css("height", imgHight).css("left", imgLeft).css("top", imgTop);
	var conSw = wiw;
	if (conSw < minSouthWidth) conSw = minSouthWidth;
	if (!contentExpand) {
		$("#contentSouth").css("width", conSw).css("height", southMinHeight);
	} else {
		$("#contentSouth").css("width", conSw).css("height", southMaxHeight);
	}
	$("#container").css("width", wiw).css("height", wih);
	$("#testWiw").html("Wiw:" + wiw + "; " + $(window).width());
	$("#testWih").html("Wih:" + wih + "; " + $(window).height());
	setPlayerPosSize();
};
function expandSouth(southMinHeight, southMaxHeight, nurAuf) {
	if (nurAuf == true && contentExpand == true) return;
	if (nurAuf == false && contentExpand == false) return;
	var homeVideo = $(".homeVideo")[0];
	var contentSouthContent = $("#contentSouthContent").css("display", "inherit");
	if ($("#contentSouthButtonMakeOn").css("display") == "inline") { // ausblenden
		contentExpand = true; // 30.04.2011
		fadeCenterMessageOut();
		$("#contentSouth").animate({
			height : southMaxHeight
		}, animation.getSpeed(), animation.getEasing(), function() {
			if (homeVideo == "[object HTMLVideoElement]") homeVideo.pause();
		});
		contentSouthContent.animate({
			height : southContentHeight
		}, animation.getSpeed(), animation.getEasing());
	} else { // Einblenden
		contentExpand = false; // 30.04.2011
		$("#contentSouth").animate({
			height : southMinHeight
		}, animation.getSpeed(), animation.getEasing(), function() {
			if (homeVideo == "[object HTMLVideoElement]") homeVideo.play();
			fadeCenterMessageIn();
		});
		contentSouthContent.animate({
			height : 0
		}, animation.getSpeed(), animation.getEasing(), function() {
			contentSouthContent.css("display", "none");
		});
	}
	;
	$("#contentSouthButtonMakeOn").toggle();
	$("#contentSouthButtonMakeOff").toggle();
	markMenuAuswahl(oldIdSouth);
};
var centerSlidePaused = false;
var centerAutoSlideLeft = false;
var centerAutoSlideRight = false;
var centerAutoSlideInterval = 2000;
function pauseCenterAutoSlide() {
	centerSlidePaused = true;
};
function runCenterSlideLeft() {
	if (centerAutoSlideLeft && !contentExpand) {
		centerAutoSlideLeft = false; // Stoppen des Slides
		return;
	}
	centerAutoSlideLeft = true;
	centerAutoSlideRight = false;
	slideCenterToLeft(centerIds);
};
function runCenterSlideRight() {
	if (centerAutoSlideRight && !contentExpand) {
		centerAutoSlideRight = false; // Stoppen des Slides
		return;
	}
	centerAutoSlideRight = true;
	centerAutoSlideLeft = false;
	slideCenterToRight(centerIds);
};
function slideCenterToLeft() {
	if (runC) return;
	deaktivateCenterButtons();
	nextId = getNextElement(centerIds, false, oldIdCenter);
	slideCenterTo(nextId, false);
};
function slideCenterToRight() {
	if (runC) return;
	deaktivateCenterButtons();
	nextId = getNextElement(centerIds, true, oldIdCenter);
	slideCenterTo(nextId, true);
};
function slideSouthToLeft(contentIds) {
	if (runS) return;
	deaktivateSouthButtons();
	nextId = getNextElement(contentIds, false, oldIdSouth);
	slideSouthTo(nextId, false);
};
function slideSouthToRight(contentIds) {
	if (runS) return;
	deaktivateSouthButtons();
	nextId = getNextElement(contentIds, true, oldIdSouth);
	slideSouthTo(nextId, true);
};
function getNextElement(contentIds, direction, oldId) {
	if (jQuery.type(contentIds) === "string")
		return contentIds;
	else if (jQuery.type(contentIds) === "array") {
		for (i = 0; i < contentIds.length; i++) {
			if (contentIds[i] == oldId) {
				var naechteId = 0;
				if (direction == true) {
					if (i > 0)
						naechsteId = contentIds[i - 1]; // Vorheriger Eintrag
					else
						naechsteId = contentIds[contentIds.length - 1]; // Letzter Eintrag
				} else {
					if (i < contentIds.length - 1)
						naechsteId = contentIds[i + 1]; // Nächster Eintrag
					else
						naechsteId = contentIds[0]; // Erster Eintrag
				}
				return naechsteId;
			}
		}
		return contentIds[1];
	}
};
function markMenuAuswahl(newId) {
	// Alle Highlightings aus dem Menü entfernen.
	for (i = 0; i < contentIds.length; i++) {
		$("#me_" + contentIds[i]).removeClass("menu_auswahl");
	}
	//Highlighten
	if (contentExpand) {
		newMenu = $("#me_" + newId).addClass("menu_auswahl");
	}
};
function slideSouthTo(contentId, right) {
	if (contentId == oldIdSouth) {
		aktivateSouthButtons();
		return;
	}
	markMenuAuswahl(contentId);
	if (nextId == tastivalId)
		setCenterImagePath(pathTastivalImages, tastivalIds);
	else
		setCenterImagePath(pathWeinImages, weinImgesIds);
	if (right) {
		var oldPos = "right";
		var newPos = "left";
	} else {
		var oldPos = "left";
		var newPos = "right";
	}
	var wrapper = $("#content_wrapper");
	var old_inner = $(".content_inner");
	if (old_inner == null) return;
	var navi_inner_width = old_inner.css("width");
	var old_box = $(".content_box");
	if (old_box == null) return;
	old_box.removeClass("content_box").addClass("content_box_" + oldPos).css("width", "100%");
	old_inner.removeClass("content_inner").addClass("content_inner_" + oldPos).css("width", navi_inner_width);
	var new_inner = $("#content_inner_" + contentId).removeClass("content_inner_wait").addClass("content_inner_" + newPos); // Ermitteln
	var new_box = $("#content_box_" + contentId); // Ermitteln
	new_box.remove();
	new_box.removeClass("content_box_wait").addClass("content_box_" + newPos); // Stylen
	if (right)
		wrapper.prepend(new_box);
	else
		wrapper.append(new_box);
	new_inner.css("width", navi_inner_width);
	new_box.animate({
		width : navi_inner_width
	}, animation.getSpeed(), animation.getEasing(), function() {
		new_inner.removeClass("content_inner_" + newPos).addClass("content_inner").css("width", "100%");
		new_box.removeClass("content_box_" + newPos).addClass("content_box").css("width", "100%");
		$(".content_view").css("width", "100%");
		old_box.removeClass("content_box_" + oldPos).addClass("content_box_wait");
		old_inner.removeClass("content_inner_" + oldPos).removeClass("content_inner_wait");
		oldIdSouth = contentId;
		aktivateSouthButtons();
	});
	old_box.animate({
		width : "0px"
	}, animation.getSpeed(), animation.getEasing());
};
function slideCenterTo(contentId, right) {
	if (contentId == oldIdCenter) {
		aktivateCenterButtons();
		return;
	}
	if (contentId === "Home") {
		var image = document.createElement("video");
		image.setAttribute("class", "homeVideo");
		image.setAttribute("tabindex", "0");
		image.setAttribute("poster", "images/center/Weinreich_1.jpg");
		var source1 = document.createElement("source");
		source1.setAttribute("src", "images/center/Weinreich_1.mp4");
		source1.setAttribute("type", "video/mp4");
		image.appendChild(source1);
		var source2 = document.createElement("source");
		source2.setAttribute("src", "images/center/Weinreich_1.ogg");
		source2.setAttribute("type", "video/ogg");
		image.appendChild(source2);
		var source3 = document.createElement("img");
		source3.setAttribute("src", "images/center/Weinreich_1.jpg");
		source3.setAttribute("class", "image_view image_view_video");
		source3.setAttribute("width", "100%");
		source3.setAttribute("height", "100%");
		image.appendChild(source3);
	} else {
		var imagePath = centerImagePath + "WeinreichFoto_" + contentId + ".jpg";
		var image = "<img class=\"image_view\" style=\"display: inline; width: " + navi_inner_width + "; height: 100%;\" src=\"" + imagePath + "\">";
	}
	if (right) {
		var oldPos = "right";
		var newPos = "left";
	} else {
		var oldPos = "left";
		var newPos = "right";
	}
	var old_inner = $(".image_inner");
	if (old_inner == null) return;
	var navi_inner_width = old_inner.css("width");
	old_inner.removeClass("image_inner").addClass("image_inner_" + oldPos).css("width", navi_inner_width);
	if (jQuery.type(image) === "string") {
		var new_inner = "<div class=\"image_inner_" + newPos + "\">" + image + "</div>";
		var new_box = "<div class=\"image_box_" + newPos + "\" >" + new_inner + "</div>";
	} else {
		var new_inner = document.createElement("div");
		new_inner.setAttribute("class", "image_inner_" + newPos);
		new_inner.appendChild(image);
		var new_box = document.createElement("div");
		new_box.setAttribute("class", "image_box_" + newPos);
		new_box.appendChild(new_inner);
	}
	var old_box = $(".image_box");
	if (old_box == null) return;
	old_box.removeClass("image_box").addClass("image_box_" + oldPos).css("width", "100%");
	if (right)
		$("#image_wrapper").prepend(new_box);
	else
		$("#image_wrapper").append(new_box);

	if (contentId === "Home") {
		var homeVideo = $(".homeVideo")[0];
		if (homeVideo == "[object HTMLVideoElement]") {
			homeVideo.play();
			homeVideo.controls = false;
			homeVideo.onended = function(e) {
				homeVideo.seeking = -1;
			};
			$(".homeVideo").click(function() {
				if (homeVideo.paused)
					homeVideo.play();
				else
					homeVideo.pause();
			});
		}
	}
	new_box = $(".image_box_" + newPos);
	new_inner = $(".image_inner_" + newPos).css("width", navi_inner_width);
	new_box.animate({
		width : navi_inner_width
	}, animation.getSpeed(), animation.getEasing(), function() {
		new_inner.removeClass("image_inner_" + newPos).addClass("image_inner").css("width", "100%");
		new_box.removeClass("image_box_" + newPos).addClass("image_box").css("width", "100%");
		$(".image_view").css("width", "100%");
		old_box.remove();
		oldIdCenter = contentId;
		aktivateCenterButtons();
	});

	old_box.animate({
		width : "0px"
	}, animation.getSpeed(), animation.getEasing());
	fadeCenterMessage(contentId);
	$(".image_view_video").click(function(){
		showVideoBox(true);
	});
};
function checkBrowser(name) {
	if (jQuery.type(centerIds) === "string") name = name.toLowerCase();
	var browser = navigator.userAgent.toLowerCase();
	if (browser.indexOf(name, 0) > -1) return true;
	return false;
}
function fadeCenterMessage(contentId) {
	if ($("#centerImageMessage_" + contentId).hasClass("centerImageMessage")) return; // Text wird bereits angezeigt!
	var oldBox = $(".centerImageMessage").css("opacity", 1);
	var newBox;
	if (centerImagePath === pathWeinImages)
		newBox = $("#centerImageMessage_" + contentId).css("display", "inline");
	else if (centerImagePath === pathTastivalImages) newBox = $("#centerImageMessage_tastival_" + contentId).css("display", "inline");
	newBox.css("opacity", 0);
	oldBox.animate({
		opacity : 0
	}, animation.getSpeed(), animation.getEasing(), function() {
		oldBox.css("display", "none");
		oldBox.removeClass("centerImageMessage").addClass("centerImageMessageWait");
		newBox.removeClass("centerImageMessageWait").addClass("centerImageMessage").css("opacity", 0);
		if (!contentExpand) {
			newBox.animate({
				opacity : 1
			}), animation.getSpeed(), animation.getEasing();
		}
	});
};
function fadeCenterMessageOut() {
	var box = $(".centerImageMessage");
	box.animate({
		opacity : 0
	}, animation.getSpeed(), animation.getEasing());
};
function fadeCenterMessageIn() {
	var box = $(".centerImageMessage");
	box.animate({
		opacity : 1
	}, animation.getSpeed(), animation.getEasing());
};
function deaktivateCenterButtons() {
	runC = true;
	$(".nav_left_center").css("cursor", "wait");
	$(".nav_right_center").css("cursor", "wait");
};
function deaktivateSouthButtons() {
	runS = true;
	$(".nav_left_south").css("cursor", "wait");
	$(".nav_right_south").css("cursor", "wait");
	$(".menuElement").css("cursor", "wait");
};
function aktivateCenterButtons() {
	runC = false;
	$(".nav_left_center").css("cursor", "pointer");
	$(".nav_right_center").css("cursor", "pointer");
};
function aktivateSouthButtons() {
	runS = false;
	$(".nav_left_south").css("cursor", "pointer");
	$(".nav_right_south").css("cursor", "pointer");
	$(".menuElement").css("cursor", "pointer");
};
function getBrowserWidth() {
	return window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
};
function getBrowserHeight() {
	return window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
};
function setCenterImagePath(path, ids) {
	if (path === centerImagePath) return; // keine Änderung
	centerIds = ids;
	centerImagePath = path;
	oldIdCenter = 0; // Auf das erste Bild setzen.
	slideCenterToLeft(centerIds);
};
function preloadCenterImages(path, centerIds) {
	if (jQuery.type(centerIds) === "string") {
		var preload = $('<img />').attr('src', path + "WeinreichFoto_" + i + ".jpg"); // 12.04.2011
		$("#preload").append(preload); // 12.04.2011
	} else if (jQuery.type(centerIds) === "array") {
		var preloads = new Array();
		for (i = 0; i < centerIds.length; i++) {
			preloads[i] = $('<img />').attr('src', path + "WeinreichFoto_" + centerIds[i] + ".jpg"); // 12.04.2011
			preloads[i].load(function() {
				$("#preload").append($(this)); // 12.04.2011
			});
		}
	}
};
function showVideoBox(visible) {
	var frame = $("#video_box");
	var player = $(".video_player");
	if (visible) {
		setPlayerPosSize();
		player[0].play();
		frame.css("display", "inline");
	} else {
		frame.css("display", "none");
		player[0].stop();
	}

}
function setPlayerPosSize(){
	var frame = $("#video_box");
	var player = $(".video_player");
	var btnCloseBox = $(".video_player_close_box");
	var btnClose = $(".video_player_close");
	var wiw = getBrowserWidth();
	var wih = getBrowserHeight();
	var plw = wiw * 0.75;
	var plh = wih * 0.75;
	var imgOrigVerh = centerMinWidth / centerMinHeight;
	var imgVerh = plw / plh;
	if (imgVerh > imgOrigVerh) { // Wenn breiter als Original
		plw =  Math.floor(plh * imgOrigVerh); // Breite an die Höhe anpassen
	} else if (imgVerh < imgOrigVerh) { // Wenn schmaler als Original
		plh = Math.floor(plw / imgOrigVerh); // Höhe an die Breite anpassen
	}
	plh += 45;
	var plt = (wih - plh) / 2;
	var pll = (wiw - plw) / 2;
	var btnS = 30;
	var btnRadius = btnS/2;
	var btnKathede = Math.cos((2 * Math.PI * 45) / 360) * btnRadius;
	var btnt = plt - btnRadius - btnKathede;
	var btnl = pll + plw - btnRadius + btnKathede;
	player.css("left", pll);
	player.css("top", plt);
	player.css("width", plw);
	player.css("height", plh);
	frame.css("width", wiw);
	frame.css("height", wih);
	btnCloseBox.css("left", btnl);
	btnCloseBox.css("top", btnt);
	btnClose.css("width", btnS);
	btnClose.css("height", btnS);
}
