var sdcIntroPosTop, sdcIntroPosLeft;

(function($) {

	var win = $(window), options, images, activeImage = -1, activeURL, compatibleOverlay, middle, centerWidth, centerHeight,
		ie6 = !window.XMLHttpRequest, hiddenElements = [], documentElement = document.documentElement,

	overlay, center, image, number;

	$(function() {
		$("body").append(
			$([
				overlay = $('<div id="lbOverlay" />').click(close)[0],
				center = $('<div id="lbCenter" />')[0]
			]).css("display", "none")
		);
	});

	$.slimbox = function(startIndex, _options) {
		
		options = $.extend({
			loop: false,				// Allows to navigate between first and last images
			overlayOpacity: 0.5,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
			overlayFadeDuration: 400,		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
			resizeDuration: 400,			// Duration of each of the box resize animations (in milliseconds)
			resizeEasing: "swing",			// "swing" is jQuery's default easing
			initialWidth: 510,			// Initial width of the box (in pixels)
			initialHeight: 695,			// Initial height of the box (in pixels)
			
			initialPosTop: sdcLayerOptions.introTop,	
			initialPosLeft:sdcLayerOptions.introLeft,	
			
			imageFadeDuration: 400,			// Duration of the image fade-in animation (in milliseconds)

			counterText: "Image {x} of {y}",	// Translate or change as you wish, or set it to false to disable counter text for image groups
			closeKeys: [27], //, 88, 67],		// Array of keycodes to close Slimbox, default: Esc (27), 'x' (88), 'c' (67)
			previousKeys: [37, 80],			// Array of keycodes to navigate to the previous image, default: Left arrow (37), 'p' (80)
			nextKeys: [39, 78]			// Array of keycodes to navigate to the next image, default: Right arrow (39), 'n' (78)
		}, _options);

		
		middle = win.scrollTop() + (win.height() / 2);
		centerWidth = options.initialWidth;
		centerHeight = options.initialHeight;
		$(center).css({top: options.initialPosTop, width: centerWidth, height: centerHeight}).show();
		//$(center).css({top: Math.max(0, middle - (centerHeight / 2)), width: centerWidth, height: centerHeight, marginLeft: -centerWidth/2}).show();
		compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed"));
		if (compatibleOverlay) overlay.style.position = "absolute";
		$(overlay).css("opacity", options.overlayOpacity).fadeIn(options.overlayFadeDuration);
		position();
		setup(1);

		stop();
		center.className = "lbLoading";


		return false;
	
	};

	$.fn.slimbox = function(startIndex,_options) {
		
		$.slimbox(0, _options);
	};


	function position() {
		
		var l = win.scrollLeft(), w = win.width();
		$([center]).css("top", options.initialPosTop);
		$([center]).css("left", options.initialPosLeft);
		if (compatibleOverlay) $(overlay).css({left: l, top: win.scrollTop(), width: w, height: win.height()});
	}

	function setup(open) {
		
		if (open) {
			$("object").add(ie6 ? "select" : "embed").each(function(index, el) {
				hiddenElements[index] = [el, el.style.visibility];
				el.style.visibility = "hidden";
			});
		} else {
			$.each(hiddenElements, function(index, el) {
				el[0].style.visibility = el[1];
			});
			hiddenElements = [];
		}
		var fn = open ? "bind" : "unbind";
		win[fn]("scroll resize", position);
		$(document)[fn]("keydown", keyDown);
	}

	function keyDown(event) {
		var code = event.keyCode, fn = $.inArray;
		return (fn(code, options.closeKeys) >= 0) ? close()
			: (fn(code, options.nextKeys) >= 0) ? next()
			: (fn(code, options.previousKeys) >= 0) ? previous()
			: false;
	}

	function stop() {
		$([center]).stop(true);
	}

	function close() {
			$.cookie('_sdc_sdc_herten', 'sdcX00', { path: '/'});
			stop();
			$(center).hide();
			$(overlay).stop().fadeOut(options.overlayFadeDuration, setup);
		return false;
	}

})(jQuery);
