<?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->label    = $entry->label;
      $this->iterator = $entry->iterator;
      $this->minutes  = explode (",",$entry->minList);
      list($this->begin, $this->end) = explode (",",$entry->hourRange);

      $colon = "";
      $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 ="<span style='font-weight:800;float:left;display:inline;'>.</span>";
        
      }
    }
  }
  
  public function outputLayout() {

    $layout = "
<label for='tab$this->scope'>$this->label</label><br>

<table style='height:28px;border: 1px solid grey;'>
  <input type='hidden' id=hid$this->scope class=$this->iterator name='$this->scope'>
  <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>
  ";  

    return $layout;
  }

  public function outputScript() {
    $script = "
  
$('#min$this->scope').on('change', function() {
  newMin = $(this).val();
  current = $('#hid$this->scope').val(); 
  $('#hid$this->scope').val(current.substring(0,3) + newMin);  
});

$('#hour$this->scope').on('change', function() {
  newHour = $(this).val();
  current = $('#hid$this->scope').val(); 
  $('#hid$this->scope').val(newHour + current.substring(2));  
});
    
$('#hid$this->scope').on('chg$this->iterator', function() {
  var duration = $(this).val();
  var hours = String(duration.substring(0,2));
  var minutes = duration.substring(3,5);
alert (minutes);
  $('#hour$this->scope option[value='+hours+']').prop('selected',true);
  $('#min$this->scope option[value='+minutes+']').prop('selected',true);
});
    ";
    return $script;
  }

}

?>