var treeData;

function loadTree(divId) {
	if (treeData != undefined && treeData.length > 1) {
		initTree(divId);
		return;
	}
	var url = ctx + '/loadCategoryData.u';
	$.ajax( {
		type :"POST",
		url :url,
		data :"",
		success : function(msg) {
			eval(msg);
			initTree(divId);
			return;
		},
		error : function(XMLHttpRequest, textStatus, thrownError) {
			return;
		}
	});
}

function loadTreeShow(divId) {
	if (treeData != undefined && treeData.length > 1) {
		initTree(divId);
		$("#" + divId).show();
		return;
	}
	var url = ctx + '/loadCategoryData.u';
	$.ajax( {
		type :"POST",
		url :url,
		data :"",
		success : function(msg) {
			eval(msg);
			initTree(divId);
			$("#" + divId).show();
			return;
		},
		error : function(XMLHttpRequest, textStatus, thrownError) {
			return;
		}
	});
}

function initTree(divId) {
	$("#" + divId).hide();
	$("#" + divId).find("#category_menu0").html('');
	$("#" + divId).find("#category_menu1").html('');
	$("#" + divId).find("#category_menu2").html('');
	var rowCount = Math.round(treeData.length / 3) + 1;
	var i = 0;
	var step = 0;
	// alert(rowCount);
	jQuery.each(treeData, function() {
		// addNode($("#category_menu"),this);
			addNode($("#" + divId).find("#category_menu" + step), this);
			i++;
			if (i == rowCount) {
				step++;
				i = 0;
			}
		});

	$("#" + divId).find('ul.category_menu').superfish( {
		hoverClass :'sfHover',
		delay :100
	});
	// showCategoryDiv(divId);
	jQuery.each($(".sub_menu_list2"), function() {
		$(this).html(
				$("#infoBoxTemplate").html().replace("<!--", "").replace("-->",
						"").replace("@@content@@", $(this).html()));
	});
	jQuery.each($(".sub_menu_list"), function() {
		$(this).html(
				$("#infoBoxTemplate").html().replace("<!--", "").replace("-->",
						"").replace("@@content@@", $(this).html()));
	});
	$("#" + divId).hide();
}

function showCategoryDiv(divId) {
	$("#" + divId).show();
}

/* Build Tree function */

function addLeaf(title, id, code, searchType) {
	if (id != undefined && title != undefined) {
		return '<li id="'
				+ id
				+ '"><div><span class="img_span">&nbsp;</span><span onclick="goCategoryWithButtonHistory(\'Learn/Browse by Category\',\'clicks on each sub category\',\''
				+ code + '\',\'' + searchType + '\')" class="a_span" style="margin-left:12px" >'
				+ title + '</span></div></li>';
	}
	return "";
}

function addFolder(title, id, code) {
	if (id != undefined && title != undefined) {
		if (code.length == 8) {
			return '<li id="'
					+ id
					+ '" class="sub_menu"><div><span class="img_span">►</span><span onclick="goCategoryWithButtonHistory(\'Learn/Browse by Category\',\'clicks on each sub category\',\''
					+ code
					+ '\')" style="float:left;margin-left:12px" class="a_span">'
					+ title
					+ '</span></div><ul class="sub_menu_list2"></ul></li>';
		}
		return '<li id="'
				+ id
				+ '" class="sub_menu"><div><span  class="img_span">►</span><span onclick="goCategoryWithButtonHistory(\'Learn/Browse by Category\',\'clicks on each sub category\',\''
				+ code + '\')" class="a_span">' + title
				+ '</span></div><ul class="sub_menu_list"></ul></li>';
	}
	return "";
}

function addNode(parentUL, node) {
	if (node.children != null) {
		parentUL.append(addFolder(node.title, node.id, node.code));
		jQuery.each(node.children, function() {
			addNode(parentUL.find("#" + node.id).children("ul"), this);
		});
	} else {
		parentUL.append(addLeaf(node.title, node.id, node.code));
	}
}

// '$(this).prepend('<div class="shadowWrapper"><div class="infoBox"><div
// class="boxTop"><div class="left"></div><div class="right"></div><div
// class="center"></div></div><div class="boxMiddle relative">');<ul></ul><img
// class="fadeBackgroundImage"
// src="'+ctx+'/images/common/text/line.png"/></div><div class="boxBottom"><div
// class="left"></div><div class="right"></div><div
// class="center"></div></div></div></div>'

function showCategoryWidget(divId) {
	loadTreeShow(divId);
	if ($.browser.msie) {
		jQuery.each($("#" + divId).find(".fadeBackgroundImage"), function() {
			$(this).height($(this).parent().attr('clientHeight'));
		});
	}
}