var LightBox = function() {} ;


LightBox.prototype = {
	images: [],
	current: 0,
	animating: false,
	
	create: function()
	{
		$('body').prepend('<div id="darken"></div><div id="lightbox"><div class="arrow_left"></div>'+
		'<div class="arrow_right"></div><div class="description">Beschreibung</div><div class="img"></div><div class="close"></div></div>') ;
		
		var self = this ;
		
		$('#lightbox_img').children().each(function() {
			
			self.images.push($(this)) ;
			
			$(this).css({display: 'none'}) ;
			
			$('#lightbox .img').append($(this)) ;
			
		}) ;
		
		
		$('#darken').click(function() {
			self.close() ;
		}) ;
		
		$('.arrow_left').click(function() {
			self.prev() ;
		}) ;
		
		$('.arrow_right').click(function() {
			self.next() ;
		}) ;	
		
		$('.close').click(function() {
			self.close() ;
		}) ;
		
	},
	
	open: function(target) {
		
		var self = this ;
		
		
		$(window).keydown(function(event) {
			if(event.keyCode == 37)
			{
				self.prev() ;
			}

			if(event.keyCode == 39)
			{
				self.next() ;
			}
		});
		
		
		this.current = 0 ;
		
		if(typeof target !== 'undefined')
		{
			this.current = target ;
		}
		
		
		//$('#lightbox').css({display: 'block'}) ;
		
		//$('#lightbox .img').html(this.images[this.current]) ;
		
		this.switchContent(false) ;
		
		$('#darken').css({display: 'block'}).stop(true).animate({opacity: 1}) ;
		
		$('#lightbox').css({display: 'block'}).stop(true).animate({opacity: 1}) ;
		
		
	},
	
	close: function() {
		
		$(window).unbind('keydown') ;
		
		$('#darken').stop(true).animate({opacity: 0}).queue(function() {
			$(this).css({display: 'none'})
		}) ;
		
		$('#lightbox').stop(true).animate({opacity: 0}).queue(function() {
			$(this).css({display: 'none'})
			
			//$('#lightbox .img').empty() ;
		}) ;
	},
	
	prev: function() {
		
		if(this.animating !== false)
		{
			return true ;
		}
		
		
		if(this.current > 0)
		{
			this.animating = true ;
			
			this.current = this.current - 1 ;
		
			this.switchContent(true) ;
		}
		
	},
	
	
	next: function() {
		
		if(this.animating !== false)
		{
			return true ;
		}
		
		
		if(this.images.length > this.current+1)
		{
			this.animating = true ;
		
			this.current = this.current + 1 ;
		
			this.switchContent(true) ;
			
		}
		
	},
	
	switchContent: function(animate) {
		
		var self = this ;
		
		var setCSS = function() {
		
		$('#lightbox .img').children().css({display: 'none'}) ;
		
		self.images[self.current].css({display: 'block'}) ;
		
		var width = (Math.round($('#lightbox').outerWidth()/2)*(-1)) ;
		var height = (Math.round($('#lightbox').outerHeight()/2)*(-1)) ;
		
		$('#lightbox').css({'margin-left': width, 'margin-top': height}) ;
		
		$('.arrow_left').css({display: 'block'}) ;
		$('.arrow_right').css({display: 'block'}) ;
		
		$('#lightbox .description').empty() ;
		
		try
		{
			$('#lightbox .description').html(self.images[self.current].attr('alt')) ;
		}
		catch(error)
		{
		}
		
		
		if(self.images.length === self.current+1)
		{
			$('.arrow_right').css({display: 'none'}) ;
		}
		
		if(self.current === 0)
		{
			$('.arrow_left').css({display: 'none'}) ;
		}
		
		}
		
		//alert(animate) ;
		
		if(animate !== false)
		{
			
			$('#lightbox').animate({opacity: 0}, 300).queue(function() {
				
				setCSS() ;
				$(this).dequeue() ;
				
			}).animate({opacity: 1}).queue(function() {
				
				self.animating = false ;
				
				$(this).dequeue() ;	
			}) ;
			
			
		}
		
		else
		{
			self.animating = false ;
			setCSS() ;
		}
				
	}
}


lightbox = new LightBox() ;
