if(!BMObjs){var BMObjs=[];};
if(!BMObjs["suggest"]) BMObjs["suggest"]=[];

var Suggest = Class.eventClass();

Object.extend (Suggest.prototype, {
	id:"",
	suggestURL:"",
	suggestTemplate:"",
	templateObj:"",
	showAirport: true,
	language:"zh",
	suggestStatus: 0,  
	localSuggestData: false,
	f_c:false,
	f_ac: "",
	f_r:false,
	initialize: function(id) {
		this.id = id;
		this.objType = "suggest";
		BMObjs["suggest"].push(this);
	},
	attach: function(f_c) {
		this.f_c = $(f_c);
		this.f_cT = this.f_c.extendBox;
		this.f_ac = this.f_c.value;
		this.f_aT(0);	
		if (!this.f_bj) {
			if (this.suggestTemplate!="") {
				this.f_bj = TrimPath.parseTemplate(this.suggestTemplate);
			} else {
				this.f_bj = TrimPath.parseTemplate(this.templateObj);
			}
		}
		Event.observe(this.f_c, "keyup", this.f_cN.bindAsEventListener(this));
		Event.observe(this.f_c, "keydown", this.f_cN.bindAsEventListener(this));
		Event.observe(this.f_c, "focus", this.f_ff.bindAsEventListener(this));
		Event.observe(this.f_c, "blur", this.f_fA.bindAsEventListener(this));

	},
	f_fA: function() {
		if (Prototype.Browser.IE && document.activeElement.tagName.toUpperCase()=="IFRAME") {
			this.f_c.f_ap = true;
      this.f_c.focus();
			return;
		}
		if (this.f_r)
		 	this.f_an(true);
		else {
			}
	},
	f_ff: function() {

	},
	f_cN: function(event) {
		var f_gL = event.keyCode;
		switch (f_gL) {
			case 40: 
				if (event.type=="keyup" && this.f_r) this.f_az(event, "next");
				break;
			case 38: 
				if (event.type=="keyup" && this.f_r) this.f_az(event, "prev");
				break;
			case 27: 
				if (event.type=="keyup") this.f_an(false);
				break;
			case 13: 
				if (event.type=="keyup") {
					if (this.suggestStatus == 2) this.f_an(true);
					else this.f_dp();
				}
				break;
			default: 
				if (event.type=="keydown") {
					if (this.f_ac.strip()=="") this.f_an(false);
				}
				else 
					if (this.f_c.value.strip()!="") {
						this.f_dp();
					} else {
						this.f_an(false);
					}
		}
	},
	f_aT: function(f_y) {
		if (f_y!=this.suggestStatus) {
			this.f_c.removeClassName("sstatus" + this.suggestStatus);
			this.suggestStatus = f_y;  
			this.f_c.addClassName("sstatus" + f_y);
		}
	},
	f_an: function(f_bv) {
		if (f_bv && this.f_r) {
			this.f_c.country = this.f_r.readAttribute("country");
			this.f_c.setValue(this.f_r.readAttribute("key"));
			this.f_c.setValidate(true);
		}   
		this.f_ac = this.f_c.value;
		this.f_aT(0);
		this.f_c.fillExtendBox();
		SystemLog.add("clear");
		this.f_r = false;
	},
	f_dp: function() {
		if (this.f_ac != this.f_c.value) {
			this.f_aT(1);	
			this.f_er();
		}
	},  
	f_er: function() {
		var _URL = this.suggestURL;
		if (!_URL) return;
		this.f_ac = this.f_c.value.replace(/[~!@#\$%\^&\*\(\)_\+<>\?:\\\\"\|\{\}`,\.\/;'\\\{\}]+/ig,"");
		var f_l = "lang=" + this.language + "&q=" + encodeURI(this.f_ac);
		f_l += ("&sa=" + (this.showAirport?"true":"false"));
		if (this.f_co && this.f_co.transport.readyState!=4 ) {
			try {
				this.f_co.transport.abort();
			} catch (e) {
			}
		}
		this.f_co = new Ajax.Request(
			_URL,
			{
				crossSite : true,
				removeScriptElement : true,
				method:	"get",
				parameters: f_l + "&callback=window.ObjectPool."+this.id+".f_es",
onException: showError
		 	});
	},
	f_es: function(f_aN) {
		if(f_aN.userInput.toUpperCase() != this.f_c.value.replace(/[~!@#\$%\^&\*\(\)_\+<>\?:\\\\"\|\{\}`,\.\/;'\\\{\}]+/ig,"").toUpperCase()) return;
		var re = new RegExp("("+this.f_ac+")","ig");
		$A(f_aN.result).each(function(f_aZ){
			f_aZ.display = f_aZ.display.replace(re, function($0,$1){
				return "<span class=\"keystring\">"+$1+"</span>";
			});
		});
		f_df = f_aN;
		f_df.showAirport = this.showAirport;
		var f_A = this.f_bj.process(f_df);
		var f_cT = this.f_c.fillExtendBox(f_A);
		f_cT.getElementsBySelector("li").each(function (f_aX) {				
			if (f_aX.readAttribute("type")!="1" && f_aX.readAttribute("type")!="2" ) {
				Event.observe(f_aX, "mouseover" , this.f_az.bindAsEventListener(this,false,f_aX));
			  Event.observe(f_aX, "click" , this.f_an.bind(this,true));
			}
		}.bind(this));
		this.f_az(false, f_cT.down("li"));
		this.f_aT(2);
		if (f_df.result.length == 0 ) this.f_c.setValidate(false);

	},
	f_az: function(event, f_P, f_aR) {
		var f_e;
		if (event && !f_P) { 
			Event.stop(event); 
			f_e = f_aR; 
		}	else if (f_P == "next") f_e = this.f_r.next();
		else if (f_P == "prev") f_e = this.f_r.previous(); 
		else if (typeof(f_P) == "object") f_e = f_P;
		if (typeof(f_e) == "undefined" || f_e==null) {
				var f_cI = this.f_r.up().immediateDescendants();
				f_e = f_cI[(f_P=="prev")?(f_cI.length - 1):0];
		} 	
		this.f_aY();
		this.f_r = f_e;
		if (f_e.readAttribute("type") == "1" || f_e.readAttribute("type") == "2") {
			this.f_az(event, (f_P=="prev")?"prev":"next");
			return;
		}	else if (f_e.readAttribute("type") == "0") {
			this.f_r = false;
		} else {
			f_e.addClassName("onhover");
		}
	},
	f_cg: function(event, f_P) {
		var f_e;
		if (f_P) f_e = f_P;
		else var f_e = Event.element(event);
		f_e.removeClassName("onhover");
	},
	f_aY: function() {
		if(this.f_r) this.f_cg(false, this.f_r);
	}
});