/**
 * @author aamaratunga
 */

BetaCountDownClock = Class.create ({
  
    initialize : function (secsSinceMidnight, imagePath, flash) {
    
		this.secsSinceMidnight = secsSinceMidnight;
    this.seconds;
		this.isSessionStarted;
		if (imagePath == null)
		  this.imagePath = '/beta/images/clock/';
		else
		  this.imagePath = imagePath;	
			
    if (flash == null)
      this.flash = true;
    else
      this.flash = flash; 
		
		// secs since midnight in GMT
		var hoursInSecs = 60 * 60;
    this.fullDay = 24 * 60 * 60;		
		this.startTime = new Array(3 * hoursInSecs, 5.5 * hoursInSecs,  9.5 * hoursInSecs, 13 * hoursInSecs, 17 * hoursInSecs, 19 * hoursInSecs, 21 * hoursInSecs, 23 * hoursInSecs);
		this.endTime   = new Array(4 * hoursInSecs, 6.5 * hoursInSecs, 10.5 * hoursInSecs, 14 * hoursInSecs, 18 * hoursInSecs, 20 * hoursInSecs, 22 * hoursInSecs, 24 * hoursInSecs);
    
		this.setSecondsRemaining();
    this.digitize();
    
    // preload digit images
    new Image().src = this.imagePath+"0.png";
    new Image().src = this.imagePath+"1.png";
    new Image().src = this.imagePath+"2.png";
    new Image().src = this.imagePath+"3.png";
    new Image().src = this.imagePath+"4.png";
    new Image().src = this.imagePath+"5.png";
    new Image().src = this.imagePath+"6.png";
    new Image().src = this.imagePath+"7.png";
    new Image().src = this.imagePath+"8.png";
    new Image().src = this.imagePath+"9.png";
    },
    
		setSecondsRemaining : function () {
			var early = true;
			for (var i = 0, len = this.startTime.length; i < len; ++i) {
	      if (this.secsSinceMidnight < this.startTime[i]) {
	          this.seconds = (this.startTime[i] - this.secsSinceMidnight);
	          this.isSessionStarted = false;
						early = false;
						break;
	      }
	      else if (this.secsSinceMidnight < this.endTime[i]) {
	          this.seconds = (this.endTime[i] - this.secsSinceMidnight);
	          this.isSessionStarted = true; 
						early = false;
						break;  
	      }   
			}
			
			if (early) {
        this.seconds = (this.fullDay - this.secsSinceMidnight) + this.startTime[0];
        this.isSessionStarted = false;
			}
			
		},
		
    tick: function() {
      this.seconds--;
			this.secsSinceMidnight++;
      
      if (this.seconds == -1)
        this.setSecondsRemaining();
      
			if (this.isSessionStarted) {
				if ($('clock').visible()) {
					$('clock').hide();
					$('sessionStartedMsg').show();
					if (this.flash)
					 this.flashMsgFor(this.seconds);
				}
			}
			else { 
        if ($('sessionStartedMsg').visible()) {
          $('sessionStartedMsg').hide();
          $('clock').show();
        }
        this.digitize();
			}

      if (this.secsSinceMidnight == 86400)
			  this.secsSinceMidnight = 1;

    },
    
    flashMsgFor: function(thisManySeconds) {
      if ($('nowInProgMsg') != null) {
        if(thisManySeconds != 0)
          Effect.Pulsate('nowInProgMsg', { duration: thisManySeconds, pulses: thisManySeconds / 2 });
      }
    },
    
    digitize: function () {
  
      var _seconds;
      _seconds = this.seconds;
  
      // convert total secs to hrs, mins and secs
      var _hours = Math.floor(_seconds / (60 * 60));
      _seconds = _seconds % (60 * 60);
			 
      var _minutes = Math.floor(_seconds / 60);
      _seconds = _seconds % 60;
      
      // convert to string 
      _minutes = _minutes + '';
      _seconds = _seconds + '';
			_hours = _hours + '';
			
			if (_hours > 9) {
        $('bhr1').src = this.imagePath  + _hours.substring(0 , 1) + '.png';
        $('bhr2').src = this.imagePath + _hours.substring(1 , 2) + '.png';
      }
      else {
        $('bhr1').src = this.imagePath + '0.png';
        $('bhr2').src = this.imagePath + _hours + '.png';
      }

      if (_minutes > 9) {
        $('bmn1').src = this.imagePath + _minutes.substring(0 , 1) + '.png';
        $('bmn2').src = this.imagePath + _minutes.substring(1 , 2) + '.png';
      }
      else {
        $('bmn1').src = this.imagePath + '0.png';
        $('bmn2').src = this.imagePath + _minutes + '.png';
      }
  
      if (_seconds > 9) {
        $('bse1').src = this.imagePath + _seconds.substring(0 , 1) + '.png';
        $('bse2').src = this.imagePath + _seconds.substring(1 , 2) + '.png';
      }
      else {
        $('bse1').src = this.imagePath + '0.png';
        $('bse2').src = this.imagePath + _seconds + '.png';
      }
    } // digitize
  
})


