/**
 * @author CHX
 * @namespace nhn.AutoCompleteExt
 * @version 090722
 */
nhn.AutoCompleteExt = jindo.$Class({
	$init: function(){
		
		//init inputbox
		var _input = this.input.$value();
		_input.focus();
		_input.select();
		
		this._elshowEn = this._getFrameElement("bl_english");
		this._elshowPn = this._getFrameElement("bl_pinyin");
		this._elShowAll = this._getFrameElement("bl_all");
		jindo.$Fn(this._viewEnglishOnly, this).attach(this._elshowEn, "click");
		jindo.$Fn(this._viewPinyinOnly, this).attach(this._elshowPn, "click");
		jindo.$Fn(this._viewAll, this).attach(this._elShowAll, "click");
		jindo.$Fn(this._initInputBox, this).attach(_input, "mousedown");
		
		jindo.$Fn(this._doSearch, this).attach(this.input, "keyup");
		
		this.suggest.hide();

	},
	
	_texts: {
		txt_showEN: "Show English",
		txt_showPY: "Show Pinyin",
		txt_fw: "View English Only",
		txt_bw: "View PinYin Only",
		txt_mw: "middle_word"
	},
	
	_initInputBox: function() {
		var _input = this.input.$value();
		var oTopPage = jindo.$('isMain');	// 判断当前是否为首页，在main.ftl最下端加了一个隐藏域其id为“isMain”。相应的BTS为：NGDN-1488
		if(oTopPage && oTopPage.value == 'topPage') {
			var vDefault = _input.defaultValue;
			if(_input.value == vDefault) {
				_input.value = "";
				_input.style.color = "";
			}
		}
	},
	
	getInputValue: function(){
		return this.input.text();
	},
	
	//提交查询 （回车）
	_doSearch: function(e){
		var key = e.key();
		if(key.enter){
			this.hide();
			this.fireEvent("actSubmit");
		}
	},

	//解析模版
	_checkTemplate: function(){
		this.listbox_fw = "";
		this.listbox_bw = "";
		this.template_fw = "";
		this.template_bw = "";
		this.template_dm = "";
		
		var opt = this._options;
		if (this.suggest._element.contentWindow.document.getElementById('atcmp').innerHTML.indexOf("@txt@") > -1) {
			this.elFrameDiv = jindo.$Element(this.suggest._element.contentWindow.document.getElementById('atcmp'));

			jindo.$Fn(this.onMouseOver, this).attach(this.elFrameDiv, "mouseover");
			jindo.$Fn(this._setLinkMove, this).attach(this.elFrameDiv, "click");
			
			this.listbox_fw = this._findElement("." + opt.listbox_fw, this.elFrameDiv.$value())[0];
			this.listbox_fw = this.listbox_fw ? jindo.$Element(this.listbox_fw) : null;
			this.listbox_bw = this._findElement("." + opt.listbox_bw, this.elFrameDiv.$value())[0];
			this.listbox_bw = this.listbox_bw ? jindo.$Element(this.listbox_bw) : null;
			
			//Intro层
			this.$introDiv = this.suggest._element.contentWindow.document.getElementById('intro_div');
			
			if (this.listbox_fw) this.template_fw = this.listbox_fw.html().replace(/^\s+|\s+$/g, "");
			if (this.listbox_bw) this.template_bw = this.listbox_bw.html().replace(/^\s+|\s+$/g, "");
			
			this.bCheckTemplate = true;
			
			// 자동완성
			this.elMoreBtn = this._findElement(".run_view", this.elFrameDiv.$value())[0];
			this.elHelpImg1 = this.suggest._element.contentWindow.document.getElementById('help_tooltip1');
			this.elHelpImg2 = this.suggest._element.contentWindow.document.getElementById('help_tooltip2');
			this.elHelpImg3 = this.suggest._element.contentWindow.document.getElementById('help_tooltip3');
			this.elFunoffBtn = this._findElement(".funoff", this.elFrameDiv.$value())[0];

		} else {
			this.suggest._element.src = this.suggest._element.src;
		}
	},
	
	_setLinkMove : function(e){
		this.input.$value().focus();
		this.input.$value().blur();
		
		this._setParameter(e);
		this.watcher.stop();
		this.hide();
		this.input.$value().value = this._getElementLast(this.selected).text();
		this.fireEvent('actSubmit');
	},
	
	//显示intro
	_showIntro: function(){
		if (!this.$introDiv) {
			this._checkTemplate();
		}
		this._elshowPn.hide();
		this._elshowEn.hide();
		this.elFrameDiv.show();
		this.listbox_fw.hide();
		this.listbox_bw.hide();
		jindo.$Element(this.$introDiv).show();
		this.show();
	},
	
	_setClickTriangle : function(e){
		if (!this.listbox_fw) this._checkTemplate();
		
		if(this.option("disabled")){
			this._showIntro();//点击箭头显示Intro
			this._setTriangleImg("show");
			if(this.input.$value().value){
				this.nListCnt = 0;
				this._searchStatus = "Triangle_false";
				this.onInput();
				
				e.stop();
			} else {
				this._showIntro();
//				var elUse=this._getFrameElement('atcmpStart');
//				elUse.show();
//				this.show();
				e.stop();
			}

		}else{
			if(this.suggest.visible()){
				this.hide();
			}else if(this.input.$value().value){
				this._searchStatus = "Triangle_true";
				this.onInput();
			}else{
				this._showIntro();
			}
			e.stop();
		}
	},
	
	_setClickDisplay: function(e){
		if (!jindo.$Element(e.element)) {
			this.hide();
			this.fireEvent('onBlur');
			return false;
		}
		if (jindo.$Element(e.element).attr('id') == this.input.attr('id')) {
			if (this.input.$value().value) {
				if (this.suggest.visible()) {
					this.hide();
				} else {
					this.onInput();
				}
			} else {//显示默认文字(Intro)
				if (this.suggest.visible()) {
					this.hide();
				} else {
					this._showIntro();
				}
			}
			
		} else {
			this.hide();
			this.fireEvent('onBlur');
		}
	},
	
	//重写paint方法
	paint: function(){
		if (this._options.disabled) {
			this._showIntro();
			return false;
		}
		if (!this.listbox_fw) this._checkTemplate();
		if (!this.listbox_fw) return false;
		//
//		this._setFunctionOffImg("off");
		
		var list_fw = [];
		var list_bw = [];
		var list_dm = [];
		
		var _v = this.getInputValue();
		var isEnChar = /^[\w ]+$/i.test(_v);//是否为英文 
		
		var aDataFw = this._data_bw;//第一组
		var aDataBw = this._data_err;//第二组
		var aDataFwErr = this._data_err;
		var aDataBwErr = this._data_bw_err;
		
		var opt = this._options;
		var self = this;
		this.nListCnt = 0;
		
		if (!this.sListMode) this.sListMode = "fw";
		var nFWListCnt = Math.min(aDataFw.length, 5);
		var nBWListCnt = Math.min(aDataBw.length, 10);
		if (isEnChar||(nFWListCnt&&nBWListCnt&&aDataFw[0].encoded==aDataBw[0].encoded)) {//去除重复项
			nFWListCnt = 0;//英文输入的时候，不显示拼音列表
		} else {
			nBWListCnt = Math.min(nBWListCnt, 5);
		}

		if (nFWListCnt > 0) {
			this.listbox_fw.show();
			for (var i = 0; i < nFWListCnt; i++) {
				list_fw[list_fw.length] = this.setTplReplace(this.template_fw, aDataFw[i], aDataFwErr[i], false);
			}
			this.nListCnt++;
			this.listbox_fw.html(list_fw.join(""));
		} else {
			this.listbox_fw.hide();
			this.listbox_fw.html("");
			
		}
		
		if (nBWListCnt > 0) {
			this.listbox_bw.show();
			for (var i = 0; i < nBWListCnt; i++) {
				list_bw[list_bw.length] = this.setTplReplace(this.template_bw, aDataBw[i], aDataBwErr[i], !isEnChar);
			}
			this.listbox_bw.html(list_bw.join(""));
			if (nFWListCnt > 0) {
				this.listbox_bw.className('_resultBox2 other');
			} else {
				this.listbox_bw.className('_resultBox2');
			}
			this.nListCnt++;
		} else {
			this.listbox_bw.hide();
			this.listbox_bw.html("");
		}
		
		this.nTotalListCnt = nFWListCnt + nBWListCnt;
		if (this.nTotalListCnt > 0) {
			//_getFrameElement将列表区内的有ID的DIV全部HIDE
			var elList = this._getFrameElement("atcmp");
			elList.show();
			//在有内容且是英文的时候显示
			this._elshowEn[isEnChar&&nFWListCnt?"show":"hide"]();
			this._elshowPn[isEnChar&&nFWListCnt?"show":"hide"]();
			this.show();
			this._searchStatus = "";
		} else {
			this.hide();
			this._showIntro();
//			this._setStatusLayer();
		}

		return this;
	}
}).extend(nhn.AjaxSuggestUS);

////////////////////////////////////////

document.domain = "nciku.cn"
//子页加载完成时候调用
function createSmartSearch() {	
	if(typeof window.smartSearch != "undefined") return;
	window.smartSearch = new nhn.AutoCompleteExt("de_searchboxQuery", "autoFrame", {
		url: "http://ac.nciku.cn/ac/select",		
		//url: SPS.autoCompleteURL,
		sFromName: "searchForm",
		triangleBtn: "cm_searchArrow",
		sListMaxLength: 45,
		sListMaxLength3: 35,
		request_type: "jsonp",
		request_data: {
			q: "{query}",
			wt: "json"
			//st: "011",
			//r_format: "jsonp",
			//q_enc: "UTF-8",
			//q_extra: "0",
			//r_enc: "UTF-8",
			//r_unicode: "0",
			//r_escape: "1",
			//r_cache: "5"
		}
	}).attach({
		actSubmit: function(e){
			if(typeof doSearch == "function") doSearch.call(this, e);
			if(typeof SPS.__inst.de_bottomsearch == "object" && typeof SPS.__inst.de_bottomsearch._requestSearch == "function") SPS.__inst.de_bottomsearch._requestSearch(e);
			if(typeof SPS.__inst.de_searchbox == "object" && typeof SPS.__inst.de_searchbox._requestSearch == "function") SPS.__inst.de_searchbox._requestSearch(e);
		}
	});
}

//ONDOMREADY的时候调用
function smartSearchInit(x) {
	if(typeof window.smartSearch != "undefined") return;
	var _frm = jindo.$("autoFrame");
	if (_frm) {
		_frm.src = "/html/reatcmp.html?"+Math.random();
	}
}
