/////////////////////////////////////////
//网站共用JS函数
//版  本：v1.2
//作  者：freewind
//    QQ: 573704282
// //////////////////////////////////////
var d, kw, oResult, oInput, currentRow, bIsLoadComplete = false;
var theTimerID = 0;
var div;
var width = 0;

function $(id){
	return document.getElementById(id);
}
(function(){
	var isIE6 = navigator.userAgent.toLowerCase().indexOf("msie 6") > -1;//判断是否为IE6   
	if(isIE6){   
		try{   
		   document.execCommand("BackgroundImageCache", false, true);   
		}catch(e){}   
	}
})();

if(typeof DOTA == "undefined" || !DOTA){
	window.DOTA = {};
}
DOTA.Ajax = function(){};
DOTA.Ajax.prototype = {
	createHttpRequest : function(){
		if(window.ActiveXObject){
			var ver = ["Microsoft.XMLHTTP","MSXML.XMLHTTP"];
			for(var i=0; i<ver.length; i++){
				try{
					return new window.ActiveXObject(ver[i]);
				}catch(e){}
			}		
		}else{
			try{
				return new XMLHttpRequest();		
			}catch(e){}
		}
		//alert("Failure to create HttpRequest!");
	},
	
	get : function(url, async, fnCallBack){
		var xmlHttp = this.createHttpRequest();
		xmlHttp.open("GET", url, async);
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 ){
				if(xmlHttp.status == 200){
					fnCallBack(xmlHttp.responseText);
				}else{
					fnCallBack("false");
				}
			}
		};
		xmlHttp.setRequestHeader("If-Modified-Since","0");
		xmlHttp.send(null);
	}
};


function setCookie(name,value){
    var Days = 365, exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name){//取cookies函数        
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) 
		return unescape(arr[2]); 
	return null;
}
function delCookie(name){//删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
function loadScript(url){
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.charset = "gb2312";
	document.getElementsByTagName("head")[0].appendChild(script);
	script.src = url;
}
function updateHits(){
	var ajax = new DOTA.Ajax();
	var url = "/services/updateHits.ashx?i=" + pageInfo;
	ajax.get(url, true, function(){});
}
function clickCount(id){
	window.pageInfo = "AD|" + id;
	updateHits();
}

function download(id){
	window.pageInfo = "Download|" + id;
	updateHits();
}
function getOffset(obj, endObj){
	var x = 0, y = 0;
	do{
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}while(obj && obj != endObj);
	return {x:x, y:y};
}
function addPageToFavorite(pageTitle, pageUrl){
	if(arguments.length == 0){
		pageTitle = document.title;
		pageUrl = window.location.href;
	}
	try{
		if(document.all){
			window.external.AddFavorite(pageUrl ,pageTitle);
		}else{
			window.sidebar.addPanel(pageTitle, pageUrl,""); 
		}
	}
	catch(e){
		alert('请同时按\r\n\r\n CTRL + D \r\n\r\n键加入收藏夹或者书签。');
	}
}
function setHomePage(strHomePage){
	if(arguments.length == 0){
		strHomePage = window.location.href;
	}
	if(document.all){
		document.body.style.behavior = "url(#default#homepage)";
		document.body.setHomePage(strHomePage);
	}else if(window.sidebar){
		if(window.netscape){
			try{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}catch(e){
				alert("Firefox暂无此功能，请手动设置。");
				return;
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    	prefs.setCharPref('browser.startup.homepage',strHomePage);
	}
}

function checkLogin(user, pass){
	user = user || "username";
	pass = pass || "password";
	try{
		if($(user).value == ""){
			alert("请输入用户名！");
			$(user).focus();
			return false;
		}
		if($(pass).value == ""){
			alert("请输入登录密码!");
			$(pass).focus();
			return false;
		}
	}
	catch(e){
		return false;
	}
	setCookie("loginUser", $(user).value);
	setTimeout(function(){getUserInfo();}, 2000);
	return true;
}

function checkKeyword(form){
	if($("kw").value == ""){
		alert("请输入搜索关键词!");
		return false;
	}
	if(form){
		form.submit();
	}
	return true;
}
function checkSearch(val){
	$("kw").value = $("txtKeyword").value;
	$("t").value = val || 0;
	$("frmSearch").submit();
}

function changeFont(size){
	if(size == 3){
		size = 16;
	}else if(size == 2){
		size = 14;
	}else{
		size = 12;
	}
	var tags = changeFontTags.split("|");
	for(var i=0; i<tags.length; i++){
	
		var obj = $(tags[i]);
		if(obj){
			obj.style.fontSize = size + "px";
			obj.style.lineHeight = size * 1.7 + (size == 12 ? 2 : 0) + "px";
		}
	}
}

function showNav(){
	this.getElementsByTagName("dt")[0].className = "on";
	this.getElementsByTagName("dd")[0].style.display = "block";
}
function hideNav(){
	this.getElementsByTagName("dt")[0].className = "";
	this.getElementsByTagName("dd")[0].style.display = "none";
}
function initEvent1(){
	$("tb1").onmouseover = showNav;
	$("tb1").onmouseout = hideNav;
	$("tb2").onmouseover = showNav;
	$("tb2").onmouseout = hideNav;
	var sm = $("searchMenu").getElementsByTagName("li");
	var prev = 0;
	for(var i = 0; i < sm.length; i++){
		(function(i){
			sm[i].onclick = function(){
				sm[prev].className = "";
				sm[i].className = "on";
				prev = i;
				$("t").value = i + 1;
			};
		})(i);
	}
	var kw = $("kw");
	if(kw){
		kw.onfocus = function(){
			if(this.value == "请输入搜索关键词"){
				this.value = "";
			}
			this.className = this.className.replace(/ gray/ig, "");
		};
		kw.onblur = function(){
			if(this.value == ""){
				this.value = "请输入搜索关键词";
				this.className += " gray";
			}
		};
	}
};

/*function initFocus(){
	var prev = 0;
	var li = $("rpttitle").getElementsByTagName("div");
	for(var i = 0; i < li.length - 1; i++){
		(function(i){
			li[i].onmouseover = function(){
				if(i != prev){
					$("focus" + prev).style.display = "none";
					li[prev].className = "";
					li[i].className = "on";
					$("focus" + i).style.display = "";
					prev = i;
				}
			};
		})(i);
	}
}*/

function initLatest(){
	var prev = 0;
	var li = $("latest").getElementsByTagName("li");
	for(var i = 0; i < li.length - 1; i++){
		(function(i){
			li[i].onmouseover = function(){
				if(i != prev){
					$("latest" + prev).style.display = "none";
					li[prev].className = "";
					li[i].className = "on";
					$("latest" + i).style.display = "";
					prev = i;
				}
			};
		})(i);
	}
}

function letterCategory(letterContainer){
	this.LC = $(letterContainer);
	this.timerID1 = 0;
	this.timerID2 = 0;
	this.timerID3 = 0;
	this.delay = 100;
	this.flag = false;
}
letterCategory.prototype = {
	init : function(){
		var self = this;
		var list = this.LC.getElementsByTagName("li");
		if(arguments.length > 0){
			this.flag = arguments[0];
		}
		for(var i=1; i<list.length; i++){  //warning    i = 0
			list[i].onmouseover = function(){
				if(self.timerID1 > 0){
					clearTimeout(self.timerID1);
				}
				var me = this;
				self.timerID1 = setTimeout(function(){
					me.className = "on";
					self.showContent(me.id);
				}, self.delay);
			};
			list[i].onmouseout = function(){
				if(self.timerID2 > 0){
					//clearTimeout(self.timerID2);
				}
				var me = this;
				self.timerID2 = setTimeout(function(){
					me.className = "";
					var content = $("content"+me.id) || $("div0");
					content.style.display = "none";
				}, self.delay);
			};
		}
	},
	showContent : function(id){
		var self = this;
		var content = $("content"+id) || $("div0");
		content.style.display = "block";
		if(this.flag){
			content.style.top = "-34px";
			content.parentNode.style.zIndex =100;
		}else{
			content.style.top = "-4px";
			content.parentNode.style.zIndex = 80;
		}
		content.onmouseover = function(){
			if(self.timerID2 > 0){
				clearTimeout(self.timerID2);
				self.timerID2 = 0;
			}
			if(self.timerID3 > 0){
				clearTimeout(self.timerID3);
				self.tieerID3 = 0;
			}
			if(this.style.display != "block")
				this.style.display = "block";
		};
		content.onmouseout = function(oEvent){
			var me = this;
			self.timerID3 = setTimeout(function(){
				$(id).className = "";
				me.style.display = "none";
			}, self.delay);
		};
	}
};

function getPYContent(){
	loadScript("/Js/Letter.js");
};
function setKeywords(kw){
	if($("kw")){
		$("kw").value = kw;
	}
	if(typeof t != 'undefined' && t > 0 && t < 10){
		$("searchMenu").getElementsByTagName("li")[t-1].onclick();
	}
	var frm = $("frmSearch");
    if(frm && frm.target){
        frm.target = "";
    }
}
function initEvent2(){	
	new activeTab("yjy_title", "yjy_content").init();
	new activeTab("rpt_title1", "rpt_content1").init();
	new activeTab("rpt_title2", "rpt_content2").init();
	new activeTab("rpt_title3", "rpt_content3").init();
	new activeTab("news_title", "news_content").init();
	new activeTab("kxx_title", "kxx_content").init();
};

function activeTab(titleContainer, contentContainer){
	this.TC = $(titleContainer);
	this.CC = $(contentContainer);
	this.prevTab = null;
}
activeTab.prototype = {
	init : function(s, e, cls, tagt, tagv){
		var len = arguments.length;
		s = len > 0 ? s : 1, e = len > 1 ? e : 1, cls = cls || "tabActive";
		tagt = tagt || "li", tagv = tagv || "div";
		
		var self = this;
		var lst = this.TC.getElementsByTagName(tagt);
		if(lst.length > 0){
			this.prevTab = lst[s];
		}
		var len = lst.length - e;
		for(var i=s; i<len; i++){
			lst[i].nIndex = i - s;
			lst[i].onmouseover = function(){
				var divs = self.CC.getElementsByTagName(tagv);
				if(self.prevTab){
					self.prevTab.className = self.prevTab.className.replace(cls, "");
					if(divs.length == len - s) divs[self.prevTab.nIndex].style.display = "none";
				}
				this.className += " " + cls;
				
				if(divs.length == len - s) divs[this.nIndex].style.display = "block";
				self.prevTab = this;
			};
		}
	}
};

function closeFloater(){
	if(floater && floaterMin){
		floater.style.display = "none";
		floaterMin.style.display = "block";
		floaterScroll = floaterMin;
		setCookie("Floater", "0");
		onScroll();
	}
}
function openFloater(){
	if(floater && floaterMin){
		floater.style.display = "block";
		floaterMin.style.display = "none";
		floaterScroll = floater;
		setCookie("Floater", "1");
		onScroll();
	}
}
var f_current = 1, f_isMin = false;
function f_onOver(n){
	$("f_td" + f_current).className = "button2";
	$("f_content" + f_current).style.display = "none";
	$("f_td" + n).className = "button1";
	$("f_content" + n).style.display = "";
	f_current = n;
}
function f_min(img){
	var f = floatBox, hei = 195;
	if(f){
		if(f_isMin){
			img.src = "/images2/small.jpg";
			f.style.height = "223px";
			f.style.top = parseInt(f.style.top) - hei + "px"
			f_isMin = false;
			nY2 -= hei;
		}else{
			img.src = "/images2/max.jpg";
			f.style.height = "31px";
			f.style.top = parseInt(f.style.top) + hei + "px";
			f_isMin = true;
			nY2 += hei;
		}
	}
}
function f_close(){
	var f = $("floatBox");
	f.innerHTML = "";
	f.parentNode.removeChild(f);
	if(floatBox){
		floatBox = null;
	}
}

function loginOut(){
	$("lblLogin").style.display = "";
	$("lblInfo").style.display = "none";
	var ajax = new DOTA.Ajax();
	var url = "/member/userservice.ashx?action=loginout&t=" + new Date().getTime();
	ajax.get(url, true, function(){});
	var user = getCookie("loginUser");
	if(user && $("username")){
		$("username").value = user;
	}
}

function getUserInfo(){
	var ajax = new DOTA.Ajax();
	var url = "/member/userservice.ashx?action=get&t=" + new Date().getTime();
	ajax.get(url, true, function(result){
		if(result && result != "false"){
			$("lblLogin").style.display = "none";
			$("lblInfo").style.display = "";
			$("lblUser").innerHTML = result;
		}else{
			var user = getCookie("loginUser");
			if(user && $("username")){
				$("username").value = user;
			}
		}
	});
}

var PCT = 0;
function initProduct(){
	if(typeof index_content == 'undefined'){
		if(++PCT < 10){
			setTimeout(arguments.callee, 1000);
		}
		return;
	}
	var pCategory, divs, id;
	pCategory = $("pCategory");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:$("pCategory"), showDelay: 100, hideDelay: 100, contentEl: 'subCategory'+id});
			}
		}
	}
	
	pCategory = $("PSC");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:$("PSC"), showDelay: 100, hideDelay: 100, contentEl: 'subCategory'+id});
				if(typeof PRCID != 'undefined' && id == PRCID){
					divs[i].style.fontWeight = "bold";
				}
			}
		}
	}
	
	pCategory = $("PW");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length-1; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:$("PW"), showDelay: 100, hideDelay: 150, contentEl: 'p1_'+id});
				if(typeof PWCID != 'undefined' && id == PWCID){
					divs[i].style.fontWeight = "bold";
				}
			}
		}
	}
	
	pCategory = $("PS");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length-1; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:$("PS"), showDelay: 100, hideDelay: 150, contentEl: 'p2_'+id});
				if(typeof PHCID != 'undefined' && id == PHCID){
					divs[i].style.fontWeight = "bold";
				}
			}
		}
	}
	
	pCategory = $("PE");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length-1; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:$("PE"), showDelay: 100, hideDelay: 150, contentEl: 'p31_'+id});
				if(typeof PECID != 'undefined' && id == PECID){
					divs[i].style.fontWeight = "bold";
				}
			}
		}
	}	
	
	pCategory = $("PP");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length-1; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:$("PP"), showDelay: 100, hideDelay: 150, contentEl: 'p32_'+id});
				if(typeof PECID != 'undefined' && id == PECID){
					divs[i].style.fontWeight = "bold";
				}
			}
		}
	}
};

function initECategory(){
	var pCategory, divs, id;
	pCategory = $("ecList");
	if(pCategory){
		divs = pCategory.getElementsByTagName("a");
		for(var i = 0; i < divs.length; i++){
			id = divs[i].getAttribute("ref");
			if(id != ""){
				new DOTA.Tooltip({target: divs[i], rendTo:pCategory, anchor:"right", fixed:pCategory, showDelay: 100, hideDelay: 100, contentEl: 'ebook_'+id});
			}
		}
	}
}

window.onload = function(){	
	bIsLoadComplete = true;
	
	if(typeof homePage != 'undefined'){
		//initMarquee();	
		//initLatest();
		initEvent2();
		initMarqueeN();
	}
	initProduct();
	if($("floatBox")){
		$("floatBox").style.display = "";
	}
	if($("tb1")){
		initEvent1();
	}
	if($("latest")){
		initLatest();
	}
	if(typeof pageInfo != 'undefined'){
		updateHits();
		if($("pinyin2")){
		    getPYContent();
			new letterCategory("pinyin2").init(true);
			new letterCategory("pinyin").init();
		}
	}
	if(typeof isReport != 'undefined'){
		getPYContent();
		new letterCategory("pinyin").init();
	}
	
	if(typeof product != "undefined"){
		if($("dyTitle")){
			new activeTab("dyTitle", "dyContent").init(1, 0, "on", "li", "div");
		}
		if($("wqTitle")){
			new activeTab("wqTitle", "wqContent").init(1, 1, "on", "li", "div");
		}
	}
	if(typeof noScroll == 'undefined'){
		initScroll();
	}
	if(typeof news != 'undefined'){
		new activeTab("fvtitle", "fvcontent").init(0, 0, "current", "div", "ul");
	}
	if(typeof ebook != 'undefined'){
		initECategory();
	}
	if(typeof SI != 'undefined'){
		initScrollImgs();
	}
	if(typeof kws != "undefined"){
		setKeywords(kws);
	}
	if($("ifmtitle")){
		new activeTab("ifmtitle", "ifmcontent").init(0, 0, "current", "div", "ul");
	}
	
	if(typeof floatBox != 'undefined' && floatBox){
		setTimeout(function(){
			if(f_isMin == false){
				f_min($("imgMin"));
			}
		}, 10000);
	}
	
	if($("frmLogin") && $("lblUser")){
		getUserInfo();
	}
};
