Gallery = function () {
	this.preload = function(number) {
		if(!in_array(this.images[number], this.preloaded)) {
			img1 = new Image();
			img1.src = this.images[number];
			this.preloaded.push(this.images[number]);
		}
	}
	
	this.setImages = function(images) {
		this.images = images;
		this.preload(0);
		this.gotimages = true;
	}
	
	this.onLoad = function() {
		if(!this.gotimages) setTimeout(methodize(this.onLoad,this), 10);
		
		document.getElementById(this.image_tag_1).src = this.images[0];
		if(this.effect == "fade") Effect.Appear(this.image_tag_1);
		else if(this.effect == "none") this.next('start');
		
		// start slideshow
		try {
			setTimeout(methodize(this.play,this), this.slideshow.speed);
		}
		catch(err) { }
		
		this.preload(1);
	};
	
	this.prev = function() {
		if(this.lock == 2) {
			this.lock = 0;
			this.count--;
			this.doEffect();
			
			try {
				if(this.slideshow.run == true) {
					this.slideshow.pause()
				}
			} 
			catch(err) { }
		}
	}
	
	this.next = function(type) {
		if(this.lock == 2) {
			this.lock = 0;
			this.count++;
			if(type == 'start') this.count = 0;
			this.doEffect();
			if((this.count+1) == this.images.length) this.preload(0);
			else this.preload(this.count+1);
			
			if(type != 'show')
			{
				try {
					if(this.slideshow.run == true) {
						this.slideshow.pause()
					}
				} 
				catch(err) { }
			}
		}
	}
	
	this.show = function(image) {
		if(this.lock == 2) {
			this.lock = 0;
			if(isNumeric(image) && image >= 0 && image < this.images.length) tmp = image;
			else if(in_array(image, this.images)) tmp = in_array(image, this.images);
			
			if(tmp != this.count) {
				this.count = tmp;
				this.doEffect();
				if((this.count+1) == this.images.length) this.preload(0);
				else this.preload(this.count+1);
			}
			else this.lock = 2;
			
			try {
				if(this.slideshow.run == true) {
					this.slideshow.pause()
				}
			} 
			catch(err) { }
		}
	}
	
	this.play = function() {
		try {
			if(this.slideshow.run == false) {
				this.slideshow.play();
			}
			else if (this.slideshow.run == true) {
				this.slideshow.pause();
			}
		}
		catch(err) { }
	}
	
	this.doEffect = function() {
		if(this.count == this.images.length) this.count = 0;
		if(this.count < 0) this.count = this.images.length-1;
		
		switch(this.effect) {
			case "none":
				this.appear();
				break;
				
			case "slide":
				this.slide();
				break;
			
			case "fade":
			default:
				this.fade();
				break;
		}
	}
	
	this.fade = function() {
		if(this.visible == 1) {
			document.getElementById(this.image_tag_2).src = this.images[this.count];
			this.visible = 2;
		}
		else if(this.visible == 2) {
			document.getElementById(this.image_tag_1).src = this.images[this.count];
			this.visible = 1;
		}

		if(this.lock == 0) {
			Effect.toggle(this.image_tag_1,'appear', { afterFinish: methodize(this.unlock, this) } );
			Effect.toggle(this.image_tag_2,'appear', { afterFinish: methodize(this.unlock, this) } );
		}
	}
	
	this.slide = function() {
		if(this.lock == 0) {
			/*document.getElementById(this.image_tag_1).src = this.images[this.count-1];
			document.getElementById(this.image_tag_2).src = this.images[this.count];
			document.getElementById('gallery_image_1').style.display = "block";
			document.getElementById('gallery_image_2').style.display = "none";
			document.getElementById(this.image_tag_1).style.display = "block";
			document.getElementById(this.image_tag_2).style.display = "block";
		//	Effect.DropOut('gallery_image_1');
			Effect.BlindDown('gallery_image_2');
			this.lock = 2;*/
		}
	}
	
	this.appear = function() {
		if(this.lock == 0) {
			document.getElementById(this.image_tag_1).style.display = "block";
			document.getElementById(this.image_tag_1).src = this.images[this.count];
			this.lock = 2;
		}
	}
	
	this.unlock = function() {
		this.lock++;
	}
	
	this.lock = 2;
	this.count = 0;
	this.visible = 1;
	
	this.slideshow = null;
	
	this.image_tag_1 = null;
	this.image_tag_2 = null; 
	
	this.preloaded = [];
	
	this.effect = "fade";
	
	return this;
};

function methodize(methodize_func,methodize_scope){
	return function() {
		methodize_func.call(methodize_scope);
	};
}

function in_array(needle, haystack){
	for(i in haystack) {
		if (needle == haystack[i]) return i;
	}
	return false;
}

function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
