var TwitterBox = new Class({
	Binds: ['nextTweet','prevTweet','setBusy'],
    Implements: [Events, Options, Class.Binds],
    options:{
        tweetContainerId: 'tweets',
		tweetClass: '.tweet',
		nextButtonId: 'next-tweet',
		prevButtonId: 'prev-tweet',
		allowLoop: false
    },
    initialize:function(options){
        
		this.setOptions(options);
		
		this.busy = false;
        
		this.tweetContainer = $(this.options.tweetContainerId);
		this.tweets = $$(this.options.tweetClass);
		this.nextButton = $(this.options.nextButtonId);
		this.prevButton = $(this.options.prevButtonId);
		
		//add events to next & previous buttons
		this.prevButton.addEvent('click',this.prevTweet);
		this.nextButton.addEvent('click',this.nextTweet);
		
		//hide next button on start unless allowLoop is true
		if(!this.allowLoop){
			Browser.Engine.trident ? this.nextButton.setStyle('visibility','hidden') : this.nextButton.tween('opacity',0);		
		}		
		
		//hide all tweets
		this.tweets.each(function(el){el.setStyle('opacity',0)});
		this.tweetCount = this.tweets.length - 1;
		
		this.tweets.each(function(el){
			el.set('tween',{'onComplete':this.setBusy});
		},this);
		
		//show latest tweet (highest index)
		this.tweets[0].setStyle('opacity',1);
		this.currentTweet = 0;	
        
	},
	
	prevTweet:function(){
		if(this.busy)
			return;
			
		this.busy = true;
		this.tweets[this.currentTweet].tween('opacity',0);
		
		if(!this.allowLoop && this.currentTweet == 0){
			Browser.Engine.trident ? this.nextButton.setStyle('visibility','visible') : this.nextButton.tween('opacity',1);
		}
		
		if(this.currentTweet == this.tweetCount){
			this.currentTweet = 0;
		} else {
			this.currentTweet++;
		}
		
		if(!this.allowLoop && this.currentTweet == this.tweetCount){
			Browser.Engine.trident ? this.prevButton.setStyle('visibility','hidden') : this.prevButton.tween('opacity',0);		
		}
		
		this.tweets[this.currentTweet].tween('opacity',1);
	},
	
	nextTweet:function(){
		if(this.busy)
			return;
		
		this.busy = true;
		this.tweets[this.currentTweet].tween('opacity',0);
		
		if(!this.allowLoop && this.currentTweet == this.tweetCount){
			Browser.Engine.trident ? this.prevButton.setStyle('visibility','visible') : this.prevButton.fade('in');
		}			
	
		if(this.currentTweet == 0){
			this.currentTweet = this.tweetCount;
		} else {
			this.currentTweet--;
		}
		
		if(!this.allowLoop && this.currentTweet == 0){
			Browser.Engine.trident ? this.nextButton.setStyle('visibility','hidden') : this.nextButton.tween('opacity',0);
		}
		
		
		this.tweets[this.currentTweet].tween('opacity',1);
	},
	
	setBusy:function(val){
		this.busy = false;
	}
});
