(function($){
	$.fn.extend({ 

		dzLightbox: function(options) {

			//Defaults ---------------------------------------------------------------------------------------------
			var defaults = {
				closeText:'Fenster schließen',
				showText:'Text einblenden',
				hideText:'Text ausblenden'
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
									  
				//Vars ---------------------------------------------------------------------------------------------
				var o = options;
				var obj = $(this);
				var containerWrap = $('.dz-ligtbox-holder');
				var win = $(window);
				var doc = $(document);
				var links = $('.dz-lightbox-link');
				var index = 0;
				var sliderIndex = 0;
				var showDesc = true;
				var layerIsOpen = false;
				
				obj.prepend('<div class="dz-lightbox-close"><span>'+o.closeText+'</span></div>');
				obj.wrapInner('<div class="dz-lightbox-container" />');
				obj.prepend('<div class="dz-lightbox-overlay" />');
				obj.prepend('<div class="dz-lightbox-wrapper" />');
				
							
				var overlay = $('.dz-lightbox-overlay', obj);
				var container = $('.dz-lightbox-container',obj);
				var thumbWrap = $('.dz-lightbox-thumbs', obj);
				var thumbs = $('.dz-lightbox-thumbs-thumb img', obj);
				var close = $('.dz-lightbox-close').click(function(){closeLayer()});
				var imageWrap = $('.dz-lightbox-image');
				var imageArea = $('.dz-lightbox-image-area');
				var desc = $('.dz-lightbox-desc',obj).css('opacity',.8);
				var descButton = $('.dz-lightbox-desc-toggle',obj).click(function(){toggleDesc()});
				var descText = $('.dz-lightbox-desc-text',obj);
				var prev = $('.dz-lightbox-prev',obj);
				var next = $('.dz-lightbox-next',obj);
				
				var thumbsPrev = $('.dz-lightbox-thumbs-prev',obj);
				var thumbsNext = $('.dz-lightbox-thumbs-next',obj);
				var thumbSlider = $('.dz-lightbox-thumbs-slider',obj);
				
				
				//Init ---------------------------------------------------------------------------------------------
				obj.css({width:win.width(),height:doc.height(),opacity:1}).hide();
				containerWrap.css({width:win.width(),height:doc.height()});
				overlay.css({width:win.width(),height:1700,opacity:.8}).click(function(event){closeLayer()});	
				container.css({marginTop:(win.height()/2-250)}).prependTo(containerWrap);
				thumbSlider.width(thumbs.length*90);
				  
				//Items ---------------------------------------------------------------------------------------------
				thumbs.each(function(index,el){
					var parent = $(el).parent();
					var image = $('.meta-image-file',parent).html();
					var desc = $('.meta-image-desc',parent).html();
					var newSliderIndex = Math.floor(((index)/7));
					
					$(el).click(function(){
						loadImage(index,image,desc,newSliderIndex);					 
					});
					if(index==0)$(el).addClass('current-image');
				});
				
				//Buttons ---------------------------------------------------------------------------------------------
				prev.click(function(){loadImageById(index,-1)}).hide();
				next.click(function(){loadImageById(index,1)});
				
				thumbsPrev.click(function(){slideThumbs(sliderIndex,-1)}).addClass('inactive');
				thumbsNext.click(function(){slideThumbs(sliderIndex,1)});
				
				//Links ----------------------------------------------------------------------------------------------
				links.click(function(){
					openLayer(parseInt($(this).attr('rel')));
				});
				
				//Load Image ---------------------------------------------------------------------------------------------
				var loadImage = function(newIndex,image,descText,newSliderIndex){
					index=newIndex;
					containerWrap.find('.current-image').removeClass('current-image');
	
					$(thumbs[index]).addClass('current-image');					
					imageWrap.animate({opacity:0},300,function(){
						imageWrap.find('img').attr('src',image);
					});
					
					imageArea.find('img')
						.one('load',function(){
							$(this).parent().parent().animate({opacity:1},500,function(){
								showHideButtons(false);
							}).find('.dz-lightbox-desc-text').html(descText);
							(showDesc) ? desc.css('marginBottom',0) : desc.css('marginBottom',-desc.height()+12);
							if(!layerIsOpen){
								layerIsOpen=true;
								imageWrap.animate({height:400},500);
								container.animate({height:490},500);
								thumbWrap.animate({marginTop:420},500);
							}			 
						})
						.each(function(){
							if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) $(this).trigger("load");
						});
					
					showHideButtons(true);
					slideThumbs(newSliderIndex,0);
				}
				
				function loadImageById(id,dir){
				
					index = id + dir;
					var parent = $(thumbs[index]).parent();
					var image = $('.meta-image-file',parent).html();
					var desc = $('.meta-image-desc',parent).html();
					var newSliderIndex = Math.floor(((index)/7));
					trace('@func loadImageById > '+index+' '+image);
					loadImage(index,image,desc,newSliderIndex);
				}
				
				
				//Show and Hide Buttons and Items ---------------------------------------------------------------------------------------------
				var showHideButtons = function(hideAll){	
					(hideAll||index==0) ? prev.hide() : prev.show();
					(hideAll||index+1>=thumbs.length) ? next.hide() : next.show();
				}
				
				
				//Thumbs Slider
				var slideThumbs = function(newSliderIndex,dir){
					if(sliderIndex!=newSliderIndex+dir){
						sliderIndex = newSliderIndex+dir;
						var newX = -sliderIndex*630;
						thumbSlider.animate({marginLeft:newX});
					}
					(sliderIndex==0) ? thumbsPrev.addClass('inactive') : thumbsPrev.removeClass('inactive');
					(thumbs.length/7<=sliderIndex+1) ? thumbsNext.addClass('inactive') : thumbsNext.removeClass('inactive');
				}
				
				//Resize Layer ---------------------------------------------------------------------------------------------
				var resizeLayer = function(){
					obj.css({width:win.width(),height:doc.height()});
					overlay.css({width:win.width(),height:doc.height()});
					container.css({marginTop:(win.height()/2-250)+win.scrollTop()});
				}
				win.resize(function(){resizeLayer()});
				
				//Toggle Description ---------------------------------------------------------------------------------------------
				var toggleDesc = function(event){
					trace(desc.css('marginBottom'));
					if(!showDesc){
						desc.animate({marginBottom:0});
						descButton.text('Text ausblenden').removeClass('closed');
					} else {
						desc.animate({marginBottom:-desc.height()+12});
						descButton.text('Text einblenden').addClass('closed');
					}
					showDesc=!showDesc;
				}
				
				
				//Close ---------------------------------------------------------------------------------------------
				var closeLayer = function(){
					containerWrap.fadeOut(500);
					obj.animate({opacity:0},500,function(){obj.hide();layerIsOpen = false;});
				}
				
				//Open ---------------------------------------------------------------------------------------------
				var openLayer = function(id){
					layerIsOpen = false;
					resizeLayer();
					imageWrap.height(0);
					container.height(90);
					thumbWrap.css({marginTop:20});
					
					var internalId = 0;
					for(var k=0;k<thumbs.length;k++){
						if($(thumbs[k]).parent().attr('id')==id) internalId = k;
					}					
					
					containerWrap.hide().fadeIn(500);
					obj.show().animate({opacity:1},500,function(){
						loadImageById(internalId,0);
					});
				}
				
    		});
    	}
	});
})(jQuery);



