document.observe("dom:loaded", function() {

  if (tweet_box = $("twitter_tweet")) {
    tweet_box.observe("keyup", update_twitter_char_count);

    update_twitter_char_count();
  }

});

function update_twitter_char_count() {

  var remaining = 140 - $F("twitter_tweet").length;
  $("tweet_character_count").update(remaining);

  if (remaining < 0) {
    $("tweet_character_count").addClassName("overlimit");
    $("enter_and_tweet").addClassName("disabled");
    $("enter_and_tweet").disabled = true;
  } else {
    $("tweet_character_count").removeClassName("overlimit");
    $("enter_and_tweet").removeClassName("disabled");
    $("enter_and_tweet").disabled = false;
  }

}

function animate_flash() {

  var flash_bar = $$("#flash div.inner").first();
  flash_bar.show();

  new Effect.BlindDown(flash_bar, { duration : 0.5 });

  new PeriodicalExecuter(function(pe) {

    new Effect.BlindUp(flash_bar, { duration : 0.5 });
    pe.stop();

  }, 7);

}


TwitterPopUpLogin = Class.create({

  initialize : function(login_url) {

    this.form = $("twitter_form");

    this.form.observe("submit", this.login.bindAsEventListener(this));

    this.login_url = login_url;

  },

  login : function(event) {

    var width  = 800;
    var height = 400;

    var login_window = window.open(this.login_url, "twitter_login",
      "left=#{left},top=#{top},width=#{width},height=#{height}".interpolate({
        left:   (document.viewport.getWidth() - width) / 2,
        top:    (document.viewport.getHeight() - height) / 2,
        width:  width,
        height: height })
    );

    // thanks, quirksmode :)
    if (window.focus) {login_window.focus()}

    if (event) event.stop();
    return false;

  },

  logged_in : function() {

    this.form.submit();

  }

});
