﻿/* Countdown */
function CountDownClass(display, min, sec, innerOptionPanel) {
    //var optionPanel = innerOptionPanel.parentNode;
    var originalRowStyle = innerOptionPanel.attr(ATTR_ROW_STYLE);
	var countDown = new CountDown(display, min, sec, innerOptionPanel);
	function CountDown(display, min, sec, innerOptionPanel) {
		this.display = display;
		this.min = min;
		this.sec = sec;
		CountDown.ticker = function() {
		    
		    if (innerOptionPanel.attr(ATTR_ROW_STYLE) == originalRowStyle)    //if the style changed on us, we are not involved anymore
		    {
		        if (countDown.sec > 0) {
		            countDown.sec--;
		        }
		        else {
		            if (countDown.min > 0) {
		                countDown.min--;
		                countDown.sec = 59;
		            }
		        }
		        countDown.display.html(PadTwoDigits(countDown.min) + ":" + PadTwoDigits(countDown.sec));
		        if (!countDown.display.hasClass("countDown")) countDown.display.addClass("countDown");
		        if ( (countDown.min > 0 || countDown.sec > 0) ) {
		            setTimeout(CountDown.ticker, 1000);
		        }
		        else if (originalRowStyle == ROW_EXPIRING_SOON) {
		            MakeLocked(innerOptionPanel, 0);
		        }
		    }
		}
	}
	setTimeout(CountDown.ticker, 1);
}

function PadTwoDigits(num) {
	if (num < 10) {
		return "0" + num;
	}
	return num;
}

/* end of countdown */

/* Clock */

function MakeArrayday(size)
{
  this.length = size;
  for(var i = 1; i <= size; i++)
    this[i] = "";
  return this;
}

function MakeArraymonth(size)
{
  this.length = size;
  for(var i = 1; i <= size; i++)
    this[i] = "";
  return this;
}

var m_hours, m_minutes, m_seconds;
var m_ticksTimer = null;
var m_refreshClockTimer = null;
var m_clockDiv, m_amPmSpan;

function StartClock(clockDiv, amPmSpan)
{
    if(clockDiv != null ) m_clockDiv = clockDiv;
    if (amPmSpan != null) m_amPmSpan = amPmSpan;
    
    var func_failureGetTime = 
        function(err)
        {
            GeneralWebServices.GetCurrentServerTime(func_successGetTime, func_failureGetTime);
        }

    var func_successGetTime = 
        function(results, userContext, methodName)
        {
            var parsedHour = results.split("-");
            m_hours = parsedHour[0];
            m_minutes = parsedHour[1];
            m_seconds = parsedHour[2];
            if(m_ticksTimer){clearInterval(m_ticksTimer);m_ticksTimer=null;}
            if(m_refreshClockTimer){clearTimeout(m_refreshClockTimer);m_refreshClockTimer=null;}
            m_ticksTimer=setInterval("work();",1000);
            m_refreshClockTimer = setTimeout("StartClock(null, null)", 120000);   //refresh from server every 120 seconds
        }
        
    var result = GeneralWebServices.GetCurrentServerTime(func_successGetTime, func_failureGetTime);
    
}

function twoDigit(_v)
{
  if(_v<10)_v="0"+_v;
  return _v;
}

function work()
{
  if (!document.layers && !document.all && !document.getElementById) return;
  var runTime = new Date();
  var dn = "AM";
  var sm_hours = m_hours;
  var sm_minutes = m_minutes;
  var sm_seconds = m_seconds;
  if (sm_hours >= 12)
  {
    dn = "PM";
    sm_hours-=12;
  }
  if (!sm_hours) sm_hours = 12;
  sm_minutes=twoDigit(sm_minutes);
  sm_seconds=twoDigit(sm_seconds);
  sm_hours  =twoDigit(sm_hours  );
  movingtime = sm_hours + ":" + sm_minutes + ":" + sm_seconds;

  m_clockDiv.html(movingtime);
  m_amPmSpan.html(dn);

  if(++m_seconds>59)
  {
    m_seconds=0;
    if(++m_minutes>59)
    {
      m_minutes=0;
      if(++m_hours>23)
      {
        m_hours=0;
      }
    }
  }
}

/*end of clock */

