var clocks = new Array();
var clinit = false;

function start_clock(idDate,idTime,time){
    cl = new clockStorage();
    cl.idDate = idDate;
    cl.idTime = idTime;
    cl.setTimeStr(time);
    clocks[clocks.length] = cl;
    if(clinit == false){
        showtime();
        clinit = true;
    }
}

var timeShowTimer = null;
function start_oftime(idTime,sec,finalMessage)
{
        clearTimeout(timeShowTimer);
        if (finalMessage==undefined)
        {
            finalMessage = '';
        }
    var cnt = clocks.length;
    var clockExists = -1;
    for(i=0;i<cnt;i++)
    {
             if (clocks[i].idTime == idTime)
             {
                clockExists = i;
                break;
      }
    }
    if (clockExists!=-1)
    {
        clockID = clockExists;
        delete(clocks[clockID]);
    } else {
        clockID = clocks.length;
    }
    cl = new clockOftimeStorage();
    cl.finalMessage = finalMessage;
    cl.idTime = idTime;
    cl.setOftime(sec);
    clocks[clockID] = cl;
    
    showtime();
    return clockID;
}

function start_fwtime(idTime,sec)
{
        clearTimeout(timeShowTimer);
    var cnt = clocks.length;
    var clockExists = -1;
    for(i=0;i<cnt;i++)
    {
             if (clocks[i].idTime == idTime)
             {
                clockExists = i;
                break;
      }
    }
    if (clockExists!=-1)
    {
        clockID = clockExists;
        delete(clocks[clockID]);
    } else {
        clockID = clocks.length;
    }
    cl = new clockFwtimeStorage();
    cl.idTime = idTime;
    cl.setFwtime(sec);
    clocks[clockID] = cl;
    
    showtime();
    return clockID;
}

function showtime(){
    var cnt = clocks.length;
    for(i=0;i<cnt;i++){
        clocks[i].tik();
        clocks[i].draw();
    }
    timeShowTimer = setTimeout("showtime();",250);
}

clockStorage = function(){
    this.Month = '';
    this.Day = '';
    this.Year = '';
    this.Hours = '';
    this.Minutes = '';
    this.Seconds = '';

    this.idDate = '';
    this.idTime = '';
    this.fl1 = false;

    this.setTimeStr = function(str){
        if (str.length==0)
        {
            var date = new Date();
        } else {
        	var date = new Date(str);
        }
        this.Day = date.getDate();
        this.Month = date.getMonth()+1;
        this.Year = date.getYear();
        this.Hours = date.getHours();
        this.Minutes = date.getMinutes();
        this.Seconds = date.getSeconds();
    }
    this.tik = function(){
        var tmp = new Date();
        var i = tmp.getMinutes();
        var s = tmp.getSeconds();
        if(i*1 == 0 && s*1 == 0 && this.fl1 == false){
            if(this.Hours < 23) this.Hours++;
            else{
                this.Day++;
                this.Hours = 0;
            }
            this.fl1 = true;
        }
        if(s*1 == 30) this.fl1 = false;
        this.Minutes = i;
        this.Seconds = s;
        delete(i);
        delete(s);
        delete(tmp);
    }
    this.draw = function(){
        var Mn = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
        if(oDiv = document.getElementById(this.idTime))
        {
            var h = this.Hours;
            if(this.Hours < 10 && this.Hours != '00') h = '0'+this.Hours;
            var i = this.Minutes;
            if(this.Minutes < 10) i = '0'+this.Minutes;
            var s = this.Seconds;
            if(this.Seconds < 10) s = '0'+this.Seconds;
            oDiv.innerHTML = h + ':' + i + ':' + s;
        }
        if(oDiv = document.getElementById(this.idDate)){
            var i = this.Day;
            if(this.Day < 10) i = '0'+this.Day;
            var h = this.Year*1 +2000;
            oDiv.innerHTML = i + ' ' + Mn[this.Month-1] + ' ' + h;
        }

        delete(oDiv); delete(h); delete(i); delete(s);


    }

}

clockOftimeStorage = function(){
    this.Days  = 0;
    this.Hours = '';
    this.Minutes = '';
    this.Seconds = '';

    this.idTime = '';
    this.fl = 0;
        this.finalMessage = '';
    
    this.setOftime = function(sec){
        this.Days    = Math.ceil((sec/3600)/24)-1;
        this.Hours   = Math.ceil(sec/(3600))-1;
        this.Minutes = Math.ceil((sec/60) - this.Hours*60)-1;
        this.Seconds = Math.ceil(sec - this.Hours*3600 - this.Minutes*60);
        this.Hours   = Math.ceil(this.Hours - this.Days*24);

        tmp = new Date();
        this.fl = tmp.getSeconds();

    }
    this.tik = function(){
        var tmp = new Date();
        var s = tmp.getSeconds();
        if(this.fl != s){
            this.Seconds--;
            this.fl = s;
        }
        if(this.Seconds < 0){
            this.Seconds = 59;
            this.Minutes--;
        }
        if(this.Minutes < 0){
            this.Minutes = 59;
            this.Hours--;
        }
        if(this.Hours < 0){
            this.Hours = 23;
            this.Days--;
        }
        if(this.Days < 0){
            this.Days = 0;
            this.Hours = 0;
            this.Minutes = 0;;
            this.Seconds = 0;
        }
    }
    
    this.isfinished = function()
    {
        if (this.Seconds > 0 || this.Minutes > 0 || this.Hours > 0 || this.Days > 0)
        {
            return false;
        } else {
            return true;
        }
    }
    
    this.draw = function()
    {
            var oDiv;
      if(oDiv = document.getElementById(this.idTime))
      {
          if (this.Seconds > 0 || this.Minutes > 0 || this.Hours > 0 || this.Days > 0)
          {
          var h = this.Hours;
          if(this.Hours < 10) h = '0'+this.Hours;
          var i = this.Minutes;
          if(this.Minutes < 10) i = '0'+this.Minutes;
          var s = this.Seconds;
          if(this.Seconds < 10) s = '0'+this.Seconds;
          if(this.Days == 0) oDiv.innerHTML = h + ':' + i + ':' + s;
          else oDiv.innerHTML = this.Days + ' day'+(this.Days>1?'s':'')+' ' +h + ':' + i + ':' + s;
           } else {
            if (this.finalMessage.length > 0)
            {
                oDiv.innerHTML = this.finalMessage;
            } else {
                oDiv.innerHTML = '00:00:00';
            }
        }
      }

      delete(oDiv); delete(h); delete(i); delete(s);
    }

}

clockFwtimeStorage = function(){
    this.Days  = 0;
    this.Hours = '';
    this.Minutes = '';
    this.Seconds = '';

    this.idTime = '';
    this.initialTime = 0;
    this.fl = 0;
        this.finalMessage = '';
    
    this.setFwtime = function(sec){
        this.Days    = Math.ceil((sec/3600)/24)-1;
        this.Hours   = Math.ceil(sec/(3600))-1;
        this.Minutes = Math.ceil((sec/60) - this.Hours*60)-1;
        this.Seconds = Math.ceil(sec - this.Hours*3600 - this.Minutes*60);
        this.Hours   = Math.ceil(this.Hours - this.Days*24);

        tmp = new Date();
        this.fl = tmp.getSeconds();

    }
    this.tik = function(){
        var tmp = new Date();
        var s = tmp.getSeconds();
        if(this.fl != s){
            this.Seconds++;
            this.fl = s;
        }
        if(this.Seconds > 59){
            this.Seconds = 0;
            this.Minutes++;
        }
        if(this.Minutes > 59){
            this.Minutes = 0;
            this.Hours++;
        }
        if(this.Hours > 23){
            this.Hours = 0;
            this.Days++;
        }
        if(this.Days < 0){
            this.Days = 0;
            this.Hours = 0;
            this.Minutes = 0;;
            this.Seconds = 0;
        }
    }
    
    this.draw = function()
    {
            var oDiv;
      if(oDiv = document.getElementById(this.idTime))
      {
          if (this.Seconds > 0 || this.Minutes > 0 || this.Hours > 0 || this.Days > 0)
          {
          var h = this.Hours;
          if(this.Hours < 10) h = '0'+this.Hours;
          var i = this.Minutes;
          if(this.Minutes < 10) i = '0'+this.Minutes;
          var s = this.Seconds;
          if(this.Seconds < 10) s = '0'+this.Seconds;
          if(this.Days == 0) oDiv.innerHTML = h + ':' + i + ':' + s;
          else oDiv.innerHTML = this.Days + ' day'+(this.Days>1?'s':'')+' ' +h + ':' + i + ':' + s;
           } else {
            if (this.finalMessage.length > 0)
            {
                oDiv.innerHTML = this.finalMessage;
            } else {
                oDiv.innerHTML = '00:00:00';
            }
        }
      }

      delete(oDiv); delete(h); delete(i); delete(s);
    }

}
