﻿var GoodsTypeTree = function(){ 
    var GoodsTypeTreeContainerID;
    var SelectedNode;
    var ExecFunctionName;
    var SubHeight;
    var InitHeight;
    this.Init = function(containerId,selectNode,functionName){
        GoodsTypeTreeContainerID = containerId;
        SelectedNode = selectNode;
        ExecFunctionName = functionName;
    },
    this.GetSelectId = function(){    
        var array = SelectedNode.attr("id").split('_');
        return array[array.length-1]+"";      
    },
    this.GetFullPath = function(id,characeter){     
        var split = characeter == undefined ?" > ":characeter;
        var currObj = $("#"+GoodsTypeTreeContainerID+" div[id$='_"+id+"']");
        var fullpath = $.trim(currObj.text().replace('>>',''));
        
        while(!currObj.parent().is("#"+GoodsTypeTreeContainerID)){
            currObj = currObj.parent().prev();
            fullpath = $.trim(currObj.text().replace('>>','')) + split + fullpath;            
        }
        return fullpath;     
    },
    this.BindEvent = function(){
    $("#"+GoodsTypeTreeContainerID+" div[depth]").hover(
        function(){
            if($(this)!=SelectedNode){
                SelectedNode.attr("class",SelectedNode.attr("class").replace("selected","unselect"));
                $(this).attr("class",$(this).attr("class").replace("unselect","over"));      
            }  
        },function(){
            if($(this)!=SelectedNode){
                $(this).attr("class",$(this).attr("class").replace("over","unselect"));
                SelectedNode.attr("class",SelectedNode.attr("class").replace("unselect","selected")); 
            }
        });
        $("#"+GoodsTypeTreeContainerID+" div[op]").click(function(){
            if(($(this).attr("op")&1)==1){
                if($(this)!=SelectedNode){            
                    SelectedNode.attr("class",SelectedNode.attr("class").replace("selected","unselect"));
                    $(this).attr("class",$(this).attr("class").replace("over","selected"));                    
                    SelectedNode = $(this);
                }
            }
            if(($(this).attr("op")&2)==2){                
                if(SubHeight==null&&InitHeight==null){
                    SubHeight = $(this).parent().parent().parent().height() - $(this).parent().parent().height(); 
                    InitHeight = $(this).parent().parent().parent().height();
                }
                var obj = $("#"+($(this).attr("id")+"_Container"));
                if(obj.css("display")=="none")
                    obj.css("display","block");
                else
                    obj.css("display","none");
                
                if(InitHeight-SubHeight>$(this).parent().parent().height())
                    $(this).parent().parent().parent().height(InitHeight);
                else
                    $(this).parent().parent().parent().height($(this).parent().parent().height()+SubHeight);
            }
            if(($(this).attr("op")&4)==4){
                var array = $(this).attr("id").split('_');
                var id = array[array.length-1]+"";
                var name = $(this).text();
                var depth = $(this).attr("depth");
                eval(ExecFunctionName+"(id)");
            }
        });        
    }
}
