/* libImageSlideshow
** Author: Toby Mackenzie
** Date: 12/07/2009

** description: for a non-controllable image slideshow in the sidebar
** parameter information:
params, passed as literal value form (like JSON):{
	// optional (can be set by setImages), array of images as JSON objects
	"images" : [
		{"id": repItemID, "path": "repImagePath", "title": "repItemTitle", "caption": "repCaption", "alt": "repOptAlt=null", "width": repOptItemWidth=null, "height": repOptItemHeight=null },
		...
	],
	// id of div or other block where slideshow takes place
	"divID" : "repDivID",
	// interval between images in milliseconds
	"interval" : repInterval
	// interval of fade between images in milliseconds
	"fadeInterval" : repFadeInterval
	}

// runs image rotation
objRotateImages();

// sets image array
setImages(prmImagesJSON)
	prmImagesJSON : [{"id": repItemID, "path": "repImagePath", "fullPath":"repFullPath", "title": "repItemTitle", "caption": "repCaption", "alt": "repAlt", "width": repItemWidth, "height": repItemHeight },...]
*/

function libImageSlideshow(prmJSONParameters) {
	this.currentImage = 0;
	this.images = (prmJSONParameters.images)?prmJSONParameters.images : null;
	this.divID = (prmJSONParameters.divID)?prmJSONParameters.divID : null;
	this.interval = (prmJSONParameters.interval)?prmJSONParameters.interval : 3000;	
	this.fadeInterval = (prmJSONParameters.fadeInterval)?prmJSONParameters.fadeInterval : 1500;	
}
	libImageSlideshow.prototype.start = function(){
		var fncPassThis = this;
		if(this.images.length > 1){
			this.insertNextPhoto();
			setInterval(function(){fncPassThis.rotateImage();}, this.interval);
		}
	}
	libImageSlideshow.prototype.rotateImage = function(){
		var fncPassThis = this;
		jQuery("#"+this.divID+" .current").animate({opacity: 0.0}, this.fadeInterval, function(){
			jQuery("#"+fncPassThis.divID+" .current").removeClass("current");
			jQuery("#"+fncPassThis.divID+" .next").addClass("current").removeClass("next");
			if(jQuery("#slideshow-"+fncPassThis.nextImageID()).size()){
				jQuery("#slideshow-"+fncPassThis.nextImageID()).addClass("next").css({opacity: 1.0});
				fncPassThis.incrementCurrentImage();
			}
			else
				fncPassThis.insertNextPhoto();
		});
	};
	libImageSlideshow.prototype.insertNextPhoto = function(){
		var fncImage = this.images[this.nextImageID()];
		var fncDivString = "";
		
		// build string of div to add
		fncDivString = fncDivString + '<div id="slideshow-'+this.nextImageID()+'" class="wp-caption alignnone next" ';
		fncDivString = fncDivString + '>';
		if(fncImage.fullPath)
			fncDivString = fncDivString + '<a href="'+fncImage.fullPath+'">';
		fncDivString = fncDivString + '<img class="size-full wp-'+fncImage.id+'" title="'+fncImage.title+'" src="'+fncImage.path+'"';
		if(fncImage.alt)
			fncDivString = fncDivString + ' alt="'+fncImage.alt+'"';
		if(fncImage.width)
			fncDivString = fncDivString + ' width="'+fncImage.width+'"';
		if(fncImage.height)
			fncDivString = fncDivString + ' alt="'+fncImage.height+'"';
		fncDivString = fncDivString + ' />';
		if(fncImage.fullPath)
			fncDivString = fncDivString + '</a>';
		if(fncImage.caption)
			fncDivString = fncDivString + '<p class="wp-caption-text">'+fncImage.caption+'</p></div>';
		
		// add div
		jQuery("#"+this.divID).append(fncDivString);
		this.incrementCurrentImage();
	}	
	libImageSlideshow.prototype.setImages = function(prmImagesJSON){
		this.images = prmImagesJSON;
	};	
	libImageSlideshow.prototype.incrementCurrentImage = function(){
		this.currentImage = this.nextImageID();
	};	
	libImageSlideshow.prototype.nextImageID = function(){
		if(this.currentImage < this.images.length - 1)
			return this.currentImage + 1;
		else
			return 0;
	};