/*
<head>
<script type="text/javascript">
TWEETTHIS.bitly("ruiz","R_2d76e3d6428376a25e685ac0ad65805a");
</script>
<link type="text/css" rel="stylesheet" href="http://mr.ruiz.googlepages.com/tweet-this.css"/>
</head>

<body>
<script type="text/javascript">
TWEETTHIS.addButton();
</script>
</body>

<form id="tweetthis_0" action="http://twitter.com/statuses/update.xml">
<input type="hidden" name="source" value="GBSharing"/>
<input type="hidden" name="status" value=""/>
</form>
*/
var TWEETTHIS = new TWEETTHIS();
function TWEETTHIS() {
	this.options = {//buttonText, buttonIcon,apiKey,login
		buttonText:"TweetThis",
		buttonIcon:"http://mr.ruiz.googlepages.com/tweet-this-small.png",
		login:"twitthis",
		apiKey:"R_f0b6b5e3a4c028b3ec97119e4f3ce16c"
	}
	this.buttons = [];

	this.BITLY = {
		VERSION:"2.0.1",
		URL_BASE:"http://api.bit.ly/shorten?",
		login:"twitthis",
		apiKey:"R_f0b6b5e3a4c028b3ec97119e4f3ce16c",
		url:function(longUrl,callback) {		
			var u = this.URL_BASE+'version='+this.VERSION+'&callback='+callback;
			if (this.login && this.apiKey) {
				u = u + '&login='+this.login + '&apiKey='+this.apiKey;
			}
			u = u + '&longUrl='+encodeURIComponent(longUrl);
			return u;
		}
	}
	
	this.bitly = function(login,apiKey) {
		this.BITLY.login = login;
		this.BITLY.apiKey = apiKey;
	}
	
	this.addButton = function(args,options) {
		var id = 'tweetthis_'+this.buttons.length;
		var longUrl = args.longUrl || document.URL;
		var message = args.message || document.title;
		var buttonText = args.buttonText || this.options.buttonText;
		var buttonIcon = args.buttonIcon || this.options.buttonIcon;
		var stp = args.stp || false;
		
		var a = document.createElement('a');
		a.className = 'ttbutton ttico_default';
		a.title = 'TweetThis on Twitter';
		a.href = 'javascript:void(0)';
		
		var s = document.createElement('span');
		s.className = 'ttbuttontext';
		s.appendChild(document.createTextNode(buttonText));
		
		a.appendChild(s);
		var self = this;
		
		this[id] = function(o) { 
			var shortUrl;
			for (var u in o.results) {
				shortUrl = o.results[u].shortUrl
				break;
			}
			var tweet = "http://twitter.com/home?status="+encodeURIComponent(message)+"+"+shortUrl;
			//alert('tweet: '+tweet);
			window.open(tweet,'_blank');
			//stp HTTP POST
			/*
			var f = document.getElementById(id+'_form');
			f.submit();
			*/
		}
		
		a.onclick = function() { self.runScript(self.BITLY.url(longUrl,"TWEETTHIS."+id)); }//function() { alert('TweetThis: '+self.BITLY.url(longUrl)); }

		var id = 'tweetthis_'+this.buttons.length;
		document.write('<span id="'+id+'"></span>');
		
		var t = document.getElementById(id);
		t.appendChild(a);
		
		if (stp) {
/*
<form id="tweetthis_0" action="http://twitter.com/statuses/update.xml">
<input type="hidden" name="source" value="GBSharing"/>
<input type="hidden" name="status" value=""/>
</form>
*/
			var f = createElement('form');
			f.setAttribute('id',id+'_form');
			f.setAttribute('action','http://twitter.com/statuses/update.xml');
			
			var s = createElement('input');
			s.setAttribute('type','hidden');
			s.setAttribute('name','source');
			s.setAttribute('value','GBSharing');
			f.appendChild(s);

			var m = createElement('input');
			m.setAttribute('type','hidden');
			m.setAttribute('name','status');
			s.setAttribute('value',message);
			f.appendChild(m);

			t.appendChild(f);
		}
		
		this.buttons.push(t);
	}

	this.runScript = function(s) {
		var d = document.createElement('script');
		d.setAttribute('type','text/javascript');
		d.setAttribute('src',s);
		document.body.appendChild(d);
	}
	
	this.addCss = function() {
		var l = document.createElement('link');
		l.setAttribute('type','text/css');
		l.setAttribute('rel','javascript');
		l.setAttribute('href',"http://mr.ruiz.googlepages.com/tweet-this.css");
		document.head.appendChild(l);
	}
}