
// ============================================== //

		// PAGE SIZE FUNCTIONS //

// ============================================== //

		_getPageSize = function() {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){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;};var windowWidth, windowHeight;if (self.innerHeight) {if(document.documentElement.clientWidth){windowWidth = document.documentElement.clientWidth;} else {windowWidth = self.innerWidth;};windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;};if(yScroll < windowHeight){pageHeight = windowHeight;} else { pageHeight = yScroll;};if(xScroll < windowWidth){	pageWidth = xScroll;		} else {pageWidth = windowWidth;};arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;};

		_getPageScroll = function() {var xScroll, yScroll;if (self.pageYOffset) {yScroll = self.pageYOffset;xScroll = self.pageXOffset;} else if (document.documentElement && document.documentElement.scrollTop) {yScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;} else if (document.body) {yScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;};arrayPageScroll = new Array(xScroll,yScroll);return arrayPageScroll;};


// ============================================== //

		// TIMER FUNCTIONS //

// ============================================== //

jQuery.fn.extend({everyTime:function(interval,label,fn,times,belay){return this.each(function(){jQuery.timer.add(this,interval,label,fn,times,belay)})},oneTime:function(interval,label,fn){return this.each(function(){jQuery.timer.add(this,interval,label,fn,1)})},stopTime:function(label,fn){return this.each(function(){jQuery.timer.remove(this,label,fn)})}});jQuery.extend({timer:{guid:1,global:{},regex:/^([0-9]+)\s*(.*s)?$/,powers:{'ms':1,'cs':10,'ds':100,'s':1000,'das':10000,'hs':100000,'ks':1000000},timeParse:function(value){if(value==undefined||value==null)return null;var result=this.regex.exec(jQuery.trim(value.toString()));if(result[2]){var num=parseInt(result[1],10);var mult=this.powers[result[2]]||1;return num*mult}else{return value}},add:function(element,interval,label,fn,times,belay){var counter=0;if(jQuery.isFunction(label)){if(!times)times=fn;fn=label;label=interval}interval=jQuery.timer.timeParse(interval);if(typeof interval!='number'||isNaN(interval)||interval<=0)return;if(times&&times.constructor!=Number){belay=!!times;times=0}times=times||0;belay=belay||false;if(!element.$timers)element.$timers={};if(!element.$timers[label])element.$timers[label]={};fn.$timerID=fn.$timerID||this.guid++;var handler=function(){if(belay&&this.inProgress)return;this.inProgress=true;if((++counter>times&&times!==0)||fn.call(element,counter)===false)jQuery.timer.remove(element,label,fn);this.inProgress=false};handler.$timerID=fn.$timerID;if(!element.$timers[label][fn.$timerID])element.$timers[label][fn.$timerID]=window.setInterval(handler,interval);if(!this.global[label])this.global[label]=[];this.global[label].push(element)},remove:function(element,label,fn){var timers=element.$timers,ret;if(timers){if(!label){for(label in timers)this.remove(element,label,fn)}else if(timers[label]){if(fn){if(fn.$timerID){window.clearInterval(timers[label][fn.$timerID]);delete timers[label][fn.$timerID]}}else{for(var fn in timers[label]){window.clearInterval(timers[label][fn]);delete timers[label][fn]}}for(ret in timers[label])break;if(!ret){ret=null;delete timers[label]}}for(ret in timers)break;if(!ret)element.$timers=null}}}});if(jQuery.browser.msie)jQuery(window).one("unload",function(){var global=jQuery.timer.global;for(var label in global){var els=global[label],i=els.length;while(--i)jQuery.timer.remove(els[i],label)}});

//
//
initGallery = function(){
	$( 'html, body' ).animate({ scrollTop: 0 }, 0.001);
	//
	var loadingXml = false;
	var blockLength = 5;
	var blockStart = 0;
	var blockEnd = 2;
	//5;
	var dateLength = 0;
	//
	var loadedItem = 0;
	var loadedTotal = 0;
	//
	var imageOpen = false;
	//
	nearBottomOfPage = function() {
    	return $(window).scrollTop() > $(document).height() - $(window).height() - 200;
  	};
	//
	$(window).scroll(function(){
		checkScroll();
		if(imageOpen){
			$('body').stopTime().oneTime(250,function() {
				positionImage();
			});
		};
	});
	//
	checkScroll = function(){
		if (loadingXml) {
      		return;
    	}
		//
    	if(nearBottomOfPage()) {
      		loadingXml=true;
			if (blockEnd<dateLength){
				blockStart+=blockLength;
				blockEnd+=blockLength;
				//
				loadXmlBlock(blockStart,blockEnd);
			}
		}
	};
	//
	loadContent = function(){
		var thisTarget = $('#gallery li').eq(loadedItem);
		thisTarget.css({
			opacity:	0
		})
		//
		var img = new Image();
		$(img).load(function () {
			thisTarget.show().animate({ opacity: 1 }, 200);
			if(loadedItem<(loadedTotal-1)){
				loadedItem++;
				setTimeout(function() { loadContent() }, 0)
			}else{
				loadingXml = false;
				checkScroll();
			}
		}).attr('src', thisTarget.find('img').attr('src'));
	};
	//
	loadXmlBlock = function(start,end){
		loadingXml = true;
		var imgPath = 'images/thumbs/'
		var imgFullPath = 'images/'
		$.ajax({
			type: "GET",
			url: "includes/xml/image_list.xml",
			dataType: "xml",
			success: function(xml) {
				dateLength = $(xml).find('menu_node').length;
				$(xml).find('menu_node').each(function(i){
					if(i>=blockStart && i<blockEnd){
						var thisDate = $(this).attr('groupDate');
						$(this).find('img_item').each(function(j){
							var thisPath = $(this).attr('urlPath');	
							loadedTotal++;
							if(j==0){
								$('#gallery').append('<li class="date" style="display:none;"><p>'+thisDate+'<\/p><a href="'+imgFullPath+thisPath+'"><img src="'+imgPath+thisPath+'" \/><\/a><\/li>');	
							}else{
								$('#gallery').append('<li style="display:none;"><a href="'+imgFullPath+thisPath+'"><img src="'+imgPath+thisPath+'" \/><\/a><\/li>');	
							}				
						});
					}
				});
				loadContent();
			}
		});
	};
	//
	showImage = function(thisTarget){
		imageOpen = true;
		var imgSrc = thisTarget.closest('li').find('a').attr('href');
		//
		$('body').append('<div id="jquery-overlay"><\/div><div id="jquery-lightbox"><div id="lightbox-content"><img src="" /><\/div><a href=# title="Thumbnails">Thumbnails<\/a><\/div>');
		//
		var arrPageSizes = _getPageSize();
		//
		$('#lightbox-content').css({opacity: 0});
		$('#lightbox-content img').css({opacity: 0});
		//
		$('#jquery-overlay').css({
			opacity:	0,
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		}).animate({opacity: 1}, 250,function(){
			positionImage();
			$('#jquery-lightbox').show();
			$('#lightbox-content').show().animate({opacity: 1}, 250,function(){
				var img = new Image();
				$(img).load(function () {
					$('#lightbox-content img').attr('src',imgSrc).show().animate({opacity: 1}, 250);
				}).attr('src', imgSrc);
			});
		});
		//
		$('#jquery-overlay,#jquery-lightbox,#jquery-lightbox a').click(function() {
			closeImage();
			return false;	
		});
		$('#lightbox-content img').click(function() {								  
			return false;	
		});		
		//
		$(window).resize(function() {
			positionImage();
		});
	};
	//
	closeImage = function() {
		imageOpen = false;
		$('#jquery-lightbox').remove();
		$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
		// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
		$('embed, object, select').css({ 'visibility' : 'visible' });
	};
	//
	positionImage = function(){
		// Get page sizes
		var arrPageSizes = _getPageSize();
		var arrPageScroll = _getPageScroll();
		// Style overlay and show it
		$('#jquery-overlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});	
		//
		$('#jquery-lightbox').css({
			top:	arrPageScroll[1] + 20,
			left:	(arrPageSizes[0]-900)/2
		});
	};
	//
	$('#gallery').bind('click', function(e){
		var target = e.target, // e.target grabs the node that triggered the event.
		$target = $(target);  // wraps the node in a jQuery object
		if(target.nodeName === 'IMG' || target.nodeName === 'img' || target.nodeName === 'A' || target.nodeName === 'a'){
			showImage($target);
		}
		return false;
	});
	//
	loadXmlBlock(blockStart,blockEnd);
};

$(document).ready(function(){
	if ($.browser.mozilla){
		//$('html').css({'height':'100%', 'margin-bottom': '1px'});
	};
	initGallery()
});

