(function($) {
  $(window).load(function () {
    var rootDirId = -1;
    var DocSets ={};     //{id,DocSet}
    GE.DirSets = {};
    var initDirPaddingleft = 20; //padding-left:40px;

    var adjustTreeContainerHeight = function(){
      var newWindowHeight = $(window).height();
      $(".tree-container").css("height", newWindowHeight- 68);
    };

    $(window).bind("resize", function(e){
      adjustTreeContainerHeight();
    });

    $("#search-field").focus(function(){
      if($(this).val() == "搜索收藏夹"){
        $(this).val("");
      }
    });

    $("#search-field").bind("keydown",function(e){
      if (e.which == 13){
        var search_term = $("#search-field").val();
        bmm.getInstance().search(search_term);
      }
    });

    $("#search-field").bind("keyup", function(e){
      if($(this).val().length > 0){
        $("#do_close").show();
      }else{
        $("#do_close").hide();
      }
    });

    $("#search-field").blur(function(e){
      if($(this).val().length == 0){
        $(this).val("搜索收藏夹");
      }
    });


    $("#do_search").click(function(){
      var search_term = $("#search-field").val();
      if($("#search-field").val() == "搜索收藏夹"){
        $("#search-field").focus();
        return;
      }
      bmm.getInstance().search(search_term);
    });

    $("#do_close").click(function(e){
      e.preventDefault();
      e.stopPropagation();
      $("#search-field").val("");
      $(this).hide();
    });

    $("#import_bmm").click(function(){
      bmm.getInstance().import();
    });

    $("#export_bmm").click(function(){
      bmm.getInstance().export();
    });

    $("#add_folder").click(function(){
      debugger;
      var dir_id = $(".select").parent().attr("id");
      bmm.getInstance().createItem(dir_id.substr(2),"新建文件夹");
    });

    $(".tree-row").click(function(){
      debugger;
      $(".tree-row").removeClass("select");
      $(this).addClass("select");
    });


    function EditItemEnd(node){
      var id  = $(node).attr("id");
      var title = $(".tf-title", node).val();
      var url = $(".tf-url", node).val();
      bmm.getInstance().updateItem(id, title, url);
    };


    /*
    node{
      children : [];
      id : "";
      title: ""
    }
     */
    var ContructDirStruction = function(domNode, treeNode, paddingleft){
      if(treeNode == "undefined") {
        return;
      }

      if(treeNode.title.length > 0) {
        var node = $("#tmpl_bm_dir_item").tmpl({
          id : treeNode.id,
          pid : treeNode.parentId,
          dirname : treeNode.title
        });
        $.tmpl.complete();
        $(".tree-links", node).css("padding-left", paddingleft+"px");
        $(".tree-children",node).hide();
        bmm.getInstance().bindTreeItemEventHandler(node);
        $(domNode).append(node);
        domNode = $(".tree-children", node);
      }

      paddingleft += 10;
      if((typeof treeNode.children != "undefined") && (treeNode.children.length > 0)){
        for(var i=0; i<treeNode.children.length; i++){
          ContructDirStruction(domNode, treeNode.children[i], paddingleft);
        }
      }else{
        $(".expand-icon",domNode.parent()).hide();
      }
    };

    //load the root dir
    $(".import-right-margin").click(function(){
    });

    addSingletonGetter(bmm);
    bmm.getInstance().init($);

    (function() {
      bmm.getInstance().loadSubtree("", true, function(id, result){
        GE.DirSets[result[0].id] = result[0];
        $(".tree-item").remove();
        ContructDirStruction($(".tree-container"), GE.DirSets[0], initDirPaddingleft);
        adjustTreeContainerHeight();
        var first_tree_item = $(".tree-item");
        var first_tree_item = first_tree_item[0];
        $($(".tree-row", first_tree_item[0])[0]).trigger("dblclick");
      });
    })();
  });
})(jQuery);

