jQuery(document).ready(function($) {
   jQuery("#tweetForm").submit(function(event) {
      event.preventDefault();
      jQuery("#tweetCharCount").html("<img src=\""+tweeter_url+"images/loading.gif\" border=\"0\" width=27 height=27 />Sending Tweet...");
      jQuery.get(jQuery("#tweetForm").attr("action")+"?tweetText="+encodeURIComponent(jQuery("#tweetText").val())+"&tweeterAction=tweet", function(responseXML, textStatus) {
         jQuery("#tweetText").val("");
         jQuery("#tweetCharCount").html("");
      }, "xml");
   });

   jQuery("#tweetText").keyup(function(event) {
      updateCharCount();  
      if(event.keyCode == 32 || event.which == 32) {
         var t = $(this).val().split(" ");
         shortenTweetURL(t[t.length-2], t.length-2);
      }
   });
   
   jQuery(".dashboard-tweet").parent().everyTime(30000, function() {
      jQuery.getJSON(site_url+"?tweeterAction=getTweets&n=10&output=json&override=all", function(responseJSON, textStatus) {
         var tweets = responseJSON.tweeterJSON;
         for(var i = tweets.length-1; i >=0; i--) {
            if(jQuery(".dashboard-tweet[id='"+tweets[i].tweet_id+"']").length == 0) {
               jQuery(".dashboard-tweet").parent().prepend('<div class="dashboard-tweet" id="'+tweets[i].tweet_id+'"><a href="http://twitter.com/'+tweets[i].username+'" target="_blank"><div class="image"><img src="'+tweets[i].image+'" border="0" /></div><div class="author">@'+tweets[i].username+'</a> said:</div><div class="text">'+rewriteTweet(tweets[i].text)+'</div></div>');
            }
            if(jQuery(".dashboard-tweet").length > 10)
               jQuery(".dashboard-tweet:last").fadeOut(1000, function() { jQuery(this).remove(); });
         }         
      });      
   });
   
   jQuery(".dashboard-tweet .reply").click(function() {
      jQuery("#tweetText").val(jQuery(this).siblings(".author").children("a").html()); updateCharCount();
   });
   
   jQuery(".dashboard-tweet .retweet").click(function() {
      jQuery("#tweetText").val("RT "+jQuery(this).siblings(".author").children("a").html()+": "+jQuery(this).siblings(".text").text()); updateCharCount();
   });
});

function updateCharCount() {
   if(jQuery("#tweetText").val().length == 0)
      jQuery("#tweetCharCount").html("");
   else
      jQuery("#tweetCharCount").html((140 - jQuery("#tweetText").val().length)+" character remaining");   
}

function rewriteTweet(tweet) {
   var r;
   r = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)\s/;
   tweet = tweet.replace(r, "<a href=\"$1\" target=\"_blank\">$1</a>");
   r = new RegExp("#([a-zA-Z0-9_]{1,50})([!@#:;,\.\^\$\\\&\?\*\(\) $])", "gim");
   tweet = tweet.replace(r, "<a href=\"http://search.twitter.com/search?q=%23$1\" target=\"_blank\">#$1</a> $2");
   r = new RegExp("@([a-zA-Z0-9_]{1,50})([!@#:;,\.\^\$\\\&\?\*\(\) $])", "gim");
   tweet = tweet.replace(r, "<a href=\"http://twitter.com/$1\" target=\"_blank\">@$1</a> $2"); 
   return tweet;
}

function shortenTweetURL(url, n) {
   var r = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/?([\w\/_\.]*(\?\S+)?)?)?)/;
   var m = url.match(r);
   if(m != null) {
      jQuery.get(site_url+"?tweeterAction=shorten&url="+url, function(responseText, textStatus) {
         var t = jQuery("#tweetText").val().split(" ");
         var m = responseText.match(r);
         if(m != null)
            t[n] = m[0];
         var str = "";
         for(var i = 0; i < t.length-1; i++)
            str += t[i]+" ";
         jQuery("#tweetText").val(str);
      }, "text");
   }
}
