﻿/**
 *	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++;
		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");
		teaserElem = document.getElementById(this.rotatorId + "_teaser");
		bodyElem = document.getElementById(this.rotatorId + "_body");
*/		
		// No block placement
		$imageElem = $("#" + this.rotatorId + "_image");
		
		// Source element
/*		sourceElem = document.getElementById(this.rotatorId + "_source");
		if (sourceElem) {
			if (article["source"] && (article["source"] != 'http://')) {
				sourceElem.innerHTML = '<a target="_blank" href="' + article["source"] + '">Source</a>';
			} else {
				sourceElem.innerHTML = '';
			}
		}
*/		
		// Load the article
/*		if (this.staticImage) {
			headlineElem.innerHTML = article["headline"];
			if ($imageElem) {
				var newBG = "url(" + article["image"] + ")";
				$imageElem
					.fadeOut("fast", function() { $(this).css({backgroundImage: newBG}); })
					.fadeIn("normal");
			}
			teaserElem.innerHTML = article["teaserText"];
		} else {
			headlineElem.innerHTML = article["headline"];
			bodyElem.innerHTML = article["teaserText"];
		}
*/		if ($imageElem) {
			var newBG = "url(" + article["image"] + ")";
			$imageElem
				.fadeOut("fast", function() { $(this).css({backgroundImage: newBG}); })
				.fadeIn("normal");
		}
		
		// 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.readMore = function() {
		if (article["source"] && (article["source"] != 'http://')) {
//			if (!article["source"].match(location.hostname)) {
			if (!article["source"].match('infinityprosports')) {
				window.open(this.articles[this.curRotation]["source"]);
			} else {
				location.href = this.articles[this.curRotation]["source"];
			}
		} else {
			location.href = this.targetPage + this.articles[this.curRotation]["id"];
		}
	}
	
	this.jumpTo = function(node) {
		this.curRotation = node - 1;
		this.rotate();
	}
	
	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;
		}
	}

}



