/**
 * news.front.homerotator.js
 *
 * @author James Ryan <james@infinityprosports.com>
 * @date 2006-02-01
 * @package ISM3
 *
 */
 
HTMLNewsRotator = function(rotatorId, staticImage) {
	this.rotatorId = rotatorId;
	this.articles = new Array();
	this.staticImage = staticImage;
	this.numArticles = 0;
	this.curRotation = -1;
	this.timeout = 0;
	this.rotationStatus = 1;
	this.targetPage = 'index.html';
	
	this.imgPlay = '';
	this.imgPlayOver = '';
	this.imgPause = '';
	this.imgPauseOver = '';
	
	if (document.getElementById(this.rotatorId + '_playgraphic')) {
		this.imgPlay = document.getElementById(this.rotatorId + '_playgraphic').value;
	}
	if (document.getElementById(this.rotatorId + '_playgraphic_over')) {
		this.imgPlayOver = document.getElementById(this.rotatorId + '_playgraphic_over').value;
	}
	if (document.getElementById(this.rotatorId + '_pausegraphic')) {
		this.imgPause = document.getElementById(this.rotatorId + '_pausegraphic').value;
	}
	if (document.getElementById(this.rotatorId + '_pausegraphic_over')) {
		this.imgPauseOver = document.getElementById(this.rotatorId + '_pausegraphic_over').value;
	}


	/**
	 * Functions
	 */
	
	this.addArticle = function(headline, teaserText, image, id, source) {
		article = new Array();
		article["headline"] = headline;
		article["teaserText"] = teaserText;
		article["image"] = image;
		article["id"] = id;
		article["source"] = source;
		this.articles.push(article);
		this.numArticles++;
		
		
	}

	this.rotate = function() {
		window.clearTimeout(this.timeout);
		this.curRotation += 1;
		if (this.curRotation == this.numArticles) {
			this.curRotation = 0;
		}
		article = this.articles[this.curRotation];
		
		titleElem = document.getElementById(this.rotatorId + "ar" + this.curRotation);
		
		// Block placement
		headlineElem = document.getElementById(this.rotatorId + "_headline");
		bodyElem = document.getElementById(this.rotatorId + "_body");
		
		// Souce element
		sourceElem = document.getElementById(this.rotatorId + "_source");
		if (sourceElem) {
			if (article["source"] != 'http://') {
				sourceElem.innerHTML = '<a target="_blank" href="' + article["source"] + '">Source</a>';
			}
			else {
				sourceElem.innerHTML = '';
			}
		}
		
		// No block placement
		teaserElem = document.getElementById(this.rotatorId + "_teaser");
		imageElem = document.getElementById(this.rotatorId + "_image");
				
		// Load the article
		if (this.staticImage) {
			headlineElem.innerHTML = article["headline"];
			if (imageElem) {
				imageElem.innerHTML = '<img src="' + article["image"] + '" />';
			}
			teaserElem.innerHTML = article["teaserText"];
		}
		else {
			headlineElem.innerHTML = article["headline"];
			bodyElem.innerHTML = article["teaserText"];
		}
		
		// If we are displaying titles
		if (titleElem) {
			for (var i = 0; i < this.numArticles; i++) {
				stitleElem = document.getElementById(this.rotatorId + "ar" + i);
				stitleElem.className = 'homerotatorlink';
			}
			titleElem.className = 'homerotatorlink_on';
		}

		if (this.rotationStatus == 1) {
			this.timeout = window.setTimeout(this.rotatorId + '.rotate()', this.duration * 1000);
		}
		
	}
	
	this.jumpTo = function(node) {
		this.curRotation = node - 1;
		this.rotate();
	}
	
	this.readMore = function() {
		if (this.articles[this.curRotation]["source"] != "http://" && this.articles[this.curRotation]["source"] != "") {
			url = this.articles[this.curRotation]["source"];
		} else {
			url = this.targetPage + this.articles[this.curRotation]["id"];
		}
		window.location = url;
	}
	
	this.navStart = function(elem) {
		if (!this.rotationStatus) {
			this.rotationStatus = 1;
			this.rotate();
		}
	}
	
	this.navStop = function(elem) {
		this.rotationStatus = 0;
		window.clearTimeout(this.timeout);
	}
	
	this.navPrev = function(elem) {
		window.clearTimeout(this.timeout);
		this.curRotation -= 2;
		if (this.curRotation == -2) {
			this.curRotation = this.numArticles - 2;
		}
		this.rotate();
	}

	this.navNext = function(elem) {
		window.clearTimeout(this.timeout);
		this.rotate();
	}

	// Toggle pause/play
	this.navToggle = function(elem) {
		if (this.rotationStatus) { // We are rotating, so lets pause.
			this.rotationStatus = 0;
			window.clearTimeout(this.timeout);
			document.getElementById(this.rotatorId + '_start').src = this.imgPlayOver;			
		}
		else { // We are paused, so lets rotate
			this.rotationStatus = 1;
			this.rotate();		
			document.getElementById(this.rotatorId + '_start').src = this.imgPauseOver;
		}
	}

	// Pause/play mouseover
	this.navPlayOver = function(elem) {
		if (this.rotationStatus) { // We are rotating, so lets print the pause graphic.
			document.getElementById(this.rotatorId + '_start').src = this.imgPauseOver;
		}
		else { // We are paused, so lets print the play graphic
			document.getElementById(this.rotatorId + '_start').src = this.imgPlayOver;
		}
	}

	// Pause/play mouseout
	this.navPlayOut = function(elem) {
		if (this.rotationStatus) { // We are rotating, so lets print the pause graphic.
			document.getElementById(this.rotatorId + '_start').src = this.imgPause;
		}
		else { // We are paused, so lets print the play graphic
			document.getElementById(this.rotatorId + '_start').src = this.imgPlay;
		}
	}

}



