var PrayerClock = Class.create();

PrayerClock.secondsInOneDay = 24 * 3600;
PrayerClock.nbSecondsToPray = 300;
PrayerClock.ReloadPageEvery = 3600; // sec

PrayerClock.prototype = {
  initialize: function(tPrayers, date) {
    this.todaySchedules = $H(tPrayers);
    this.prayerName = '';
    this.prevPrayerName = null;    
    this.date = date;
    this.secondsSincePrevPrayer = 999999; // Garder ce nombre > PrayerClock.nbSecondsToPray
    this.dates = this._schedulesToDates();
  },
  
  determineNextPrayer: function(date) {
    this.date = date;
    
    new PeriodicalExecuter(function(pe) {
      location.reload(true)      
    }, PrayerClock.ReloadPageEvery);
    
    if      (date < this.dates.fajr) return this.prayerName = 'fajr';
    else if (date < this.dates.dohr) return this.prayerName = 'dohr';
    else if (date < this.dates.asr) return this.prayerName = 'asr';
    else if (date < this.dates.maghreb) return this.prayerName = 'maghreb';
    else if (date < this.dates.icha) return this.prayerName = 'icha';
    else {
      return this.prayerName = 'fajri';
    }
  },
  
  determinePrevPrayer: function(date) {
    if (this.prayerName == 'fajr'        ) return this.prevPrayerName = null;
    else if (this.prayerName == 'dohr'   ) return this.prevPrayerName = 'fajr';
    else if (this.prayerName == 'asr'    ) return this.prevPrayerName = 'dohr';
    else if (this.prayerName == 'maghreb') return this.prevPrayerName = 'asr';
    else if (this.prayerName == 'icha'   ) return this.prevPrayerName = 'maghreb';
    else if (this.prayerName == 'fajri'   ) return this.prevPrayerName = 'icha';
  },
  
  toString: function() {
    var remainingTime = this.timeBeforeNextPrayer();
    this.determinePrevPrayer();
    if (this.prevPrayerName != null) this.timeLeftSincePrevPrayer();
    if (this._isTimeToPray()) {
      var msg = "Il est l'heure de prier";
      this._isBeginningOfPrayer() ? this.runAdhan() : '';
    } else {
      if (this.prayerName == 'fajri') this.prayerName = 'fajr';
      remainingTime = this.toDigit(remainingTime['hours'], remainingTime['minutes'], remainingTime['seconds']);
      var msg = this.prayerName + ' dans '+ remainingTime[0] +':'+ remainingTime[1] +':'+ remainingTime[2] +'';
    }
    return msg;
  },

  timeBeforeNextPrayer: function() {
    var schedule = eval('this.dates.'+ this.prayerName);
    this.secondsBeforeNextPrayer = (schedule - this.date) / 1000;
    var sec = this.secondsBeforeNextPrayer;
    var n = PrayerClock.secondsInOneDay;
    j   = Math.floor (sec / n);
    h   = Math.floor ((sec - (j * n)) / 3600);
    mn  = Math.floor ((sec - ((j * n + h * 3600))) / 60);
    sec = Math.floor (sec - ((j * n + h * 3600 + mn * 60)));
    
    return { hours: h, minutes: mn, seconds: sec };                                    
  },
  
  timeLeftSincePrevPrayer: function() {
    var schedule = eval('this.dates.'+ this.prevPrayerName);
    this.secondsSincePrevPrayer = (this.date - schedule) / 1000;
    return this.secondsSincePrevPrayer;
  },
  
  run: function(date) {
    this.determineNextPrayer(date);
    return this.toString();
  },
  
  runAdhan: function() {
    new Ajax.Updater('tttt',
      '/calendars/run_adhan',
      {asynchronous:true, evalScripts:true, method:'post'}
    );
  },
  
  toDigit: function(houres, minutes, seconds) {
    houres = houres.toString(), minutes = minutes.toString(), seconds = seconds.toString();
    if (houres.length == 1)  houres  = "0" + houres;
    if (minutes.length == 1) minutes = "0" + minutes; 
    if (seconds.length == 1) seconds = "0" + seconds;
    return [houres, minutes, seconds]
  },
  
  _isTimeToPray: function() {
    if (this.secondsSincePrevPrayer < PrayerClock.nbSecondsToPray) {
      return true;
    }
    return false;
  },
  
  _isBeginningOfPrayer: function() {
    return (Math.round(this.secondsSincePrevPrayer) == 5) ? true : false;
  },
                                                                                     
  _schedulesToDates: function() {
    var dates = $H();
    var ttt = this.date;
    this.todaySchedules.each(function(schedule) {
      var time = schedule.value.split(':');
      var mday = ttt.getDate()
      eval("dates."+schedule.key+" = PrayerClock.prototype._scheduleToDate(time[0], time[1], new Date(ttt.getFullYear(), ttt.getMonth(), mday))")
    });
    return dates;
  },
  
  _scheduleToDate: function(hours, minutes, date) {  
    date.setHours(hours);
    date.setMinutes(minutes);
    return date;
  }
};