/* Developed by Tristan Brehaut for Yell Adworks - Genetically Engineered by Lauris */

/* lightbox and dimTheLights HTML */
var dimTheLightsHTML = '<div class="dimTheLights" id="dimTheLights"></div>';
var lightBoxHTML = '<div class="lightBox" id="lightBox"><div id="lightBoxIMGWrap" class="lightBoxIMGWrap"></div><div id="lightBoxLoading" class="lightBoxLoading"></div><div id="lightBoxCaption" class="lightBoxCaption"></div><div id="btnClose" class="btnClose"></div><div class="backward"></div><div class="forward"></div></div>';
var sitemapBoxHTML = '<div id="sitemapBox"><h3>Sitemap</h3><div id="btnCloseSitemap" class="btnClose"></div>!MENU!<p style="text-align:right; color: #ddd; text-transform:uppercase; font-size:10px; line-height: 12px;">'+Date()+'</p></div>';
var padding = 10;
var lightboxArray = new Array();
var current = 0;
var lightboxImageCount = 0;
var nrnr = 0;

$(document).ready( function(){
	/* append lightbox to page */
	$('body').append( dimTheLightsHTML, lightBoxHTML, sitemapBoxHTML.replace( /!MENU!/, $('.sitemenu').html() ) );
	
	/* do sitemap */
	$('#sitemapBox').css({display: 'none'}).center();
	$('#sitemap').click( function(){
		$('#dimTheLights').css( { height: $(document).height(), width: $(window).width(), opacity: '0.8' } );
		$('#dimTheLights, #sitemapBox, #btnCloseSitemap').fadeIn( 'slow' );
		return false;
	});
	
	/* collect the images */
	$('a[rel="lightbox"], a[lang="lightbox"], div[lang="lightbox"]').each( function(index){
		lightboxArray[index] = new Array ($(this).attr('href'), $(this).attr('title'));
		lightboxImageCount++;
	});
	
	/* lightbox activate on click */
	$('a[rel="lightbox"], a[lang="lightbox"], div[lang="lightbox"]').click( function(){
		
		/* set dimTheLights to cover document/window, fade in elements */
		$('#dimTheLights').css( { height: $(document).height(), width: $(window).width(), opacity: '0.8' } );
		
		/* loading */
		$('#lightBox').css( { width: 200, height: 200 } ).center();
		
		/* find position in array and process image load */
		for(key in lightboxArray){
			if(lightboxArray[key][0]==$(this).attr('href')){
				number = current = parseInt(key);
				loadImage(key);
			}
		}
		return false;
	});
	
	$('.forward').click( function(){
		current += 1; if( current > lightboxImageCount-1 ) current = 0;
		loadImage(current);
	});
	$('.backward').click( function(){
		current -= 1; if( current < 0 ) current = lightboxImageCount-1;
		loadImage(current);
	});
	

	/* close functions on btnClose and dimTheLights click, preventing close on lightbox click */
	$('#dimTheLights, #btnClose, #btnCloseSitemap').click(function(){
		$('#dimTheLights, #lightBox, #lightBoxLoading, #sitemapBox').fadeOut( 'fast' );
		return false;
	});
	
	$('#lightBox').click( function(){ return false; }).bind('contextmenu', function(){
		$('#dimTheLights, #lightBox, #lightBoxLoading').fadeOut( 'fast' );
		return false;
	});

	/* keep lightbox centered */
	$(window).scroll( function(){
		$('#dimTheLights').css( { height: $(document).height(), width: $(window).width() } );
		$('#lightBox').center();
		$('#sitemapBox').center();
	});
	$(window).resize( function(){
		$('#dimTheLights').css( { height: $(document).height(), width: $(window).width() } );
		$('#lightBox').center();
		$('#sitemapBox').center();
	});

});


/* LOADING IMAGE NOW */
function loadImage (number) {
	//current = number;
	$('#dimTheLights, #lightBox').fadeIn( 'slow' );
	$('#lightBoxLoading').css( 'display', 'block' );
	/* set img src and caption from <a> attributes */
	$('#lightBoxIMGWrap').html( '<img src=' + lightboxArray[number][0] + ' id="lightBoxIMG" />');
	$('#lightBoxCaption').html( lightboxArray[number][1] );
	$('#lightBoxIMG, #lightBoxCaption, #btnClose').css( 'display', 'none' );
	
			
	$('#lightBoxIMG').load( function(){
		/* remove loading */
		$('#lightBoxLoading').css( 'display', 'none' );
		/* reset height and width */
		imgWidth = $(this).width();
		imgHeight = $(this).height();
		if(imgWidth == 0) {
			loadImage (number);
		}
		
		$('#lightBoxCaption').css( { width: imgWidth } )
		captionHeight = $('#lightBoxCaption').height();
		if( captionHeight >= 1 ) { 
			captionHeight = captionHeight + (padding*2);
		} else {
			captionHeight = padding;
		}
		
		if( lightboxImageCount <= 1 ){
			$('.backward, .forward').css( 'display', 'none' );
		} else {
			$('.backward, .forward').css('height', imgHeight);
			$('.forward').css('width', '50%');
			$('.backward').css('width', '50%');
		}
		
		lbTop = (($(window).height() - imgHeight) / 2) + $(window).scrollTop() - (captionHeight) - 18;
		lbLeft = ($(window).width() - imgWidth) / 2;
		$('#lightBox').animate( 
			{ top: lbTop, left: lbLeft, width: imgWidth, height: imgHeight + captionHeight }, 
			{ queue: false, duration: 'fast', complete: function() {
				$('.lightBox').css('overflow', 'visible');
				$('#lightBoxIMG, #lightBoxCaption, #btnClose').fadeIn( 'fast' );
			} }
		);
	});
}

/* center function */
(function( $ ){
	$.fn.center = function () {
		this.css('position','absolute');
		this.css('top', ( ($(window).height() - this.height()) / 2 ) + $(window).scrollTop() - 10 );
		this.css('left', ( ($(window).width() - this.width()) / 2 ) );
		return this;
	};
})( jQuery );

