var ie = (document.all) ? true : false;
var $$ = function (id) {
	return document.getElementById(id);
};
var C = {//原Class
	c: function() {//原create
		return function() { this.i.apply(this, arguments); }
	}
}
String.prototype.trim=function(){
　　    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function EX(destination, source) {//原Extend
	for (var property in source) {
		destination[property] = source[property];
	}
}

function EA(list, fun){
	var len;
	for (var i = 0, len = list.length; i < len; i++) { if(list.length<len){i--;len=list.length}  fun(list[i], i);}
};

function ame(obj,eventName,fun){//原addMyEvent
	if(ie)
		obj.attachEvent("on"+eventName,fun);
	else
		obj.addEventListener(eventName,fun,false);
};
function  B(object, fun) {//原Bind
	return function() {
		return fun.apply(object, arguments);
	}
}
//var TR = {//原Try
//  t: function() {//原these
//    var returnValue;
//    for (var i = 0, length = arguments.length; i < length; i++) {
//      var lambda = arguments[i];
//      try {
//        returnValue = lambda();
//        break;
//      } catch (e) { }
//    }
//    return returnValue;
//  }
//};
//var AC = {
//  g: function() {//原getTransport
//    return TR.t(
//      function() {return new XMLHttpRequest()},
//      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
//      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
//    ) || false;
//  }
//};
var AutoComplete = C.c();
AutoComplete.prototype = {
	i: function(op){
		q = this;//q代表该对象
		q.op = {};
		EX(q.op, op || {});
		q.url = q.op.url;
		q.lUrl="";
		try{
			q.lUrl = $$lUrl+"s.gif";//日志提交地址 
			q.tp = $$type;//页面类型0代表网页搜索
			q.ps = $$position;//位置
		}catch(e){}
		q.u = 'liOutA';//原liOutClassName
		q.v = 'liOverA';//原liOverClassName
		q.ul = document.createElement("ul");
		q.uln = q.ul.childNodes;
		q.c = q.op.callClickBack;//原callClickBack
		q.t = $$(q.op.inputID);//原txt
		q.tv = q.t.value;//原txtValue
		q.o = co(q.t.parentNode);//原q.o
		q.la;//原listArray
		q.l = document.createElement("div");
		q.l.id = "liveDiv";
		q.l.className = "frameUi";
		q.t.parentNode.appendChild(q.l);
		q.l.appendChild(q.ul);
		var tempWidth = 37;
		var tempTop = 4;
		if (q.op.noTrigger) {
			tempWidth = 0;
			tempTop = 0;
		}
		q.lt = q.l.style;//sl表示l的样式
		q.lt.width = q.t.clientWidth + tempWidth + "px";
		q.lt.left = q.o[0] + "px";
		q.lt.top = q.o[1] + q.t.clientHeight + tempTop + "px";
		q.lt.position = "absolute";
		q.n = -1;
		q.al = 0;//原allNum
		q.hl();
		q.s();
	},
	cl: function(){//原createLi。创建li
		q.al = q.la.length;
		EA(q.la, B(q, function(arr){
				var li = document.createElement("li");
				li.className = q.u;
				q.ul.appendChild(li);
				li.id = "al_" + q.uln.length-1;
				var strong = document.createElement("strong");
				strong.innerHTML = arr.value.replace(q.t.value.trim(), q.t.value.trim() + "<font style='font-weight:bold;'>");
				strong.innerHTML += "</font>";
				li.appendChild(strong);
				if(arr.img!=""&&arr.img){
					var em = document.createElement("em");
					em.innerHTML += "<img src=\""+arr.img+"\" />";
					li.appendChild(em);
				}
		}));
		q.ae();
	},
	sl: function(){//原showLive。显示列表
		q.rl();
		q.cl();
		if(q.la.length>0)
			q.lt.display = "block";
	},
	gr: function(url){//原getRemote，请求后台。
//		q.ts = AC.g();
//		q.ts.open('post',url,"true");
//		q.ts.onreadystatechange = B(q,function(){
//    		if (q.ts.readyState == 4) {
//				q.o = co(q.t.parentNode);
//				q.lt.left = q.o[0] + "px";
//				q.la = eval(q.ts.responseText);
//				if(q.la)
//					q.sl(q.la);
//			}
//		});
//		q.ts.send(null);
		var head = document.getElementsByTagName("head")[0] || document.documentElement;
		var script = document.createElement("script");
		script.src = url+"&temp="+new Date().getTime();
		var delayKey = q.t.value;
		script.onload = script.onreadystatechange = function(){
				q.o = co(q.t.parentNode);
				q.lt.left = q.o[0] + "px";
				try{
					if(!this.readyState ||
							this.readyState === "loaded" || this.readyState === "complete"){
						q.la = callBackData;
						if(q.la && delayKey==q.t.value)
							q.sl(q.la);
						callBackData =null;
						script.onload = script.onreadystatechange = null;
						if ( head && script.parentNode ) {
							head.removeChild( script );
						}
					}
				}catch(e){}
				
		};
		head.insertBefore( script, head.firstChild );
	},
	fs: function(){//原forstart,输入框内容变化时调用
		if (q.t.value != "") {
			q.tv = q.t.value;
			q.gr(q.url+ encodeURIComponent(q.tv));
		}
		else{
			q.rl();
		}
	},
	s: function(){//原start,启动输入框监听，变化时调用forstart
		setInterval(B(q, function(){
			if (q.tv != q.t.value) {
				if(q.ci)
					q.ci = false;
				else
					q.fs();
				q.tv = q.t.value;
			}
		}), 100);
		
		ame(q.t, "click", B(q, function(event){
			spe(event);
//			q.mh();
		}));
	},
//	mh:function(){//新添morehide，隐藏morediv
//		var moreDiv =$("moreDivA");
//		if(moreDiv)
//			moreDiv.style.display = "none";
//	},
	gn: function(){//原getNowID，获取当前li中选中的ID
		EA(q.uln, B(q, function(li, index){
			if (li.className == q.v) 
				q.n = index;
		}));
	},
	ae: function(){//原addEvent，绑定事件
		q.n = -1;
		window.onresize = B(q, function(){
			q.o = co(q.t.parentNode);
			q.lt.left = q.o[0] + "px";
		});
		document.onkeydown = B(q, function(e){
				var e = window.event || e;
				switch (e.keyCode) {
					case 38:
						if (q.lt.display == "block") {
							q.gn();
							q.n--;
							if (q.n < 0) {
								q.n = q.al - 1;
							}
							q.p(e);
							q.chi();
						}
						break;
					case 40:
						if(q.lt.display=="block"){
							q.gn();
							q.n++;
							if (q.n >= q.al) {
								q.n = 0;
							}
							q.chi();
							q.p(e);
						}
						break;
					case 13:
						var liveDiv = q.l;
						q.rl();
						q.log();
						eval(q.c + "()");
					case 27:
						q.rl();
						q.p(e);
					default:
						break;
				}
		});
		EA(q.uln, B(q, function(li){
			ame(li, "mouseover", B(q, function(){
				for (var j = 0; j < q.uln.length; j++) {
					q.uln[j].className = q.u;
				}
				li.className = q.v;
			}));
			ame(li, "mouseout", B(q, function(){
				for (var j = 0; j < q.uln.length; j++) {
					q.uln[j].className = q.u;
				}
			}));
			ame(li, "click", B(q, function(){
				if (li.innerText)
					q.t.value = li.innerText;
				else {
					var clns = li.childNodes[0].childNodes;
					for (var i = 0; i < clns.length; i++) {
						if (i) {
							q.t.value += clns[i].textContent;
						}
						else {
							q.t.value = clns[i].textContent;
						}
					}
				}
				q.log();
				q.ci = true;
				q.rl();
				eval(q.c + "()");
			}));
		}));
		ame(document, "click", B(q, function(event){
			if (event.button == 0) 
				q.hl();
		}));
	},
	log:function(){
		if(q.lUrl){
			q.gn();
			var url = q.lUrl+"?n="+q.n+"&w="+ encodeURIComponent(q.tv)+"&p="+q.ps+"&t="+q.tp+"&temp="+new Date().getTime();
			var img=window["JK_LOG_S"+new Date().getTime()]=new Image();
			img.src = url;
		}
	},
	chi: function(){//原changeItem，修改li选中的样式
		EA(q.uln, B(q, function(li){
			li.className = q.u;
		}));
		var nowCld = q.uln[q.n];
		nowCld.className = q.v;
		if (nowCld.innerText) {
			q.t.value = nowCld.innerText;
		}
		else {
			var cNodes =  nowCld.childNodes[0].childNodes
			for (var i = 0; i < cNodes.length; i++) {
				var t = cNodes[i].textContent
				if (i == 0)
					q.t.value = t;
				else
					q.t.value += t;
			}
		}
		q.ci = true;
	},
	p:function(e){//阻止默认行为方法
		if(e.preventDefault){
			e.preventDefault();
		}else{
			e.returnValue = false;
		}
	},
	rl: function(){//原removeLive方法，删除live时调用
		q.ul.innerHTML = "";
		q.hl();
	},
	hl:function(){//隐藏live方法
		q.lt.display = "none";
	}
}

function co(element) {//获取元素位置方法
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
    }
	 while (element);
    return [valueL, valueT];
}
function spe(event){//阻止向上冒泡事件方法
		if (event.stopPropagation)
			event.stopPropagation();
		else
			event.cancelBubble = true;
}

//function showMoreA(obj,event){
//		var moreDiv =$("moreDivA");
//		var liveDiv = $("liveDiv");
//		if(liveDiv)
//			liveDiv.style.display = "none";
//		var s = moreDiv.style;
//	    ame(document,"click",function(){
//			s.display ="none";
//		});
//		if(s.display=="none"){
//			s.display ="block";
//			s.position = "absolute";
//			var pos = co(obj);
//			s.left =pos[0]+ "px";
//			s.top ="25px";
//		}else{
//			s.display ="none";
//		}
//		var e =event || window.event;
//		spe(e);
//}
