<?php use_helper('ModalBox', 'Javascript'); ?>

<div>
  <?php echo input_tag('keyword', '', array('style'=>'width:225px;margin-left:5px', 'onkeyup'=>'searchLesson(this.value);')) ?>
  <?php echo submit_tag('&darr;', array('style'=>'width:22px;', 'onclick'=>'nextSearchLesson();')) ?>
  &nbsp;<i>(từ khóa tìm kiếm, có thể dùng tiếng Việt không dấu)</i>
  <div style="float:left;margin-left:5px" id="listlesson">
    <?php include_component('present', 'searchlesson', array('cat_base'=>$catItem->base, 'title'=>$title)) ?>
  </div>
  <div style="width:180px;height:410px;overflow:auto;float:left;border:1px #A9A9A9 solid;margin-top:2px;margin-left:10px">
    <div id="smpltree_box"></div>
  </div>
  <div class="clear"></div>
  <center>
    <h3>
      <?php echo link_to_function('Chọn tên bài', 'onSelectLesson($("lessonlist").value)') ?> |
      <?php echo m_finish('Đóng') ?>
    </h3>
  </center>
</div>

<?php $sf_context->getResponse()->addJavascriptCommand('
  selecttree = new dhtmlXTreeObject("smpltree_box","100%","auto",0);
  selecttree.setImagePath("/plugins/dhtmlx/images/");
  selecttree.loadXMLString("'.blogStore::$catTree->getTree(null, '/'.$catItem->type.'/').'");
  selecttree.setOnClickHandler(function(id){treeOpenLesson(id);});
  selecttree.selectItem("tr'.$cat_id.'", false);
  selecttree.openItem("tr'.$cat_id.'");
') ?>

<script>
onSelectLesson = function(id) {
  if ($("lessonlist").disabled) return false;
  if (id == '') {
    alert("Bạn phải chọn tên bài trong danh sách");
    return false;
  }
  var cat_id = selecttree.getSelectedItemId().match(/[0-9]+/gi);
  var url = "<?php echo url_for('/present/updatelesson') ?>/entry_id/<?php echo $entry_id ?>/cat_id/" + cat_id + "/lesson/"+id;
  new Ajax.Updater("selectlesson", url, {asynchronous:true, evalScripts:true});
  Modalbox.hide();
  show_confirm_wait();
}

treeOpenLesson = function(id) {
  var cat_id = id.match(/[0-9]+/gi);
  var url = "<?php echo url_for('/present/selectlessonlist') ?>/cat_id/" + cat_id;
  $("lessonlist").disabled = true;
  if ($("autosearch") != undefined) $("autosearch").style.display = "none";
  new Ajax.Updater("listlesson", url, {asynchronous:true, evalScripts:true, parameters: {title: "<?php echo mm_urlencode($title) ?>"}, onComplete: function() {
    if ($("keyword").value != "") searchLesson($("keyword").value);
    $("keyword").focus();
  }});
}

searchLesson = function(keyword, from) {
  var skey = vietDecode(keyword).toLowerCase();
  var list = $("lessonlist").options;
  var min = 1000;
  var id = -1;
  for (var c = 1; c <= 2; c++) {
    if (skey == "") break;
    if (c == 2) skey = skey.replace(/[^\w]/g, '');
    for (var i = from? from: 0; i < list.length; i++) {
      var sdata = vietDecode(list[i].text).toLowerCase();
      if (c == 2) sdata = sdata.replace(/[^\w]/g, '');
      var pos = sdata.indexOf(skey);
      if (from != undefined && pos >= 0) {id = i; break}
      if (pos >= 0 && pos < min) {min = pos; id = i}
    }
    if (id != -1) break;
  }
  $("lessonlist").selectedIndex = id;
}

nextSearchLesson = function() {
  searchLesson($("keyword").value, $("lessonlist").selectedIndex + 1);
  $("keyword").focus();
}
</script>