(function(){
  var d, t, c, first = true, 
    start = new Date('Fri, 16 Oct 2009 09:00:00 PDT').getTime(),
    target = new Date('Sat, 17 Oct 2009 09:00:00 PDT').getTime();
  
  function init(r){
    c = 0;
    d = r.responseText.evalJSON();
    t = d.tweets;
    totals();
    if(first){
      if($('tweets')) tweets();
      countdown();
      first = false;
    }
  }
  
  function totals(){
    $('total').innerHTML = d.total;
  }
  
  function tweets(){
    emit(t[c++ % t.length]);
    setTimeout(tweets, 5000);
  }
  
  function countdown(){
    function diffInWords(there, then){
      var ms = then-there, sec = ms/1000, min = ms/1000/60, 
        hours = (min/60).floor(), days = (hours/24).floor();
      hours = hours%24;
      min = min.floor()%60;
      sec = sec.floor()%60;
      return hours+'h '+min+'m '+sec+'s';
    }
    var now = (new Date).getTime();
    if(now<start){
      $('countdown').innerHTML = diffInWords(now,start) + '<br/>till record attempt begins';
    } else if(now<target){
      $('countdown').innerHTML = '<span>'+diffInWords(now,target) + '</span>to set the Guinness World Record';
    } else {
      $('countdown').innerHTML = '';
    }
    setTimeout(countdown, 500);
  }
  
  function emit(tweet){
    var el = new Element('div', { className: 'tweet' }), span;
    $('tweets').insert(el);
    el.innerHTML = '<div class="flow1"></div><div class="flow2"></div><span> '+
      tweet[2].escapeHTML() + '</span><div class="author"><img src="'+tweet[7]+'"/>'+
      '<a href="'+tweet[5]+'">@'+tweet[4].split('(').first()+'</a></div>';
      
    span = el.down('span');
    
    el.observe('click', function(){
      window.open(tweet[6]);
    });
    
    el.setStyle('font:30px/38px Arial;');
    if(span.offsetHeight>140) el.setStyle('font:24px/30px Arial;');
    if(span.offsetHeight>140) el.setStyle('font:20px/26px Arial;');
    if(span.offsetHeight>140) el.setStyle('font:17px/20px Arial;');
    if(span.offsetHeight>140) el.setStyle('font:14px/16px Arial;');
    
    if(Prototype.Browser.IE)
    el.setStyle('left:'+(Math.random()*450)+'px;top:800px').morph('margin-left:'+(-40-(Math.random()*200))+'px;top:-350px',{
      transition: 'linear',
      propertyTransitions: { marginLeft: 'pulse', top: 'sinusoidal' },
      duration: 15 + Math.random()*10,
      after: function(){ el.remove(); }
    });
    else
    el.setStyle('opacity:0;left:'+(Math.random()*450)+'px;top:800px').morph('margin-left:'+(-40-(Math.random()*200))+'px;top:-350px;opacity:1',{
      transition: 'linear',
      propertyTransitions: { marginLeft: 'pulse', top: 'sinusoidal', opacity: 'mirror' },
      duration: 15 + Math.random()*10,
      after: function(){ el.remove(); }
    });
  }
  
  function update(){
    new Ajax.Request('beatcancer.json?t='+(new Date).getTime(),{ method: 'get', onComplete: init });
  }
  
  $('countdown').setStyle('cursor:pointer').observe('click',function(){
    location.href = 'http://www.guinnessworldrecords.com/';
  });
  
  update();
  setInterval(update, 60000);
})();