﻿var Learn=window.Learn||{};
Learn.namespace=function(_1){
	if(!_1||!_1.length){return null;}
	var _2=_1.split(".");
	var _3=Learn;
	for(var i=(_2[0]=="Learn")?1:0;i<_2.length;++i){
		_3[_2[i]]=_3[_2[i]]||{};
		_3=_3[_2[i]];
	}
	return _3;
};
Learn.namespace("util");
Learn.namespace("mod");

if(!Learn.util.Event){
	Learn.util.Event = function(){
		return{
			addListener: function(obj,sAction,fn){
				if(obj.addEventListener){
					obj.addEventListener(sAction,fn,false);
				}else{
					if(obj.attachEvent){
						obj.attachEvent("on"+sAction,fn);
					}
				}
			},
			removeListener:function(obj,sAction,fn){
				if(!fn||!fn.call){
					return false;
				}
				if(obj.removeEventListener){
					obj.removeEventListener(sAction,fn,false);
				}else{
					if(obj.detachEvent){
						obj.detachEvent("on"+sAction,fn);
					}
				}
				return true;
			},
			stopEvent:function(ev){
				this.stopPropagation(ev);
				this.preventDefault(ev);
			},
			stopPropagation:function(ev){
				if(ev.stopPropagation){
					ev.stopPropagation();
				}else{
					ev.cancelBubble=true;
				}
			},
			preventDefault:function(ev){
				if(ev.preventDefault){
					ev.preventDefault();
				}else{
					ev.returnValue=false;
				}
			}
		}
	}
	();
}

Learn.util.hasClass = function(objNode,sClassName){
	var pattern=new RegExp("(^|\\s)"+sClassName+"(\\s|$)");
	return pattern.test(objNode['className'])
};
Learn.util.removeClass = function(objNode,sClassName){
	var pattern=new RegExp("(^|\\s)"+sClassName+"(\\s|$)");
	var c=objNode['className'];
	if(pattern.test(c)){
		objNode['className']=c.replace(pattern,' ');
	}
};
Learn.util.addClass = function(objNode,sClassName){
	var pattern=new RegExp("(^|\\s)"+sClassName+"(\\s|$)");
	var c=objNode['className'];
	if(!pattern.test(c)){
		objNode['className']=[c,sClassName].join(' ');
	}
};
Learn.util.calculateLocation = function(oInput,na){
	var wb = 0;
	while (oInput) {
		wb += oInput[na];
		oInput = oInput.offsetParent;
	}
	return wb;
}
Learn.util.getHTTPObject = function(){
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
Learn.mod.tabs = function(sModuleName){
	this.sModuleName = sModuleName;
	this.sTabTag = "li";
	this.sClickTag = "a";
	this.iClickTagNum = 1;
	this.sOnClassName = "on";
	this.sTrigger = "click";
	this.oCurrTab = null;
	this.sSearchName = "";
	this.oSearch = null;
	this.sActionMode = "search";
	this.sIframeName = "";
	this.sContentTag = "div";
	this.sContentName = "";
	this.sContentTag2 = "div";
	this.sContentName2 = "";
	this.oCurrContent = null;
	this.oCurrContent2 = null;
	this.oContent = new Array(1);
	this.oContent2 = new Array(1);
};
Learn.mod.tabs.prototype.tabAction = function(e, oSelf){
	var objNode = e.srcElement||e.target;
	Learn.util.removeClass(oSelf.oCurrTab,oSelf.sOnClassName);
	if(this.sActionMode == "content"){
		Learn.util.removeClass(oSelf.oCurrContent,oSelf.sOnClassName);
		if(this.sContentName2.length>0){
			Learn.util.removeClass(oSelf.oCurrContent2,oSelf.sOnClassName);
		}
	}
	var oNode = objNode;
	var oContentNode;
	while(true){
		if(oNode.parentNode){
			oNode = oNode.parentNode;
			if(oNode.tagName.toLowerCase() == oSelf.sTabTag){
				oSelf.oCurrTab = oNode;				
				break;
			}
		}else
			break;
	}
	Learn.util.addClass(oSelf.oCurrTab,oSelf.sOnClassName);
	if(this.oSearch != null){
		if(typeof(objNode.href) != "undefined"){
			this.oSearch.changeSearchBar(objNode);
		}
	}
	if(this.sActionMode == "iframe"){
		var sLink = objNode.href||"";		
		if(sLink != ""){
			eval(this.sIframeName+".location='"+sLink+"'");
		}
	}
	if(this.sActionMode == "content"){
		var objTabs = document.getElementById(this.sModuleName).getElementsByTagName(this.sTabTag);
		var iTabLen = objTabs.length;
		for(i=0; i<iTabLen; i++){
			if(objTabs[i] == this.oCurrTab){
				Learn.util.addClass(oSelf.oContent[i],oSelf.sOnClassName);
				oSelf.oCurrContent = oSelf.oContent[i];
				if(this.sContentName2.length>0){
					Learn.util.addClass(oSelf.oContent2[i],oSelf.sOnClassName);
					oSelf.oCurrContent2 = oSelf.oContent2[i];
				}
				break;
			}
		}
	}
	objNode.blur();
	return false;
};
Learn.mod.tabs.prototype.initTabs = function(){
	if(this.sSearchName != "" && document.getElementById(this.sSearchName) != null){
		this.oSearch = new Learn.mod.oSearch();
	}
	var oTabsTemp = document.getElementById(this.sModuleName).getElementsByTagName(this.sTabTag);
	var sModuleNodeName = document.getElementById(this.sModuleName).nodeName;
	if(oTabsTemp!=null){
		var iTemp = 0;
		for(i=0; i<oTabsTemp.length; i++){
			var bFlag = true;
			var tempParent = oTabsTemp[i];
			while(bFlag){
				tempParent = tempParent.parentNode;
				if(tempParent.nodeName == sModuleNodeName)
					break;
			}
			if(tempParent.id==this.sModuleName){
				if(iTemp==0){
					objTabs = new Array(1);
				}
				objTabs[iTemp] = oTabsTemp[i];
				iTemp++;
			}
		}
	}
	var iTabLen = objTabs.length;
	var objContent = null;
	var objContent2 = null;
	if(this.sActionMode == "content"){
		var oContentTemp = document.getElementById(this.sContentName).getElementsByTagName(this.sContentTag);
		var iTemp = 0;
		if(oContentTemp!=null){
			for(i=0; i<oContentTemp.length; i++){
				if(oContentTemp[i].parentNode.id==this.sContentName){
					if(iTemp==0){
						objContent = new Array(1);
					}
					objContent[iTemp] = oContentTemp[i];
					iTemp++;
				}
			}
		}
		if(this.sContentName2.length>0){
			var oContentTemp = document.getElementById(this.sContentName2).getElementsByTagName(this.sContentTag2);
			var iTemp = 0;
			if(oContentTemp!=null){
				for(i=0; i<oContentTemp.length; i++){
					if(oContentTemp[i].parentNode.id==this.sContentName2){
						if(iTemp==0){
							objContent2 = new Array(1);
						}
						objContent2[iTemp] = oContentTemp[i];
						iTemp++;
					}
				}
			}
		}
	}
	for(i=0; i<iTabLen; i++){
		if(this.sActionMode == "content" && typeof(objContent[i]) != "undefined")
			this.oContent[i] = objContent[i];
		if(this.sActionMode == "content" && this.sContentName2.length>0 && typeof(objContent2[i]) != "undefined")
			this.oContent2[i] = objContent2[i];
		if(Learn.util.hasClass(objTabs[i],this.sOnClassName)){
			this.oCurrTab = objTabs[i];
			if(this.sActionMode == "content" && typeof(objContent[i]) != "undefined")
				this.oCurrContent = objContent[i];
			if(this.sActionMode == "content" && this.sContentName2.length>0 && typeof(objContent2[i]) != "undefined")
				this.oCurrContent2 = objContent2[i];				
		}
		var objClick = objTabs[i].getElementsByTagName(this.sClickTag);
		var iClickLen = objClick.length||0;
		if(iClickLen>1)
			iClickLen=this.iClickTagNum;
		for(j=0; j<iClickLen; j++){
			var self = this;
			objClick[j].order = (i+1);
			var oSelf=this;
			var fAction = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				self.tabAction(e,self);
				return false;
			}
			Learn.util.Event.addListener(objClick[j],this.sTrigger,fAction);
		}
	}
};
Learn.mod.oSearch = function(){
	this.sSubmitName = "搜尋";
	this.oForm = null;
	this.oInput=null;
	this.oKeyword = null;
	if(document.getElementById("searchkey") != null && document.getElementById("searchkey").getElementsByTagName("input").length>0)
		this.oInput = document.getElementById("searchkey").getElementsByTagName("input")[0];
	this.oArea = document.getElementById("searcharea")
	this.oHelp = document.getElementById("helpbtn");
	this.oImg = document.getElementById("searchicon");
	this.oCate = document.getElementById("searchcate");
	if(document.getElementById("searchbtn") != null && document.getElementById("searchbtn").getElementsByTagName("a").length>0)
		this.oSubmit = document.getElementById("searchbtn").getElementsByTagName("a")[0];
};
Learn.mod.oSearch.prototype.changeSearchBar = function(oNode){
	var sSearch = oNode.firstChild.nodeValue||"";
	sSearch = sSearch.replace("找",""); 
	var arrAct = oNode.href.split("*-");			
	var arrAct2 = arrAct[2].split("/");
	if(this.oForm != null) this.oForm.action = arrAct[0] + ".cfm";
	if(this.oImg != null) this.oImg.src = arrAct[1];
	if(this.oInput != null)	this.oInput.size = arrAct2[0];	
	if(this.oArea != null){
		if(arrAct2[1] == "1")
			this.oArea.style.display = "inline";
		else
			this.oArea.style.display = "none";
	}		
	if(this.oHelp != null){
		if(arrAct2[2] == "1")
			this.oHelp.style.display = "inline";
		else
			this.oHelp.style.display = "none";
		
	}
	if(this.oCate != null){
		if(arrAct2[3] == "1")
			this.oCate.style.display = "inline";
		else
			this.oCate.style.display = "none";
	}
	if(this.oSubmit != null)
		this.oSubmit.firstChild.nodeValue = sSearch + this.sSubmitName;
	if(this.oKeyword != null){
		if(arrAct2[4] == "1"){
			Learn.util.Event.removeListener(this.oKeyword.oInput,"keyup",this.oKeyword.fKeyPress);
			Learn.util.Event.addListener(this.oKeyword.oInput,"keyup",this.oKeyword.fKeyPress);
		}else{
			Learn.util.Event.removeListener(this.oKeyword.oInput,"keyup",this.oKeyword.fKeyPress);			
		}
	}
};
/*自動建議關鍵字*/
Learn.mod.keyword = function(oInput,oArea,sPanelName,sClassName){
	this.sPanelName = sPanelName;
	this.sPanelClassName = sClassName;
	this.sUrl = "/cfdocs/edu/search/auto_keyword.cfm";
	this.sOldKeyword = "";
	this.sADLink = "";
	this.oInput = oInput;
	this.oPanel = null;
	this.oPanelItems = new Array();
	this.oCurrItem = null;
	this.iCurrItemNum = -1;
	this.oArea = oArea;
	this.oHttp = Learn.util.getHTTPObject();
	this.fKeyPress = null;
	this.initKeyword();
};
Learn.mod.keyword.prototype.initKeyword = function(){
	var oDIV = document.createElement("DIV");
	oDIV.id = this.sPanelName;
	oDIV.className = this.sPanelClassName;
	oDIV.style.visibility = "hidden";	
	oDIV.style.left = Learn.util.calculateLocation(this.oInput,"offsetLeft") + "px";
	oDIV.style.top = Learn.util.calculateLocation(this.oInput,"offsetTop") + this.oInput.offsetHeight  -1 + "px";
	document.body.appendChild(oDIV);
	this.oPanel = oDIV;
	this.oInput.autocomplete = "off";
	var self = this;
	var fAction = function(e){
		if(e){
			Learn.util.Event.stopEvent(e);
		}
		self.actionKeyPress(e,self);
		return false;
	}
	this.fKeyPress = fAction;
	Learn.util.Event.addListener(this.oInput,"keyup",fAction);
	var fAction2 = function(e){
		if(e){
			Learn.util.Event.stopEvent(e);
		}
		self.hidePanel();
		return false;
	}
	Learn.util.Event.addListener(this.oInput,"blur",fAction2);
};
Learn.mod.keyword.prototype.actionKeyPress = function(e,oSelf){
	oActive = e.srcElement||e.target;
	var sKeyWord = oActive.value;
	switch(e.keyCode){
		case 13:
			if(oSelf.oCurrItem != null){
				if(Learn.util.hasClass(oSelf.oCurrItem,"on")){
					oSelf.showValue();
				}else if(Learn.util.hasClass(oSelf.oCurrItem,"adon")){
					oSelf.showAD();
				}
			}else{
				oSelf.showKeyword(sKeyWord);
			}
			break;
		case 38:
			oSelf.setSelectedItem(-1);
			break;
		case 40:
			oSelf.setSelectedItem(1);
			break;
		default:
			oSelf.showKeyword(sKeyWord);
	}
};
Learn.mod.keyword.prototype.showKeyword = function(sValue){
	if(sValue == ""){
		this.hidePanel();
	}else if(sValue != this.sOldKeyword){
		this.sOldKeyword = sValue;
		var url = this.sUrl + "?keyword="+encodeURI(sValue);
		if(this.oArea != null && typeof(this.oArea.options) != "undefined"){
			url += "&area="+this.oArea.options[this.oArea.selectedIndex].value;
		}
		this.oHttp.open("GET",url,true);
		var self = this;
		var fAction = function(e){
			if(e){
				Learn.util.Event.stopEvent(e);
			}
			self.handleHttpResponse(e,self);
			return false;
		}		
		this.oHttp.onreadystatechange = fAction;
		this.oHttp.send(null);
	}
};
Learn.mod.keyword.prototype.handleHttpResponse = function(e,oSelf){
	if (oSelf.oHttp.readyState == 4) {
		var  results = oSelf.oHttp.responseText.split("@@");
		if(results[1] == "false"){
		 	oSelf.hidePanel();
			return;
		}else{
			if(results[2] == ""){
				oSelf.hidePanel();
				return;
			}
			var arrKey = results[2].split(",");
			oSelf.displayPanel(arrKey,results[3]);
		}
	}
};
Learn.mod.keyword.prototype.hidePanel = function(){
	this.oPanel.style.visibility = "hidden";
	this.sOldKeyword = "";
	this.oPanelItems.length = 0;
	this.oCurrItem = null;
	this.iCurrItemNum = -1;
};
Learn.mod.keyword.prototype.displayPanel = function(arrValue,sAdString){
	while(this.oPanel.childNodes.length > 0){
		this.oPanel.removeChild(this.oPanel.childNodes[0]);
	}
	this.oPanelItems.length = 0;
	this.oCurrItem = null;
	this.iCurrItemNum = -1;
	var iCnt = arrValue.length || 0;
	if(iCnt > 0){
		for(i=0; i<iCnt; i++){
			var arrText = arrValue[i].split("^");
			var oItem = document.createElement("DIV");
			var oPic = document.createElement("I");
			oPic.innerHTML = "<img src='/image/school/icon_point.gif' width='3' height='3' hspace='4' vspace='4' align='middle'>";
			oItem.appendChild(oPic);
			var oContant = document.createElement("SPAN");
			oContant.setAttribute("id","_item_"+(i+1));
			var oText = document.createElement("B");
			oText.innerHTML = arrText[0];
			oContant.appendChild(oText);
			var oNum = document.createElement("u");
			oNum.innerHTML = "&nbsp;("+arrText[1]+"筆)";
			oContant.appendChild(oNum);
			var self = this;
			var fmOver = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				var oNode =  e.srcElement||e.target;
				self.mover(oNode,self,"on");
				return false;
			}
			Learn.util.Event.addListener(oContant,"mouseover",fmOver);
			var fmOut = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				var oNode =  e.srcElement||e.target;
				self.mout(oNode,self,"on");
				return false;
			}
			Learn.util.Event.addListener(oContant,"mouseout",fmOut);
			var fmDown = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				self.showValue();
			}
			Learn.util.Event.addListener(oContant,"mousedown",fmDown);
			oItem.appendChild(oContant);
			this.oPanel.appendChild(oItem);
			this.oPanelItems[this.oPanelItems.length] = oContant;
		}
		if(sAdString != ""){
			var arrText = sAdString.split("^");
			var oItem = document.createElement("DIV");
			var oPic = document.createElement("I");
			oPic.innerHTML = "<img src='/image/school/icon_point.gif' width='3' height='3' hspace='4' vspace='4' align='middle'>";
			oItem.appendChild(oPic);
			var oContant = document.createElement("SPAN");
			oContant.setAttribute("id","_ad_1");
			var oText = document.createElement("A");
			oText.setAttribute("href",arrText[1]);
			oText.setAttribute("target","_blank");
			oText.innerHTML = arrText[0];
			oContant.appendChild(oText);
			var oADPic = document.createElement("u");
			oADPic.innerHTML = "<img src='/image/school/icon_ad.gif' width='17' height='8' align='top'>";
			oContant.appendChild(oADPic);
			var self = this;
			var fmOver = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				var oNode =  e.srcElement||e.target;
				self.mover(oNode,self,"adon");
				return false;
			}
			Learn.util.Event.addListener(oContant,"mouseover",fmOver);
			var fmOut = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				var oNode =  e.srcElement||e.target;
				self.mout(oNode,self,"adon");
				return false;
			}
			Learn.util.Event.addListener(oContant,"mouseout",fmOut);
			var fmDown = function(e){
				if(e){
					Learn.util.Event.stopEvent(e);
				}
				self.showAD();
			}
			Learn.util.Event.addListener(oContant,"mousedown",fmDown);
			oItem.appendChild(oContant);
			this.oPanel.appendChild(oItem);
			this.oPanelItems[this.oPanelItems.length] = oContant;
		}
		this.setWindowOnResize();
		this.oPanel.style.visibility="visible";
	}else{
		this.hidePanel();
	}
};
Learn.mod.keyword.prototype.mover = function(oNode,oSelf,sClassName){
	if(oSelf.oCurrItem != null){
		if(Learn.util.hasClass(this.oCurrItem,"on")){
			this.mout(this.oCurrItem,self,"on");
		}else if(Learn.util.hasClass(this.oCurrItem,"adon")){
			this.mout(this.oCurrItem,self,"adon");
		}
	}	
	while(oNode.tagName != "SPAN"){
		if(oNode.parentNode)
			oNode = oNode.parentNode;
	}
	for(i=0;i<oSelf.oPanelItems.length;i++){
		if(oNode.id == oSelf.oPanelItems[i].id){
			oSelf.iCurrItemNum = i;
			break;
		}
	}
	oSelf.oCurrItem = oNode;
	Learn.util.addClass(oSelf.oCurrItem,sClassName);
	return false;
};
Learn.mod.keyword.prototype.mout = function(oNode,oSelf,sClassName){
	while(oNode.tagName != "SPAN"){
		if(oNode.parentNode)
			oNode = oNode.parentNode;
	}
	Learn.util.removeClass(oNode,sClassName);
	oSelf.oCurrItem = null;
	oSelf.iCurrItemNum = -1;
	return false;
};
Learn.mod.keyword.prototype.showValue = function(){
	if(this.oCurrItem!=null){
		var oValue = this.oCurrItem.getElementsByTagName("B");
		var iCount = oValue.length||0;
		if(iCount>0){
			this.oInput.value = oValue[0].firstChild.nodeValue;
			this.hidePanel();
		}
	}
};
Learn.mod.keyword.prototype.showAD = function(){
	if(this.oCurrItem!=null){		
		var oValue = this.oCurrItem.getElementsByTagName("A");
		var iCount = oValue.length||0;
		if(iCount>0){
			if(typeof(oValue[0].href) != "undefined"){
				window.open(oValue[0].href);
			}
			this.hidePanel();
		}
	}
};
Learn.mod.keyword.prototype.setSelectedItem = function(iStep){
	if(this.oCurrItem == null && iStep<0)
		return;
	if(this.oPanelItems.length == (this.iCurrItemNum+1) && iStep>0)
		return;
	var iNext = this.iCurrItemNum + iStep;
	var self = this;
	if(this.oCurrItem != null){
		if(Learn.util.hasClass(this.oCurrItem,"on")){
			this.mout(this.oCurrItem,self,"on");
		}else if(Learn.util.hasClass(this.oCurrItem,"adon")){
			this.mout(this.oCurrItem,self,"adon");
		}
	}	
	this.iCurrItemNum = iNext;
	this.oCurrItem = this.oPanelItems[iNext];
	var oValue = this.oCurrItem.getElementsByTagName("B");
	var iCount = oValue.length||0;
	if(iCount>0){
		this.mover(this.oCurrItem,self,"on");
	}else{
		this.mover(this.oCurrItem,self,"adon");
	}
};
Learn.mod.keyword.prototype.setWindowOnResize = function(){
	this.oPanel.style.left = Learn.util.calculateLocation(this.oInput,"offsetLeft") + "px";
	this.oPanel.style.top = Learn.util.calculateLocation(this.oInput,"offsetTop") + this.oInput.offsetHeight  -1 + "px";
}
