        
$(document).ready(function(){

  if ($('#shortcut_shortener').val()) {
    $('#shortcut_shortener').focus();
    $('#shortcut_shortener').get(0).select();  
  }

  $("#sourceurl").focus(function(event){
    $('#error_msg').hide();    
  });

  $("#sourceurl").keyup(function(event){
    $('#error_msg').hide();
    $('#newurl').hide();  
    
    	
    if (event.keyCode == 13) { 
      bmuli_shorten_url();            
      return false;
    }
  });

  
  $("div#generate a").click(function(event){
    
     bmuli_shorten_url();        
     return false;
   
  });


  function bmuli_shorten_url() {      
      var url = encodeURIComponent($('#sourceurl').val());      
      $('#error_msg').hide();
      $('#newurl').hide();
      $.getJSON("http://bmu.li/shorten?longUrl="+url,
          function(data){
            if (data.code != 'success') {
              $('#error_msg').text(data.message);          
              $('#error_msg').show();
            } else {
              $('#desturl').val(data.message);
              $('#newurl').show();              
            }
            $('#desturl').focus();
            $('#desturl').get(0).select();          
          });
  }

});        

