var defta = '';
function initApp ()
{	
	// Quick shortening
	$('#quick-short').submit(function() {
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
		if (regexp.test($('#qs').val()))
		{
			$.post("ajax", { act:'url', url:$('#qs').val() }, function(data) {
				if (data == 0)
				{
					alert('Error : '+data);
				}
				else
				{
					var nurl = 'http://twi.mn/'+data;
					$('#qs').val(nurl);
					jQuery.facebox('<div id="fb-tit">Your short URL has been generated</div><div><input name="fb-ta" id="fb-ta" type="text" value="'+nurl+'" /></div><div id="fb-foo">You can copy (CTRL + C) it and past it (CTRL + V)</div>');
					$('#fb-ta').select();
				}
			});
		}
		else
		{
			alert('Not a valid URL!');
		}
		return false;
	});
	
	// Rich Text Editor
	$('#twimn').rte({
		content_css_url: "js/rte/rte.css",
		media_url: "js/rte/"
	});
}

function doPostMsg (msg)
{
	if (msg != '' && msg != defta)
	{
		$('#notification').attr('disabled', 'disabled');
		$('#update-button').css('background-image', 'url(images/twit-send.png)');
		$.post("ajax", { act:'msg', msg:msg }, function(data) {
			switch (data)
			{
				// Not connected
				case 'connect' :
					$(location).attr('href', 'connect');
				break;
				case 'success' :
					$('#notification').removeAttr('disabled');
					$('#notification-content').html('Message sent! Thank you!');
					$('#notification').addClass('success');
					$('#notification').slideDown();
					$('#update-button').css('background-image', 'url(images/twit-this.png)');
				break;
				case 'error' :
				default :
					$('#notification').removeAttr('disabled');
					$('#notification-content').html('Error! Message could not be sent!');
					$('#notification').addClass('error');
					$('#notification').slideDown();
			}
		});
	}
}

function sendSessMsg (msg)
{
	$('#twimn').val(msg);
	doPostMsg($('#twimn').val());
}
