//-----------------------------------------------------------------------------
//CountDownTimer
//
//    //----------
//    //How to use
//    //----------
//
//    var options = {
//      "beat"        : 300,      //clock
//      "startTime"   : 10000,    //msec
//      "callbackFunc": getTimer  //callback function name. this func name is outer function.
//    }
//    var myTimer = new CountDownTimer(options);
//
//  NOTE:
//    callbackFunc gets remainingtime by msec.
//
//-----------------------------------------------------------------------------

var CountDownTimer = Class.create({

  //arguments -----------------------------------------------------------------
  isActive      : true,
  callbackFunc  : function(){ alert("[WARNNING] callbackFunc is not defined.") },
  beat          : 500,          //default 500msec
  startTime     : 5000,         //default 5sec
  endDate       : undefined,
  nowTime       : undefined,
  prevPauseTime : undefined,
  endTime       : undefined,
  baseTimer     : undefined,


  //methods -------------------------------------------------------------------
  initialize: function(args){

    if(args){

      this.isActive       = args.isActive;

      this.callbackFunc   = args.callbackFunc;

      this.beat           = args.beat;
      this.startTime      = args.startTime;

      this.endDate        = new Date();
      this.nowTime        = this.endDate.getTime();
      this.endDate.setTime(this.nowTime+this.startTime);
      this.endTime        = this.endDate.getTime();
      this.prevPauseTime  = this.nowTime;
      this.callbackFunc(this.startTime);

      //start baseclock
      var self = this;
      this.baseTimer      = setInterval(
                              function(){
                                self.beatTime();
                              },
                              this.beat
                            );


    }
    else{

      alert("[WARNNING] Init CountDownTimer Class require argument");

    }

  },


  setStartTime: function(starttime){
    this.startTime = starttime;
  },


  startTimer: function(){

    this.isActive=true;

  },


  pauseTimer: function(){

    this.isActive=false;

  },


  resetTimer: function(){

    this.endDate        = new Date();
    this.nowTime        = this.endDate.getTime();
    this.prevPauseTime  = this.nowTime;
    this.endDate.setTime(this.nowTime+this.startTime);
    this.endTime        = this.endDate.getTime();
    this.isActive       = false;
    this.callbackFunc( this.perseTime(this.startTime) );

  },


  beatTime: function(){

    var nowDate   = new Date();
    this.nowTime  = nowDate.getTime();

    if( !this.isActive ){

      var tempEndTime     = this.endDate.getTime() + Number( this.nowTime - this.prevPauseTime );
      this.endDate.setTime( tempEndTime );

    }

    var remainingTime = this.endDate.getTime() - this.nowTime;
    this.callbackFunc( this.perseTime(remainingTime) );
    this.prevPauseTime   = this.nowTime;

  },

	//ミリ秒を00:00:00にパース
	perseTime: function(msec){
		var tempMark=" ";
		if(msec<0){
			msec = 0-msec;
			tempMark="-";
		}
		var hour	 = this.zeroPadding( Math.floor(msec/1000/60/60) ,2 );
		var min		 = this.zeroPadding( Math.floor(msec/1000/60) ,2 );
		var sec		 = this.zeroPadding( Math.floor(msec/1000)%60 ,2 );
		return tempMark+hour+":"+min+":"+sec;
	},
	//ゼロパディング
	zeroPadding: function(targetNumber,digitNum){
		var tempNum=Math.pow(10,digitNum)+Number(targetNumber);
		tempNum = String(tempNum).slice(1);
		return tempNum;
	},
	finishFunc: function(){
		alert("do nothing");
	}

});
