var Scrollers = new Array();
function Scroller( Name, Type, Height, Timeout, DefaultClass ) {
	
	this.Element = document.getElementById( Name );
	if ( this.Element == null )
		throw 'Element is empty';
	
	this.ActiveID = null;
	this.IntervalID = -1;
	this.Objects = new Array();
	this.Started = false;
	
	this.Height = Height;
	this.ElementName = Name;
	this.Type = Type;
	this.Timeout = ( Timeout > '' ) ? Timeout : 3500; /* Setting the default when not set in param */
	this.DefaultClass = ( DefaultClass > '' ) ? DefaultClass : 'Tweet'; /* Setting the default when not set in param */
			
	this.init();
}

Scroller.prototype.getElements = function() {
	/* Getting all elements */
	var Elements = this.Element.childNodes;
	var Counter = 1;
	/* Loop trought the element to be sure there are only objects inside */
	for( var i = 0; i < Elements.length; i++ ) {
		var Element = Elements[ i ];
		if( Element.tagName == this.Type ) {
			/* Element is equal to default object type, push it in the array */
			Element.className = 'Invisible';
			Element.id = this.ElementName + 'Object' + Counter;
			this.Objects.push( Element );
			Counter++;
		} else {
			/* Element is not an equal to default, we don't need this element */
		}
	}
	this.setDefaults();
	
	return this.Objects;
}

Scroller.prototype.setDefaults = function() {
	/* Loop trought the element to set defaults */
	for( var i = 0; i < this.Objects.length; i++ ) {
		var Object = this.Objects[ i ];
		/*
		 * Set default top
		 */
		Object.style.top = '-' + this.Height + 'px';
		/*
		 * Set default classname
		 */
		Object.className = this.DefaultClass;
	}
	for( var i = 0; i < this.Objects.length; i++ ) {
		var Object = this.Objects[ i ];
		if ( i == 0 ) {
			/*
			 * The first one needs to be visible
			 */
			Object.style.top = '0px';
			this.ActiveID = 0;
		}
		return;
	}
}

Scroller.prototype.start = function() {
	
	if ( this.Objects.length == 1 ) {
		this.Started = false;
		this.Element.className = 'Done';
	} else if ( this.Objects.length > 1 ) {
		this.Started = true;
		this.Element.className = 'Done';
	} else if ( this.Objects.length == 0 ) {
		this.Started = false;
		this.Element.className = 'Empty';
	} else {
		this.Started = false;
		this.Element.className = 'Error';
		throw 'An error accured';
	}
	
	if ( this.Started === true ) {
		this.setLoop();
	}

	return this.Started;
	
}

Scroller.prototype.loop = function() {
	
	if ( this.Started ) {
		var Height = this.Height;
		var Next = ( this.ActiveID + 1 > ( this.Objects.length - 1 ) ) ? 0 : this.ActiveID + 1;
			
		var Object = this.Objects[ this.ActiveID ];
		$( Object ).animate({
			opacity: 0,
			top: Height
		}, 1000 );
		
		var Object = this.Objects[ Next ];
		$( Object ).animate({
			opacity: 1,
			top: 0
		}, 1000 );
		
		var Object = this.Objects[ this.ActiveID ];
		$( Object ).animate({
			opacity: 0,
			top: ( Height * -1 ) 
		});
		
		this.ActiveID = Next;
	}
}

Scroller.prototype.clearLoop = function() {
	window.clearTimeout( this.IntervalID );
}

Scroller.prototype.setLoop = function() {
	this.IntervalID = setInterval( 'Scrollers[ '+ this.CycleID +' ].loop();', this.Timeout );
}

Scroller.prototype.init = function() {
	this.getElements();
	this.CycleID = ( Scrollers.push( this ) - 1 );
	this.start();
}

var TweetScroller = null;
$(document).ready(function(){
	TweetScroller = new Scroller( 'TweetHolder', 'DIV', 115 );
	$('#TweetHolder').find('a').attr('target','_blank');
});
