var intTickSpeed = 5000;
var intTickPos = 0;
var tickLocked = false;
var fadeTimerID;
var autoTimerID = 0;
var intTypeSpeed = 16;
var intCurrentPos = 0;
var currentText = '';
var currentLink = '';
var strText = '';
var isFirstPass = true;

var news_Ticker = Class.create();
news_Ticker.prototype = {
    messages: new Array(),
	links: new Array(),
    counter: 0, 
	stringCounter : 0,
	typeSpeed : 16,
	tickSpeed : 5000,
	currentNews: null,
	globalTicker : null,
    target: null, 
	source: null,
    initialize: function(target, source, options) 
    {
        this.target = $(target); 
		this.source = $(source);

        Element.cleanWhitespace(this.source);
        $A($(this.source).childNodes).each(
				function(sel) {
	           		this.messages.push(sel.childNodes[0].innerHTML);
	           		this.links.push(sel.childNodes[0].getAttribute("href"));
		        }.bind(this));  

		this.startTicker();
    },

	startTicker : function() {
		if (this.globalTicker ==null) {
			this.globalTicker = new PeriodicalExecuter(
									this.startCurrentNews.bind(this),
									this.tickSpeed/1000);
		}
		this.startCurrentNews();
	},
	stopTicker : function() {
		this.stopCurrentNews();
		this.globalTicker.stop();
	},

    startCurrentNews: function() {
		if (this.currentNews==null) {
			this.currentNews = new PeriodicalExecuter(
						function() {
							if (this.stringCounter==0) {
								this.target.update();					
								this.target.appendChild(Builder.node('a', {	
																			href:this.links[this.counter],
																			id:'lastNews'
																			} ));
							}
							if (this.stringCounter<this.messages[this.counter].length){
								character= this.messages[this.counter].charAt(this.stringCounter);
								if (character==" ") 
									character = '&#160;';
								new Insertion.Bottom('lastNews', character);
								this.stringCounter++;
							}
							else {
								this.stringCounter = 0;
								this.counter ++;
								if (this.counter==this.messages.length)
									this.counter = 0;
									
								this.currentNews.stop();
								this.currentNews = null;
							}
							
						}.bind(this),
						this.typeSpeed/1000);
		}						
    },
	finishCurrentNews: function(){
		this.target.update('<a href="'+this.links[this.counter]+'" id="lastNews">'+this.messages[this.counter]+'</a>');
	},
    stopCurrentNews: function()
    {
		if (this.currentNews!=null)
			this.currentNews.stop();
		this.currentNews = null;
		this.stringCounter=0;
		this.finishCurrentNews();
		//this.target.update();
    },
	nextCurrentNews: function()
	{
		if (this.currentNews!=null)
			this.currentNews.stop();
		this.currentNews = null;
		this.stopCurrentNews();
		this.counter++;
		if (this.counter==this.messages.length)
			this.counter=0;
		this.startCurrentNews();
	},

	previousCurrentNews: function()
	{
		if (this.currentNews!=null)
			this.currentNews.stop();
		this.currentNews = null;
		this.stopCurrentNews();
		this.counter--;
		if (this.counter<0)
			this.counter=this.messages.length-1;
		this.startCurrentNews();
	}
};
