/*
 * afterUpdateCallback - external callback function,
 * that called on every clock update
 */
function Clock(afterUpdateCallback){
   
    
    var _that = this;
    
    var _updateClockInterval = null;
  
    /**
     * init setInterval method, callback function will 
     * update General.dateTime every second
     */
    
    this.startClockUpdate = function(){
        _updateClockInterval = window.setInterval(_updateDateTime, 1000);
    }
    /**
     * clears the interval callback method
     */
    this.stopClockUpdate = function(){
        window.clearInterval(_updateClockInterval);
    }
    /**
     * Called form the constructor, and gets server time
     * 
     */    
    this.syncFromServer = function(){
        $.get('rpcProxy/dummy', function(data, text, xhr) {
            //parser header that sent by the server to time in ms
            //and init General.dateTime
            _that.resetDateTime(Date.parse(xhr.getResponseHeader('Date')));
            _that.startClockUpdate();
   
        });
    }
    /*
     * servrerTime - server time in ms
     */
    this.resetDateTime = function(serverTime){
        var serverTime = new Number(serverTime);
        General.dateTime = new Date(serverTime + AppData.timeZoneOffset);
    }
    /*
     * called by setInterval every second
     */
    var _updateDateTime = function(){
        General.dateTime.setSeconds(General.dateTime.getSeconds() + 1);
        if(typeof afterUpdateCallback == 'function'){
            afterUpdateCallback();
        }
    }
    
    //constructor
    this.syncFromServer();
    
}








