/*===================
	导航
===================*/

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
    	if (typeof element == 'string') element = document.getElementById(element);
		if (arguments.length == 1)  return element;
		elements.push(element);
	}
	return elements;
}

String.prototype.pxToNum = function() {
	var num = Number(this.replace("px",""));
	if (isNaN(num)) num = null;
	return num;
}

function initNav() {
	this.objName = "nav";
	var navList;
	var tmp = new Array();;
	var imgArr = new Array();;
	var len;
	this.init = function() {
		
		navList = $(this.objName).getElementsByTagName("LI");
		len = navList.length;
		
		for (var i=0; i<len; i++) {
			if (this.checkIsTrue(navList[i])) {
				var img = navList[i].getElementsByTagName("IMG")[0];
				tmp.push(new Array(navList[i],img));
			}
		}
		navList = tmp;
		tmp = null;
		len = navList.length;
		this.setClick(navList,imgArr,len);
		
	}
	
	this.checkIsTrue = function(obj) {
		if (obj.className == "tabNomorl") return true;
		return false;
	}
	
	this.setClick = function(newNav,newImg,len) {
		for (var i=0; i<len; i++ ) {
			this.setClickInfo(i);
		
			newNav[i][0].onmouseover = function() {
				this.className = "tabHover";
				newNav[this.i][1].src = newImg[this.i].src;
			}
			newNav[i][0].onmouseout = function() {
				this.className = "tabNomorl";
				newNav[this.i][1].src = newNav[this.i][1].src.replace("_2","_1");
			}
		}
	}
	
	this.setClickInfo = function(i) {
		var img = new Image();
		img.src = navList[i][1].src.replace("_1","_2");
		imgArr.push(img);
		navList[i][0].i = i;
	}
	len = null;	
}

function initHccNav() {
	this.objName = "hcc_nav";
	this.checkIsTrue = function(obj) {
		if (obj.className.indexOf("nomorl") != -1) return true;
		return false;
	}
	this.setClick = function(newNav,newImg,len) {
		for (var i=0; i<len; i++ ) {
			this.setClickInfo(i);
		
			newNav[i][0].onmouseover = function() {
				this.className = this.className.replace("nomorl","hover")
				newNav[this.i][1].src = newImg[this.i].src;
			}
			newNav[i][0].onmouseout = function() {
				this.className = this.className.replace("hover","nomorl")
				newNav[this.i][1].src = newNav[this.i][1].src.replace("_2","_1");
			}
		}
	}
	
}
initHccNav.prototype = new initNav();

function intiHccMain() {
	var objName = "hcc_main";
	var objList = $("hcc_main").getElementsByTagName("LI");
	if(objList.length == 0){return false;}
	var nowNum = [];
	var reList = new Array();
	for (var i=0; i<objList.length; i++) {
		var tmpNode = objList[i].firstChild;
		while (!tmpNode.className) {
			tmpNode = tmpNode.nextSibling;
		}
		if (tmpNode.className == "hccml_title") {
			var t = tmpNode.nextSibling;

			while (!t.className) {
				t = t.nextSibling;
			}
			var h = t.offsetHeight;
			t.style.display = "none";
			var tmphcc = new hccShow(i,tmpNode,t,h,nowNum,reList);
			reList.push(tmphcc);
			
		}
	}
	
	reList[0].pshow();
	
}
function hccShow(idn,obj,expMain,h,nowNum,reList) {
	var speed = 0.50666;
	var range = 1;
	var mainH = h;
	var interval;
	var isOpen = false;
	var btn = obj;
	var main = expMain;
	var oldClassName = btn.className;
	var btnImg = btn.getElementsByTagName("DIV")[2];
	var tarH;
	var isMoving = false;
	this.pshow = function() {
		show("p");
	}
	this.phidden = function() {
		btn.className = oldClassName;
				btnImg.className = "hccm_btn_nomorl";
		tarH = 0;
		isMoving = true;
		interval = setInterval(moving,120)
	}
	var hidden = this.phidden;
	btn.onmouseover = function() {
		this.className = "hccml_title_h";
	}
	btn.onmouseout = function() {
		if (!isOpen) {
			this.className = oldClassName;
			btnImg.className = "hccm_btn_nomorl";
		}
	}
	btn.onclick = function() {
		if (nowNum[1] == "2") {
			if (!isOpen) {
				
				if (idn != nowNum[0])
				//if (nowNum.length > 0) {
					reList[nowNum[0]].phidden();
				//}
				
				show();
			} else {
				hidden();

			}
		}
	}
	function show(q) {
		nowNum[1] = "1";
		btn.className = "hccml_title_h";
		btnImg.className = "hccm_btn_hover";
		main.style.display = "block";
		if (q != "p") {
			main.style.height = "0px";
			tarH = mainH;
			isMoving = true;
			interval = setInterval(moving,120)
		} else {
			isOpen = !isOpen;
			main.style.height = mainH + "px";
			nowNum[0] = idn
			nowNum[1] = "2";
		}
	}
	
	
	
	function moving() {
		var offsetH = (tarH - (main.style.height).pxToNum())*speed;
		if (Math.abs(offsetH) < 1) {
			offsetH = offsetH >0 ? 1:-1;
		}
		nowNum[1] = "1";
		main.style.height = ((main.style.height).pxToNum() + offsetH ) + "px";
		if (Math.abs(tarH - (main.style.height).pxToNum()) <= range) {
			clearInterval(interval);
			main.style.height = tarH + "px";
			if (tarH == 0) {
				main.style.display = "none";
			} else {
				nowNum[0] = idn;
			} 
			isOpen = !isOpen;
			nowNum[1] = "2";
		}
	}
	
}

function initJoinMain() {
	var mainStr = $("alert_main").innerHTML;
	
	$("alert_main").parentNode.removeChild($("alert_main"));
	
	var objName = "join_main";
	var objList = $(objName).getElementsByTagName("LI");
	
	var len = objList.length;
	var offset = 41;
	for (var i=0; i<len; i++) {
		var alink = objList[i].getElementsByTagName("A")[0];
		
		var btn = objList[i].getElementsByTagName("IMG");
		
		//获得职位编号
		var id = alink.id.replace("pos_","");
		
		
		btn = btn[btn.length-1];
		btn.id = id;
		
		btn.style.cursor = "pointer";
		var jmId = alink.id;
		
		btn.onclick = function() {
			new rolinAlertWindow(mainStr,this.id);
		}
		
		if (i%2 == 1) {
			var tmpp = getNextNode(getFirstChild(objList[i]));
			var oldp = getNextNode(getFirstChild(objList[i-1]));
			var nh = tmpp.offsetHeight;
			var oh = oldp.offsetHeight;
			if (nh < oh) {
				tmpp.style.height = (oh - offset) + "px";
			} 
			if (nh > oh) {
				oldp.style.height = (nh - offset) + "px";
			}
		}
	}
}

function initCaseList() {
	//获取对象
	var btnArr = $("case_list_btn").getElementsByTagName("DIV");
	var nextBtn = btnArr[1];
	var preBtn = btnArr[0];
	var obj = $("case_list");
	var caseBox = obj.parentNode;
	var itemNum = obj.getElementsByTagName("LI").length;
	var baseWidth = 129;
	var dir = -1;
	var fx = 0;
	var speed = 0.3666;
	var range = 2.5;
	var mInterval;
	var interval;
	var tarX;
	
	// 初始化
	obj.innerHTML += obj.innerHTML;
	obj.style.width = baseWidth*itemNum *2+ "px";
	preBtn.onclick = function() {
		dir = 1;
		this.className = "clb_pre_hover"
		nextBtn.className = "clb_next_nomorl"
	}
	nextBtn.onclick = function() {
		dir = -1;
		this.className = "clb_next_hover"
		preBtn.className = "clb_pre_nomorl"
	}
	if (itemNum > 5) {
		initCase();
	}
	
	function initCase() {
		interval = setInterval(change,3000);
	}
	
	function moveObj(x) {
		if (x/baseWidth == 1) {
			x = -(itemNum)*baseWidth;
			obj.style.left = x;
			x += dir*baseWidth;
		}
		
		if (x/baseWidth == -(itemNum + 1) ) {
			obj.style.left = "0px";
			x = dir*baseWidth;
			
		} 
		fx = x;
		tarX = x;
		if (mInterval) clearInterval(mInterval);
		mInterval = setInterval(moving,45);
	}
	
	function change() {
		fx += dir*baseWidth;
		moveObj(fx);
	}
	
	function moving() {
		var x = obj.style.left.pxToNum();
		obj.style.left = ( x + (tarX-x)*speed) + "px";
		if (Math.abs(tarX - (obj.style.left).pxToNum()) <= range) {
			clearInterval(mInterval);
			obj.style.left = tarX + "px";
		}
	}

}

function initAboutNav() {
	var objName = "about_nav";
	var obj = $(objName);
	var list = obj.getElementsByTagName("LI");
	var listHover;
	for (var i=0; i<list.length; i++) {
		if (list[i].className == "about_nav_hover") {
			listHover = list[i];
			break;
		}
	}
	var newHtml = listHover.innerHTML;
	if (i == 5) {
		listHover.parentNode.style.backgroundImage = "url(Images/about_nav_r_bg2.gif)";
	} else if (i == 0) {
		listHover.parentNode.parentNode.style.backgroundImage = "url(Images/about_nav_l_bg2.gif)";
		listHover.style.backgroundImage = "url(Images/about_cnav2.gif)"
		listHover.style.backgroundPosition = "right"
	} else {
		newHtml += "<span></span>";
	}
	listHover.innerHTML = newHtml;
	var img = listHover.getElementsByTagName("IMG")[0];
	img.src = changeNav(img);
	
	function changeNav(imgObj) {
		var tmpsrc = (imgObj.src)
		tmp = tmpsrc.substr(tmpsrc.length - 5)
		var newTmp = tmp.replace("1","2");
		tmpsrc = tmpsrc.replace(tmp,newTmp);
		return tmpsrc;
	}
}

function setImgAlpha() {
	var list = $("advan_main").getElementsByTagName("IMG");
	for (var i=0; i<list.length; i++) {
		setAlphaPng(list[i]);
	}
}

function initFound() {
	var obj = [$("exp1"),$("exp2"),$("exp3")];
	
	var closeBtn1 = getFirstChild(getFirstChild(obj[0]));
	var closeBtn2  = getFirstChild(getFirstChild(obj[1]));
	var closeBtn3  = getFirstChild(getFirstChild(obj[2]));
	closeBtn1.onclick = function() {
		obj[0].style.display = "none";
	}
	closeBtn2.onclick = function() {
		obj[1].style.display = "none";
	}
	closeBtn3.onclick = function() {
		obj[2].style.display = "none";
	}
	var btnList = $("btnBox").getElementsByTagName("IMG");
	for (var i=0; i<btnList.length; i++) {
		btnList[i].i = i;
		btnList[i].style.cursor = "pointer";
		btnList[i].onclick = function() {
			var j = 0;
			if (this.i == 0) j = 1;
			obj[this.i].style.display = "block";
			obj[j].style.display = "none";
		}
	}
	if (document.location.hash == "#1") {
		btnList[0].onclick();
	}
	if (document.location.hash == "#2") {
		btnList[1].onclick();
	}
	if (document.location.hash == "#3") {
		btnList[2].onclick();
	}
}

function initCaseSidebar() {
	var cInterval;
	var speed = 0.0966;
	var range = 1.5;
	var baseH = 198;
	
	var obj = $("hcs_mainbox");
	obj.style.top = "0px";
	
	var list = obj.getElementsByTagName("LI");
	var len = list.length;
	var h = list[0].offsetHeight;
	var btnObj = getFirstChild(obj.parentNode.parentNode)
	var btnUp = btnObj.getElementsByTagName("SPAN")[0];
	var btnDown = getNextNode(getNextNode(btnObj)).getElementsByTagName("SPAN")[0];
	var bi = 0;
	
	if (len > 4) {
		btnUp.onmouseover = function() {
			if (bi < 0) {
				this.style.backgroundPosition = "0 -22px";
			}
		}
		btnDown.onmouseover = function() {
			if ( bi > (4 - len)) {
				this.style.backgroundPosition = "-22px -22px";
			}
		}
		
		btnUp.onmouseout = function() {
			this.style.backgroundPosition = "0px 0px";
		}
		btnDown.onmouseout = function() {
			this.style.backgroundPosition = "-22px 0px";
		}
		
		
		btnUp.onclick = function() {
			if (bi < 0) {
				bi++;
				mov();
			}
		}
		btnDown.onclick = function() {
			if (bi > (4 - len)) {
				bi--;
				mov();
			}
		}
	}
	
	function mov() {
		clearInterval(cInterval);
		cInterval = setInterval(moving,5);
	}
	
	function moving() {
		var t = obj.style.top.pxToNum();
		var ra = (bi*baseH-t)*speed;
		if (Math.abs(ra) < 1) {
			ra = ra > 0 ? 1:-1;
		}
		obj.style.top = ( t + ra ) + "px";
		if (Math.abs(bi*baseH - obj.style.top.pxToNum()) <= range) {
			clearInterval(cInterval);
			obj.style.top = bi*baseH + "px";
		}
	}
}
