Retweet = {};
Retweet.Button = Class.create({
	initialize: function(element, options){
		if(!window.BitlyClient){
			console.error('this widget needs the bitly javascript client!');
		}
	
		this.element = $(element);
		this.options = Object.extend({
			showStats: true,
			clientname: null,
			getShortenUrl: function(){ return window.location.href;},
			updateButton: function(context, data){
				if(context.element.down('h2 a')){
					var text = context.element.down('h2 a').innerHTML;
				}else{
					var text = context.element.down('h2').innerHTML;
				}
				if(context.options.clientname){
					context.element.down('span.retweet-button').update("<a href=\"http://twitter.com/home/?status=RT %40" + context.options.clientname + ' ' + escape(text) + ' ' + data['shortUrl'] + "\" class=\"btn-retweet\">retweet</a>");
				}else{
					context.element.down('span.retweet-button').update("<a href=\"http://twitter.com/home/?status=RT " + escape(text) + ' ' + data['shortUrl'] + "\" class=\"btn-retweet\">retweet</a>");
				}
			},			
			updateStatsInfo: function(context, data){
				if(data.clicks > 0){
					context.element.down('span.retweet-button a').update(context.element.down('span.retweet-button a').innerHTML + ' (' + data.clicks + ' clicks)');
				}
			}
		}, options || {});
		this.onReady();
	},
	shortenUrl: function(){
		BitlyClient.shorten(this.options.getShortenUrl(this), 'Retweet.CallBacks.onShortenUrl');
	},
	onReady: function(){
		/**
		 * Cache the callback functions, so that you can stop the observing
		 */
		this.callBackShortenUrl = this.onShortenUrl.bindAsEventListener(this);
		this.waitForShortenUrl(this.callBackShortenUrl);
		if(this.options.showStats){
			this.callBackOnStatsReceived = this.onStatsReceived.bindAsEventListener(this);
			this.waitForStats(this.callBackOnStatsReceived);
		}
		this.shortenUrl();
	},
	waitForShortenUrl: function(callback){
		document.observe('bitly:shortenurl', callback);
	},
	waitForStats: function(callback){
		document.observe('bitly:stats', callback);
	},
	onStatsReceived: function(event){
		if(event.memo.response.results.hash == this.element.identify().substr(1)){
			document.stopObserving('bitly:stats', this.callBackOnStatsReceived);
			if(event.memo.response.results){
				this.options.updateStatsInfo(this, event.memo.response.results);
			}
		}
	},
	getStatsForUrl: function(){
		BitlyClient.stats(this.bitlyUrl, 'Retweet.CallBacks.onStats');
	},
	onShortenUrl: function(event){
		var result = $H(event.memo.response.results).get(this.options.getShortenUrl(this));
		if(result){
			document.stopObserving('bitly:shortenurl', this.callBackShortenUrl);
			this.bitlyUrl = result['shortUrl'];
			this.element.writeAttribute('id', 'b' + result['hash']);
			this.options.updateButton(this, result);
			if(this.options.showStats){
				this.getStatsForUrl();
			}
		}
	}
});

Retweet.CallBacks = {
	onShortenUrl: function(data){
		document.fire('bitly:shortenurl', {
			response: data
		});
	},
	onStats: function(data){
		document.fire('bitly:stats', {
			response: data
		});
	}
};