

function ImageSwitcher(img1_id, img2_id, prev_id, next_id) {
	this.images = new Array();
	this.images_original = new Array();
	this.eff1 = null;
	this.eff2 = null;

	this.villa_pic_no = 0;

	this.imgElement1 = $(img1_id);
	this.imgElement2 = $(img2_id);
	this.id_btnPrev = prev_id;
	this.id_btnNext = next_id;

	this.addImage = function(url, original_url) { this.images.push(url); this.images_original.push(original_url); }

	this.showPic = function(img_no) {
		if (img_no < 0 || img_no >= this.images.length || img_no == this.villa_pic_no)
			return;

		this.villa_pic_no = img_no;

		this.cancelEffects();

		if (this.imgElement2.onTop) {
			this.imgElement1.onerror = function() { this.image_switcher.error_fallback(this); }
			this.imgElement1.src = this.images[this.villa_pic_no];
			
			this.eff1=new Effect.Fade(this.imgElement2);
			this.eff2=new Effect.Appear(this.imgElement1);

			this.imgElement1.onTop = true;
			this.imgElement2.onTop = false;
		} else {
			this.imgElement2.onerror = function() { this.image_switcher.error_fallback(this); }
			this.imgElement2.src = this.images[this.villa_pic_no];
			this.eff1=new Effect.Fade(this.imgElement1);
			this.eff2=new Effect.Appear(this.imgElement2);

			this.imgElement1.onTop = false;
			this.imgElement2.onTop = true;
		}



		// set next/previous button
		if (this.villa_pic_no+1 >= this.images.length && $(this.id_btnNext))
			$(this.id_btnNext).style.visibility = 'hidden';
		else if ($(this.id_btnNext))
			$(this.id_btnNext).style.visibility = '';
		if (this.villa_pic_no > 0 && $(this.id_btnPrev))
			$(this.id_btnPrev).style.visibility = '';
		else if ($(this.id_btnPrev))
			$(this.id_btnPrev).style.visibility = 'hidden';
	}
	
	this.error_fallback = function(img) {
		img.src = this.images_original[this.villa_pic_no];
	}

	this.showNextPic = function() {
		this.showPic(this.villa_pic_no+1);
	}


	this.showPrevPic = function() {
		this.showPic(this.villa_pic_no-1);
	}
	
	this.hasNext = function() {
		if (this.villa_pic_no+1 >= this.images_original.length)
			return false;
		else
			return true;
	}
	this.hasPrev = function() {
		if (this.villa_pic_no == 0)
			return false;
	}



	this.cancelEffects = function() {
		if (this.eff1)
			this.eff1.cancel();
		if (this.eff2)
			this.eff2.cancel();
	}

	this.init = function() {
		if ($(this.id_btnPrev)) {
			$(this.id_btnPrev).handler = this;
			$(this.id_btnPrev).observe('click', function() {this.handler.showPrevPic();});
		}
		if ($(this.id_btnNext)) {
			$(this.id_btnNext).handler = this;
			$(this.id_btnNext).observe('click', function() {this.handler.showNextPic();});
		}


		this.imgElement1.style.zIndex = 2;
	}


	this.init();
}
