(function($) {
	$.fn.gallery = function(){
		var gallerysets = $('ul.galleryset');
		var gallerynum = 0;
		
		var loaderprogress = new Image();
		loaderprogress.src = "loading.gif";
			
		$.each(gallerysets, function() {
			// adds the list items to the carousel
			if ($(this).find('li').length > 5){
				$(this).after('<br><a id="gallery' + gallerynum + '" class="detail galleryclosed" href="#">More Photos..</a>');
				$('#gallery' + gallerynum).bind('click', gallerybtn);
				$(this).attr({id: 'gallery' + gallerynum});
			}
			
			galleryimages = $(this).find('a');
			$.each(galleryimages, function(){
				$(this).bind("mouseenter", galleryhoverin);
				$(this).bind("mouseleave", galleryhoverout);
				$(this).bind("click", galleryopen);
				
				$(this).find('img').after('<span class="galleryshade"></span>');
				$(this).find('span.galleryshade').css({
					opacity: '0',
					display: 'block'
				})
			});
			
			gallerynum += 1;
		});
		
		function galleryhoverin(){
			$(this).find('span.galleryshade').stop();
			$(this).find('span.galleryshade').fadeTo('fast', .7);
		}
		
		function galleryhoverout(){
			$(this).find('span.galleryshade').stop();
			$(this).find('span.galleryshade').fadeTo('fast', 0);
		}
		
		function galleryopen(){
			
			$(this).find('span.galleryshade').stop();
			$(this).find('span.galleryshade').fadeTo('fast', 0);
			
			var box_picture = $(this).attr('href');
			var box_title = $(this).find('.gallerytitle').html();
			var box_desc = $(this).find('.gallerydesc').html();
						
			$('body').append('<div id="galleryoverlay"></div><div id="gallerywrapper"><div id="gallerybox"><div id="galleryimgcon"><img id="galleryimg"></div><div id="galleryinfo"><h2>' + box_title + '</h2><p>' + box_desc + '</p><p>Click anywhere to exit.</p></div></div></div>');
			
			$('#galleryoverlay').css({opacity: 0});
			$('#galleryoverlay').fadeTo('fast', .3);
			
			$('#galleryoverlay, #gallerywrapper, #gallerybox').click(function(){
				box_finish();
				return false;
			});
			
			var arrPageSizes = ___getPageSize();
			$('#galleryoverlay').css({
				width: arrPageSizes[0],
				height: arrPageSizes[1]
			});
			var arrPageScroll = ___getPageScroll();
			
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#gallerywrapper').css({
				top: arrPageScroll[1] + (arrPageSizes[3] / 30),
				left: arrPageScroll[0]
			});
			
			var galleryloader = new Image();
			galleryloader.onload = function() {
				//var imgmargin = (500 - galleryloader.height) / 2;
				//alert(imgmargin);
				$('#galleryimg').attr('src', box_picture);
				$('#galleryimg').css({
					display: 'block',
					opacity: 0
				});
				$('#galleryimg').fadeTo('fast', 1, function(){
					$('#galleryimgcon').css({background: 'none'});					
				});
				
				galleryloader.onload=function(){};
				//alert(box_picture);
				
			};
			galleryloader.src = box_picture;
			/*
			// If window was resized, calculate the new overlay dimensions
			$(window).resize(function() {
				// Get page sizes
				$('#galleryoverlay').css({
					width:0,
					height:0
				});
				var arrPageSizes = ___getPageSize();
				// Style overlay and show it
				$('#galleryoverlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = ___getPageScroll();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				$('#gallerywrapper').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
			*/
			$(this).blur();
			return false;
		}
		
		
		function gallerybtn(){
			var galleryselect = $('ul#' + $(this).attr('id'));
			var galleryli = 'ul#' + $(this).attr('id') + ' li';
			var galleryimg = $(galleryli);
			var galleryheight = $(galleryli + ':e(0)').height() + parseInt($(galleryli + ':e(0)').css("margin-bottom"));
			if ($(this).hasClass('galleryclosed')){
				var galleryheight = Math.ceil(galleryimg.length / 6) * galleryheight;
				$(this).removeClass('galleryclosed');
				$(this).text('Less Photos');
			} else {
				$(this).addClass('galleryclosed');
				$(this).text('More Photos');
			}
			$('ul#' + $(this).attr('id')).animate({
				height: galleryheight
			}, 700 );
			
			$(this).blur();
			return false;
		}
		
		function enable_keyboard_navigation() {
			$(document).keydown(function(objEvent) {
				box_finish();
			});
		}	
		///////////////////////////////////////////////
		function box_finish() {
			$('#galleryoverlay, #gallerywrapper').remove();
		};
		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 Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth;
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 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;
		};
		/**
		/ THIRD FUNCTION
		* getPageScroll() by quirksmode.com
		*
		* @return Array Return an array with x,y page scroll values.
		*/
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
	};
})(jQuery);