var _lightBox = false;

$(document).ready(function() {
	if (!_lightBox) {
		_lightBox = true;
		var a = $("a[rel='lightbox']");
		a.click(function() {
			this.blur();
			lightBox($(this).attr('href'), $(this).attr('rev'), false);
			return false;
		});
	}
});

function lightBox(src, galid, soft) {
	var img = new Image();

	var anchor = $('a[href="' + src + '"]');

	var title = anchor.attr('title');

	if (!title) {
		title = anchor.find('img').attr('title');
		if (!title) title = '';
	}

	$("#lightbox_controls_prev").unbind();
	$("#lightbox_controls_next").unbind();

	if (soft && $("#lightbox_image").get(0)) {

		$("#lightbox_controls").fadeOut(100);
		$("#lightbox_controls_prev").fadeOut(100);
		$("#lightbox_controls_next").fadeOut(100);

		$("#lightbox_image").fadeOut(250, function() {

			$("#lightbox_image").remove();

			$("#lightbox_frame").css('background-image', 'url(lightbox/load.gif)');

			img.src = src;

			if (img.width && img.height) {
				setTimeout(function() { lightBoxShow(img, galid, title); }, 500);
			} else {
				$(img).load(function() {
					lightBoxShow(this, galid, title);
				});
			}
		});

	} else {

		img.src = src;

		if (img.width && img.height) {
			setTimeout(function() { lightBoxShow(img, galid, title); }, 500);
		} else {
			$(img).load(function() {
				lightBoxShow(this, galid, title);
			});
		}

		$('object').hide();
		$('embed').hide();

		$("#lightbox_image").remove();

		var shader = initLightBoxShader();

		var frame = getLightBoxFrame();

		frame.hide();

		var x = Math.round($("body").width() / 2 - frame.width() / 2);
		var y = Math.round(winHeight() / 2 - frame.height() / 1.5) + $(document).scrollTop();
		y = Math.max(y, 20);
		frame.css("left", x);
		frame.css("top", y);

		shader.fadeTo(300, 0.5, function() {
			frame.fadeIn(300);
		});

	}
}

function lightBoxShow(img, galid, title) {

	var w = img.width;
	var h = img.height;

	var closer = $("#lightbox_closer");

	var controls = $("#lightbox_controls");
	controls.data("galid", galid);
	controls.data("src", img.src);
	controls.data("title", title);

	var frame = $("#lightbox_frame");
	frame.css('background-image', 'none');
	frame.prepend('<img id="lightbox_image" src="' + img.src + '" width="' + w + '" height="' + h + '" alt="" />');

	var image =  $("#lightbox_image");

	var dw = w + parseInt(image.css("margin-left"), 10) + parseInt(image.css("margin-right"), 10);
	var dh = h + parseInt(image.css("margin-top"), 10) + parseInt(image.css("margin-bottom"), 10);

	if (!dw) dw = w + 8;
	if (!dh) dh = h + 8;

	var dx = Math.round($("body").width() / 2 - dw / 2);
	var dy = Math.round(winHeight() / 2 - dh / 2) + $(document).scrollTop();
	dy = Math.max(dy, 20);

	frame.animate({
		width:dw,
		height:dh,
		left:dx,
		top:dy
		},
		500,
		"swing",
		function() {
			image.fadeIn(300, lightBoxOn);
		}
	)

}

function initLightBoxShader() {
	$("#lightbox_shader").remove();
	$("body").append('<div id="lightbox_shader"></div>');

	var shader = $("#lightbox_shader");

	shader.css("opacity", 0);

	shader.click(function() {
		lightBoxClose();
	});

	shader.css("top", 0);
	shader.css("height", Math.max(winHeight(), Math.max($(document).height(), $('body').height())) + "px");

	return shader;
}

function getLightBoxFrame() {
	if ($("#lightbox_frame").get(0)) {
		var frame = $("#lightbox_frame");
		frame.html("");
	} else {
		$("body").append('<div id="lightbox_frame"></div>');
		var frame = $("#lightbox_frame");
	}
	frame.append('<a id="lightbox_closer">&nbsp;</a>');
	frame.append('<div id="lightbox_controls">&nbsp;</div>');
	frame.append('<a id="lightbox_controls_prev">&nbsp;</a>');
	frame.append('<a id="lightbox_controls_next">&nbsp;</a>');

	var closer = $("#lightbox_closer");
	closer.hide();

	var controls = $("#lightbox_controls");
	controls.hide();
	controls.data("galid", false);
	controls.data("src", false);
	controls.data("title", '');

	ctrl_prev = $("#lightbox_controls_prev");
	ctrl_prev.hide();
	ctrl_next = $("#lightbox_controls_next");
	ctrl_next.hide();

	frame.css('background-image', 'url(lightbox/load.gif)');

	return frame;
}

function lightBoxButtonsIn() {
	var closer = $("#lightbox_closer");
	var prev = $("#lightbox_controls_prev");
	var next = $("#lightbox_controls_next");
	closer.stop();
	prev.stop();
	next.stop();
	closer.fadeTo(150, 1);
	if (prev.data("visible")) prev.fadeTo(150, 1);
	if (next.data("visible")) next.fadeTo(150, 1);
}

function lightBoxButtonsOut() {
	var closer = $("#lightbox_closer");
	var prev = $("#lightbox_controls_prev");
	var next = $("#lightbox_controls_next");
	closer.stop();
	prev.stop();
	next.stop();
	closer.fadeTo(150, 0);
	prev.fadeTo(150, 0);
	next.fadeTo(150, 0);
}

function lightBoxOn() {
	initLightBoxActions(true);
}

function initLightBoxActions(init) {
	var frame = $("#lightbox_frame");
	var closer = $("#lightbox_closer");
	var prev = $("#lightbox_controls_prev");
	var next = $("#lightbox_controls_next");
	closer.show();
	closer.fadeTo(0, 0);
	var controls = $("#lightbox_controls");
	var image = $("#lightbox_image");
	if (init) {
		if (controls.data("galid")) {
			initLightBoxControls(controls);
		}
		closer.click(function() {
			lightBoxClose();
		});
		image.mouseover(function() {
			lightBoxButtonsIn();
		});
		image.mouseout(function() {
			lightBoxButtonsOut();
		});
		closer.mouseover(function() {
			lightBoxButtonsIn();
		});
		closer.mouseout(function() {
			lightBoxButtonsOut();
		});
		prev.mouseover(function() {
			lightBoxButtonsIn();
		});
		prev.mouseout(function() {
			lightBoxButtonsOut();
		});
		next.mouseover(function() {
			lightBoxButtonsIn();
		});
		next.mouseout(function() {
			lightBoxButtonsOut();
		});
	} else {
		closer.unbind();
		prev.unbind();
		next.unbind();
		image.unbind();
		frame.unbind();
	}
}

function initLightBoxControls(controls) {
	var images = new Array();
	var galid = controls.data("galid");
	var src = controls.data("src");
	var current = -1;
	var ostr = '';
	$("a[rel='lightbox']").each(function() {
		if ($(this).attr("rev") == galid) {
			ostr += '\npush: ' + $(this).attr('href');
			images.push($(this).attr('href'));
			if (lightBoxMatchSrc($(this).attr('href'), src)) current = images.length - 1;
		}
	});
	var ostr = '<h6 id="lightbox_title">' + controls.data('title') + '</h6>';
	for (var i=0; i<images.length; i++) {
		var lsrc = "'" + images[i] + "'";
		var active = (current == i)? 'lightbox_current' : '';
		ostr += '<a onclick="lightBox(' + lsrc + ', ' + galid + ', true)" class="lightbox_image ' + active + '">' + (i + 1) + '</a> ';
	}
	ostr += '<span>Bild&nbsp;' + (current + 1) + '&nbsp;von&nbsp;' + images.length + '</span><div class="lb_clear"></div>';
	controls.html(ostr);
	controls.fadeIn(400);
	$("#lightbox_controls_prev").click(function() {
		if (current > 0) lightBox(images[current - 1], galid, true);
	});
	$("#lightbox_controls_next").click(function() {
		if (current < images.length - 1) lightBox(images[current + 1], galid, true);
	});
	if (current > 0) {
		$("#lightbox_controls_prev").show();
		$("#lightbox_controls_prev").data("visible", true);
	} else {
		$("#lightbox_controls_prev").hide();
		$("#lightbox_controls_prev").data("visible", false);
	}
	$("#lightbox_controls_prev").fadeTo(0, 0);
	if (current < images.length - 1) {
		$("#lightbox_controls_next").show();
		$("#lightbox_controls_next").data("visible", true);
	} else {
		$("#lightbox_controls_next").hide();
		$("#lightbox_controls_next").data("visible", false);
	}
	$("#lightbox_controls_next").fadeTo(0, 0);
}

function lightBoxMatchSrc(str1, str2) {
	str1 = str1.split("/");
	str1 = str1[str1.length - 1];
	str2 = str2.split("/");
	str2 = str2[str2.length - 1];
	return (str1 == str2);
}

function lightBoxClose() {
	$('object').show();
	$('embed').show();
	initLightBoxActions(false);
	$("#lightbox_shader").fadeOut(200);
	$("#lightbox_frame").fadeOut(200, function() {
		$("#lightbox_shader").remove();
	});
}

function winHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} else if (document.body.offsetHeight) {
		return document.body.offsetHeight;
	} else {
		return $("body").height();
	}
}