function CategoryTree(url){
    // clear tree;
    jQuery('#category > ul > li > ul').remove();

    if(jQuery("ul.simpleTree > li > a").size() ==0)jQuery('<a href="#" class="add"><img src="./common/js/plugins/ui.tree/images/iconAdd.gif" alt="'+lang_cmd_insert+'" title="'+lang_cmd_insert+'" /></a>').bind("click",function(e){addNode(0,e);}).appendTo("ul.simpleTree > li");

    //ajax get data and transeform ul il
    jQuery.get(url,function(data){
        jQuery(data).find("node").each(function(i){
            var text = jQuery(this).attr("text");
            var node_srl = jQuery(this).attr("node_srl");
            var parent_srl = jQuery(this).attr("parent_srl");
            var url = jQuery(this).attr("url");

            // node
            var node = jQuery('<li id="tree_'+node_srl+'"><span>'+text+'</span></li>');

            // button
            jQuery('<a href="#" class="add"><img src="./common/js/plugins/ui.tree/images/iconAdd.gif" alt="'+lang_cmd_insert+'" title="'+lang_cmd_insert+'" /></a>').bind("click",function(e){
                addNode(node_srl,e);
                return false;
            }).appendTo(node);

            jQuery('<a href="#" class="modify"><img src="./common/js/plugins/ui.tree/images/iconModify.gif" alt="'+lang_cmd_modify+'" title="'+lang_cmd_modify+'" /></a>').bind("click",function(e){
                modifyNode(node_srl,e);
                return false;
            }).appendTo(node);

            jQuery('<a href="#" class="delete"><img src="./common/js/plugins/ui.tree/images/iconDel.gif" alt="'+lang_cmd_delete+'" title="'+lang_cmd_delete+'" /></a>').bind("click",function(e){
                deleteNode(node_srl);
                return false;
            }).appendTo(node);

            // insert parent child
            if(parent_srl>0){
                if(jQuery('#tree_'+parent_srl+'>ul').length==0) jQuery('#tree_'+parent_srl).append(jQuery('<ul>'));
                jQuery('#tree_'+parent_srl+'> ul').append(node);
            }else{
                if(jQuery('#category ul.simpleTree > li > ul').length==0) jQuery("<ul>").appendTo('#category ul.simpleTree > li');
                jQuery('#category ul.simpleTree > li > ul').append(node);
            }

        });

        //button show hide
        jQuery("#category li").each(function(){
            if(jQuery(this).parents('ul').size() > max_category_depth) jQuery("a.add",this).hide();
            if(jQuery(">ul",this).size()>0) jQuery(">a.delete",this).hide();
        });

        // draw tree
        simpleTreeCollection = jQuery('.simpleTree').simpleTree({
            autoclose: false,
            afterClick:function(node){
                //alert("text-"+jQuery('span:first',node).text());
            },
            afterDblClick:function(node){
                //alert("text-"+jQuery('span:first',node).text());
            },
            afterMove:function(destination, source, pos){

                jQuery("#category_zone_info").html("");

                if(destination.size() == 0){
                    Tree(xml_url);
                    return;
                }
                var category_srl = jQuery("#fo_category_item input[name=category_srl]").val();
                var parent_srl = destination.attr('id').replace(/.*_/g,'');
                var target_srl = source.attr('id').replace(/.*_/g,'');
                var brothers = jQuery('#'+destination.attr('id')+' > ul > li:not([class^=line])').length;
                var mode = brothers >1 ? 'move':'insert';
                var source_srl = pos == 0 ? 0: source.prevAll("li:not(.line)").get(0).id.replace(/.*_/g,'');
                jQuery.exec_json(
                		"cashbook.procCashbookCategoryItemMove",
                		{
                			"category_srl":category_srl,
		                	"parent_srl":parent_srl,
		                	"target_srl":target_srl,
		                	"source_srl":source_srl,
		                	"mode":mode
                		},
                function(data){
                    if(data.error>0) Tree(xml_url);
                });

            },

            // i want you !! made by sol
            beforeMovedToLine : function(destination, source, pos){
//                if(typeof(destination.id) == 'undefined') return false;
                return (jQuery(destination).parents('ul').size() + jQuery('ul',source).size() <= max_category_depth);
            },

            // i want you !! made by sol
            beforeMovedToFolder : function(destination, source, pos){
//                if(typeof(destination.id) == 'undefined') return false;
                return (jQuery(destination).parents('ul').size() + jQuery('ul',source).size() <= max_category_depth-1);
            },
            afterAjax:function()
            {
                //alert('Loaded');
            },
            animate:true
            ,docToFolderConvert:true
        });


        // image url replace
//        jQuery("#menu ul.simpleTree img").attr("src",function(){ return jQuery(this).attr("src").replace("images/","./common/js/plugins/ui.tree/images/");});

        // open all node
        nodeToggleAll();
    },"xml");
}

function nodeToggleAll(){
    jQuery("[class*=close]", simpleTreeCollection[0]).each(function(){
        simpleTreeCollection[0].nodeToggle(this);
    });
}



/**
 * 노드수정
 * 
 * @param node_srl
 * @param e
 * @return
 */
function modifyNode(node_srl,e){
    jQuery('#category_zone_info').html('');
    jQuery("#tree_"+node_srl+" > span").click();
    var params ={
            "parent_srl":0
            ,"category_item_srl":node_srl
            };

    jQuery.exec_json('cashbook.dispCashbookCategoryItemInputTpl', params, function(data){
        jQuery('#category_zone_info').html(data.tpl).css('position','absolute').css("left",e.pageX).css("top",e.pageY).css('display','block');
    });
}

/**
 * 노드추가
 * 
 * @param node_srl
 * @param e
 * @return
 */
function addNode(node_srl,e){

    jQuery('#category_zone_info').html('');
    jQuery("#tree_"+node_srl+" > span").click();

    var params ={
            "category_item_srl":0
            ,"parent_srl":node_srl
            };

    jQuery.exec_json('cashbook.dispCashbookCategoryItemInputTpl', params, function(data){
        jQuery('#category_zone_info').html(data.tpl).css('position','absolute').css("left",e.pageX).css("top",e.pageY).css('display','block');
    });
}

/**
 * 노드삭제
 * 
 * @param node_srl
 * @return
 */
function deleteNode(node_srl){

    if(confirm(lang_confirm_category_item_delete)){
        jQuery('#category_zone_info').html('');
        var params ={
                "category_item_srl":node_srl
                ,"category_srl":jQuery("form input[name=category_srl]").val()
                };
        jQuery.exec_json('cashbook.procCashbookCategoryItemDelete', params, function(data){
        	CategoryTree(xml_url);
        });
    }
}

/**
 * category_item 입력후
 * 
 * @param ret_obj
 * @return
 */
function completeCashbookCategoryItemInsert(ret_obj) {
    jQuery('#category_zone_info').html('');
    CategoryTree(xml_url);
}

function doMoveCashbookCategoryInfo() {
	var $ = jQuery;
	$(function(){ $('#fo_category_item').appendTo(document.body); $('#category_zone_info').css('width', '550px'); });
}