function popUpForm() {
  $('#popup-errors').html('');
  $('#fader').fadeIn(800);
  $('#pop-up-story-form').fadeIn(100);
  
  var message = $('#top_story_text').val();
  $('#story_text').val(message);
  $('#twitter-button').attr('href','http://twitter.com/home?status=' + escape(message + ' #gopublic http://bit.ly/11Xg52'));
  return false;
}

function dismissPopUp() {
  $('#fader').fadeOut(300);
  $('.pop-up').fadeOut(100);
}

function updateStoryCount() {
  var input = $('#top_story_text');
  if(input.val() == input.attr('title'))
    var remaining_chars = 140;
  else
    var remaining_chars = 140 - input.val().length;
  $('#twitter-count').html(String(remaining_chars));
}

function validateStory() {
  var input = $('#top_story_text');
  var length = input.val().length;
  if(length > 140) {
    alert("Your story must be no more than 140 characters long");
    return false;
  }else if(length == 0 || input.val() == input.attr('title')) {
    alert("Please type you story into the text box");
    return false;
  }
  return true;
}

LiveFeed = {
  queue: [],
  delay: 10000,
  start: function(_since) {
    LiveFeed.since = _since;
    LiveFeed.interval = setInterval(LiveFeed.poll, LiveFeed.delay);
  },
  stop: function() {
    clearInterval(LiveFeed.interval);
  },
  enqueue: function(fxn) {
    LiveFeed.queue.push(fxn);
  },
  enqueue_next: function(fxn) {
    LiveFeed.queue.unshift(fxn);
  },
  poll: function() {
    var fxn = LiveFeed.queue.shift();
    if(fxn) fxn();
  },
  getSince: function() {
    return LiveFeed.since;
  }
};

$(function(){
  $('.scrollpane').jScrollPane();
  
  $('#fader').click(dismissPopUp);
  
  $('#top_story_text').change(updateStoryCount).keyup(updateStoryCount);
  
  $('input.text, textarea').focus(function() {
    var input = $(this);
    if( input.attr('title') == input.val() ) {
      input.val('');
      input.removeClass('placeholder');
    }
  }).blur(function() {
    var input = $(this);
    if( input.val() == '' ) {
      input.val(input.attr('title'));
      input.addClass('placeholder');
    }
  })
  
  // bust out of frames... needed for clearspring widget
  if (top != self) {
    top.location=self.location;
  }
});