link_to = function(url, text, id){
	return "<a id=\"" + id + "\" href=\"" + url + "\">" + text + "</a>";
}

JSlideshow = Class.create();
Object.extend(JSlideshow.prototype, {
	initialize: function(options){
		this.div         = 'slideshow';
		this.instance    = 'slideshow';
		this.img         = 'slideshow_feature_img';
		this.timeout     = 4;
		this.current_idx = 1;
		Object.extend(this, options);
		if(!$(this.div)){ document.write("<div id=\"" + this.div + "\"></div>")}
		this.photos = options['photos'];
		this.loadImage((this.current_idx - 1));
		new Insertion.Bottom(this.div, this.buildNavigation(this));
		this.currentLink();
		if(this.timeout != 0){ autoLoad = setInterval(this.instance + ".autoImage()", this.timeout * 1000); }
	},
	loadImage: function(idx){
		this.current_idx = idx;
		src = this.photos[idx];
		if(!$(this.img)){ new Insertion.Top(this.div, "<img id=\"" + this.img + "\" src=\"#\" />")}
		$(this.img).src = src;
		this.showImage(this.img);
		this.currentLink();
	},
	currentLink: function(){
	  $$("#" + this.div + " .navigation a.current").each(function(x){x.className = ""});
		if($("link" + this.current_idx)){$("link" + this.current_idx).className = "current";}
	},
	showImage: function(img){
	  $(img).hide();
	  new Effect.Appear(img);
	},
	prevImage: function(){
	  if(this.current_idx == 0){
	    n = (this.photos.length - 1)
	  } else {
  	  n = (this.current_idx - 1)
	  }
	  this.loadImage(n);
	},
	nextImage: function(){
	  if(this.current_idx == (this.photos.length - 1)){
	    n = 0
	  } else {
  	  n = (this.current_idx + 1)
	  }
	  this.loadImage(n);
	},
	autoImage: function(){
	  if(this.current_idx == (this.photos.length - 1)){
	    if(autoLoad){ clearInterval(autoLoad);}
	  } else {
	    slideshow.nextImage();
	  }
	},
	buildNavigation: function(obj){
	  var instance = this.instance
		var count = this.photos.length;
		var html = "<div class=\"navigation\">";
		if(count > 0){
			html += link_to("javascript:" + instance + ".prevImage();", '&#171;', 'ss_back_arrow') + " ";
			(count).times(function(n){
				html += link_to("javascript:" + instance + ".loadImage("+n+");", n+1, "link" + n);
				if(n!=(count-1)){ html+= " | ";}
			});
			html += " " + link_to("javascript:" + instance + ".nextImage();", '&#187;', 'ss_next_arrow');
		}
		html += "</div>";
		return html;
	}
});