jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but IE Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // IE Mac...would also work in IE 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {// all except iE
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
function getPageScroll(){
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // IE 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other IE
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft; 
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
function setLightboxDimensions(){
	var pageSizesArray = getPageSize();
	var pageScrollArray = getPageScroll();
	var lightboxWidth = $("#lightbox").width();
	var lightboxHeight = $("#lightbox").height();
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var documentWidth = pageSizesArray[0];
	var documentHeight = pageSizesArray[1];
	var overlayOpacity = 0.75;
	var lightboxTop = pageScrollArray[1] + (pageSizesArray[3] / 4);
	var lightboxLeft = pageScrollArray[0];
	$("#lightbox-bg").css({
		width: documentWidth,
		height: documentHeight,
		opacity: overlayOpacity
	});
	$("#lightbox").css({
		top:	lightboxTop,
		left:	lightboxLeft
	});
}
$(function(){
	//Some vars to set
	var closeLightboxOnClick = true;
	
	//Resize the lightbox when the user resizes the browser window
	$(window).resize(function(){
		setLightboxDimensions();
	});
	
	//Preload Images
	$("a.lightbox-trigger").each(function(){
		$.preloadImages($(this).attr("href"));
	});
	
	//Create lightbox
	var lightboxWidth = $("#lightbox").width();
	var lightboxHeight = $("#lightbox").height();
	$("#lightbox-bg,#lightbox,#lightbox-close").hide();
	$("a.lightbox-trigger").click(function (){
		setLightboxDimensions();
		//$("#lightbox").contents().replaceWith('<img src="" alt="" />');
		$("#lightbox img").attr({src: $(this).attr("href")}).load();
		
		
		$("#lightbox-bg,#lightbox,#lightbox-close").fadeIn(500);
		return false;
	});
	
	//Close the lightbox on click
	$("#lightbox-bg,#lightbox-close,#lightbox-container").click(
		function(e){
			if($(e.target).is("#lightbox") && !closeLightboxOnClick) return false; //Don't close if the click is within the lightbox
			$("#lightbox-bg,#lightbox,#lightbox-close").fadeOut(500);
		}
	);
});