﻿/* 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_now, m_clientShift, m_serverShift=0;
var m_years, m_months, m_days;
var m_hours, m_minutes, m_seconds, m_dn;
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)
        {
			InitTime(results);

            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);
    
}

$(document).ready(function() 
{
		
});
		
function InitTime(serverTime)
{
            var timeParts = serverTime.split("-");
            m_years   = timeParts[0]; 
            m_months  = timeParts[1]; 
            m_days    = timeParts[2];
            m_hours   = timeParts[3];
            m_minutes = timeParts[4];
            m_seconds = timeParts[5];
            
            var serverTime		= new Date(m_years, m_months-1, m_days, m_hours, m_minutes, m_seconds);
            var clientTime		= new Date();
            
            var shiftMinutes	= (clientTime.getTime() - serverTime.getTime()) / 1000.0 / 60.0;
            m_serverShift		= Math.round(shiftMinutes / 15.0) * 15 * 60 * 1000;
            m_now				= new Date(serverTime.getTime() + m_serverShift);
            m_clientShift		= m_now.getTime() - clientTime.getTime();
            
            m_years   = m_now.getFullYear();
            m_months  = m_now.getMonth()+1;
            m_days    = m_now.getDay();
            m_hours   = m_now.getHours();
            m_minutes = m_now.getMinutes();
            m_seconds = m_now.getSeconds();
}

function LocalFromServerTime(serverTime, lineSeparator)
{     
            // server time
            var parts		= serverTime.split("-");            
            var s_years		= parts[0]; 
            var s_months	= parts[1]; 
            var s_days		= parts[2];
            var s_hours		= parts[3];
            var s_minutes	= parts[4];
            var s_seconds	= parts[5];
            
            var serverTime	= new Date(s_years, s_months-1, s_days, s_hours, s_minutes, s_seconds);            
            var localTime	= new Date(serverTime.getTime() + m_serverShift);

	//console.log("LocalFromServerTime: " + MultiLang.Culture);
	//console.log("Globalize.culture().name: " + Globalize.culture().name);

	var culture = $("form[id='aspnetForm']").attr("culture");
	Globalize.culture(culture);
        var localString = Globalize.format(localTime, "d") + lineSeparator + Globalize.format(localTime, "t"); 
			
	return localString;           
}

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 m_now		= new Date(runTime - m_clientShift);
  //var movingtime	= m_now.toLocaleTimeString().split(" ");
  
  //m_clockDiv.html(movingtime[0]);

  //var culture = $('#cultureInput').val();
  Globalize.culture(MultiLang.Culture);

  m_clockDiv.html(Globalize.format(m_now, "T"));
  //m_amPmSpan.html(movingtime.length > 1 ? movingtime[1] : "");


}

/*end of clock */


