var lastKlick = new Date().getTime();
var minInterval = 50;
var modalOpacity = 0.9;

var galerieObjects = new Array();
var currentlyDisplayedGalerie = "";
var loadedJSVideo = null;

function galerieObject(type) {

	if (type == undefined)
		type = "image";

	this.pref_mini = "";
	this.pref_medium = "";
	this.pref_big = "";
	this.type = type;
	this.description = "";
	
	this.once = false;

	this.onView = function(){
		switch(this.type){
			case "image":
				break;
			case "videoJSObjekt":
				var dasVideo = $(this.videoDiv).find("video");
				if(!this.once){
					this.once = true;
					$(dasVideo).VideoJS();
				}
			break;
			default:
		}
	};
	
	this.onHide = function(){
		switch(this.type){
			case "image":
				break;
			case "videoJSObjekt":
				break;
			default:
		}
	};
	
	
}

function initPreviewImageProperties() {
	var counterSpalte = 0;
	var counterZeile = 0;
	$("#lightbox_imagePreviewWrapper img").each(function() {
		counterSpalte++;

		if (counterZeile == 0) {
			$(this).css("margin-top", "0px");
		}

		if (counterZeile == 4) {
			$(this).css("margin-bottom", "0px");
		}

		if (counterSpalte == 1) {
			$(this).css("margin-left", "0px");
		} else if (counterSpalte >= 4) {
			$(this).css("margin-right", "0px");
			counterSpalte = 0;
			counterZeile++;
		}
	});
}

function initPreviewImageMouseover() {

	$("#lightbox_imagePreviewWrapper img").unbind("mouseenter mouseleave").mouseenter(function() {
		$("#lightbox_imagePreview_lineLeft,#lightbox_imagePreview_lineBottom").stop().css("opacity", 1);

		$("#lightbox_headline_text").text($(this).attr("alt"));
		var currentPos = $(this).prevAll().filter("img").length;
		var spalte = currentPos % 4;
		var zeile = (currentPos - spalte) / 4;

		var maxPos = $(this).parent().find("img").length;
		var maxZeile = (maxPos % 4 == 0) ? maxPos / 4 : (maxPos + 4 - maxPos % 4) / 4;

		// Linker Mouseover
		$("#lightbox_imagePreview_lineLeft").height($(this).height()).css("top", ($(this).height() + 10) * zeile).css("visibility", "visible");

		// Rechter Mouseover
		$("#lightbox_imagePreview_lineBottom").width($(this).width()).css("left", ($(this).width() + 10) * spalte).css("visibility", "visible");
		$("#lightbox_imagePreview_lineBottom").width($(this).width()).css("top", ($(this).height() + 10) * maxZeile);

	}).mouseleave(function() {
		$("#lightbox_imagePreview_lineLeft,#lightbox_imagePreview_lineBottom").animate({
			opacity : 0
		}, 2000);
	}).addClass("linkCursor");
}

function initPreviewImageClick() {
	$("#lightbox_imagePreviewWrapper img").unbind("click").click(function() {
		var currentPos = $(this).prevAll().filter("img").length;
		showDetailImage(currentPos, false);
	});
}

function initDetailImageClick() {
	$("#lightbox_imageDetail_leftKlick,#lightbox_arrowLeft").unbind("click").click(function() {
		prevImageKlick();
	});

	$("#lightbox_imageDetail_rightKlick,#lightbox_arrowRight").unbind("click").click(function() {
		nextImageKlick();
	});
}

function initDashboardClick() {
	$("#lightbox_dashboard").unbind("click mouseenter mouseleave").click(function() {
		showDashboard();
	}).addClass("linkCursor");
	
	if($.browser.msie){
		$("#lightbox_dashboard").css({opacity: 1});
	}
	else{
		$("#lightbox_dashboard").hover(function() {
			$(this).stop().animate({
				opacity : 1
			});
		}, function() {
			$(this).stop().animate({
				opacity : 0.6
			});
		});
	}
}


function initCloseClick() {
	$("#lightbox_close").unbind("click mouseenter mouseleave").click(function() {
		closeLightbox();
	}).addClass("linkCursor");

	if($.browser.msie){
		$("#lightbox_close").css({opacity: 1});
	}
	else{
		$("#lightbox_close").hover(function() {
			$(this).stop().animate({
				opacity : 1
			});
		}, function() {
			$(this).stop().animate({
				opacity : 0.6
			});
		});
	}
}

function initModalClick() {
	$("#lightbox_modal").unbind("click").click(function() {
		closeLightbox();
	});
}

function initSetup() {
	if ($("#lightbox_wrapper").size() == 0) {
		$(
				'<div id="lightbox_wrapper"><div id="lightbox_nav"><div id="lightbox_arrowLeft" title="vorheriges Bild"></div><div id="lightbox_headline"><span id="lightbox_headline_left"></span><span id="lightbox_headline_text"></span><span id="lightbox_headline_right"></span></div><div id="lightbox_arrowRight" title="nächstes Bild"></div><div id="lightbox_dashboard" title="zur Übersicht"></div><div id="lightbox_close" title="schliessen"></div></div><div id="lightbox_imagePreviewWrapper"><div id="lightbox_imagePreview_lineLeft"></div><div id="lightbox_imagePreview_lineBottom"></div></div><div id="lightbox_imageDetailWrapper"><div id="lightbox_imageDetail_imageWrapper"><div id="lightbox_imageDetail_lineLeft"></div><div id="lightbox_imageDetail_leftKlick"></div><img src=""/><div id="lightbox_imageDetail_rightKlick"></div><div id="lightbox_imageDetail_lineRight"></div></div></div></div><div id="lightbox_modal"></div>')
				.appendTo("body");
	}

	$("#lightbox_imageDetail_leftKlick,#lightbox_arrowRight,#lightbox_imageDetail_rightKlick,#lightbox_arrowLeft").unbind('click');
	$("#lightbox_imageDetailWrapper, #lightbox_imagePreviewWrapper").hide().css("opacity", 0);
	$("#lightbox_modal,#lightbox_wrapper").css("opacity", 0).hide();
}

function closeLightbox() {
	$("#lightbox_modal,#lightbox_wrapper").stop().animate({
		opacity : 0
	}, function() {
		$(this).hide();
	});
	stopAndHideAllRunningPlayer();
}

/**
 * Inits everything, based on Jason-URL - outdated
 * 
 * @param {Object}
 *            jsonURL
 */
function initGalerieWithJSON(jsonURL) {
	initSetup();

	$.getJSON(jsonURL, function(data) {
		var item = "";

		// Bildergalerie hinzufügen
		var countItems = countProperties(data.grafiken);
		countItems = (countItems > 16) ? 16 : countItems; // Maximal 16
		// Bilder sind
		// erlaubt
		for (i = 0; i < countItems; i++) {
			item += '<img src="' + data.grafiken[i]["media-thumb"] + '" alt="' + data.grafiken[i]["description"] + '" rel="' + data.grafiken[i]["media-big"] + '"/>';
		}

		$("#lightbox_imagePreviewWrapper img").remove();
		$("#lightbox_imagePreviewWrapper").append(item);

	});

	initCloseClick();
	initDashboardClick();
	initDetailImageClick();

	initPreviewImageMouseover();
	initPreviewImageProperties();
	initPreviewImageClick();

	initModalClick();
}

function initGalerieOutOfContext(contextID) {
	initSetup();

	var galerie = "a[rel='" + contextID + "']";
	currentlyDisplayedGalerie = contextID;

	if (contextID in galerieObjects) {
		// Diese Galerie wurde bereits initialisiert
	} else {
		galerieObjects[contextID] = new Array();
		var aktGal = galerieObjects[contextID];

		$("a[rel='" + contextID + "']").each(function(index, element) {
			var obj = new galerieObject("image");

			obj.description = $(element).attr("title");
			obj.pref_mini = $(element).find("img.contentPartFloat_media-thumb-mini").eq(0).attr("src");
			obj.pref_medium = $(element).find("img.contentPartFloat_media-thumb-medium").eq(0).attr("src");
			obj.pref_big = $(element).find("img.contentPartFloat_media-thumb-big").eq(0).attr("src");

			if (obj.pref_big == null) {
				obj.pref_big = $(element).attr("href");
			}

			// Suche Video-Quellen
			var videoObjekt = $(element).find(".video-js-box");

			if (videoObjekt.length > 0) {
				obj.videoDiv = videoObjekt;
				obj.type = "videoJSObjekt";
			}

			galerieObjects[contextID].push(obj);
		});
	}

	// Galerie Ausgabe erstellen
	// var anz = galerieObjects[contextID].length;
	var counter = 0;
	var item = "";
	for (i = 0; i < galerieObjects[contextID].length; i++) {
		var obj = galerieObjects[contextID][i];
		item += '<img src="' + obj.pref_mini + '" alt="' + obj.description + '" rel="' + obj.pref_big + '"/>';
		counter++;
	}

	$("#lightbox_imagePreviewWrapper img").remove();
	$("#lightbox_imagePreviewWrapper").append(item);

	initCloseClick();
	initDashboardClick();
	initDetailImageClick();

	initPreviewImageMouseover();
	initPreviewImageProperties();
	initPreviewImageClick();

	initModalClick();
}

/**
 * Before this function, Galerie needs to bee initialized
 */
function showDashboard() {
	
	stopAndHideAllRunningPlayer();
	
	$("#lightbox_imageDetail_leftKlick,#lightbox_arrowRight,#lightbox_imageDetail_rightKlick,#lightbox_arrowLeft,#lightbox_dashboard,#lightbox_arrowLeft,#lightbox_arrowRight")
			.unbind('click mouseenter mouseleave');
	$("#lightbox_dashboard,#lightbox_arrowLeft,#lightbox_arrowRight").removeClass("linkCursor");

	$("#lightbox_imagePreviewWrapper").stop(true, true).show().animate({
		opacity : 1
	}, 1000);
	
	if($.browser.msie){
		$("#lightbox_dashboard").css({
			opacity : 1
		});
	}else{
		$("#lightbox_dashboard").stop().animate({
			opacity : 0.6
		});
	}
	
	$("#lightbox_imageDetailWrapper").stop(true, true).animate({
		opacity : 0
	}, 1000).hide();

	$("#lightbox_wrapper").stop().show().animate({
		opacity : 1
	});

	$("#lightbox_modal").stop().show().animate({
		opacity : modalOpacity
	});
}

function showDetailImage(index, fadeOutOldImage) {

	if (index < 0 || index > $("#lightbox_imagePreviewWrapper img").size() - 1) {
		return;
	}

	stopAndHideAllRunningPlayer();
	initDetailImageClick();
	initDashboardClick();
	initCloseClick();

	$("#lightbox_imagePreviewWrapper").stop(true, true).animate({
		opacity : 0
	}, 1000).hide();

	$("#lightbox_imageDetailWrapper,#lightbox_wrapper").stop(true, true).show().animate({
		opacity : 1
	}, 1000);

	$("#lightbox_modal").stop().show().animate({
		opacity : modalOpacity
	});

	var prevImageObject = galerieObjects[currentlyDisplayedGalerie][index];
	var detailImageObject = $("#lightbox_imageDetail_imageWrapper img");

	var detailImagePath = prevImageObject.pref_big;
	var detailImageTitel = prevImageObject.description;

	// Preload Image
	var imgPreload = $('<img/>').load(function() {
		// After preloading
		var imageWidth = this.width;
		var imageHeigt = this.height;

		if (fadeOutOldImage) {
			$(detailImageObject).stop(true, true).fadeOut(250, function() {
				$(detailImageObject).attr({
					src : detailImagePath,
					title : detailImageTitel,
					rel : index
				});
				_afterShowDetailImageLoaded(index, imageWidth, imageHeigt, detailImageTitel);
			}).fadeIn(750);
		} else {
			$(detailImageObject).stop(true, true).attr({
				src : detailImagePath,
				title : detailImageTitel,
				rel : index
			});
			_afterShowDetailImageLoaded(index, imageWidth, imageHeigt, detailImageTitel);
			$(detailImageObject).fadeIn(750);
		}
	});

	switch(prevImageObject.type){
		case "image":
			// Start preloading
			imgPreload.attr('src', detailImagePath);
			prevImageObject.onView();
			break;
		case "videoJSObjekt":
			$(detailImageObject).fadeOut(100);
			// Remove old
			var test = $("#lightbox_imageDetail_imageWrapper .video-js-box");
			$("#lightbox_imageDetail_imageWrapper .video-js-box").remove();
			
			// Append new
			$("#lightbox_imageDetail_imageWrapper").append( prevImageObject.videoDiv );
			$(prevImageObject.videoDiv).find("video").css({height : "562px", 
				width : "754px"});
			
			// Preview Image
			$(prevImageObject.videoDiv).fadeIn(750);
			prevImageObject.onView();
			$(detailImageObject).attr({rel : index,
				src : detailImagePath,
				alt : detailImageTitel}
			); // Zum Vorwärts/rückwärts blättern
			_afterShowDetailImageLoaded(index, 754, 562, detailImageTitel, true);
			
			break;
		default:
	}
}

function stopAndHideAllRunningPlayer(){
	$('.video-js-box').each(function(){ 
		if($("video").length > 0){
			var test = $("video");
			$("video").each(function(index, element){
				element.pause();
			});
		}
		$(this).hide();
	});
}

function _afterShowDetailImageLoaded(index, width, height, title, hideLines) {

	if(hideLines == undefined)
		hideLines = false;
	
	$("#lightbox_imageDetail_imageWrapper").css({
		height : height,
		width : width
	});

	$("#lightbox_imageDetail_leftKlick, #lightbox_imageDetail_rightKlick").css("height", height + "px").css("width", parseInt(width / 3) + "px");

	var test = $("#lightbox_imageDetailWrapper").width();
	var test2 = $("#lightbox_imageDetailWrapper").outerWidth();
	var lineOpac = hideLines ? 0 : 0.6;
	lineOpac = ($.browser.msie) ? 0 : lineOpac;	 // Wenn IE, dann keine
													// Opacity sondern immer
													// ausblenden

	$("#lightbox_imageDetail_imageWrapper").css({
		left : parseInt((test2 - width) / 2)
	});
	$("#lightbox_imageDetail_lineLeft,#lightbox_imageDetail_lineRight").css("height", height);

	// Wenn klickWeiter / zurück möglich
	if (index == 0) {
		$("#lightbox_imageDetail_lineLeft").stop().css("opacity", 0);
		$("#lightbox_imageDetail_leftKlick,#lightbox_arrowLeft").unbind('mouseenter mouseleave');
		$("#lightbox_imageDetail_leftKlick,#lightbox_arrowLeft").unbind("click").removeClass("linkCursor");
		
		$("#lightbox_arrowLeft").stop().animate({
			opacity : lineOpac
		});
	} else {
		if($.browser.msie){
			$("#lightbox_imageDetail_leftKlick,#lightbox_arrowLeft").unbind('mouseenter mouseleave').css({opacity:1});
		}else{
			$("#lightbox_imageDetail_leftKlick,#lightbox_arrowLeft").unbind('mouseenter mouseleave').hover(function() {
				$("#lightbox_imageDetail_lineLeft,#lightbox_arrowLeft").stop().animate({
					opacity : 1
				}, 500);
			}, function() {
				$("#lightbox_arrowLeft").stop().animate({
					opacity : 0.6
				}, 500);
				$("#lightbox_imageDetail_lineLeft").stop().animate({
					opacity : 0
				}, 500);
			});
		}

		$("#lightbox_imageDetail_leftKlick,#lightbox_arrowLeft").unbind("click").click(function() {
			prevImageKlick();
		}).addClass("linkCursor");
	}

	if ($("#lightbox_imagePreviewWrapper img").size() - 1 <= index) {
		$("#lightbox_imageDetail_lineRight").stop().css("opacity", 0);
		$("#lightbox_imageDetail_rightKlick,#lightbox_arrowRight").unbind('mouseenter mouseleave');
		$("#lightbox_imageDetail_rightKlick,#lightbox_arrowRight").unbind("click").removeClass("linkCursor");
		$("#lightbox_arrowRight").stop().animate({
			opacity : 0.6
		});
	} else {
		if($.browser.msie){
			$("#lightbox_imageDetail_rightKlick,#lightbox_arrowRight").unbind('mouseenter mouseleave').css({opacity:1});
		}else{
			$("#lightbox_imageDetail_rightKlick,#lightbox_arrowRight").unbind('mouseenter mouseleave').hover(function() {
				$("#lightbox_imageDetail_lineRight,#lightbox_arrowRight").stop().animate({
					opacity : 1
				}, 500);
			}, function() {
				$("#lightbox_arrowRight").stop().animate({
					opacity : 0.6
				}, 500);
				$("#lightbox_imageDetail_lineRight").stop().animate({
					opacity : 0
				}, 500);
			});
		}
		
		$("#lightbox_imageDetail_rightKlick,#lightbox_arrowRight").unbind("click").click(function() {
			nextImageKlick();
		}).addClass("linkCursor");
	}

	$("#lightbox_headline_text").text(title);

}

function nextImageKlick() {
	// Schutz vor Doppelklicks
	var nowTime = new Date().getTime();
	if (nowTime - lastKlick < minInterval) {
		return;
	} else {
		lastKlick = nowTime;
	}

	var aktIndex = parseInt($("#lightbox_imageDetail_imageWrapper img").attr("rel"));
	var nextIndex = aktIndex + 1;

	if ($("#lightbox_imagePreviewWrapper img").size() - 1 <= aktIndex) {
		return;
	}

	showDetailImage(nextIndex, true);
}

function prevImageKlick() {
	// Schutz vor Doppelklicks
	var nowTime = new Date().getTime();
	if (nowTime - lastKlick < minInterval) {
		return;
	} else {
		lastKlick = nowTime;
	}

	var aktIndex = parseInt($("#lightbox_imageDetail_imageWrapper img").attr("rel"));
	var nextIndex = aktIndex - 1;

	if (aktIndex == 0) {
		return;
	}

	showDetailImage(nextIndex, true);
}

