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];
		// don not hide since moving to left nav
		//$(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();
			}
		}
	}

};