// This function decodes the any string
// that's been encoded using URL encoding technique
function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

var Slideshow = Class.create();

Slideshow.prototype = {
	
	initialize: function(outerContainer,padding,id){
		
		if(!padding)
			padding=3;
		
		this.id=id;
		outerContainer.innerHTML="";
				
		this.images=new Array();	
		this.navItems=new Array();				
				
		this.loading=document.createElement("div");
		this.loading.setAttribute("class","loading");
		this.loading.setAttribute("id","loading"+id);
		outerContainer.appendChild(this.loading);				
				
		Element.hide(this.loading.id);	
	
		this.nav = document.createElement("p");
		this.nav.setAttribute("id","nav"+id);
		this.nav.className="slideshowNavContainer";		
		outerContainer.appendChild(this.nav);
		
		this.enlargeLink=document.createElement("a");
		this.enlargeLink.className="slideshowMagnify";
		this.enlargeLink.innerHTML="Enlarge";
		this.enlargeLink.setAttribute("rel","lightbox");
		this.nav.appendChild(this.enlargeLink);

		this.container=document.createElement("div");
		this.container.className="slideshow";
		this.container.setAttribute("id","container"+id);
		outerContainer.appendChild(this.container);

		this.anchor = document.createElement("a");
		this.anchor.setAttribute("id","anchor"+id);
		this.anchor.setAttribute("rel","lightbox");		
		this.container.appendChild(this.anchor);

		this.imageTag = document.createElement("img");
		this.imageTag.setAttribute("id","imageTag"+id);
		this.anchor.appendChild(this.imageTag);

		this.captionPar = document.createElement("div");
		this.captionPar.setAttribute("id","captionPar"+id);
		outerContainer.appendChild(this.captionPar);

		/*
		
		objImageData.setAttribute('id','imageData');
		objImageDataContainer.appendChild(objImageData);
		
		*/
		
	},
	
	addSlide: function(imagePath,caption,linkUrl){
		this.images.push({imagePath:imagePath,caption:caption,linkUrl:linkUrl});
		this.addNav(this.images.length);
	},
	
	addNav: function(index){
		var navItem = document.createElement("a");
		navItem.className="slideshowNav";
		navItem.setAttribute("href",this.images[index-1].imagePath);
		var ss=this;
		navItem.onclick=function(){
			ss.showSlide(index);
			return false;
		}
		navItem.innerHTML=index;
		this.nav.appendChild(navItem);
		this.navItems.push(navItem);
	},
	
	showSlide: function(index){
		
		Element.hide(this.imageTag.id);
		//Element.hide(this.captionPar.id);
		Element.show(this.loading.id);
		
		var preload=new Image();
		var ss=this;
		
		this.anchor.setAttribute("href",this.images[index-1].linkUrl);
		this.anchor.setAttribute("caption", this.images[index-1].caption)
		this.anchor.onclick=function(){ myLightbox.start(this); return false; }
		this.enlargeLink.setAttribute("href",this.images[index-1].linkUrl);
		this.enlargeLink.setAttribute("caption",this.images[index-1].caption);
		this.enlargeLink.onclick=function(){ myLightbox.start(this); return false; }
		if(this.navItems[this.currentSelected-1])
 			this.navItems[this.currentSelected-1].className="";

		this.currentSelected=index;
		
		if(this.navItems[this.currentSelected-1])
 			this.navItems[this.currentSelected-1].className="selected";		
		
		preload.onload=function(){
			ss.imageTag.src=ss.images[index-1].imagePath;
			ss.captionPar.innerHTML=URLDecode(ss.images[index-1].caption);
			new Effect.Appear(ss.imageTag.id, { duration: 0.5, from:0, to:1, queue: 'end', afterFinish: function(){ Element.hide(ss.loading.id) } });
			//Element.hide(ss.loading.id);
			Element.show(ss.captionPar.id);
			ss.container.style.height=this.height+"px";			
		}
		
		preload.src=this.images[index-1].imagePath;
	},
	
	display: function(){
		this.showSlide(1);
		initLightbox(); // make sure lightbox initializes after the slideshow
	}

}



function swap(image,src,caption,href,link){
	image.src=src;
}