/* jslint verified 2009.02.02 */
/*jslint browser: true, onevar: true, undef: true, white: false, eqeqeq: true */
/*global document, $, $$, Ajax, cancelBubble */

var Twitter = function (id, data) {
	var _charCount, _tweet, _tUN, _tPW,
		name = 'Tra.kz URL Shortened Page',
		_here = window.location.protocol + '//' + window.location.host,
		_fullURL = null,
		_initialTwt = _here,
		_tLink, _fLink, _mLink, _thanksTwitter, _twAuthDiv, _aLink,
		_twitterAjaxURL = '/api/twitter',
		_twitterURL = 'http://twitter.com/home/?in_reply_to=TrakzURLs&status=', // = text
		_myspURL = 'http://www.myspace.com/Modules/PostTo/Pages/?l=6&', // u = URL, t = title, c = content
		_fbURL = 'http://www.facebook.com/share.php?'; // u = URL, t = title

	function showThanksPane() {
		_twAuthDiv.doFade(false, _twAuthDiv.cloak.bind(_twAuthDiv));
		_thanksTwitter.setFaded(0).show();
		_thanksTwitter.doFade(true);
	}
	function hideThanksPane() {
		_twAuthDiv.doFade(true, _twAuthDiv.reveal.bind(_twAuthDiv));
		_thanksTwitter.doFade(false, _thanksTwitter.hide.bind(_thanksTwitter));
		try {
			_tweet.focus();
			_tweet.select();
		} catch (e) { }
	}

	function checkChars(e) {
		cancelBubble(e);
		var txt = _tweet.scan(),
			left = 140 - txt.length;
		if (left < -20) {
			_tweet.update(txt.substr(0, 159));
			left = -20;
		}
		_charCount.update(left);
		if (left < 20 && left >= 10) { _charCount.stylify('color', '#5C0002'); }
		else if (left < 0) { _charCount.stylify('color', '#F00'); }
		else if (left < 10) { _charCount.stylify('color', '#D40D12'); }
		else { _charCount.stylify('color', '#999'); }
		if (txt !== _initialTwt) {
			_initialTwt = false;
			_tLink.attrib('href', _twitterURL+encodeURIComponent(txt));
			_fLink.attrib('href', _fbURL+'u='+encodeURIComponent(_fullURL)+'&t='+encodeURIComponent(name));
			_mLink.attrib('href', _myspURL+'u='+encodeURIComponent(_fullURL)+
				'&t='+encodeURIComponent(name)+'&c='+encodeURIComponent('<p>'+txt+'</p>'));
		}
	}
	function afterTweet(request) {
		var resp;
		try { resp = JSON.parse(request.responseText); }
		catch (ex) { resp = {y: 0, err: 'Unknown error.'}; }
		if (resp.y) {
			// switch to thanks page.
			showThanksPane();
		} else {
			window.alert("Sorry, Twitter had a problem:\n\n" + resp.err);
		}
	}
	function tweet() {
		var text = _tweet.scan(true),
			un = _tUN.scan(), pw = _tPW.scan(),
			params = "un="+encodeURIComponent(un)+"&pw="+encodeURIComponent(pw)+"&txt="+encodeURIComponent(text),
			ajax = new Ajax(_twitterAjaxURL, {method: 'POST', parameters: params, onComplete: afterTweet, onFailure: afterTweet});
		if (text.length === 0) {
			window.alert('You need to enter something to tweet!');
			try { _tweet.focus(); }
			catch (e) { }
		} else if (un.length === 0) {
			window.alert('You need to enter a username!');
			_tUN.focus();
		} else if (pw.length === 0) {
			window.alert('You need to enter a password!');
			_tPW.focus();
		} else {
			ajax.send();
		}
	}

	function setupTwitterSection() {
		_twAuthDiv = $('tw_auth');
		_thanksTwitter = $('thankstwit');
		_thanksTwitter.create('div').create('a').update('<< tweet again').setClick(hideThanksPane);
		_charCount = $('tw_count').update(140);
		_tweet = $('tweet_txt');
		_tweet.addEvent('keydown', checkChars);
		_tweet.addEvent('keyup', checkChars);
		_tUN = $('tUN');
		_tPW = $('tPW');
		_tUN.addEvent('keydown', function (e) { if (window.enterKey(e)) { _tPW.focus(); } });
		_tPW.addEvent('keydown', function (e) { if (window.enterKey(e)) { tweet(); } });
		$('tw_update').setClick(tweet);
		_tLink = $('tw_link').attrib('href', _twitterURL);
	}
	function setupOtherSection() {
		_fLink = $('link_fb').attrib('href', _fbURL);
		_mLink = $('link_mysp').attrib('href', _myspURL);
		_aLink = $('link_addthis');
		_aLink.addEvent('mouseover', function () { return addthis_open(_aLink, '', _fullURL, _fullURL); });
		_aLink.addEvent('mouseout', addthis_close);
		_aLink.setClick(function () { return addthis_sendto(); });
	}
	function setupDom() {
		setupTwitterSection();
		setupOtherSection();
		_tLink.attrib('href', _twitterURL+encodeURIComponent(_initialTwt));
		_fLink.attrib('href', _fbURL+'u='+encodeURIComponent(_fullURL)+'&t='+encodeURIComponent(name));
		_mLink.attrib('href', _myspURL+'u='+encodeURIComponent(_fullURL)+
			'&t='+encodeURIComponent(name)+'&c='+encodeURIComponent('<p>'+_initialTwt+'</p>'));
		checkChars();
	}
	function setShortened(short, audio) {
		_fullURL = short;
		_tweet.update(short + ' ' + (audio === true ? "\u266A " : ''));
		checkChars();
		$('tw_main').show();
		$('initShare').hide();
		try { _tweet.focus(); }
		catch (e) { }
	}

	window.addLoad(setupDom);
	return {setShortened: setShortened};
}();