/* Developed by Tristan Brehaut for Yell Adworks */

/* 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 padding = 10;
var sparklesArray = new Array();
var current = 0;
var nrnr = 0;
$(document).ready( function(){
	/* append lightbox to page */
	$('body').append(dimTheLightsHTML, lightBoxHTML);
	
	/* collect the sparkles*/
	$('a[rel="lightbox"], a[lang="lightbox"], div[lang="lightbox"]').each( function(index){
		sparklesArray[index] = new Array ($(this).attr('href'), $(this).attr('type'));
	});
	
	
	
	/* 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 sparklesArray){
			if(sparklesArray[key][0]==$(this).attr('href')){
				loadImage(key);
				current = key;
			}
		}
		return false;
	});
	
	$('.forward').click( function(){
		loadImage((parseInt(current) + 1));
	});
	$('.backward').click( function(){
		loadImage((parseInt(current) - 1));
	});
	

	/* close functions on btnClose and dimTheLights click, preventing close on lightbox click */
	$('#dimTheLights, #btnClose').click(function(){
		$('#dimTheLights, #lightBox, #lightBoxLoading').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();
	});
	$(window).resize( function(){
		$('#dimTheLights').css( { height: $(document).height(), width: $(window).width() } );
		$('#lightBox').center();
	});

});
	

/* LOADING IMAGE NOW, get to ze choppa*/
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=' + sparklesArray[number][0] + ' id="lightBoxIMG" />');
	$('#lightBoxCaption').html( sparklesArray[number][1] );
	$('#lightBoxIMG, #lightBoxCaption, #btnClose').css( 'display', 'none' );
	
			
	$('#lightBoxIMG').load( function(){
		/* remove loading */
		$('#lightBoxLoading').css( 'display', 'none' );
		/* reset height and width */
		imgWidth = $(this).width();
		//alert($('#lightBoxIMG').width());
		imgHeight = $(this).height();
		if(imgWidth == 0) {
			loadImage (number);
		}
		
		//$('#lightBox').css( { width: (imgWidth+(padding*2)) } ).center();
		$('#lightBoxCaption').css( { width: imgWidth } )
		captionHeight = $('#lightBoxCaption').height();
		if( captionHeight >= 1 ) { 
			captionHeight = captionHeight + (padding*2);
		} else {
			captionHeight = 25;
		}
		$('.backward, .forward').css('height', (imgHeight-padding));
		//$('#lightBox').css( { width: 200, height: 200 } ).center();
		
			$('.forward').css('width', '50%');
			$('.backward').css('width', '50%');
		if(parseInt(current) + 1 == sparklesArray.length){
			$('.forward').css('display', 'none');
			$('.backward').css('width', '100%');
		}else		{
			$('.forward').css('display', 'block');
			
		}
		
		if((parseInt(current) - 1) < 0 ){
			$('.backward').css('display', 'none');
			$('.forward').css('width', '100%');
		}else{
			$('.backward').css('display', 'block');
			
		}
		
		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 */
jQuery.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;
}

