var nbaDraftTwitterTicker = function() {

	//Set the ticker speed.
	var defaultSpeed = 7;

	//Set the character limit.
	var defaultCharLimit = 127;

	//Set the character limit.
	var defaultArchiveCharLimit = 97;

	//Number in archive.
	var defaultArchiveCount = 10;

	//Set the data file path.
	var dataFile = '/30s/hjson/nbacom/2010/draft/twitter_ticker.html';

	//Get the current server.
	var currentURI = window.location.toString();	

	if (currentURI.indexOf("http://nba-webdev-preview") > -1) {
		var pagePrefix = 'http://nba-webdev-preview.nba.com';
		var dataPrefix = 'http://nba-webdev-preview.nba.com/data';
	} else if (currentURI.indexOf('http://dev') > -1) {
		var pagePrefix = 'http://dev.nba.com';
		var dataPrefix = 'http://nba-webdev-preview.nba.com/data';
	} else if (currentURI.indexOf('http://nba-ref-preview') > -1) {
		var pagePrefix = 'http://nba-ref-preview.nba.com';
		var dataPrefix = 'http://nba-ref-preview.nba.com/data';
	} else {
		var pagePrefix = 'http://www.nba.com';
		var dataPrefix = 'http://data.nba.com/data';
	}

	return {
		archiveCount:defaultArchiveCount,
		archiveCharLimit:defaultArchiveCharLimit,
		currentCharLimit:defaultCharLimit,
		currentSpeed:defaultSpeed,
		currentTweet:0,
		jsonPath:dataPrefix + dataFile,
		paused:false,
		init:function(){

			//Set a flag to note the script just initialized.
			this.firstRun = true;

			//Set the ticker output location.
			this.tweetTicker = $$('#nbaTwitter .nbaTweetPods .nbaTweet')[0];

			//Get a list of the available tweet archive pods.
			this.tweetArchivePods = $$('#nbaTwttrContent .nbaTwitNic');

			//Create a "span" for the CSI Manager to use.
			this.csiReturn = 'csiTwitterTickerReturn';
			var csiDateTimeReturn = document.createElement('span');
			csiDateTimeReturn.setAttribute('id',this.csiReturn);
			csiDateTimeReturn.setAttribute('style','display:none;');
			document.getElementsByTagName('body')[0].appendChild(csiDateTimeReturn);

			//CSI Manager Call
			this.csiInstance = CSIManager.getInstance('csiTwitterTicker');
			this.csiConfig = this.csiInstance.getConfigForId(this.csiReturn);
			this.csiConfig.parent = this;
			this.csiInstance.setConfigForId(this.csiReturn,this.csiConfig);

			this.loadJson();

			return true;
		},
		buildAnchor:function(tweetURI,tweetAccount) {

			var newAnchor = document.createElement('a');
			newAnchor.href = "javascript:window.open('"+tweetURI+"');void(0);";
			newAnchor.appendChild(document.createTextNode(tweetAccount));

			return newAnchor;
		},
		buildURI:function(screenName,tweetId) {
			return 'http://twitter.com/'+screenName+'/statuses/'+tweetId;
		},
		clearChildren:function(parent) {
			while(parent.hasChildNodes()){
				parent.removeChild(parent.lastChild);
			}
		},
		fadeArchiveOut:function(i) {
			if (i < this.archiveCount) {
				if (this.paused === false) {
					Effect.Fade(this.tweetArchivePods[i],{duration:0.5});
					window.setTimeout('nbaDraftTwitterTicker.loadArchivePod('+i+')',550);
				} else {
					window.setTimeout('nbaDraftTwitterTicker.fadeArchiveOut('+i+')',550);
				}
			}
		},
		fadeTickerOut:function() {
			if (this.paused === false) {
				Effect.Fade(this.tweetTicker,{duration:1.0});
				window.setTimeout('nbaDraftTwitterTicker.loadTicker()',1050);
			} else {
				window.setTimeout('nbaDraftTwitterTicker.fadeTickerOut()',1050);
			}
		},
		loadArchivePod:function(i) {

			//Clear the current pod.
			this.clearChildren(this.tweetArchivePods[i]);

			//Set the Twitter fields
			var tweetAccount = '@' + this.jsonData[i].user.screen_name;
			var tweetURI = this.buildURI(this.jsonData[i].user.screen_name,this.jsonData[i].id);

			//Building what the final string will be to get the proper length.
			var stringLength = tweetAccount.length + this.jsonData[i].text.length;
			if (stringLength > this.archiveCharLimit) {
				var tweetMessage = ': '+this.jsonData[i].text.substr(0,(this.archiveCharLimit - tweetAccount.length))+'...';
			} else {
				var tweetMessage = ': '+this.jsonData[i].text;
			}

			//Add the link.
			this.tweetArchivePods[i].appendChild(this.buildAnchor(tweetURI,tweetAccount));

			//Add the rest of the content.
			this.tweetArchivePods[i].appendChild(document.createTextNode(tweetMessage));

			Effect.Appear(this.tweetArchivePods[i],{duration:0.5});
			//Effect.Appear(this.tweetArchivePods[i],{duration:0.0});

			window.setTimeout('nbaDraftTwitterTicker.fadeArchiveOut('+(i+1)+')',550);
			//nbaDraftTwitterTicker.fadeArchiveOut((i+1));
		},
		loadData:function(jsonData,csiReturn,configData) {
			if (jsonData) {
				configData.parent.jsonData = jsonData.slice(0);
				if (this.firstRun === true) {
					this.firstRun = false;
					configData.parent.loadTicker();
				} else {
					configData.parent.fadeTickerOut();
				}

				//Load Tweets into the Archive section.
				configData.parent.fadeArchiveOut(0);
			}
		},
		loadJson:function() {
			//alert("Loading JSON...");
			this.csiInstance.call(this.jsonPath,null,this.csiReturn,this.loadData);
		},
		loadTicker:function() {

			//Set the Twitter fields
			var tweetAccount = '@' + this.jsonData[this.currentTweet].user.screen_name;
			var tweetURI = this.buildURI(this.jsonData[this.currentTweet].user.screen_name,this.jsonData[this.currentTweet].id);

			//Building what the final string will be to get the proper length.
			var stringLength = tweetAccount.length + this.jsonData[this.currentTweet].text.length;
			if (stringLength > this.currentCharLimit) {
				var tweetMessage = ': '+this.jsonData[this.currentTweet].text.substr(0,(this.currentCharLimit - tweetAccount.length))+'...';
			} else {
				var tweetMessage = ': '+this.jsonData[this.currentTweet].text;
			}

			//Set the current pod.
			this.clearChildren(this.tweetTicker);

			//Add the link.
			this.tweetTicker.appendChild(this.buildAnchor(tweetURI,tweetAccount));

			//Add the rest of the content.
			this.tweetTicker.appendChild(document.createTextNode(tweetMessage));

			this.currentTweet++;

			Effect.Appear(this.tweetTicker,{duration:1.0});

			if (this.currentTweet < this.jsonData.length) {
				window.setTimeout('nbaDraftTwitterTicker.fadeTickerOut()',(this.currentSpeed * 1000));
			} else {
				this.currentTweet = 0;
				this.jsonData = [];
				window.setTimeout('nbaDraftTwitterTicker.loadJson()',(this.currentSpeed * 1000));
			}
		}
	}
}();

