/*
	Lightbox JS: Fullsize Image Overlays 
	by David Ryman - DavidRyman.com

	Loosely based on code by Lokesh Dhakar - http://www.huddletogether.com

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)

	Initially this plugin only displays a single image. More development is likely.
	Possible improvements:
	Display of next and previous images based on the group. e.g. rel=lightbox[GROUP]
	configurable display type, e.g. slide-in/out, fade-in/out
	For images larger than screen, let user click to enlarge.
	
	Table of Contents
	-----------------
	showLightbox	-	Load image into dummy element and set up a load event for it to continue.
	showContinue	-	Display image, center it and if larger than the viewport, resize, and probably more to come....
	hideLightbox	-	Hide image.
	initLightbox	-	Function runs on window load, going through link tags looking for rel="lightbox".
				These links receive onclick events that enable the lightbox display for their targets.
				The function also inserts html markup at the top of the page which will be used as a container for the overlay pattern and the inline image.
				Also sets up a hidden dummy image tag and sets the maximum image size to less than the viewport size.

*/
//
// Configuration
//
// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = '../../plugins/lightbox_jquery_plugin/images/loading.gif';
var closeButton = '../../plugins/lightbox_jquery_plugin/images/lightbox-btn-close.gif';
var nextButton = '../../plugins/lightbox_jquery_plugin/images/lightbox-btn-next.gif';
var previousButton = '../../plugins/lightbox_jquery_plugin/images/lightbox-btn-prev.gif';
var toolong = '../../plugins/lightbox_jquery_plugin/images/toolong.gif';
var max = {width: 800, height: 700};
//
// showLightbox()
// Preloads images. Places new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	$("#dummy").attr({'src':objLink});
}
function showContinue(objLink)
{
	$("#loadingimage").attr({'src':objLink});
	if ($("#dummy").attr('height') > max.height)
		{$("#loadingimage").css({'height':max.height + 'px','width':''});$("#dummy").css({'height':max.height + 'px','width':''});}
	if ($("#dummy").attr('width') > max.width)
		{$("#loadingimage").css({'width':max.width + 'px','height':''});$("#dummy").css({'width':max.width + 'px','height':''});}

//	alert(objLink + '\nheight w d ' + $(window).height() + ' ' + $(document).height() + '\n width w d ' + $(window).width() + ' ' + $(document).width() + '\n' + $(window).scrollTop() + "\nloadingimage " + $("#loadingimage").attr('width') + ' ' + $("#loadingimage").attr('height') + "\ndummy " + $("#dummy").attr('width') + ' ' + $("#dummy").attr('height'));

	$("#loadingimage").css({'top':($(window).scrollTop() + (($(window).height() - $("#dummy").attr('height')) /2) + 'px'),'left':((($(window).width() - 20 - $("#dummy").attr('width')) / 2) + 'px')});
//	alert(objLink + '\nheight w d ' + $(window).height() + ' ' + $(document).height() + '\n width w d ' + $(window).width() + ' ' + $(document).width() + '\n' + $(window).scrollTop() + "\nloadingimage " + $("#loadingimage").attr('width') + ' ' + $("#loadingimage").attr('height') + "\ndummy " + $("#dummy").attr('width') + ' ' + $("#dummy").attr('height'));
	$("#jquery-overlay").css({'display':'block'});
	$("#loadingimage").css({'display':'block'});
	$("#closebtn").css({'top':$("#loadingimage").css('top'),'left':$("#loadingimage").css('left')});
	$("#closebtn").css({'display':'block'});
//	alert('now'); 
//	setTimeout('1000',function(){return true;});
}
//
// hideLightbox()
//
function hideLightbox()
{
	$('#loadingimage').css({'display':'none'});
	$('#jquery-overlay').css({'display':'none'});
	$('#closebtn').css({'display':'none'});
	$('#dummy').css({'display':''});
	$('#loadingimage').attr({'src':''});
}
//
// initLightbox()
//
$(document).ready(function(){ initLightbox(); $("a[rel^='lightbox']").click(function(event){event.preventDefault(); showLightbox(this); }) })

function initLightbox()
{
	$("body").append("<div id='jquery-overlay'></div>");
	$("#jquery-overlay").css({'background-color':'#000','opacity':'0.9','display':'none','position':'absolute','top':'0px','left':'0px','zIndex':'90','width':$(document).width(),'height':$(document).height()});
	$("#jquery-overlay").click(function(event){hideLightbox();});
	$("body").append("<img id='loadingimage'>");
	$("#loadingimage").css({'background-color':'#000','opacity':'1','zIndex':'150','position':'absolute','top':'0px','left':'0px','display':'none'});
	$("body").append("<img id='dummy'>");
	$("#dummy").css({'opacity':'0','zIndex':'-1','position':'absolute','top':'0px','left':'0px'});
	$("#dummy").load(function(event){event.preventDefault(); showContinue($(this).attr('src'));});
	$("body").append("<img id='closebtn'>");
	$("#closebtn").css({'zIndex':'200','position':'absolute','top':'0px','left':'0px','opacity':'0.2','display':'none'});
	$("#closebtn").attr({'src':closeButton});
	$("#closebtn").click(function(event){hideLightbox();});
	max.width = $(window).width() - 100;
	max.height = $(window).height() - 100;

}
