var expand = "";
var closeds = "";
EWS.ESHOP.JsTree = function(div_t1, div_t2,url,objId) {
	this.id = objId;
	this.curId = 0;
	var _this = this;
    expand = "http://"+url+"/theme/eshop/style/img/m_.gif";
    closeds = "http://"+url+"/theme/eshop/style/img/p.gif";
	function SkyObject() {
		var id = 0;
		var className = "SkyObject";

		this.getId = function() {
			return id;
		};
		this.setId = function(_id) {
			id = _id;
		};
		this.setClassName = function(name) {
			className = name;
		};
		this.getClassName = function() {
			return className
		};
		this.getClassName2 = function() {
			return name
		};

		addToContainer(this);

		this.getElement = function() {
			return window.document.getElementById(this.divid() + this.getId());
		};
		
		this.RTime = function() {
			var dd = new Date();
			var nodeId = dd.getFullYear().toString() + dd.getMonth().toString()
					+ dd.getDate().toString() + dd.getUTCHours().toString();
					//+ dd.getMinutes().toString() ;
			return nodeId.substring(0, 12);
		};

		// 每个选项的ID (动态生成)
		this.namelink = function() {
			return  objId+"_namelink" + this.RTime();
		};

		// +-展开的ID
		this.children = function() {
			return objId+"_children" +this.RTime();;
		};

		// +-连接<a ID=""
		this.statelink = function() {
			return objId+"_statelink"+this.RTime();
		};
		
		// 每一页层的<div ID=""
		this.divid = function() {
			return objId+"_divid"+this.RTime();
		};
	};

	var hxdObjPointer = 0;
	var hxdContainer = [];

	function addToContainer(obj) {
		obj.setId(hxdObjPointer);
		hxdContainer[hxdObjPointer] = obj;
		hxdObjPointer++;
	};

	function getFromContainer(id) {
		return hxdContainer[id];
	};

	function Node(data, parentNode) {

		SkyObject.call(this);
		var pNode = parentNode;
		var value = data;
		var state = "closed";
		var children = [];
		this.setClassName("Node");
		var childrenInited = false;

		this.getRoot = function() {
			var current = this;
			var parent = null;
			while (true) {
				try {
					parent = current.getParentNode();
				} catch (e) {
				}

				if (parent == null || parent == "undefined") {
					return current;
				} else {
					current = parent;
					parent = null;
				}
			};
		};

		this.getParentNode = function() {
			return pNode;
		};

		this.getValue = function() {
			return value;
		};

		this.onSelected = function() {
			var element = window.document.getElementById(this.namelink()
					+ this.getId());
			element.style.color = "blue";
			var root = this.getRoot();
			root.setCurrentNodeId(this.getId());
			root.onNodeChange();
		};
		this.onUnSelected = function() {
			var element = window.document.getElementById(this.namelink()
					+ this.getId());
			if (element != null)
				element.style.color = "black";
			if (this.getId() == root.getCurrentNodeId()) {
				var root = this.getRoot();
				root.setCurrentNodeId(-1);
			};
		};

		this.removeChild = function(node) {
			if (node == null || node == "undefined")
				return;
			var newChildren = [];
			var index = 0;
			for (var i = 0; i < children.length; i++) {
				var child = children[i];
				if (node.getId() != child.getId()) {
					newChildren[index] = child;
					index++;
				}
			}

			delete children;
			children = newChildren;

			var element = window.document.getElementById(this.children()
					+ this.getId());
			if (element != null) {
				if (children.length > 0)
					element.removeChild(node.getElement());
				else
					this.getElement().removeChild(element);
			}
			this.repaint();
		};

		this.addChild = function(node) {
			children[children.length] = node;
			var element = window.document.getElementById(this.children()
					+ this.getId());

			if (element == null) {
				if (value.children != null && value.children.length > 0) {
					this.paintChildren();
					element = window.document.getElementById(this.children()
							+ this.getId());
				}
				element = window.document.createElement("div");
				element.id = this.children() + this.getId();
				this.getElement().appendChild(element);
			}
			state = "opened";
			element.style.display = "block";

			element.className = "t"; // yaozhg
			node.paint(element);
			this.repaint();
		};

		this.changeState = function() {
			var stateLink = window.document.getElementById(this.statelink()
					+ this.getId());
			if (state == "opened") {
				stateLink.innerHTML = "<img alt='' class='picclose'  src='"+closeds+"' />";
				state = "closed";
			} else {
				state = "opened";
				stateLink.innerHTML = "<img alt='' class='picclose' src='"+expand+"' />";
			};

			var childrenElement = window.document.getElementById(this
					.children()
					+ this.getId());
			if (childrenElement == null) {
				if (state == "opened")
					this.paintChildren();
			} else {
				if (state == "opened") {
					childrenElement.style.display = "block";
				} else {

					childrenElement.style.display = "none";
				}
			};
		};

		this.paintChildren = function() {
			childrenInited = true;
			var nodeElement = this.getElement();
			var childrenElement = null;
			if (value.children != null && value.children.length > 0) {
				childrenElement = window.document.createElement("div");
				childrenElement.id = this.children() + this.getId();
				for (var i = 0; i < value.children.length; i++) {
					var childNode = new Node(value.children[i], this);
					children[i] = childNode;
					childNode.paint(childrenElement);
				}
				childrenElement.style.display = "block";
				childrenElement.className = "node_one"; // 在第一个节点前加上css
				nodeElement.appendChild(childrenElement);
			};

		};

		this.repaint = function() {
			var statelink = document.getElementById(this.statelink()
					+ this.getId());
			var namelink = document.getElementById(this.namelink()
					+ this.getId());

			if (children != null && children.length > 0) {
				if (state == "opened") {
					statelink.innerHTML = "<img alt='' class='picclose'  src='"+expand+"' />";
				} else {
					statelink.innerHTML = "<img alt='' class='picopen'  src='"+closeds+"' />";
				}
				
				statelink.href = 'javascript:JsTree.stateClick(' + this.getId()
						+ ')';
			} else {
				statelink.innerHTML = " . ";
			}
			namelink.innerHTML = value.name;
		};

		this.paint = function(parent) {
			var nodeElement = window.document.createElement("div");
			// nodeElement.style.position = "relative";
			// nodeElement.style.left="18px";
			nodeElement.className = "tree_node_div";
			nodeElement.id = this.divid() + this.getId();

			var statelink = window.document.createElement("a");
			statelink.id = this.statelink() + this.getId();
			if (value.children != null && value.children.length > 0) {
				if (state == "opened") {
					statelink.innerHTML = "<img alt='' class='picclose'  src='"+expand+"' />";
				} else {
					statelink.innerHTML = "<img alt='' class='picopen'  src='"+closeds+"' />";
				};
				statelink.setAttribute('key',this.getId());
				statelink.onclick = function(id){
					_this.stateClick(this.getAttribute('key'));
				};
//				statelink.href = 'javascript:void(0)';
//				statelink.href = 'javascript:JsTree.stateClick(' + this.getId()
//						+ ')';
			} else {
				statelink.innerHTML = "<img alt='' class='picclose'  src='"+expand+"' />"; // 无下线分类图标
			}

			nodeElement.appendChild(statelink);

			var namelink = window.document.createElement("a");
			namelink.id = this.namelink() + data.id;
			namelink.onclick = function(){
				_this.nodeClick(data.id+"");
			};
			namelink.href = "javascript:void(0)";
			//namelink.href = "javascript:JsTree.nodeClick('" + data.id + "')"; // 点击还回ID名称
			// namelink.onclick=function(){

			// }
			namelink.onmouseover = function(){
					this.className = 'selfhover';
			}
				
			namelink.onmouseout = function(){
					this.className = '';
			}
			namelink.innerHTML = value.name;
			nodeElement.appendChild(namelink);

			if (state == "opened") {
				paintChildren();
			};
			if (parent.type == "nodesPane"){
				nodeElement.style.left = 2;
			}else{
				nodeElement.style.left = 20;
			};
			parent.appendChild(nodeElement);
		};

	};

	function Tree(id) {
        this.id = id;
		SkyObject.call(this);

		var children = [];
		var title = "title";
		var element = null;
		var parent = null;
		var value = null;
		var currentNodeId = -1;
		var preNodeId = -1;

		this.setCurrentNodeId = function(id) {
			preNodeId = currentNodeId;
			currentNodeId = id;
		};

		this.getPreNodeId = function() {
			return preNodeId;
		};

		this.getCurrentNodeId = function() {
			return currentNodeId;
		};

		this.getCurrentNode = function() {
			if (currentNodeId < 0) {
				return null;
			} else
				return getFromContainer(currentNodeId);
		};

		this.onNodeChange = function() {
		};

		this.bindData = function(data) {
			value = data;
		};

		this.addChild = function(data) {
			var node = new Node(data, this);
			children[children.length] = node;
			var treeElement = this.getElement();
			node.paint(treeElement);
		};

		this.addChildToSelectedNode = function(data) {
			var selectednode = this.getCurrentNode();
			var node = new Node(data, selectednode);
			selectednode.addChild(node);
		};

		this.removeChild = function(node) {
			var parent = node.getParentNode();
			if (parent.getId() == this.getId()) {
				var element = this.getElement();
				element.removeChild(node.getElement());
				var newChildren = [];
				var index = 0;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];
					if (node.getId() != child.getId()) {
						newChildren[index] = child;
						index++;
					}
				}
				delete children;
				children = newChildren;

			} else {
				parent.removeChild(node);
			}

		};

		this.getElement = function() {
			var nodesPane = window.document.getElementById(this.divid()
					+ this.getId());
			if (nodesPane == null) {
				nodesPane = window.document.createElement("div");
				nodesPane.id = this.divid() + this.getId();
				nodesPane.type = "nodesPane";
				if (value != null && value.length > 0) {
					for (var i = 0; i < value.length; i++) {
						var node = new Node(value[i], this);
						children[i] = node;
						node.paint(nodesPane);
					}
				}
			};
			return nodesPane;
		};

		this.paint = function(parent) {
			var nodesPane = window.document.createElement("div");
			nodesPane.id = this.divid() + this.getId();
			if (value != null && value.length > 0) {
				for (var i = 0; i < value.length; i++) {
					var node = new Node(value[i], this);
					children[i] = node;
					node.paint(nodesPane);
				}
			}

			parent.appendChild(nodesPane);
		};
	};
	// 上面为构造菜单，下面为调用方法级事件

	var jsTreeflag = false;
	var jsTreeopens = false;
	var tree1 = $('#'+objId).find("#"+div_t1)[0];
	var tree2 = $('#'+objId).find("#"+div_t2)[0];
//	var tree1 = document.getElementById(div_t1);
//	var tree2 = document.getElementById(div_t2);

	function newTree(data,id) {
		var parent1 = $('#'+objId).find("#"+div_t2)[0];
		tree1 = new Tree(id);
		tree1.bindData(data);
		tree1.paint(parent1);
		tree1.onNodeChange = function() {
		}
	};

	document.documentElement.onmousedown = function() {

		if (!jsTreeflag) {
			var tree2 = $('#'+objId).find("#"+div_t2)[0];
			tree2.style.display = "none";
			AllProduct();

		}
	};

	function AllProduct() {
		if (_this.curId == 0) {
			var tree1 = $('#'+objId).find("#"+div_t1)[0];
			var topNode = EWS.Language?EWS.Language['E01006']['E010063']:'所有产品';
			tree1.innerHTML = "<img alt='' class='picopen'  src='"+closeds+"' />"+topNode+"";
		}

	};
	this.stateClick = function stateClick(id) {
		var node = getFromContainer(id);
		if(typeof node.changeState == 'function')//点击顶级时没有changeState
			node.changeState();
	};

	this.nodeClick = function nodeClick(id) {
		_this.curId = id;
		SkyObject.call(this);
//		jsTreeCategoryId = id;
		var namelink = document.getElementById(this.namelink() + id);
		var _s = namelink.innerHTML;

		var tree1 = $('#'+objId).find("#"+div_t1)[0];
		tree1.innerHTML = _s;

		var tree2 = $('#'+objId).find("#"+div_t2)[0];
		tree2.style.display = "none";
		AllProduct();
	};

	tree1.onclick = function opentree() {
		if (this.curId == 0) {
			var tree1 = $('#'+objId).find("#"+div_t1)[0];
			tree1.innerHTML = "请选择分类";
		};

		var tree2 = $('#'+objId).find("#"+div_t2)[0];
		tree2.style.display = "block";
	};

	tree1.onmouseover = function setFlag() {
		jsTreeflag = true;
	};
	tree1.onmouseout = function setFlag(p) {
		jsTreeflag = false;
	};

	tree2.onmouseover = function setFlag(p) {
		jsTreeflag = true;
	};
	tree2.onmouseout = function setFlag(p) {
		jsTreeflag = false;
	};
	this.newtree2 = function(tagUrl,obj,id,callback) {
		var tree2 = $('#'+objId).find("#"+div_t2)[0];
		tree2.style.display = "none";
		var roots = "";
		$.ajax({
				url : tagUrl,
				type : "get",
				mode : "queue",
				cache : false,
				success : function(data, status) {
					newTree(eval("(" + data + ")").tree[0].children,id);
					obj.stateClick(0); // 0表示最顶级
					AllProduct();
					callback();
				}
			});
	};

};

//if(document.all){
//window.attachEvent('onload', function(){
//	    alert(this.config.htmlUrl)
//	JsTree = new EWS.Order.JsTree("showNode", "hideNode");
//	JsTree.newtree2("http://www1.khews.local/EWS_Interface/Order_ListAllCatalog.aspx?cid=jewbo");
//});
//} else {
//	alert(this.config.htmlUrl)
//	window.addEventListener('load', function(){
//			JsTree = new EWS.Order.JsTree("showNode", "hideNode");
//		JsTree.newtree2("http://www1.khews.local/EWS_Interface/Order_ListAllCatalog.aspx?cid=jewbo");
//	
//	},false);
//}
