/**
 * Primary Constructor
 */
function slideText() {
	// Setup: Variables
	this.images = new Array();
	this.links = new Array();
	this.delay = 5;
	this.duration = 0.5;
	this.effect = Effect.Fade;
	this.random = false;
	this.wait = false;
	this.stall = false;
	this.width = 100;
	this.height = 100;
	this.div = null;
	this.image = null;
	this.cur = 0;
	
	// Setup: Functions
	this.addSlide = addSlide;
	this.addLink = addLink;
	this.setOptions = setOptions;
	this.setSize = setSize;
	this.render = render;
	this.next = next;
	this.previous = previous;
	this.advance = advance;
	this.pause = pause;
	this.jump = jump;
	this.changeButtons = changeButtons;
	
	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, link) {
	this.images[this.images.length] = image;
	this.links[this.links.length] = link;
}
function addLink(link) {
	this.links[this.links.length] = link;
}

/**
 * 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('<a href="'+ this.links[0] +'" id="slideshowframe" style="display: block; 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('</a>');
	
	// 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() {
	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.div.href = this.links[this.cur % this.images.length];
	this.effect(this.image, {duration: this.duration});
	this.changeButtons();
}

/**
 * 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.div.href = this.links[this.cur % this.images.length]
	this.effect(this.image, {duration: this.duration});
	this.changeButtons();
}

/**
 * Advances the slideshow if wait is set to false.
 */
function advance() {
	if(!this.wait && !this.stall) {
		this.next();
	}
	this.stall = false;
}

/**
 * Pauses and resumes playback of the slideshow.
 */
function pause() {
	this.wait = !this.wait;
}


/**
 * Skips to the indicated image/caption pair in the slideshow.
 */
function jump(num) {
	this.cur = num-1;
	this.image.setStyle({display: 'block'});
	this.image.src = this.images[this.cur % this.images.length];
	
	this.div.setStyle({background: 'url(' + this.images[this.cur % this.images.length] + ') no-repeat'});
	this.div.href = this.links[this.cur % this.images.length]
	this.effect(this.image, {duration: this.duration});
	this.stall = true;
	this.changeButtons();
}

function changeButtons() {
	var button;
	
	for(var i = 0; i < this.images.length; i++) {
		button = $('homeSlide'+(i+1));
		if(i == this.cur % this.images.length) {
			setClassName(button,'current');
		} else {
			setClassName(button,'');
		}
	}
}