/****************************************************
	ZR JQuery Lightbox zoom script 2.1	 
	basic image zoom and gallery functionality
****************************************************/

var lbStage = 'ready';
var lbArray = new Array();
var lbTitleArray = new Array();
var lbGallery = false;
var zoomDetail = false;

//Set up all the HTML for the lightbox
var lbLoadHTML = '<img src="/images/lightbox/lightbox-load.gif" alt="loading" title="loading" id="lightboxload" />'
var maskHTML = '<div id="mask" onclick="closeLightBox();" title="Click to Close"></div>'+lbLoadHTML;
var lightBoxHTML = '<div id="lightbox"><div id="lightboxutil"><img src="/images/lightbox/close.png" alt="Close" title="Close" onclick="closeLightBox();" id="lbclose" /></div></div>';
var lbCloseBtn = '<img src="/images/lightbox/mini-close.png" id="lbminiclose" onclick="captionFade();" alt="Hide Caption" title="Hide Caption" />';
var prevLink = '';
var nextLink = '';

//core function that sets up the mask and the light box
var lightBoxZoom = function(lmnt){
	maskLoad();
	//Set up arrays for Lightbox Galleries Mode.
	$(".zoom").each(function (i) {
		lbArray[i] = this.href;
		lbTitleArray[i] = this.title;
	});
	if(lbArray.length > 1) { 
		lbGallery = true;
	}
	lbLoadContent(lmnt);
}

//function to set up the backound and preloader
var maskLoad = function() { 
	//Set up all the positioning variables
	docHeight = $(document).height();
	winHeight = $(window).height();
	winHeightFrag = Math.round(winHeight*1)/12;
	winWidth = $(window).width();
	scrollTop = $(window).scrollTop();
	lbTopPos = scrollTop + winHeightFrag;
	lbLoadTopPos = (((Math.round(winHeight)/2)+scrollTop)-16);
	lbLoadLeftPos = ((Math.round(winWidth)/2)-16);
	lbStage = 'loading';
	$("body").prepend(maskHTML);
	$("#mask").css("height",docHeight);
	$("#lightboxload").css("top",lbLoadTopPos);
	$("#lightboxload").css("left",lbLoadLeftPos);
}

//Core function that loads the Image to the lightbox
var lbLoadContent = function(lmnt){
	//Set up the Image properties
	var imgPath = lmnt.href;
	var lbCaption = lmnt.title;
	var lbImg = new Image();
	
	$(lbImg).load(function() { 
		//This all happens after the image loads
		var lbImgHeight = this.height;
		var lbImgWidth = this.width;
		var targetImgHeight = parseInt(winHeight * .67);
		var targetImgWidth = parseInt(winWidth * .83);
		var lbHeightDiff = lbImgHeight - targetImgHeight;
		var lbWidthDiff = lbImgWidth - targetImgWidth;
		/*alert(
			'window height is ' + winHeight + 
			'\nwindow width is ' + winWidth + 
			'\nImage height is ' + lbImgHeight + 
			'\nImage width is ' + lbImgWidth +
			'\nTarget height is ' + targetImgHeight + 
			'\nTarget width is ' + targetImgWidth +
			'\nHeight Difference is ' + lbHeightDiff + 
			'\nWidth Difference  is ' + lbWidthDiff 
		);*/
		$("#lightboxload").replaceWith(lightBoxHTML);
		$(this).attr('id','lightboximage');
		$("#lightbox").append(this);
		
		//Resizing the image if it is too large for the browser window
		if (lbHeightDiff > 0 || lbWidthDiff > 0) { 
			if (lbHeightDiff > lbWidthDiff) { 
				this.height = targetImgHeight;
				if($.browser.msie == true) {  
					//set the other dim for IE
					var imgDimRatio = targetImgHeight / lbImgHeight;
					this.width = parseInt(lbImgWidth * imgDimRatio);
				}
			} else
			if (lbWidthDiff > lbHeightDiff) { 
				this.width = targetImgWidth;
				if($.browser.msie == true) {  
					//set the other dim for IE
					var imgDimRatio = targetImgWidth / lbImgWidth;
					this.height = parseInt(lbImgHeight * imgDimRatio);
				}
			}
		}

		//Set up Previous and next content for Lightbox Galleries
		if(lbGallery == true){
			lbx=0;
			lastImg = lbArray.length - 1;
			for (lbx=0; lbx<lbArray.length; lbx++){
				if(imgPath == lbArray[lbx]){
					lbCaption = lbTitleArray[lbx];
					switch (lbx){
						case 0:
							prevImg = lbArray[lastImg];
							nextImg = lbArray[lbx+1];
							prevCaption = lbTitleArray[lastImg];
							nextCaption = lbTitleArray[lbx+1];
						break;
						case lastImg:
							prevImg = lbArray[lbx-1];
							nextImg = lbArray[0];
							prevCaption = lbTitleArray[lbx-1];
							nextCaption = lbTitleArray[0];
						break;
						default:
							prevImg = lbArray[lbx-1];
							nextImg = lbArray[lbx+1];
							prevCaption = lbTitleArray[lbx-1];
							nextCaption = lbTitleArray[lbx+1];
					}
				}
			}
			var prevLink = '<a href="'+prevImg+'" onclick="prevNextBox(this); return false;" title="'+prevCaption+'" class="lightboxprev"><img src="/images/lightbox/previous.png" alt="previous" /></a>';
			var nextLink = '<a href="'+nextImg+'" onclick="prevNextBox(this); return false;" title="'+nextCaption+'" class="lightboxnext"><img src="/images/lightbox/next.png" alt="next" /></a>';
			$("#lightboxutil").prepend(prevLink+nextLink);
			$("#lightboximage").wrap('<a href=' + nextImg + ' onclick="prevNextBox(this); return false;" />');
		}
		
		//Add in the caption if it exists
		if(lbCaption != '') { 
			$("#lightbox").append('<div id="lightboxcaption"></div>');
			$("#lightboxcaption").width($('#lightbox').width()-12);
			$("#lightboxcaption").html(lbCaption);
		}
		
		$("#lightbox").css("top",lbTopPos);
		var lbwidth = $("#lightbox").width();
		lbLeftPos = (Math.round(winWidth/2))-(Math.round(lbwidth/2));
		$("#lightbox").css("left",lbLeftPos);
		//Must have PNG fix enabled to fix the close button PNG on the fly
		$("#lightboxutil img").css("behavior","url(/iepngfix.htc)");
		$("#lightbox").fadeIn('normal',function() {
		//set the docHeight again because the lightbox can push it down.
		docHeight = $(document).height();
		$("#mask").css("height",docHeight);
			lbStage = 'loaded';
		});
	}).attr('src',imgPath).error(function () {
			 alert('there was an error loading the image');
			 $("#lightboxload").remove();
				$("#mask").remove();
				lbImg = '';
				lbStage = 'ready';
	});
}

var prevNextBox = function(lmnt){
	$("#lightbox").fadeOut('fast', function(){
		$("#lightbox").remove();
		$("#mask").after(lbLoadHTML);
		$("#lightboxload").css("top",lbLoadTopPos);
		$("#lightboxload").css("left",lbLoadLeftPos);
		lbStage = 'loading';
		lbLoadContent(lmnt);
	});
}

/****************************************************
	ZR Close Lightbox													
****************************************************/
var closeLightBox = function(){
	if (lbStage == 'loading') { 
		if ($.browser.msie == false) {
			//do nothing if it is IE until the window is done loading.
			stop();
			$("#lightboxload").remove();
			$("#lightbox").remove();
			$("#mask").remove();
			lbStage = 'ready';
		}
	} else {
		$("#lightbox").fadeOut('fast', function(){
			$("#lightbox").remove();
			$("#mask").remove();
			lbStage = 'ready';
		});
	}
}


/****************************************************
	Attach Lightbox
****************************************************/
$(function(){
	//Load all the images that need to be loaded.
	lightBoxLoader= new Image(); 
  lightBoxLoader.src="/images/lightbox/lightbox-load.gif";
	lightboxclose= new Image(); 
	lightboxclose.src="/images/lightbox/close.png"; 
	lightboxinfo= new Image(); 
	lightboxinfo.src="/images/lightbox/info.png"; 
	lightboxprev= new Image(); 
	lightboxprev.src="/images/lightbox/previous.png"; 
	lightboxnext= new Image(); 
	lightboxnext.src="/images/lightbox/next.png"; 
	
	//add lightbox functionality to any links to images. 
	$(".entry a").each(function(){
			linkPath = this.href;
			linkArray = linkPath.split('.');
			ext = linkArray[linkArray.length-1];
			ext = ext.toLowerCase();
			//alert(ext);
			if(ext == 'jpg' || ext == 'png' || ext == 'gif' || ext == 'jpeg'){
				altText = $(this).children('img').attr('alt');
				$(this).addClass('zoom');
				$(this).attr('title',altText);
			}
		});
	
	//attach lightbox to the zoom class
	$('.zoom').click(function(){
		lightBoxZoom(this);
		return false;
	});
});