
function menuTreeClickHandler() {
  var currentId = menuTree.selectedId;
}

function clickTab(active,unactive) {
  $(active).show();
  $(unactive).hide();
  $(unactive+'tab').removeClassName('tab-menu-active');
  $(unactive+'tab').addClassName('tab-menu-unactive');
  $(active+'tab').addClassName('tab-menu-active');
}

menuTreeItem = Class.create();
menuTreeItem.prototype = {
  text: null,
  link: null,
  allow: null,
  deny: null,
  show: null,
  hide: null,
  key: null,
  child: null,
  obj:null,
  setParam: function(obj, name) {
    try {
      this[name] = unescape(trim(obj[name]));
      delete obj[name];
    } catch (e) {}
  },
  initialize: function(key, obj) {
    this.key = key;
    this.child = {};
    this.setParam(obj, 'text');
    this.setParam(obj, 'link');
    this.setParam(obj, 'allow');
    this.setParam(obj, 'deny');
    this.setParam(obj, 'hide');
    try {
      var children = obj['child'];
      for (var i in children) {
        var child = children[i];
        this.child[i] = new menuTreeItem(i, child);
      }
      delete obj['child'];
    } catch (e) {}
    this.obj = obj;
  },
  getXML: function() {
    var xml = "<item id='" + this.key +  "' text='" + this.text + "'>";
    for (var i in this.child) {
      xml += this.child[i].getXML();
    }
    xml += "</item>";
    return xml;
  },
  getYML: function(level) {
    var yml = level + this.key + ":\n";
    yml += level + "  text: " + this.text + "\n";
    yml += level + "  link: " + this.link + "\n";
    if (null!=this.allow) yml += level + "  allow: " + this.allow + "\n";
    if (null!=this.deny) yml += level + "  deny: " + this.deny + "\n";
    if (null!=this.show) yml += level + "  show: " + this.show + "\n";
    if (null!=this.hide) yml += level + "  hide: " + this.hide + "\n";

    var first = true;
    for (var i in this.child) {
      if (first) {
        yml += level + "  child:\n";
        first = false;
      }
      yml += this.child[i].getYML(level+"    ");
    }
    return yml;
  },
  getSubMenuFromId: function(id) {
    var ret = this.child[id];
    if (ret!=null) {
      return ret;
    } else {
      for (var i in this.child) {
        var ret = this.child[i].getSubMenuFromId(id);
        if (ret!=null) return ret;
      }
      return null;
    }
  },
  canModify: function() {
    if (obj['changestatus']!=null) return false;
    for (var i in this.child) {
      if (false==this.child[i].canModify()) return false;
    }
    return true;
  }
}

menuTreeManager = Class.create();
menuTreeManager.prototype = {
  items: null,
  menuTree: null,
  url: null,
  menutype: null,
  initialize: function(divName, objs, url, menutype) {
    this.url = url;
    this.menutype = menutype;
    this.items = {};
    for (var key in objs) {
      var ob = objs[key];
      this.items[key] = new menuTreeItem(key, ob);
    }
    this.menuTree=new dhtmlXTreeObject(divName, "100%" , 'auto', 0);
    this.menuTree.setImagePath("/plugins/dhtmlx/images/");
    this.menuTree.loadXMLString(this.getXML());
    this.menuTree.setOnClickHandler(this.onClickHander);
    this.menuTree.openItem('-1');
    this.menuTree.selectItem('-1');
    this.menuTree.manager = this;
  },

  getXML: function() {
    var xml = "<tree id='0'>";
    xml += "<item id='-1' text='Gốc'>";
    for (var key in this.items) {
      var item = this.items[key];
      xml += item.getXML();
    }
    xml += "</item>";
    xml += "</tree>";
    return xml;
  },

  getYML: function() {
    var yml = "";
    for (var key in this.items) {
      var item = this.items[key];
      yml += item.getYML("");
    }
    return yml;
  },

  onClickHander: function() {
    var me = this.manager;
    me.showItem(null);
  },

  showItem: function(item) {
    if (item==null) {
      item = this.menuTree.getSelectedItemId();
    }
    if (item==null) return;
    item = this.getItemFromId(item);
    if (item==null) return;
    $('ed_menu_name').value = item.text;
    $('ed_menu_url').value = item.link;
  },

  getItemFromId: function(id) {
    var ret = this.items[id];
    if (ret!=null) {
      return ret;
    } else {
      for (var i in this.items) {
        var ret = this.items[i].getSubMenuFromId(id);
        if (ret!=null) return ret;
      }
      return null;
    }
  },

  changeItem: function(id) {
    if (id==null) id = this.menuTree.getSelectedItemId();
    var item = this.getItemFromId(id);
    var text = $('ed_menu_name').value;
    var link = $('ed_menu_url').value;
    if (item!=null) {
      item.text = text;
      item.link = link;

      this.menuTree.setItemText(id, text);
      this.setSaved(false);
    }
  },

  setSaved: function(saved) {
    if (saved) {
      $('menu_comment').style.display = 'none';
      //$('submit_button').disable();
    } else {
      $('menu_comment').style.display = 'block';
      //$('submit_button').enable();
    }
  },

  saveToServer: function() {
    var menu_content = this.getYML();
    $('menu_result').innerHTML = "";
    new Ajax.Updater('menu_result', this.url, {
      asynchronous:false, evalScripts:true, method: 'post',
      parameters: {menu: menu_content, type: this.menutype}
    });
    if ($('menu_result').innerHTML=='OK') {
      this.setSaved(true);
      window.location.reload();
    } else {
      alert('Xin loi, khong the luu lai menu do loi he thong');
    }
  },

  insertMenuFromTemplate: function(otherMenuManager) {
    var idSource = otherMenuManager.menuTree.getSelectedItemId();
    if (idSource==undefined || idSource=='-1') {
      alert('Ban phai chon menu mau phia ben phai'); return;
    }
    var idDest = this.menuTree.getSelectedItemId();
    if (idDest==undefined) {
      alert('Ban phan chon menu chua menu con duoc chuyen sang'); return;
    }

    var itemSource = otherMenuManager.getItemFromId(idSource);
    if (idDest==-1) {
      // Insert into root
      this.items[idSource] = itemSource;
    } else {
      var itemDest = this.getItemFromId(idDest);
      itemDest.child[idSource] = itemSource;
    }
    this.menuTree.deleteItem('-1');
    this.menuTree.loadXMLString(this.getXML());
    this.menuTree.openItem(idDest);
    this.menuTree.selectItem(idDest);
    this.setSaved(false);
  },

  insertManual: function() {
    var idDest = this.menuTree.getSelectedItemId();
    if (idDest==undefined) {
      alert('Ban phai chon menu cha cho menu moi'); return;
    }
    var text = $F('cr_menu_name');
    var link = $F('cr_menu_url');
    if (text=='' || text==undefined) {
      alert('Ban phan nhap vao ten menu'); return;
    }
    if (link=='' || link==undefined) {
      alert('Ban phan nhap vao lien ket cho menu'); return;
    }
    var id;
    for (var i=0; ; i++) {
      id = 'user'+i;
      if (this.getItemFromId(id)==null) break;
    }
    var newMenuItem = new menuTreeItem(id, {text: text, link: link});
    if (idDest==-1) {
      // Insert into root
      this.items[id] = newMenuItem;
    } else {
      var itemDest = this.getItemFromId(idDest);
      itemDest.child[id] = newMenuItem;
    }

    this.menuTree.deleteItem('-1');
    this.menuTree.loadXMLString(this.getXML());
    this.menuTree.openItem(idDest);
    this.menuTree.selectItem(idDest);
    this.setSaved(false);
  },

  deleteItem: function() {
    var idDest = this.menuTree.getSelectedItemId();
    if (idDest==undefined || idDest==-1) {
      alert('Ban phai chon de xoa'); return;
    }
    if (!confirm('Ban co chac chan xoa menu nay khong?')) return;
    var idParent = this.menuTree.getParentId(idDest);
    var itemParent;
    if (idParent==undefined || idParent==-1) {
      itemParent = this.items;
      idParent = -1;
    } else {
      itemParent = this.getItemFromId(idParent);
      itemParent = itemParent.child;
    }
    delete itemParent[idDest];

    this.menuTree.deleteItem('-1');
    this.menuTree.loadXMLString(this.getXML());
    this.menuTree.openItem(idParent);
    this.menuTree.selectItem(idParent);
    this.setSaved(false);
  },

  mdebug: function(obj) {
    for (var i in obj) {
      alert(i + " " + obj[i]);
    }
  },

  objectToArray: function(obj) {
    var ret = [];
    for (var i in obj) {
      ret.push({key:i, value: obj[i]});
    }
    return ret;
  },

  arrayToObject: function(arr) {
    var obj = {};
    for (var i=0; i<arr.length; i++) {
      obj[arr[i].key] = arr[i].value;
    }
    return obj;
  },

  moveItem: function(offset) {
    var id = this.menuTree.getSelectedItemId();
    if (id==undefined || id==-1) return;
    var item = this.getItemFromId(id);
    var idParent = this.menuTree.getParentId(id);
    if (idParent==undefined || idParent==-1) {
      itemParent = this.items;
      idParent = -1;
    } else {
      itemParent = this.getItemFromId(idParent);
      itemParent = itemParent.child;
    }

    var children = this.objectToArray(itemParent);
    if (children.moveItem(id, offset)) {
      if (idParent==undefined || idParent==-1) {
        this.items = this.arrayToObject(children);
      } else {
        itemParent = this.getItemFromId(idParent);
        itemParent.child = this.arrayToObject(children);
      }
      this.menuTree.deleteItem('-1');
      this.menuTree.loadXMLString(this.getXML());
      this.menuTree.selectItem(id);
      this.setSaved(false);
    }
  }
}

Array.prototype.swap=function(a, b)
{
	var tmp=this[a];
	this[a]=this[b];
	this[b]=tmp;
}

Array.prototype.moveItem=function(id, offset) {
  var i;
  for (i=0; i<this.length; i++) {
    if (this[i].key==id) break;
  }
  if (i==this.length) return false;
  var j = i + offset;
  if (j<0) j=0;
  if (j>=this.length) j = this.length-1;
  if (i==j) return false;
  this.swap(i, j);
  return true;
}

function changeMenuType() {
  var currentType = $F('add_type');
  var types = ['add_manual', 'add_template', 'add_entry'];
  for (var i=0; i<types.length; i++) {
    if (types[i]==currentType)
      $(types[i]).show();
    else
      $(types[i]).hide();
  }
}

function trim(str) {
  var chars = "";
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}