/**
 * featured.front.rotator.js
 *
 * @author James Ryan <james@infinityprosports.com>
 * @date 2006-02-01
 * @package ISM3
 *
 */
 
HTMLFeaturedRotator = function(rotatorId, blockId, pluginId) {
	this.rotatorId = rotatorId;
	this.archive = new Array();
	this.featured = 0;
	this.cache = new Array();
	this.curRotation = -1;
	this.url = 'index.html?content_type=plugin&block_id=' + blockId + '&plugin_id=' + pluginId + '&id=';

	this.xmlhttpreq = 0;
	this.featuredElem = document.getElementById(this.rotatorId + '_data');

	// Load xmlhttprequest object
	if (document.all) {
		this.xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!this.xmlhttpreq && typeof XMLHttpRequest != 'undefined') {
		try {
			this.xmlhttpreq = new XMLHttpRequest();
		} catch (e) {
			this.xmlhttpreq = false;
		}
	}


	/**
	 * Functions
	 */
	
	this.run = function() {
		this.loadEntity(this.featured);
	}
	
	this.handleResponse = function() {
		if (this.xmlhttpreq.readyState == 4) {
			this.featuredElem.innerHTML = this.xmlhttpreq.responseText;
		}		
	}
	
	this.loadEntity = function(id) {
		if (!this.cache[id] && id) {
			
			var url = this.url + id;
			var ref = this;
			this.featuredElem.innerHTML = '<blink>Loading...</blink>';
	
			// Submit the xmlhttp request
			this.xmlhttpreq.open("GET", url, true);
			this.xmlhttpreq.onreadystatechange = function() {
				if (ref.xmlhttpreq.readyState == 4) {
					ref.featuredElem.innerHTML = ref.xmlhttpreq.responseText;
					var cp_d = ref.featuredElem.getElementsByTagName("script");
					for (var cp_x = 0; cp_x < cp_d.length; cp_x++) {
						with (window) {
							eval(cp_d[cp_x].text);
						}
					}
				}
			};
			this.xmlhttpreq.send(null);

		}
		else if (id) {
			ref.featuredElem.innerHTML = this.cache[id];
		}
	}
	
	this.addArchive = function(id) {
		this.archive.push(id);
	}

	this.next = function() {
		
		// If we are displaying the primary featured entity
		if (this.curRotation == -1) {
			return 0;
		}
		else if (this.curRotation == this.archive.length - 1) {
			this.curRotation = -1;
			this.loadEntity(this.featured);
		}
		
		// Or a normal archived entry
		else {
			this.curRotation += 1;
			this.loadEntity(this.archive[this.curRotation]);
		}
	}
	
	this.prev = function() {
		
		// If we are displaying the primary featured entity
		if (this.curRotation == -1) {
			this.curRotation = this.archive.length - 1;
		}
		
		// Decrement by 1
		else if (this.curRotation > 0) {
			this.curRotation -= 1;
		}
		else {
			return 0;
		}
		
		this.loadEntity(this.archive[this.curRotation]);
	}

}


