 $(document).ready(function() 
 {
   var more = '... http://tweeeet.com/uniquekey';
   $('#tweetbox').val(more);
   $('#tweeeetbox').val('');
   
   $('#tweeeetbox').keyup(function()
   {
      var tweet = this.value.substr(0, 140 - more.length) + more;
      $('#tweetbox').val(tweet.replace(/[\n\r\t]/g,' '));
      $('#tweetcount').text(140 - tweet.length + ' characters left');
      $('#tweeeetcount').text(5000 - this.value.length + ' characters left');
   });   
  
   $('#create input').click(function(event)
   {
      if (this.value  == 'new')
      {           
         $('#cover').show();
         $('#tweetbox').css('background', '#FFDDDD');      
         $('#tweetbox').val(more);
         $('#tweeeetbox').val('');
         $('#willLook').text('Will look something like...');         
   		$('#view').hide();
 		   $('#directPost').fadeOut();
         $('#loginStatus').fadeOut(); 
         this.value = 'create';
         event.preventDefault();
      }
   });  
   $('#form').submit(function()
   {
      if ($('#tweeeetbox').val().length == 0)
      {
         $('#tweeeetbox').val('try putting something here!')
         return false;
      }
      $.post(this.action, $(this).serialize(), callback);
      return false;
   });
   $('#direct').submit(function()
   {
      $('#tweeet').val($('#tweetbox').val());
      $.post(this.action, $(this).serialize(), directCallback);      
      return false;
   });
   $('#tweeeetbox').autogrow();   
});
   
function viewTweet() 
{
   top.location = tweetURL;
}

var directCallback = function(data, status)
{
   var text;
   var color;
   if (data == '#3')
   {
      text = 'Twitter said your username or password wasn\'t valid';
      color = '#FF4444';   
   }
   else if (data.length  == 2 && data[0] == '#') 
   {
      text = 'Something went wrong posting to twitter. Maybe you should just copy & paste for now!';
      color = '#FF4444';
   }
   else
   {
      text = 'Thanks. Your post has been submitted to twitter!';
      color = '#000000';
   }
   $('#loginStatus').css('color', color);
   $('#loginStatus').text(text);
   $('#loginStatus').fadeIn(); 
}   
var callback = function(data, status)
{
   if (data.length  == 2 && data[0] == '#')
   {
      $('#tweetbox').val('Noooo... Something has gone wrong. We\'re real sorry. Rest assured that the error has been reported and we\'ll fix\'er up as soon as possible! Maybe try again?');
      return;
   }
   $('#tweetbox').val($('#tweetbox').val().replace(/uniquekey$/, data));   
   tweetURL = "http://tweeeet.com/" + data;
   $('#cover').hide();
   $('#willLook').text('Is...');
   $('#tweetbox').css('background', '#DDFFDD');
   $('#create input').val('new');
   $('#view').show();
   $('#directPost').fadeIn();
}