// JavaScript Document
//Example usage:
/*
var arr = new Array();
arr.push({ imgpath:'uploads/Blue_hills.jpg'});
arr.push({ imgpath:'uploads/Sunset.jpg'});
arr.push({ imgpath:'uploads/Water_lilies.jpg'});
arr.push({ imgpath:'uploads/Winter.jpg'});
//Pass the image element, and next,previous link elements
var slide= new Slideshow(arr,{ prev_link:$('prev_link'),next_link:$('next_link'),img_element:$('img')});
*/
var Slideshow=Class.create({
//@param $arr is a 2-dimensional array
//Returns the image path
initialize:function(arr,options){ 
	this.currpage=1;
	this.arr = arr;
	this.imgpath = 'imgpath';
	this.total=this.arr.length;
	this.options = options;
	this.next_link=this.options.next_link;
	this.prev_link=this.options.prev_link;
	this.img_element=this.options.img_element;
	this.paging_text=this.options.paging_text;
	this.showImage();
	this.next_exists();
	this.prev_exists();
	if(this.paging_text){
		this.paging_text.innerHTML='Image '+this.currpage+'/'+this.total;
	}
	Event.observe(this.next_link, 'click', this.showNext.bindAsEventListener(this));
	Event.observe(this.prev_link, 'click', this.showPrev.bindAsEventListener(this));
	//alert(this.next_link.id+":"+this.prev_link.id+":"+this.img_element.id);
},
 
currSlide : function(){
				if(this.currpage<=this.total){
					return this.arr[this.currpage-1].imgpath;
				}
			},
showImage : function (){
				//alert('curr slide '+this.currSlide());
				if(this.currSlide()){this.img_element.src=this.currSlide();}
			},
showNext : function(event){
				//alert(Event.findElement(event).id+":"+this.imgpath);
				this.currpage++;
				this.prev_exists();
				this.next_exists();
				if(this.paging_text){
					this.paging_text.innerHTML='Image '+this.currpage+'/'+this.total;
				}
				this.showImage();
			},

showPrev : function(event){
				this.currpage--;
				this.next_exists();
				this.prev_exists(); 
				if(this.paging_text){
					this.paging_text.innerHTML='Image '+this.currpage+'/'+this.total;
				}
				this.showImage();
			},
next_exists : function(){
				
				if(this.total>this.currpage){ 
					this.next_link.style.display='inline';
					//alert('next '+this.total+":"+this.currpage);
					return true;
				}else{
					this.next_link.style.display='none';	
				}
			},
prev_exists : function(){
				if(this.currpage>1){
						this.prev_link.style.display='inline';
						//alert('prev '+this.total+":"+this.currpage);
						return true;
				}else{
						//alert('no prev '+this.total+":"+this.currpage);
						this.prev_link.style.display='none';
				}
			}
});