/*  Time Ticker 1.0
 *  PHP passes array of (seconds) then  
 *  this method converts each item (seconds) to "1d 1h:1m:1s" format
 *  $Id: ticker2.js 16 2006-09-29 03:11:46Z rreyes $
 */

var TK = {
  Source:[],
  Format:"",
  BTN :"",
  TOBJ:new Object(),  
  Init:function(){
  	var pref = "ticker";    
  	var objH =1
    if (document.getElementById || document.all) {          
      for(var i=1;objH;++i){
        var id = pref + i;
        objH = document.getElementById ? document.getElementById(id) : document.all[id];
		if(objH){
    	  TK.TOBJ[id] = objH;
    	  TK.Exec(id);
		}	
      }
    }
  },
  Convert:function(sec){
    if(sec){
      TTS = parseInt(sec);
      TS  = parseInt(TTS % 60);
      TM  = parseInt(TTS / 60 % 60);
      TH  = parseInt(TTS / 3600 % 24);
      TD  = parseInt(TTS / 3600 / 24);      
      SVAL =(TD ? TD+'d ':'')+(TH ? TH+'h ':'')+(TM ? TK.LZ(TM)+'m:':'00m:')+(TS ? TK.LZ(TS)+'s':'00s');
      if(TK.Format){
       SVAL = TK.Format;
       SVAL = SVAL.replace(/%d%/g,TD);
       SVAL = SVAL.replace(/%h%/g,TH);
       SVAL = SVAL.replace(/%m%/g,TM);
       SVAL = SVAL.replace(/%s%/g,TS);
      }
      return SVAL;
    }
  },
  LZ:function (i){
	   return (i<10 ? "0"+i:i);
  },
  Exec:function(id){
    TK.Set(id);
    setTimeout('TK.Exec(\"' + id + '\")',1000);
  },
  Set:function(id){
    var _tsource = TK.Source[parseInt(id.substr(6))-1];
    if(_tsource && Number(_tsource)>1){
       if(Number(_tsource)<59){
          TK.TOBJ[id].style.color ='#FF0000';
       } 
       TK.TOBJ[id].innerHTML = TK.Convert((TK.Source[parseInt(id.substr(6))-1]) ? --TK.Source[parseInt(id.substr(6))-1]:--TK.Source.length);
    }else{
       if(typeof TK.onEnd =='function'){
           TK.onEnd();
       }else{ 
       TK.TOBJ[id].innerHTML = "Already Ended";
           if(TK.BTN){
              var btn = document.getElementById(TK.BTN);
              btn.setAttribute('disabled','disabled');
           }
       }
    }
  }
}

if (window.attachEvent) {
	window.attachEvent('onload', TK.Init);
}else if (window.addEventListener) {
	window.addEventListener("load", TK.Init, false);
}else { 
	window.onload = TK.Init;
}

