/**
 * Copyright (c) 2009 Mark S. Kolich
 * http://mark.kolich.com
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
*/

Kolich = {};

Kolich.Twitter = {};
Kolich.Twitter.div = {};
Kolich.Twitter.count = 0;
Kolich.Twitter.trigger = false;
Kolich.Twitter.tracker = "/tracker/Twitter/";

Kolich.Twitter.setup = function(){
  Kolich.Twitter.div = $("#lifestream");
  Kolich.Twitter.div = $("#lifestream");
  if(!Kolich.Twitter.div.is(":hidden")){
    Kolich.Twitter.div.show(Kolich.Twitter.load);
  }
  Kolich.Twitter.div.click(function(e){
    var url = encodeURIComponent(self.location.href);
    $.get(Kolich.Twitter.tracker+"?"+url);
    if(e.target.tagName!="A"){
      if(Kolich.Twitter.count<4){
        Kolich.Twitter.div.show(Kolich.Twitter.load);
      }
      else {
        alert("Dude, are you serious?  Stop " +
               "refreshing my Twitter stream like " +
               "a mad-man.");
      }
    }
  });
}

Kolich.Twitter.load = function(){
  Kolich.Twitter.count += 1;
  var token = (Math.random()*100).toString();  
  Kolich.Twitter.div.load("http://games2009.crossfit.com/templates/twitter/index.php?"+token.substr(3,8),function(r){    
     Kolich.Twitter.div.show();     
     
if(!Kolich.Twitter.trigger){
        Kolich.Twitter.trigger = !Kolich.Twitter.trigger;
        setTimeout("Kolich.Twitter.clearTrigger()",20000);
     }
  });
}

Kolich.Twitter.clearTrigger = function(){
  Kolich.Twitter.count = 0;
  Kolich.Twitter.trigger = !Kolich.Twitter.trigger;
}


// jQuery sets everything up when the DOM is ready.
$(document).ready(function(){
    Kolich.Twitter.setup();
});