/**
 * Primary Constructor
 */
(function(global) {

global.slideText = function() {
	// Setup: Variables
	this.images = new Array();
	this.delay = 5;
	this.duration = 0.5;
	this.effect = Effect.Fade;
	this.random = false;
	this.wait = false;
	this.width = 100;
	this.height = 100;
	this.div = null;
	this.image = null;
	this.cur = 0;
	
	// Setup: Functions
	this.addSlide = addSlide;
	this.setOptions = setOptions;
	this.setSize = setSize;
	this.render = render;
	this.next = next;
	this.previous = previous;
	this.advance = advance;
	this.pause = pause;
	
	this.delay = this.delay * 1000;
}

/**
 * Adds the specified slide to the slideshow.
 * 
 * @param string image the image URL to use
 * @param string caption an optional caption for the image
 */
function addSlide(image) {
	this.images[this.images.length] = image;
}

/**
 * Sets new options for the slideshow.
 * 
 * @param object options a collection of options
 */
function setOptions(options) {
	if(Object.isNumber(options.delay)) this.delay = options.delay * 1000;
	if(Object.isNumber(options.duration)) this.duration = options.duration;
	if(Object.isFunction(options.effect)) this.effect = options.effect;
	this.random = options.random;
	this.pause = options.pause;
}

/**
 * Sets a new size for the slideshow DIV.
 * 
 * @param integer width the new width in pixels
 * @param integer height the new height in pixels
 */
function setSize(width, height) {
	this.width = width;
	this.height = height;
}

/**
 * Renders the slideshow using the current settings.
 */
function render() {
	// Images
	document.writeln('<div id="slideshowframe" style="height: ' + this.height + 'px; width: ' + this.width + 'px; overflow: hidden; background: url(' + this.images[0] + ') no-repeat;">');
	document.writeln('<img id="slideshowimg" src="' + this.images[0] + '" style="display: none" />');
	
	for(var i = 1; i < this.images.length; i++) {
		document.writeln('<img src="' + this.images[i] + '" style="display: none" />');
	}
	
	document.writeln('</div>');
	
	// Set Up Transition
	this.div = $('slideshowframe');
	this.image = $('slideshowimg');
	this.cur = 0;
	
	if(this.options && this.options.css) {
		this.div.setStyle(this.options.css);
	}
}

/**
 * Skips to the next image/caption pair in the slideshow.
 */
function next() {
	var that = this;
	
	that.image.onload = function() {
		that.image.setStyle({display: 'block'});
		
		if (that.random) {
			that.cur = parseInt(Math.random() * that.images.length);
		}
		
		that.div.setStyle({
			background: 'url(' + that.images[that.cur % that.images.length] + ') no-repeat'
		});
		that.effect(that.image, {
			duration: that.duration
		});
	};
	
	that.image.src = that.images[that.cur++ % that.images.length];
}

/**
 * Skips to the previous image/caption pair in the slideshow.
 */
function previous() {
	this.wait = true;
	if(this.cur == 0) {
		this.cur = this.images.length;
	}
	
	this.image.setStyle({display: 'block'});
	this.image.src = this.images[this.cur-- % this.images.length];
	
	if(this.random) {
		this.cur = parseInt(Math.random() * this.images.length);
	}
	
	this.div.setStyle({background: 'url(' + this.images[this.cur % this.images.length] + ') no-repeat'});
	this.effect(this.image, {duration: this.duration});
}

/**
 * Advances the slideshow if wait is set to false.
 */
function advance() {
	if(!this.wait) {
		this.next();
	}
}

/**
 * Pauses and resumes playback of the slideshow.
 */
function pause() {
	this.wait = !this.wait;
}

})(window);
