function initPage(dateNow,twitterUsername) {
	$("#myData").tablesorter().tablesorterPager({container: $('#pager')});
	$('#shortener').submit(function () {
		loading(1,'#longurl');
		longUrl = $('#longurl').val();
		$('#shortenButton').val('Making');
		if(longUrl.length == '') {
			$('#success').fadeOut('fast');
			$('#alert').html('<span class="large">Whoops!</span> You can\'t shorten nothing!').fadeIn('fast');
			loading(0,'#longurl');
		}
		else {
			if (longUrl.match("lwh.me")) {
				$('#success').fadeOut('fast');
				$('#alert').html('<span class="large">Whoops!</span> You can\'t shorten an already shotened url!').fadeIn('fast');
				loading(0,'#longurl');
			}
			else {
				if (!longUrl.match("://")) {
					$('#success').fadeOut('fast');
					$('#alert').html('<span class="large">Whoops!</span> You didn\'t specify a protocol eg. http:// so we added http:// for you.').fadeIn('fast');
					$('#longurl').val('http://'+longUrl);
					loading(0,'#longurl');
				}
				else {
					$('#alert').fadeOut('fast');
					$('#success').fadeOut('fast');
					$.ajax({data: {longurl: longUrl}, url: 'shorten.php', complete: function (XMLHttpRequest, textStatus, longUrl) {
						var longUrl = $('#longurl').val();
						var fullResponse = XMLHttpRequest.responseText;
						if(fullResponse.match("#")) {
							var resultData = XMLHttpRequest.responseText.split('::::');
							var shortUrl = resultData[2];
							var colour = resultData[0];
							$('#longurl').val(shortUrl);
							$('#hidden').focus();
							$('#longurl').focus();
							/* $('#longUrl').html(longUrl); */
							/* $('#shortUrl').html(shortUrl); */
							/* $('#tweetShortUrl').html(shortUrl); */
							/* $('#tweetShortUrl').attr({
								href: "http://twitter.com/home?status="+shortUrl,
								title: "Tweet the shortened URL "+shortUrl
							}); */
							/* $('#result').fadeIn('fast'); */
							/*$('.colours').append('<div class="colour" style="background: '+colour+';"></div>').fadeIn('fast');*/
							
							$('#shortUrl').html(shortUrl);
							$('#success').fadeIn('fast');
							//$('#myData tbody').prepend('<tr><td>'+shortUrl+'</td><td>'+longUrl+'</td><td>'+dateNow+'</td><td>0</td><td><a class="tweetLink" href="http://twitter.com/home?status=http://lwh.me/'+shortUrl+'" title="Tweet '+shortUrl+'" target="_blank"></a></td></tr>');
							//$('#myData tbody tr:first').css('background','#CCFF99');
							if(twitterUsername != '') {
								updateUsersTable(twitterUsername);
							}
							loading(0,'#longurl');
						}
						else {
							var shortUrl = XMLHttpRequest.responseText;
							$('#longurl').val(shortUrl);
							$('#hidden').focus();
							$('#longurl').focus();
							/* $('#longUrl').html(longUrl); */
							/* $('#shortUrl').html(shortUrl); */
							/* $('#tweetShortUrl').html(shortUrl); */
							/* $('#tweetShortUrl').attr({
								href: "http://twitter.com/home?status="+shortUrl,
								title: "Tweet the shortened URL "+shortUrl
							}); */
							/* $('#result').fadeIn('fast'); */
							$('#shortUrl').html(shortUrl);
							$('#twitterUsername').hide();
							$('#success').fadeIn('fast');
							loading(0,'#longurl');
						}
					}});
				}
			}
		}
		$('#shortenButton').val('Shorten');
		return false;
	});
}

function loading(status,element) {
	if(status == '1') {
		$(element).addClass('loading').addClass('inactive');
		$('#shortenButton').addClass('inactive');
	}
	if(status == '0') {
		$(element).removeClass('loading').removeClass('inactive');
		$('#shortenButton').removeClass('inactive');
	}
}

function updateUsersTable(twitterUsername) {
	$('#userUrlData').html('<div class="loading"></div>');
	$.post('usersTable.php', {twitterUsername: twitterUsername}, function(data){
		var resultData = data.split('::::');
		$('#noShortened').html(resultData[0]);
		$('#noClicks').html(resultData[1]);
		$('#userUrlData').html(resultData[2]);
		$("#myData").tablesorter().tablesorterPager({container: $('#pager')});
	});
}
