/**
 * NSQuoteRoll
 * @author Nate Schulz
 * @version 1.0
 *
 * More information: http://theoreticalmedia.com/
 */
Widgets = [];
var NSQuoteRoll = function(attr) {
	for (var i in attr) this[i] = attr[i];
	if (this.customClass == undefined) this.customClass = "NSQuoteRoll";
	this.div = new Elem("div", this.id, this.customClass);
	this.visible = false;
	this.currentIndex = -1;
	this.init();
	if ( this.standalone == undefined ) this.standalone = false;
	if ( this.randomize == undefined ) this.randomize = false;

	this.div.widget = this;
	Widgets[this.id] = this;

	if ( this.randomize ) this.randomQuote();
	else this.nextQuote();
	return this.div;
};
NSQuoteRoll.prototype = {
	init: function () {
		this.div.textField = new Elem("p", this.id+"_text");
		this.div.reviewSource = new Elem("span", this.id+"_source");
		this.div.appendChild(this.div.textField);
		this.div.appendChild(this.div.reviewSource);
	},
	setReviewText: function(t) {
		this.div.textField.innerHTML = t;
	},
	setReviewSource: function(s) {
		this.div.reviewSource.innerHTML = s;
	},
	randomQuote: function() {
		r = Math.random();
		r *= this.quotes.length;
		r = Math.round( r );
		this.loadQuoteIndex( r );
	},
	loadQuoteIndex: function(i) {
		i %= this.quotes.length;
		if ( i == this.currentIndex && this.randomize ) this.randomQuote();
		else if ( i == this.currentIndex ) return;
		var that = this;
		id = this.id;
		$(this.div).fadeOut(function() {
			id = that.id;
			$W(id).setReviewText($W(id).quotes[i].text);
			$W(id).setReviewSource($W(id).quotes[i].source);
			$W(id).currentIndex = i;
			$($W(id).div).fadeIn();
			if ( $W(id).standalone ) setTimeout( "$W('"+id+"').randomQuote()", $W(id).delay );
		});
	},
	nextQuote: function() {
		nextIndex = ( ( this.currentIndex + 1 ) % this.quotes.length );
		this.loadQuoteIndex(nextIndex);
	}
};
var $W = function(id) {
	return Widgets[id];
};
var $Obj = function(id) {
	return document.getElementById(id);
};
var Elem = function(type, id, styleClass, content) {
	if (type == "clear") return new Elem("div", "", "clear");
	
	var _elem = document.createElement(type);
	if (id != undefined && id != "") _elem.setAttribute("id", id);
	_elem.setClass = function(className) {
		_elem.setAttribute("class", className);
		_elem.setAttribute("className", className);
	};
	if (styleClass != undefined && styleClass != "") _elem.setClass(styleClass);
	if (content != undefined) _elem.innerHTML = content;
	return _elem;
};
