﻿/**
 *	news.rotator.bg_image.js
 *	==	loads images as background-image rather than inline
 *
 *	@author		Jason McLaughlin <jasonm@infinityprosports.com>
 *	@date		10-22-2009
 *	@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");
		
		// No block placement
		teaserElem = document.getElementById(this.rotatorId + "_teaser");
		imageElem = document.getElementById(this.rotatorId + "_image");
				
		// Load the article
		headlineElem.innerHTML = article["headline"];
		if (this.staticImage) {
			if (imageElem) {
				imageElem.style.backgroundImage = 'url(' + article["image"] + ')';
			}
			teaserElem.innerHTML = article["teaserText"];
		} else {
			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 (article["source"] && (article["source"] != "http://")) {
			if (!article["source"].match(document.domain)) {
				window.open(article["source"]);
			} else {
				window.location = article["source"];
			}
		} else {
			window.location = this.targetPage + this.articles[this.curRotation]["id"];
		}
	}
	
	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;
		}
	}

}



