//////////////////////////////////////////////////////////////////////////
//TIME
//////////////////////////////////////////////////////////////////////////
var g_StartDate;
var g_CurrentDate;
var g_TimerArray = new Array;
var g_TimerId = 0;
//////////////////////////////////////////////////////////////////////////
function AddTimer ( Timer )
{
    Timer.m_TimerId = g_TimerId;
    g_TimerArray.push( Timer );
    Timer.Start();
    g_TimerId++;
}
//////////////////////////////////////////////////////////////////////////
function RemoveTimer(TimerName)
{
    for( var index = 0; index < g_TimerArray.length ; index++ )
    {
        if(g_TimerArray[ index ].m_Name== TimerName)
        {
            g_TimerArray.splice(index,1);
            return;
        }
    }
}
//////////////////////////////////////////////////////////////////////////
//CLASS CTimer
//////////////////////////////////////////////////////////////////////////
function CTimer()
{
    this.m_NumberRepeate = 0;
    this.m_RepeateCount = 1;
    this.m_Interval=0;
    this.m_NextOnTime = 0;
    this.m_StartTime = 0;
    this.m_OnTimeFunction = null;
    this.m_FirstExecutionOnStart = true;
    this.m_Name ="Timer";
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.SetName = function( Name ) 
{
    this.m_Name = Name;
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.SetNumberRepeate = function( NumberRepeate ) 
{
    this.m_NumberRepeate = NumberRepeate;
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.SetInterval = function( IntervalTime ) 
{
    this.m_Interval = IntervalTime;
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.SetOnTimeFunction = function( function_body ) 
{
    this.m_OnTimeFunction = function_body;
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.SetFirstExecutionOnStart = function( state ) 
{
    this.m_FirstExecutionOnStart = state;
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.Start = function() 
{
    var time= new Date();
    hours= time.getHours();
    mins= time.getMinutes();
    secs= time.getSeconds();
    milisec = time.getMilliseconds()/1000;
    etime=hours*3600+mins*60+secs+milisec ;

    this.m_NextOnTime = etime;
    this.m_StartTime = new Date();
    this.m_NextOnTime += this.m_Interval; 
    
    if( this.m_FirstExecutionOnStart )
    {
        //this.m_RepeateCount++;
        this.m_OnTimeFunction();
    }
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.OnTime = function() 
{
    this.m_RepeateCount++;
    if( this.m_RepeateCount < this.m_NumberRepeate )
    {
        this.m_NextOnTime += this.m_Interval; 
    }
    this.m_OnTimeFunction();
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.TestTimer = function( TestTime ) 
{
    if( this.m_RepeateCount < this.m_NumberRepeate )
    {
        if( TestTime >= this.m_NextOnTime )
        {
            this.OnTime();
        }
    }
}
//////////////////////////////////////////////////////////////////////////
CTimer.prototype.GetDebugInfo = function( current_time ) 
{
    var time_display = Math.abs( current_time - this.m_NextOnTime );
    
    
    debug_info='<div class="DebugConsol_Sucess">';
    debug_info+='<ul class="DebugConsol_Message">';
    debug_info+='<li>Timer Id:'+this.m_TimerId+'</li>';
    
    
    if( this.m_NumberRepeate != this.m_RepeateCount)
    {
        debug_info+='<li>Start time:'+this.m_StartTime+' Interval:'+this.m_Interval+' Next occurence:'+time_display+' sec</li>';
    }else
    {
        debug_info+='<li>Start time:'+this.m_StartTime+' Interval:'+this.m_Interval+'</li>';
    }
    
    
    debug_info+='<li>Number repeat:'+this.m_NumberRepeate+' Actual reapeat count:'+this.m_RepeateCount+'</li>';
    debug_info+='</ul>';
    debug_info+='</div>';
    return debug_info;
}



//////////////////////////////////////////////////////////////////////////
function display()
{
    rtime=etime-ctime;
    if (rtime>60)
    {
        m=parseInt(rtime/60);
    }
    else
    {
        m=0;
    }
    s=parseInt(rtime-m*60);
    if(s<10)
    {
        s="0"+s;
    }

    window.status+="Time Remaining :  "+m+":"+s;
    
}

//////////////////////////////////////////////////////////////////////////
function InitTime()
{
    g_StartDate = new Date();
    var time= new Date();
    hours= time.getHours();
    mins= time.getMinutes();
    secs= time.getSeconds();
    milisec = time.getMilliseconds()/1000;
    //time.getMilliseconds
    etime=hours*3600+mins*60+secs+milisec;
    etime += 30;
    Update();
}
//////////////////////////////////////////////////////////////////////////
function Update()
{
    var time= new Date();
    g_CurrentDate = new Date();
    hours= time.getHours();
    mins= time.getMinutes();
    secs= time.getSeconds();
    milisec = time.getMilliseconds()/1000;
    current_time=hours*3600+mins*60+secs+milisec;
    
    for( var index = 0; index < g_TimerArray.length ; index++ )
    {
        g_TimerArray[ index ].TestTimer( current_time );
    }

    if(document.getElementById('Debug_Consol_Message_Count_Time'))
    {
        document.getElementById('Debug_Consol_Message_Count_Time').innerHTML=g_TimerArray.length;
        document.getElementById('DEBUG_TIME_INFO').innerHTML="";
        document.getElementById('DEBUG_TIME_INFO').innerHTML+="Start time :"+g_StartDate.toLocaleString()+"<br/>";
        document.getElementById('DEBUG_TIME_INFO').innerHTML+="Current time :"+g_CurrentDate.toLocaleString()+"<br/>";
        document.getElementById('DEBUG_TIME_INFO').innerHTML+="Timer list:<br/>";
    }
    for( var index = 0; index < g_TimerArray.length ; index++ ) {
        if (document.getElementById('DEBUG_TIME_INFO')) {
            document.getElementById('DEBUG_TIME_INFO').innerHTML += g_TimerArray[index].GetDebugInfo(current_time);
        }
    }

    //display();
    
    window.setTimeout("Update()",10);
}