var tree;
var xmldoc;
var lastSelHTML = "";
var lastSelItem = null;
initTree();
function initTree() {
	//lastSelHTML = window.external.GetLastSelectWeb();
	if (document.getElementById) {	
		tree = new WebFXTree('全部搜索网站');
		tree.setBehavior('classic');
		
		var xmlfile = "js/allEngine.xml";
		xmldoc = new ActiveXObject("MSXML2.DOMDocument");
		xmldoc.async=false;
		xmldoc.load(xmlfile);
		xmldoc.setProperty("SelectionLanguage", "XPath");	
		var nodes = xmldoc.childNodes;		
		for (var j=0; j<nodes.length; j++ ) {
			var node = nodes[j];		
			loadNodes(tree, node);
		}
		document.getElementById("treeDiv").innerHTML = tree;
		tree.expandChildren();
		if (lastSelItem) {
			lastSelItem.select();
			webFXTreeHandler.selected = lastSelItem;
			selectSiteTree();
		}
	}
}

function searchTree(key) {
	if (key == "") {
		initTree();
		return;
	}
	
	tree = new WebFXTree('全部搜索网站');
	tree.setBehavior('classic');
	
	var path = "//engine[contains(@name,'" + key + "')]";
	var nodes = xmldoc.selectNodes(path);
	
	for (var j=0; j<nodes.length; j++ ) {
		var node = nodes[j];
		var name = node.getAttribute("name");
		var html = node.getAttribute("html");
		var site = node.getAttribute("site");
		var checkBoxTreeItem = new WebFXTreeItem(name, 'javascript:selectSiteTree();', '','' ,'' ,html,site);
		tree.add(checkBoxTreeItem);
	}
	document.getElementById("treeDiv").innerHTML = tree;
	tree.expandChildren();
}

function loadNodes(treeNode, xmlNode) {
	var nodes = xmlNode.childNodes;
	for( var j=0; j<nodes.length; j++ ) {
		var node = nodes[j];
		var name = node.getAttribute("name");
		if (node.tagName == "cate") {
			var treeItem
			if (isAllShow) {
				treeItem = new WebFXTreeItem(name);
			} else {
				treeItem = new WebFXCheckBoxTreeItem(name);
			}
			treeNode.add(treeItem);
			loadNodes(treeItem, node);
		} else {			
			var k = node.getAttribute("k");
			if (!isAllShow) {
				if (k != "1") {
					continue;
				}
			}
			
			var checkBoxTreeItem;
			if (isAllShow) {
				var html = node.getAttribute("html");
				var site = node.getAttribute("site");	
				checkBoxTreeItem = new WebFXTreeItem(name, 'javascript:selectSiteTree();', '','' ,'' ,html,site);
				if (html == lastSelHTML) {
					lastSelItem = checkBoxTreeItem;
				}
				
			} else {
				var id = node.getAttribute("id");
				var url = node.getAttribute("url");
				var cfg = node.getAttribute("cfg");
				var isUTF8 = node.getAttribute("u");
				var isPost = node.getAttribute("p");
				var isSupportCity = node.getAttribute("city");

				checkBoxTreeItem = new WebFXCheckBoxTreeItem(name, id, url, cfg, isUTF8, isPost);
				if (isSupportCity == "0") {
					checkBoxTreeItem.icon = "images/brkpi_obj.gif";
				}
			}
			treeNode.add(checkBoxTreeItem);
		}		
	}
}

function selectedAll() {
	for (var i = 0; i < tree.childNodes.length; i++) {		
		if (tree.childNodes[i]._checked) {
			selectTreeItem(tree.childNodes[i]);
		}
		selectTreeItem(tree.childNodes[i]);
	}
}

function UnSelectAll() {
	for (var i = 0; i < tree.childNodes.length; i++) {		
		if (!tree.childNodes[i]._checked) {
			selectTreeItem(tree.childNodes[i]);
		}
		selectTreeItem(tree.childNodes[i]);
	}
}

function getSelectItems(parObj) {
	for (var i = 0; i < parObj.childNodes.length; i++) {
		var child = parObj.childNodes[i];
		if (child.folder) {
			getSelectItems(child);
		} else {
			if (child._checked) {
				selectUrls.push(child._postURL);
				selectEngines.push(child._EngineCfg);
				selectIDs.push(child._engineId);
				selectNames.push(child.text);
				selectIsUTF8.push(child._isUTF8);
				selectIsPost.push(child._isPost);
			}
		}
	}
}

function selectSiteTree() {	
	var name = tree.getSelected().text;
	document.getElementById("siteName").innerText = name;
	document.getElementById("detailNews").src = "http://www.blogou.com/getWebSiteDetail.asp?name=" + escape(name);
}

function getSelectEngineHTML() {
	return tree.getSelected()._html;
}

function getSelectEngineName() {	
	return tree.getSelected().text;
}