function QuoteCycler(o)
{
	this.quotes = null;
	this.interval = 15;
	this.index = 0;

	this.attach = QuoteCycler_class.attach;
	this.init = QuoteCycler_class.init;
	this.start = QuoteCycler_class.start;
	this.cycle = QuoteCycler_class.cycle;
	this.timer_id = null;

	this.attach(o);
}

function create_method_closure(obj, method)
{
	function closure(arg)
	{
		method.call(obj, arg);
	}
	
	return closure;
}

var QuoteCycler_class = {
	attach: 
		function attach(o)
		{
			this.anchor = o;
		},
	
	init:
		function init(url)
		{
			if(url)
				this.url = url;
			AJAX.request(this.url,null,AJAX.create_standard_handler(create_method_closure(this, this.start)));
		},
	
	start:
		function start(data)
		{
//			alert("Data retrieved; length = " + data.length);
			if(data.length<=0)
				setTimeout(create_method_closure(this, this.init), 5000);
			with (Math) {
				this.index = floor(data.length * random());
			}
			this.quotes = data;
			this.cycle();
		},

	cycle:
		function cycle()
		{
			clearTimeout(this.timer_id);
			t = document.createElement("p");
			c = document.createElement("cite");
			q = this.quotes[this.index];

			if(q) {
				t.innerHTML = q.text;
				c.innerHTML = "&mdash; " + q.citation;
				
				if(this.anchor) {
					while(this.anchor.childNodes.length>0)
						this.anchor.removeChild(this.anchor.childNodes[0]);
					this.anchor.appendChild(t);
					this.anchor.appendChild(c);
				} else {
					return;
				}
			} else {
				t.innerHTML = "Error loading quotes.";
				this.anchor.appendChild(t);
				return;
			}
			this.index = (this.index+1)%this.quotes.length;

			this.timer_id = setTimeout(create_method_closure(this, this.cycle), this.interval * 1000);
		}
};
