<?php 

class SelectHourMin {

  private $scope;
  private $label;
  private $iterator;
  private $minutes;
  private $begin;
  private $end;
  private $colon;
  private $point;

  public function __construct($entry) {
 
    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->iterator = $entry->iterator;
      $this->minutes  = explode (",",$entry->minList);
      list($this->begin, $this->end) = explode (",",$entry->hourRange);

      $this->colon = "";
      $this->point = "";
      if ($entry->format == 'time') {
        $this->colon = "<span style=font-weight:800;float:left;display:inline;background:white;>:</span>";
      }  
      else if ($entry->format == 'duration') {
        $this->point = "&#8228;";
        
      }
  
      if (isset($entry->label))
        $this->label = "<label>$entry->label</label>";
      else 
        $this->label = "";
  
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        $this->messageList = "";

        for ($i=0; $i<count($messages); $i++) {
          $this->messageList  .= "notify" . $messages[$i] . "('hid$this->scope'); ";
        }
      }

      if (null !== $entry->refresh) {
        $refreshes = explode(",", $entry->refresh);
        $this->refreshList = "";

        for ($i=0; $i<count($refreshes); $i++) {
          $this->refreshList  .= "refresh" . $refreshes[$i] . "(refRowIds); ";
        }
      }

    }
  }
  
  public function outputLayout() {

    $layout = "

$this->label
<table style='height:28px;border: 1px solid grey;'>
  <input type='hidden' id=hid$this->scope class=$this->iterator name='$this->scope' value='00:00'>
  <tr>
    <td>  
      <select id='hour$this->scope' name='hour$this->scope'
        style='height:18px;width:43px;direction:rtl;border:none;display:inline;padding-top:3px;'>
    ";
    for ($i=$this->begin; $i<$this->end; $i++) {
      $inner = sprintf("%02d", $i);
      $layout .= "
        <option value=$inner>$inner</option>
      ";
    }
  
    $layout .= "
      </select>   
    </td>
    <td>
      $this->colon
    </td>
    <td>
      <select id='min$this->scope' name='min$this->scope'
        style='height:18px;width:45px;border:none;display:inline;padding-top:3px;'>
	";
	
    for ($i=0; $i<count($this->minutes); $i++) {
      $selAttr = "";
      if ($this->minutes[$i] == $this->minValue)
        $selAttr = "selected";

      $inner = $this->minutes[$i];
      $layout .= "
        <option value=$inner $selAttr>$this->point$inner</option>
      ";
    }
  
    $layout .= "
      </select>
    </td>
  </tr>
</table>
</label>
  ";  

    return $layout;
  }

  public function outputScript() {
    $script = "

var now = '00:00';    
$(document).ready(function() {
//alert ('$this->scope');
  if ('$this->scope' == 'rolestarttime' || '$this->scope' == 'roleendtime') { 
    var d = new Date();

    var minutes = d.getMinutes();
    var hours = d.getHours();

    var m = (((minutes + 7.5)/15 | 0) * 15) % 60;
    var h = ((((minutes/105) + .5) | 0) + hours) % 24;
    now = String('0' + h).slice(-2) + ':' + String('0' + m).slice(-2);

    $('#hid$this->scope').val(now).trigger('chg$this->iterator');
  } 
});  
  
$('#min$this->scope').on('change', function() {
  var refRowIds = ['00','00'];
  var newMin = $(this).val();
  var current = $('#hid$this->scope').val(); 
  $('#hid$this->scope').val(current.substring(0,3) + newMin);
  $this->messageList 
  $this->refreshList 
});

$('#hour$this->scope').on('change', function() {
  var refRowIds = ['00','00'];
  var newHour = $(this).val();
  var current = $('#hid$this->scope').val(); 
  $('#hid$this->scope').val(newHour + current.substring(2));
  $this->messageList 
  $this->refreshList 
});
    
$('#hid$this->scope').on('chg$this->iterator', function() {
  var duration = String('0' + $(this).val()).slice(-5);
  var hours = String(duration.substring(0,2));
  var minutes = duration.substring(3,5);
  $('#hour$this->scope option[value='+hours+']').prop('selected',true);
  $('#min$this->scope option[value='+minutes+']').prop('selected',true);
});
    
function notify$this->scope (inputId) {
  var clockStart = $('#hidrolestarttime').val();
  var decimalStart = +(clockStart.substring(0,2)) + +(clockStart.substring(3,5)/60);

  var clockEnd = $('#hidroleendtime').val();
  var decimalEnd = +(clockEnd.substring(0,2)) + +(clockEnd.substring(3,5)/60);
  var breakDur = $('#hidbreakduration').val();

  var decimalDiff = '00.00';
  if ((decimalEnd - decimalStart) - breakDur >= 0) 
    decimalDiff = String(Number((decimalEnd - decimalStart) - breakDur).toFixed(2));
//alert (decimalDiff);    
    
  var duration = String('0' + decimalDiff).slice(-5);
//alert (duration);    
  $('#hidhoursduration').val(duration).trigger('chg$this->iterator');

}  

function refresh$this->scope (rowId) {
  var delimiter = ('$this->point' == '') ? ':' : '.';
  $('#hid$this->scope').val('00'+delimiter+'00');
  $('#hour$this->scope option[value='+rowId[0]+']').prop('selected',true);
  $('#min$this->scope option[value='+rowId[1]+']').prop('selected',true);
}
    ";
  
    return $script;
  }
}  


?>