$(function(){
	$("#rolin").find(".menuh2").bind("mouseover",function(){
		var title = $(this).find(".title_id").html();
		if(title != null && title.length > 11){
			$(this).addClass("menu-li-h-l");
		} else {
			$(this).addClass("menu-li-h");
		}
	});
	$("#rolin").find(".menuh2").bind("mouseout",function(){
		var className = this.className;
		var title = $(this).find(".title_id").html();
		if(className.indexOf("current")<0){
			if(title != null && title.length > 11){
				$(this).removeClass("menu-li-h-l");
			} else {
				$(this).removeClass("menu-li-h");
			}
		}
	});
});


function loadMenu() {
	//rolinTab("rolin")
}

function rolinTab(obj) {
	
	// 取得菜单列表对象
	var list = $$$(obj).getElementsByTagName("li");
	
	var state = {
		show : false,
		hidden : false,
		showObj : false
	};
	
	// 获取菜单列表
	for ( var i = 0; i < list.length; i++) {
		var tmp = new rolinItem(list[i], state, list.length);
		if (i == 0) {
			// 控制初始化打开是否显示菜单
			// tmp.pShow();
		}
	}
}

function rolinItem(obj, state, len) {
	var speed = 0.0666;
	var range = 1;
	var interval;
	var tarH;
	var tar = this;
	var head = getFirstChild(obj);
	var content = getNextChild(head);
	// 获取单击的div
	var h2div = head.getElementsByTagName("div")[0];
	// 二级菜单所有div
	var allsubmenu = content.getElementsByTagName("div");
	var isOpen = false;
	// 判断是否含有子菜单
	var flag = false;
	this.pHidden = function() {
		if (isOpen) {
			hidden();
		}
	}
	this.pShow = show;

	// 设置content实际工作的高度
	var baseH = content.offsetHeight;
	
	content.style.display = "none";

	// 二级菜单的鼠标事件
	for(var i = 0; i < allsubmenu.length; i++) {
		allsubmenu[i].onmouseover = function () {
			this.style.background = "#f1f1f1 no-repeat";
		}
		allsubmenu[i].onmouseout = function () {
			this.style.background = "#ffffff no-repeat";
		}
	}


	head.onmouseover = mouseoverhead;
	head.onmouseout = mouseouthead;
	
	function mouseoverhead() {
		// mouseover 背景
		this.style.background = "url("+contentPath+"/res/sites/images/treebg.jpg)";
		// mouseover 菜单选项边框样式
		this.style.borderBottom = "1px solid #bac4ce";
		// mouseover 字体颜色
		this.style.color = "#ffffff";
	}

	function mouseouthead() {
		// mouseout 背景色
		this.style.background = "#FFF"
		// mouseout 菜单选项边框样式
		this.style.borderBottom = "1px solid #bac4ce";
		// mouseout 字体颜色
		this.style.color = "#6a6f75";
	}

	h2div.onmouseover = mouseoverh2;
	h2div.onmouseout = mouseouth2;

	function mouseoverh2() {
		this.style.color = "#ffffff";
	}
	
	function mouseouth2() {
		this.style.color = "#6a6f75";
	}

	head.onclick = function() {
		// click 背景
		this.style.background = "url("+contentPath+"/res/sites/images/treebg.jpg)";
		// click 菜单选项边框样式
		this.style.borderBottom = "1px solid #bac4ce";
		// 判断是否处于显示状态
		if (!state.show && !state.hidden) {
			if (!isOpen) {
				head.onmouseout = null;
				h2div.onmouseout = null;
				show();
			} else {
				hidden();
			}
		}
	}

	function show() {

		state.show = true;
		if (state.openObj && state.openObj != tar) {
			state.openObj.pHidden();
		}
		// 初始化菜单项样式
		content.style.height = "0px";
		content.style.display = "block";
		content.style.width = "180px";
		// 横向
		content.style.overflowX = "hidden";
		// 下拉
		content.style.overflowY = "hidden";
		state.openObj = tar;
		
		// 设置最终菜单项高度
		tarH = baseH;
		
		// 如果没发现子菜单，则设置子菜单高度为 0.0001 px
		if (tarH == 0 || tarH <= 20) {
			// 没有子菜单
			flag = true;
			tarH = 0.1;
		}

		interval = setInterval(move, 10);
	}

	function showS() {
		isOpen = true;
		state.show = false;
	}

	function hidden() {
		state.hidden = true;
		tarH = 0;
		interval = setInterval(move, 10);
	}

	function hiddenS() {
		head.style.borderBottom = "none";
		head.onmouseout = mouseouthead;
		h2div.onmouseout = mouseouth2;
		head.onmouseout();
		h2div.onmouseout();
		content.style.display = "none";
		isOpen = false;
		state.hidden = false;
	}

	function move() {
		var dist = (tarH - content.style.height.pxToNum()) * speed;
		
		if (Math.abs(dist) < 1) {
			dist = dist > 0 ? 1 : -1;
		}
		
		// 如果没高度 则将显示高度设置为 0 
		if (content.style.height.pxToNum() + dist < 0 ) {
			content.style.height = "0px";
		} else {
			content.style.height = (content.style.height.pxToNum() + dist) + "px";
		}
		
		if (Math.abs(content.style.height.pxToNum() - tarH) <= range) {
			clearInterval(interval);
			
			if (tarH == 0.1 && flag == true) {
			// 如果没有子菜单 则将内容高度直接设为0 将该层设置为不显示
				content.style.height = "0px";
				content.style.display  = "none";
				flag = false;
			} else {
			// 如果有，则按正常高度显示菜单
				content.style.height = tarH + "px";
			}
			
			if (tarH != 0) {
				showS();
			} else {
				hiddenS();
			}
		}
	}

}

var $$$ = function($$$) {
	return document.getElementById($$$)
};

String.prototype.pxToNum = function() {
	return Number(this.replace("px", ""))
}

function getFirstChild(obj) {
	var result = obj.firstChild;
	while (!result.tagName) {
		result = result.nextSibling;
	}
	return result;
}

function getNextChild(obj) {
	var result = obj.nextSibling;
	while (!result.tagName) {
		result = result.nextSibling;
	}
	return result;
}

function mouseover(entity) {
	entity.style.background = "#f1f1f1 no-repeat";
}

function mouseout(entity) {
	entity.style.background = "#ffffff no-repeat";
}

function openPage(url, frame) {
	window.open(url, frame)
}

function showTab(m,n,p){
	var menu = document.getElementById(p).getElementsByTagName("li");
	for(i=0;i<menu.length;i++){
		menu[i].className = (i+1)==n?"selected":"";
		var showdiv = document.getElementById("" + m + (i + 1));
		if(showdiv == null){
			continue;
		}
		if( n == i + 1 ){
			showdiv.style.display = "block";
		} else {
			showdiv.style.display = "none";
		}
		
	}
}
function showTabEn(m,n,p,cla_sel,cla_nosel){
	var menu = document.getElementById(p).getElementsByTagName("a");
	for(i=0;i<menu.length;i++){
		menu[i].className = (i+1)==n?cla_sel:cla_nosel;
		var showdiv = document.getElementById("" + m + (i + 1));
		if( n == i + 1 ){
			showdiv.style.display = "block";
		} else {
			showdiv.style.display = "none";
		}
		
	}
}
