var bar={
	//配置说明：
	/*
	配置信息来自inputbar输入框后邻的隐藏输入框。其值为与配置文件结构相似的json格式。
	使用缺省值可不填相应键值对。
	sopen:搜索时是否弹出新窗口
	selopen:选择下拉列表时是否弹出新窗口
	shiftw:下拉列表的宽度偏移
	shiftl:下拉列表的左偏移
	shiftt:下拉列表的高度偏移
	serverurl:ajax服务端的路径
	button:是否显示按钮。
	defreline:初始化文字是否换行。
	defthrline:初始化文字是否是短文字。短文字三行
	showdeflist:第一次是否显示下拉提示列表
	shiftresizel:下拉列表在重设大小时的偏移
	*/
	config:{sopen:true,selopen:false,shiftw:0,serverurl:"/AutoSearch/ajax.aspx",shiftl:0,shiftt:0,button:false,defreline:true,defthrline:false,showdeflist:true,shiftresizel:0}
	,
	inputbar:null
	,
	rooturl:""
	,
	post:null
	,
	startinput:false
	,
	init:function(key){
	    $("#btnFSearch").remove();
		this.inputbar=$("#"+key);
		this.loadConfig();
		this.inputbar.css({backgroundColor:"#F2F2F2",paddingLeft:"3px",color:"#999"});
		//if(this.inputbar.val()=="")
		//    this.inputbar.val("搜索类别或品牌");
		var _this=this;
		this.inputbar.click(function(){if(!_this.startinput){this.select();$(this).css("color","black");_this.showdefaultList();_this.startinput=true;}});
		var xy=this.getElementPos(key);
		var bar_width=this.inputbar.css("width").replace("px","")*1;
		this.inputbar.mousedown(function(){return false;});//已删除bar.showdefaultList();
		this.post=$("<form style='margin:0px;margin-left:4px;display:inline' action='http://so.pcpop.com/cgi-bin/pdt' method='GET' target='_blank'><input type='submit' "+(this.config.button?"":" style='width:0px;height:0px;"+($.browser.msie?"":"display:none;")+"'")+" value='查  找' id='JQuerysubmitBut'/><input type='hidden' name='query' id='query' value=''/></form>");
		this.inputbar.after(this.post);
		if(this.inputbar.val()!="搜索类别或品牌")
			this.inputbar.next().find("#query").val(this.inputbar.val());
		if($.browser.msie)
		{
			this.inputbar.keydown(function(){bar.doselect(null);});
			this.inputbar.keyup(function(){bar.doinput(null);});
			var barw=bar_width-1;
			if(window.clientInformation.appVersion.indexOf("MSIE 6.")==-1)
				barw=bar_width;
			this.container.css({
			width:(this.config.shiftw+barw)+"px",
			top:xy.y+20+this.config.shiftt,
			left:xy.x+this.config.shiftl
			});
		}
		else
		{
			this.container.css({
			width:(this.config.shiftw+bar_width-3)+"px",
			top:xy.y+20+this.config.shiftt,
			left:xy.x-2+this.config.shiftl
			});
			this.inputbar.attr("onkeydown","bar.doselect(event)");
			this.inputbar.attr("onkeyup","bar.doinput(event)");
		}
		window.onresize=function(){_this.resetLoc(key);};
		this.container.hide();
		this.container.each(function(i){this.onselectstart=function(){return false;}});
		var container=this.container;
		$("body").mouseup(function(){container.hide()});
		$("body").append(this.container);
	},
	resetLoc:function(key){
	    var xy=this.getElementPos(key);
	    var bar_width=this.inputbar.css("width").replace("px","")*1;
	    if($.browser.msie)
		{
			var barw=bar_width-1;
			if(window.clientInformation.appVersion.indexOf("MSIE 6.")==-1)
				barw=bar_width;
			this.container.css({
			width:(this.config.shiftw+barw)+"px",
			top:xy.y+20+this.config.shiftt,
			left:xy.x-2+this.config.shiftresizel
			});
		}
		else
		{
			this.container.css({
			width:(this.config.shiftw+bar_width-3)+"px",
			top:xy.y+20+this.config.shiftt,
			left:xy.x-2+this.config.shiftresizel
			});
		}
	},
	loadConfig:function(){
		var json=null;
		try{json=eval("("+this.inputbar.next().val()+")");}catch(errr){return false};
		if(json==null)
			return false;
		for(o in this.config){if(json[o]!=null){this.config[o]=json[o];}}
		return true;
	},
	container:$("<ul style='padding:0px;margin:0px;position:absolute;font-size:12px;background-color:#F2F2F2;border:1px solid #666;cursor:default;overflow:hidden;'></ul>")	,
	tempbar:"",
	doselect:function(e){
		var kc=0;
		if($.browser.msie)
			kc=event.keyCode;
		else
			kc=e.which; 
		if(!(kc==38 || kc==40 || kc==13))
			return false;
		var sel=null;
		this.container.find(".s_input_li").each(function(){if(this.style.color=="white"){sel=$(this);}})
		var first=this.container.find(".s_input_li:first");
		var last=this.container.find(".s_input_li:last");
		switch(kc)
		{
			case 38:
				if(sel==null)
					sel=last;
				else
					if(sel.attr("id")==first.attr("id"))
						sel=last;
					else
						sel=sel.prev();
				this.inputbar.val(sel.text());
				this.post.find("#query").val(this.inputbar.val());//查询隐藏框
				this.keydownSel(sel.get(0));
			break;
			case 40:
				if(sel==null)
					sel=first;
				else
					if(sel.attr("id")==last.attr("id"))
						sel=first;
					else
						sel=sel.next();
				this.inputbar.val(sel.text());
				this.post.find("#query").val(this.inputbar.val());//查询隐藏框
				this.keydownSel(sel.get(0));
			break;
		}
		if(sel!=null && kc==13)
			this.mousedownSel(sel.get(0));
		else if(sel==null && kc==13)
		{
		    if(this.inputbar.val()!="")
			{
				if(this.config.sopen){	
					this.post.find("#JQuerysubmitBut").focus();
				}else
				{
					if(window.top!=null)
						window.top.location.href="http://so.pcpop.com/cgi-bin/pdt?query="+this.post.find("#query").val();
					else
						window.location.href="http://so.pcpop.com/cgi-bin/pdt?query="+this.post.find("#query").val();
				}
			}
		}
		return true;
	},
	catchData:new Array(),
	getcatchData:function(key)
	{
		for(var i=0;i<this.catchData.length;i++)
			if(this.catchData[i].key==key)
				return this.catchData[i];
		return null;
	},
	doinput:function(e){
		var kc=0;
		if($.browser.msie)
			kc=event.keyCode;
		else
			kc=e.which; 
		if(kc==38 || kc==40)
			return false;
		if(this.tempbar==this.inputbar.val())
			return false;
		this.post.find("#query").val(this.inputbar.val());//查询隐藏框
		this.tempbar=this.inputbar.val();
		if(this.tempbar.length==0)
		{
			this.showdefaultList();
			return true;
		}
		var cjson=this.getcatchData(this.tempbar);
		if(cjson!=null)
		{
			this.showList(cjson.list);
			return true;
		}
		var self=this;
		$.getJSON(this.config.serverurl+"?rnd="+Math.random()+"&key="+encodeURIComponent(this.tempbar)+"&jsoncallback=?",function(j){
				try{
					var json=j;
					if(json)
					{
						self.rooturl=json.root;
						self.catchData[self.catchData.length]=json;
						self.showList(json.list);
					}
				}catch(err){}
			});
		return true;
	},
	showdefaultList:function()
	{
		if(!this.config.showdeflist)
			return false;
		if(this.inputbar.val().length!=0)
			return false;
		this.container.empty();
		if(this.config.defthrline)
		{
		    this.container.append($("<li style='background-color:#F2F2F2;padding:0px;margin:0px;list-style-type:none;white-space:nowrap;padding-left:4px;padding-top:4px;padding-bottom:4px;'>输入要查找的品牌<br/>或产品线如\"笔记本<br/>\",\"诺基亚手机\"等</li>"));
		    this.container.append($("<li style='background-color:#F2F2F2;padding:0px;list-style-type:none;margin:0px;padding-top:4px;padding-bottom:4px;'><a id='s_input_li_close' style='cursor:pointer;color:blue;float:right;margin-right:5px;'>关闭</a></li>"));
		}
		else
		{
		    if(this.config.defreline)
		    {
		        this.container.append($("<li style='background-color:#F2F2F2;padding:0px;margin:0px;list-style-type:none;white-space:nowrap;padding-left:4px;padding-top:4px;padding-bottom:4px;'>输入要查找的品牌或产品线<br/>如\"笔记本\"\"诺基亚手机\"等</li>"));
		        this.container.append($("<li style='background-color:#F2F2F2;padding:0px;list-style-type:none;margin:0px;padding-top:4px;padding-bottom:4px;'><a id='s_input_li_close' style='cursor:pointer;color:blue;float:right;margin-right:5px;'>关闭</a></li>"));
		    }else{
		        this.container.append($("<li style='background-color:#F2F2F2;padding:0px;margin:0px;list-style-type:none;white-space:nowrap;padding-left:4px;'><table cellpadding='0' cellspacing='0' style='width:100%;height:0px;'><tr><td>输入要查找的品牌或产品线&nbsp;如\"笔记本\"\"诺基亚手机\"等</td><td style='text-align:right;'><a id='s_input_li_close' style='cursor:pointer;color:blue;margin-right:5px;'>关闭</a></td></tr></table></li>"));
		    }
		}
		this.container.show();
		return false;//must.event can't to body
	}
	,
	showList:function(json){
		var self=this.container;
		var root=this.rooturl;
		this.container.hide();
		var me=this;
		this.container.empty();
		if(json){
			if(json.length>0){
				for(var i=0;i<json.length;i++)
					this.container.append($("<li id='s_input_li_"+i+"' class='s_input_li' style='padding:0px;margin:0px;list-style-type:none;white-space:nowrap;padding-left:4px;padding-top:4px;padding-bottom:4px;'><a href='"+root+json[i].ename+"/"+json[i].id+"_1.html' style='display:block;width:100%;height:100%;color:black;text-decoration:none;'>"+json[i].name+"</a></li>"));
				this.container.append($("<li style='padding:0px;list-style-type:none;margin:0px;padding-top:4px;padding-bottom:4px;'><a id='s_input_li_close' style='cursor:pointer;color:blue;float:right;margin-right:5px;'>关闭</a></li>"));
				this.container.find(".s_input_li").mouseover(function(){$(this).parent().find(".s_input_li").css({backgroundColor:"#F2F2F2",color:"black"});$(this).parent().find(".s_input_li").find("a").css({color:"black"});$(this).css({backgroundColor:"#36c",color:"white"});$(this).find("a").css({color:"white"})});
				this.container.find(".s_input_li").mousedown(function(){var item=json[this.id.replace('s_input_li_','')];if(me.config.selopen){$(this).find('a').attr("target","_blank");$(this).find('a').focus();}else{window.parent.location.href=(root+item.ename+"/"+item.id+"_1.html");}});
				this.container.find("#s_input_li_close").click(function(){self.hide();});
				this.container.show();
			}
		}},
		keydownSel:function(obj)
		{
			if(obj==null)
				return;
			$(obj).parent().find(".s_input_li").css({backgroundColor:"#F2F2F2",color:"black"});$(obj).parent().find(".s_input_li").find("a").css({color:"black"});$(obj).css({backgroundColor:"#36c",color:"white"});$(obj).find("a").css({color:"white"})
		},
		mousedownSel:function(obj)
		{
			if(obj==null)
				return;
			if(this.config.selopen){$(obj).find('a').attr("target","_blank");}else{window.parent.location.href=$(obj).find('a').attr("href");}
			$(obj).find('a').focus();
		}
		,
		getElementPos:function(elementId) { 
			var ua = navigator.userAgent.toLowerCase(); 
			var isOpera = (ua.indexOf('opera') != -1); 
			var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof 
			var el = document.getElementById(elementId); 
			if(el.parentNode === null || el.style.display == 'none')  
			{ 
				return false; 
			} 
			var parent = null; 
			var pos = []; 
			var box; 
			if(el.getBoundingClientRect)    //IE 
			{ 
				box = el.getBoundingClientRect(); 
				var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop); 
				var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); 
		 
				return {x:box.left + scrollLeft, y:box.top + scrollTop}; 
			} 
			else if(document.getBoxObjectFor)    // gecko 
			{ 
				box = document.getBoxObjectFor(el); 
		            
				var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0; 
				var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0; 
		 
				pos = [box.x - borderLeft, box.y - borderTop]; 
			} 
			else    // safari & opera 
			{ 
				pos = [el.offsetLeft, el.offsetTop]; 
				parent = el.offsetParent; 
				if (parent != el) { 
					while (parent) { 
						pos[0] += parent.offsetLeft; 
						pos[1] += parent.offsetTop; 
						parent = parent.offsetParent; 
					} 
				} 
				if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' ))  
				{ 
						pos[0] -= document.body.offsetLeft; 
						pos[1] -= document.body.offsetTop; 
				}  
			}    
			if (el.parentNode) { parent = el.parentNode; } 
			else { parent = null; } 
			while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML')  
			{ // account for any scrolled ancestors 
				pos[0] -= parent.scrollLeft; 
				pos[1] -= parent.scrollTop; 
				if (parent.parentNode) { parent = parent.parentNode; }  
				else { parent = null; } 
			} 
			return {x:pos[0], y:pos[1]}; 
		} 	
};
function killErrors(){return true;} 
window.onerror = killErrors; 
//调用
$(function(){
	bar.init("inputbar");
});
