/**
 * @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
    this.sanfran = 7 * 60 * 60;
    this.sanfranBeta = 9 * 60 * 60;
    this.sydney =  11 * 60 * 60;
    this.sydneyBeta = 13 * 60 * 60;
    this.london = 17 * 60 * 60;
    this.londonBeta = 22  * 60 * 60;
    this.fullDay = 24 * 60 * 60;		
    
		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 () {
      if (this.secsSinceMidnight < this.sanfran) {
          this.seconds = (this.sanfran - this.secsSinceMidnight);
					this.isSessionStarted = false;
			}
			
      else if (this.secsSinceMidnight < this.sanfranBeta) {
				  this.seconds = (this.sanfranBeta - this.secsSinceMidnight);
          this.isSessionStarted = true;   
			}   
			
      else if (this.secsSinceMidnight < this.sydney) {
          this.seconds = (this.sydney - this.secsSinceMidnight);
          this.isSessionStarted = false;
			}
				
      else if (this.secsSinceMidnight < this.sydneyBeta) {
				  this.seconds = (this.sydneyBeta - this.secsSinceMidnight);
          this.isSessionStarted = true;   
			}   
			 
      else if (this.secsSinceMidnight < this.london) {
          this.seconds = (this.london - this.secsSinceMidnight);
          this.isSessionStarted = false;
			}		
			
      else if (this.secsSinceMidnight < this.londonBeta) {
				  this.seconds = (this.londonBeta - this.secsSinceMidnight);
          this.isSessionStarted = true;  
			}   
			 
      else { 
          this.seconds = (this.fullDay - this.secsSinceMidnight) + this.sanfran;
          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
  
})

