function beforeCallb() {
  $('#loading').show();
  return true;
}
function afterCallb(s) {
  eval(s);
  $.fn.quodeTree.massAjaxLoad(tree, true);
  for (i in pathval) {
    //$("#tree ."+i).each(function() {
    $("#"+i).each(function() {
      var $this = $.fn.quodeTree.makeNode($(this));
      if (!$this.hasMark()) $this.addMark();
    });
  }
}
function finalyCallb(s) {
  $('#loading').hide();
}

var tree;
var current = {};
var pathval = {};

$(document).ready(function(){


 tree = $("#tree .root > ul").quodeTree({
   leafClick: function(node) {
     //node.addClass("sel");
     //var fpath, a = /px\S+/.exec(node.attr("class"));
     var fpath, a = /px\S+/.exec(node.attr("id"));
     if (a && (fpath = a[0])) {

       $("#frag1tmp").hide();

       if (pathItem[fpath]) {
        if (current.fpath) {
          if ($('#val').val()) {
            pathval[current.fpath] = $('#val').val();
            if (!current.node.hasMark()) current.node.addMark();
          }
          else {
            delete pathval[current.fpath];
            if (current.node.hasMark()) current.node.removeMark();
          }
        }
        current.fpath = fpath;
        current.node = node;

        $('#val').val('');
        var ipath = fpath.substr(0, fpath.lastIndexOf('x'));
        $('#path').html(path[ipath]+'/'+pathItem[fpath].name);
        $('#desc').html(pathItem[fpath].desc);
        $('#type').html("Type: "+pathItem[fpath].typ);
        $('#def').html("Default: "+pathItem[fpath].def);
        $("#def").attr('title', pathItem[fpath].def);
        $('#val').val(pathval[fpath]); // ? pathItem[fpath].val : '');

        $("#tabs ul").tabs("select", 0);

        $('#frag1').show();
        $('#val').focus();
       }
     }
   }
 });

 $("#tabs ul").tabs();
 $('#frag1').hide();
 $('#tabs ul').bind('tabsselect', function(event, ui) {
  if (current.fpath) {
   if ($('#val').val()) {
     pathval[current.fpath] = $('#val').val();
     if (!current.node.hasMark()) current.node.addMark();
   }
   else {
     delete pathval[current.fpath];
     if (current.node.hasMark()) current.node.removeMark();
   }
  }
 });

 $("#setdef").click(function () {
  $('#val').val($("#def").attr("title"));
  if (!current.node.hasMark()) current.node.addMark();
 });
 $("#gen").click(function () {
  var s = "";
  var ipath;
  for (i in pathItem) {
   if (pathval[i]) {
    ipath = i.substr(0, i.lastIndexOf('x'));
    s += path[ipath]+'/'+pathItem[i].name+'='+pathval[i]+"<br"+"/>\n";
   }
  }
  $('#res').html(s);
 });
 $("#resetall").click(function () {
  pathval = current = {};
  $('#res').html("");
  $('#val').val("");

  $("#tree a, #tree span").each(function() {
    var $this = $.fn.quodeTree.makeNode($(this));
    if ($this.hasMark()) $this.removeMark();
  });


 });
 $("#reset").click(function () {
  delete pathval[current.fpath];
  $('#val').val("");
  if (current.node.hasMark()) current.node.removeMark();
 });

 $("#form").handleUpload({
  submitButtonId: "upload",
  fileHandlerUrl: "dip/mpcustom/upload.php",
  printTxtIn: "dummy",
  beforeCallback: beforeCallb,
  afterCallback: afterCallb,
  finalyCallback: finalyCallb
 });

 $("#tmp").hide();
 $("#mpc").show();

});
