var Timebox = function() { this.initialize(); }

$.extend(Timebox.prototype, {
  expire: 120,  // number of minutes the cookie lasts for
  clocktimer: undefined,
  dateObj: undefined,
  show: true,
  run_function: false,
  clock: undefined,
  
  initialize: function() {
    var clocks = $('.clock');
    if (clocks.length > 0) { this.clock = clocks[0]; }
    
    if (!this.dateObj) { this.dateObj = this.startTimeCookie(); }
    this.start();
  },
  start: function()
  {
  
    if (!this.clock) { return; }

    var cdateObj = new Date();
    var t = (cdateObj.getTime() - this.dateObj.getTime());

    var s = parseInt(t/1000);
    var m = parseInt(s/60);
    var h = parseInt(m/60);

    if (m < 2) {
      var ts = (s % 60);
      var tm = (m % 60);

      var ms = Math.round((t % 1000)/10);
      if (ms>99) {ms=0;}
      if (ms==0) {ms='00';}
      if (ms>0&&ms<=9) { ms = '0'+ms; }

      if (ts>0) { ds = ts; if (ts<10) { ds = '0'+ts; }} else { ds = '00'; }
      var dm=tm;
      if (dm>0) { if (dm<10) { dm = '0'+dm; }} else { dm = '00'; }
      var dh=h-1;
      if (dh>0) { if (dh<10) { dh = '0'+dh; }} else { dh = '00'; }

      readout = dm + ':' + ds + '.' + ms;
    } else {
      readout = '02:00:00';
      if (s < 121) { this.run_function = true; }
    }

    this.clock.innerHTML = readout;
    
    if (dm < 2) { var self=this; window.setTimeout(function() {self.start();},50); }
    
    if (this.show && this.run_function) { this.explode(); this.run_function = false; }
  },
  startTimeCookie: function() {
    if (!this.get_cookie("starttime")) {
      var starttime = new Date();
      var cookie_string = "starttime=" + escape(starttime.toGMTString());
      var expires = new Date(); 
      expires.setTime( expires.getTime() + ((60000)*this.expire) ); // expire cookie in x minutes
      cookie_string += "; expires=" + expires.toGMTString();
      document.cookie = cookie_string; // save cookie
        
      return starttime;
    } else {
      return new Date(this.get_cookie("starttime"));
    }
  },
  get_cookie: function( cookie_name )
  {
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results ) return ( unescape ( results[2] ) );
    else return null;
  },
  delete_cookie: function( cookie_name )
  {
    var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
  },
  explode: function()
  {
      var bgc = $("#clockarea").css('backgroundColor');
      var h = $("#clockarea").css('height');
      var w = $("#clockarea").css('width');
	  $("#clockarea div:first")
 		.fadeOut(1000, function () {
			$("#clockarea").animate({backgroundColor: '#666666', height: "82", width: "246"}, 1000, 
					function() {
						$("#clockarea div:last").fadeIn(1000).fadeTo(20000,1).fadeOut(1000,
							function() {
								$("#clockarea").animate({backgroundColor: bgc, height: h, width: w}, "slow", 
									function() { $("#clockarea div:first").fadeIn(1000); }
								);
							}
						);
					})
 		})
	   	return true;
  }
});

$(document).ready(function () { new Timebox(); });