/* Objects and methods used */
var fakeWhale = {
  usernameSelector : 'input#tweet_username',
  tweetBoxSelector : 'textarea.countdown',
  tweetLengthSelector : 'div#tweetLength',
  featuredLeaderboardSelector : 'div#featuredLeader',
  recentLeaderboardSelector: 'div#recentLeader',
  topLeaderboardSelector: 'div#topLeader',
  username : function () {
    return jQuery(this.usernameSelector).val();
  },
  tweetBox : function () {
    return jQuery(this.tweetBoxSelector);
  },
  tweet: function () {
    return this.tweetBox().val();
  },
  setTweet: function (tweet) {
    jQuery(this.tweetBoxSelector).val(tweet);
  },
  lengthFunction: function (tweetLength) {
    var recommendedLength = 140;
    var maxLength = 255;
     if (tweetLength < recommendedLength){
       return (recommendedLength - tweetLength) + ' characters remaining';
     } else if (tweetLength < maxLength) {
       return (recommendedLength - tweetLength) + " but who cares!"
     } else {
       this.setTweet(this.tweet().substring(0, maxLength));
       return "Ok thats enough"
     }
     
  },
  remaining : function () {
    return this.lengthFunction(this.tweet().length)
  },
  showPopup: function () {
    jQuery('div#popup').show();
    jQuery('div#overlay').show();
  },
  showAdvanced: function () {
    if (jQuery('div#advanced-options').is(":hidden")) {
      jQuery('div#advanced-options').slideDown('slow');
    } else {
      jQuery('div#advanced-options').slideUp('slow');
    }
  },
  selectFeatured: function() {
    this.selectLeaderboard(this.featuredLeaderboardSelector);
  },
  selectTop: function () {
    this.selectLeaderboard(this.topLeaderboardSelector);
  },
  selectRecent: function () {
    this.selectLeaderboard(this.recentLeaderboardSelector);
  },
  selectLeaderboard: function(selector) {
    jQuery(this.featuredLeaderboardSelector).hide();
    jQuery(this.recentLeaderboardSelector).hide();
    jQuery(this.topLeaderboardSelector).hide();
    jQuery(selector).show();
  }
  
}



/* ------------------------------------------------------------- */
/* jQuery bindings */


jQuery(document).ready(function () {
  jQuery(fakeWhale.tweetBoxSelector).keyup( function () {
    jQuery(fakeWhale.tweetLengthSelector).text(fakeWhale.remaining());
  });
  
  // Are we on the new t-shirt page? If so, fire off the call to create
  jQuery('#cafepress-item-new').each(function(i) {
    var tweet_id = jQuery(this).attr('data-tweet_id');
    jQuery.post("/tweets/" + tweet_id + "/cafepress_items.json", null, function(data, textStatus) {
      var item_id = data.id;
      
      // Send them to the new item!
      window.location.replace("/tweets/" + tweet_id + "/cafepress_items/" + item_id);
    },
    "json"
    );
  });
  
  
  
  
}); //end jQuery ready function