Type.registerNamespace("CronoTimer");

CronoTimer.Timer = function(secondiInizio, durataSecondi, labelToShow) {
    this._secondiInizio = secondiInizio;
    this._durataSecondi = durataSecondi; 
    this._labelToShow = labelToShow;
    this._pause = false;
}

CronoTimer.Timer.prototype = {

    getSecondiInizio: function() {
        return this._secondiInizio;
    },

    getLabelToShow: function() {
        return this._labelToShow;
    },
    
     getDurataSecondi: function() {
        return this._durataSecondi;
    },
    
    getPause: function() {
        return this._pause;
    },
    
    setPause: function(value) {
        this._pause = value;
    },
    
    dispose: function() {
        
    },
    
    callTimer: function() {       
       ret = it.hoepli.hoepliTest.web.TestTimerService.GetTimer(timerCrono.getSecondiInizio() + ';' + timerCrono.getDurataSecondi(), timerCrono.OnComplete, timerCrono.OnTimeOut, timerCrono.OnError);
        return(true);
    },
    
    OnComplete: function(arg) {        
        //alert(document.getElementById(timerCrono.getLabelToShow()).innerHTML);
        var teaTime = new Date();
        teaTime.setHours(0); // 17 means 5 o'clock in the evening
        teaTime.setMinutes(0);
        teaTime.setSeconds(0);
        teaTime.setMilliseconds(timerCrono.getDurataSecondi() - arg + 60000);
        var minutes = teaTime.getMinutes()
        if (minutes < 10)
        minutes = "0" + minutes
        document.getElementById(timerCrono.getLabelToShow()).innerHTML = teaTime.getHours() + ":" + minutes;
        if (timerCrono.getDurataSecondi() - parseFloat(arg) < 0)
        {
            //var postback = new Sys.WebForms.PostBackAction();
            //postback.set_target('btnStop');
            //postback.set_eventArgument('');
            //postback.performAction();
            document.getElementById('btnTimeStop').click();
            document.getElementById(timerCrono.getLabelToShow()).innerHTML = "0:00";
        }
        else        
        {
            setTimeout(function(){timerCrono.callTimer()}, 60000);
        }
    },

    OnTimeOut: function(arg) {
        setTimeout(function(){timerCrono.callTimer()}, 20000);
    },

    OnError: function(arg) {
        setTimeout(function(){timerCrono.callTimer()}, 60000);
    }

}
CronoTimer.Timer.registerClass('CronoTimer.Timer', null, Sys.IDisposable);

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

