/**
 * @author ozzysun
 */
function $InitJQuery(){
    document.write("<link rel='stylesheet' href='/JSLib/jqueryPlugin/jquery-treeview/jquery.treeview.css' type='text/css' />");
    //document.write("<script src='/JSLib/jquery-1.2.6.min.js'></script>");
    document.write("<script src='/JSLib/jqueryPlugin/jquery-treeview/jquery.treeview.js'></script>");
    jQuery.noConflict();
}$InitJQuery();

function $JTreeView(_collapsed, _unique){
    var _instance = this;
    //--method----
    this.load = __load;
    this.setParameter = _setParameter;
    //--property----
    this.clickFun = null;
    this.id = null;
    this.folderTag = null;
    this.fileTag = null;
    this.folderImg = null;
    this.fileImg = null;
    this.collapsed = _collapsed;
    this.unique = _unique;
    //--for check--
    this.parentStr = "myp";
    this.parentNum = 0;
    this.loadFlag1 = 0;
    this.loadFlag2 = 1;
    this.checkID;
    this.checkTime = 800;
    //--private method--------------
    function _setParameter(_containerID, _rootTag, _folderTag, _fileTag, _clickFun, _folderImg, _fileImg){
        this.id = _containerID;
        this.rootTag = _rootTag;
        this.folderTag = _folderTag;
        this.fileTag = _fileTag;
        this.clickFun = _clickFun;
        if (_folderImg != null || _folderImg != "") {
            this.folderImg = _folderImg;
        }
        if (_fileImg != null || _fileImg != "") {
            this.fileImg = _fileImg;
        }
    }
    function __load(_path,_errorHandler){
        var _instance = this;
            jQuery("#" + _instance.id).ready(function(){
            jQuery("#" + _instance.id).hide();
            jQuery("#" + _instance.id).empty();
            jQuery("#" + _instance.id).append("<ul class='filetree'></ul>");	           
            jQuery.get(_path, function(data){
				var isError=jQuery(data).find("error")[0];
				if(isError){
					//alert(_errorHandler);
					//eval(_errorHandler)();	
				}else{
					var _root = jQuery(data).find(_instance.rootTag);
					jQuery("#"+_instance.id+">.filetree").empty();				
                	parseNode(_root, jQuery("#"+_instance.id+">.filetree"), _instance);
					 _instance.checkID = setInterval(function(){
						if (_instance.loadFlag1 == _instance.loadFlag2) {
							clearInterval(_instance.checkID);
							afterComplete(_instance)
						}
						else {
							_instance.loadFlag1 = _instance.loadFlag2
						}
						
					}, _instance.checkTime);	
				}
                
            });
        });
    };
    function parseNode(_dataRoot, _nodeRoot, _inst){
        var _instance = _inst;		
        _dataRoot.find(">*").each(function(){
            if (this.tagName == _instance.folderTag) {
                _instance.parentNum++;               
				var _parentID = _instance.id+ _instance.parentNum;
				
                _nodeRoot.append("<li><span class='folder'>" + jQuery(this).attr("name") + "</span><ul id=" + _parentID + "></ul></li>");
                
                parseNode(jQuery(this), jQuery("#" + _parentID), _instance);
                
            }else if(this.tagName == _instance.fileTag) {                    
					if(jQuery(this).attr('tip')!=null){
						if (_instance.clickFun != null) {
                        _nodeRoot.append("<li><span class='file' onclick='" + _instance.clickFun + "(\"" + jQuery(this).attr('url') + "\")' title=\""+jQuery(this).attr('tip')+"\">" + jQuery(this).attr('name') + "</span></li>");
	                    }
	                    else {
	                        _nodeRoot.append("<li><span class='file' title=\""+jQuery(this).attr('tip')+"\">" + jQuery(this).attr('name') + "</span></li>");
	                    }
					}else{
						if (_instance.clickFun != null) {
                        	_nodeRoot.append("<li><span class='file' onclick='" + _instance.clickFun + "(\"" + jQuery(this).attr('url') + "\")'>" + jQuery(this).attr('name') + "</span></li>");
	                    }
	                    else {
	                        _nodeRoot.append("<li><span class='file'>" + jQuery(this).attr('name') + "</span></li>");
	                    }
					}
					
              }
            _instance.loadFlag2++;
        })
    }
    function afterComplete(_instance){
    
        jQuery("#" + _instance.id).treeview({
            unique: _instance.unique,
            collapsed: _instance.collapsed
        });
        
        //--change css---				
        
        if (_instance.folderImg != null) {
            jQuery(".filetree span.folder").css("background", "url(" + _instance.folderImg + ") 0 0 no-repeat");
        }
        if (_instance.fileImg != null) {
            jQuery(".filetree span.file").css("background", "url(" + _instance.fileImg + ") 0 0 no-repeat");
        }
        jQuery(".filetree span.file").css("cursor","pointer");
        jQuery("#" + _instance.id).show();	
	    IContainer.RemoveURL(5,"  ");		
    }
	

    
}

