
var Gallery = Class.create();

Gallery.prototype = {
  initialize: function(thumbContainer, galleryContainer, ele) {

		var anchors = thumbContainer.getElementsBySelector('a');
		this.img_array = [];
		this.preloader_array = [];
		this.img_id = 0;
		this.ele = ele;
		this.thumb_container = thumbContainer;
		this.gallery_container = galleryContainer;
		this.preloaded = [];
		this.caption_array = [];
		this._adding_events($(this.thumb_container));
		
		for (var i = 0; i < anchors.length; i++){
			this.img_array.push(anchors[i].href);
			this.preloader_array.push(anchors[i].href);
			// this._preloader(anchors[i].href,i);
			this.caption_array.push(anchors[i].title);
		}
		
		this.imgPreloader = new ImagePreloader(this.preloader_array);
						
 	},

	_preloader: function(path,i){
		this.preloaded[i] = document.createElement('img');
		this.preloaded[i].setAttribute('src',path);
	},
	
	_adding_events: function(ele){
		var scope = this;		
		ele.immediateDescendants().each(function(s,index){
			Event.observe(s,'click',function(e){
				scope.show(index);
				Event.stop(e);
			});
			Event.observe(s,'mouseover',function(e){
				scope.imgPreloader.unshift(s);
				Event.stop(e);
			});
			
		});
		
	},

	change_caption: function(){
		if(this.caption_array.length == this.img_array.length){
			$(this.ele.id + '_caption').update(this.caption_array[this.img_id]);
		}
	},
  
	next: function(){
		this.img_id = parseInt(this.img_id) + 1;
		if(this.img_id < this.img_array.length){
			$(this.ele).src = this.img_array[this.img_id];
		}else{
			this.img_id = 0;
			$(this.ele).src = this.img_array[0];
		}
		this.change_caption();
	},

	prev: function(){
		this.img_id = parseInt(this.img_id) - 1;
		if(this.img_id < 0){
			this.img_id = this.img_array.length - 1;
			$(this.ele).src = this.img_array[this.img_id];
		}else{
			$(this.ele).src = this.img_array[this.img_id];
		}
		this.change_caption();
	},
	
	show: function(int){
		this.img_id = parseInt(int);
		$(this.ele).src = this.img_array[this.img_id];
		$(this.thumb_container).hide(); 
		$(this.gallery_container).show(); 
		this.change_caption();
	},
		
	close: function() {
		this.img_id = 0;
		$(this.ele).src = this.img_array[this.img_id];
		$(this.gallery_container).hide(); 
		$(this.thumb_container).show();
	}
	
};

var ImagePreloader = Class.create();
ImagePreloader.prototype = {
  initialize: function(urlArray) {
		this._loadedImages = {};
		this._queue = urlArray;
		this.load();
		this.oncomplete = function(){ return; };
 	},

	unshift: function(url){
		return this._queue.unshift(url);
	},

	shift: function(url){
		return this._queue.shift(url);
	},

	load: function(){
		if (this._isLoading) return;
		this._isLoading = true;
		this._loadNext();
	},
	
	_loadNext: function(){
		var nextURL = this.shift().toString();
		var img_obj = new Image();
		img_obj.src = nextURL;
		var scope = this;
		
		img_obj.onload = function(){
			scope._loadedImages[nextURL] = this;
			if(scope._queue.length == 0){
				scope._isLoading = false;
				scope.oncomplete();
			}else{
				scope._loadNext();
			}
		}
	}

};


/*
<script type="text/javascript" charset="utf-8">
//<![CDATA[
	var img_array = new Array('/images/photo_gallery/big/200276126-001.jpg','/images/photo_gallery/big/200393273-001.jpg','/images/photo_gallery/big/200404977-001.jpg','/images/photo_gallery/big/200425045-001.jpg','/images/photo_gallery/big/200475989-001.jpg','/images/photo_gallery/big/200510133-001.jpg','/images/photo_gallery/big/619032.jpg','/images/photo_gallery/big/683055_YoungCoupleNight.jpg','/images/photo_gallery/big/71260876_5.jpg','/images/photo_gallery/big/CBP0001081_P.jpg','/images/photo_gallery/big/CBP1020540.jpg','/images/photo_gallery/big/DVP0702009.jpg','/images/photo_gallery/big/FAN2021937_P.jpg','/images/photo_gallery/big/FRIT_SR_Selects_1457_nogirl.jpg','/images/photo_gallery/big/FRIT_SR_Selects_1466.jpg','/images/photo_gallery/big/FRIT_Selects_1086_denim.jpg','/images/photo_gallery/big/FRIT_Selects_1357_BR_adjusted.jpg','/images/photo_gallery/big/ISP2036998.jpg','/images/photo_gallery/big/MidAtl_MD_BR_Austin1.jpg','/images/photo_gallery/big/MidAtl_MD_BR_Cinema37.jpg','/images/photo_gallery/big/MidAtl_MD_BR_ElmNght.jpg','/images/photo_gallery/big/SBP0337430.jpg','/images/photo_gallery/big/Terrace.jpg','/images/photo_gallery/big/brxbxp49119_weights.jpg','/images/photo_gallery/big/corner.jpg','/images/photo_gallery/big/festival.jpg','/images/photo_gallery/big/stk33108lce.jpg');
	var caption_array = new Array();
	Event.observe(window, 'load', init, false);
	function init(){
		return myGallery = new Gallery(img_array,caption_array,'gallery_photo');
	}	
//]]>
</script>
*/

