//input class

function SuggestClient2(input, width){

	this.id = "suggest_" + input.form.name + "_" + input.name;
	this.tableId = this.id + "Table";
	this.width = width;
	this.input = input;
	this.input.setAttribute("autocomplete", "off");
	if(!this.input) alert("input not exists!");
	this.dataFromServer = "";
	this.currRow = null;
	
	//创建显示表单
	this._createDiv = function(){
		this.div = document.createElement("div");
		this.div.id = "_" + this.id + "Div";
		this.div.className = "suggestStyle";
//		if(this.width) this.div.style.width = this.width + "px";
		var theParent = null;//this.input.parentElement ? this.input.parentElement : this.input.parentNode;
		if(!theParent) theParent = document.body;
		theParent.appendChild(this.div);
	}
	
	//定位函数
	this._calcPosition = function(){
		var left = this.input.offsetLeft;
		var top = this.input.offsetTop;
		var parent = this.input;
		while(parent = parent.offsetParent){
			top += parent.offsetTop;
			left += parent.offsetLeft;
		}
		this.div.style.left = (left - 100) + "px";
		
		this.div.style.top = top + (this.input.offsetHeight + 5) + "px";
	}
	
	//键盘以及点击操作事件
	this._captureEvent = function(){
		this.input.obj = this;
		
		this.input.oldSize = this.input.value.length;
		this.input.onkeyup = function(evt){
			evt = evt ? evt : window.event;
			var charCode = (evt.charCode) ? evt.charCode : (evt.which ? evt.which : evt.keyCode);
			
			//键盘操作“Esc”注册
			if(charCode == 27){
				if(this.obj.div.style.visibility == "visible"){
					this.obj._pressEsc();
				}
			}
/*
			//键盘操作“↑”注册
			else if(charCode == 38){
				if(this.obj.div.style.visibility == "visible"){
					this.obj._pressUp();
				}
			}

			//键盘操作“↓”注册
			else if(charCode == 40){
				if(this.obj.div.style.visibility == "visible"){
					this.obj._pressDown();
				}
			}

			//键盘操作“←”注册
			else if(charCode == 37){
				if(this.obj.div.style.visibility == "visible"){
					this.obj._pressLeft();
				}
			}
			//键盘操作“→”注册
			else if(charCode == 39){
				if(this.obj.div.style.visibility == "visible"){
					this.obj._pressRight();
				}
			}
*/
			var dataChanged = this.value.length != this.oldSize;
			this.oldSize = this.value.length;
			if(dataChanged && charCode != 13){
				this.obj._dataChanged(this.value);
			}
		}

		this.input.onkeypress = function(evt){
			evt = evt ? evt : window.event;
			var charCode = (evt.charCode) ? evt.charCode : (evt.which ? evt.which : evt.keyCode);
			if(charCode == 13){
				if(this.obj.div.style.visibility == "visible"){
					return this.obj._pressEnter();
				}
			}
		}
		
		if(!SuggestClient2.prototype.setOnResize){
			SuggestClient2.prototype.setOnResize = 1;
			this.oldBodyResize = document.body.onresize;
			document.body.obj = this;
			document.body.onresize = function(){
				if(document.body.obj.oldBodyResize) 
					document.body.obj.oldBodyResize();
				if(document.body.obj.div.style.visibility == "visible"){
					document.body.obj._calcPosition();
				}
			}
		}
		else{
			this.oldBodyResize2 = document.body.onresize;
			document.body.obj2 = this;
			document.body.onresize = function(){
				if(document.body.obj2.oldBodyResize2) 
					document.body.obj2.oldBodyResize2();
				if(document.body.obj2.div.style.visibility == "visible"){
					document.body.obj2._calcPosition();
				}
			}
		}
		
		if(!SuggestClient2.prototype.setOnClick){
			SuggestClient2.prototype.setOnClick = 1;
			this.oldDocumentClick = document.onclick;
			document.obj = this;
			document.onclick = function(){
				if(document.activeElement.id != "k"){
					if(document.obj.oldDocumentClick) 
						document.obj.oldDocumentClick();
					if(document.obj.div.style.visibility == "visible"){
						document.obj.hide();
					}
				}
			}
		}
		else{
			this.oldDocumentClick2 = document.onclick;
			document.obj2 = this;
			document.onclick = function(){
				if(document.activeElement.id != "k"){
					if(document.obj2.oldDocumentClick2) 
						document.obj2.oldDocumentClick2();
					if(document.obj2.div.style.visibility == "visible"){
						document.obj2.hide();
					}
				}
			}
		}

		//获得焦点事件
		this.input.onfocus = function(){
			if(this.value == '快速搜车'){this.value='';}
			this.obj._dataChanged(this.value);
		}
		
		//失去焦点事件
		this.input.onblur = function(){
			if(this.value.length == 0){this.value='快速搜车';}
		}
	}
	
	//输入框数据发生变化时,将参数传给后台处理,返回JS操作代码
	this._dataChanged = function(keyword){

		if(keyword.length != 0 && keyword != '快速搜车'){
			document.getElementById('clear_button').style.display = "block";
		}else{
			document.getElementById('clear_button').style.display = "none";
		}

		var scriptid = document.getElementById('ajaxProxy2');
		var html_doc = document.getElementsByTagName('head')[0];
		var js_over = false;
		if(scriptid) html_doc.removeChild(scriptid);
			
		script = document.createElement('script');		
		script.id = 'ajaxProxy2';
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', "http://tools.xcar.com.cn/search/suggest_server.php?id=" + this.id + "&k=" + encodeURIComponent(keyword));
		html_doc.appendChild(script);

		script.onload = script.onreadystatechange = function(){
			if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete'){
				return;
			}
			script.onreadystatechange = script.onload = null;
			eval(data_for_top_search);
		}			

		/*
		try{
			var request = proxy.contentWindow.createReq();
		}
		catch (e) {}
		
		if (request != null){
			request.onreadystatechange = function(){
				if (request.readyState == 4 && request.responseText){
					eval(request.responseText);
				}
			}

			request.open("GET", "/search/suggest_server.php?id=" + this.id + "&k=" + encodeURIComponent(keyword), true);
			request.send(null);
		}
		*/
	}
/*
	//键盘事件-向左键
	this._pressLeft = function(){

	}

	//键盘事件-向右键
	this._pressRight = function(){

	}
	//键盘事件-向下键
	this._pressDown = function(){
		var tbl = document.getElementById(this.tableId);
		if(!this.currRow){
			this.currRow = tbl.rows[0];
		}
		else{
			var index = this.currRow.rowIndex + 1;
			if(index > tbl.rows.length-1) 
				index = tbl.rows.length-1;
			this.currRow = tbl.rows[index];
		}
		this._rowOver(this.currRow);
	}	
	//键盘事件-向上键
	this._pressUp = function(){
		if(!this.currRow) return;
		var index = this.currRow.rowIndex - 1;
		if(index < 0)
			index = 0;
		var tbl = document.getElementById(this.tableId);
		this.currRow = tbl.rows[index];
		this._rowOver(this.currRow);
	}
*/
	//键盘事件-回车键
	this._pressEnter = function(){
		if(this.currRow){
			this._rowClick(this.currRow);
			return false;
		}
		else{
			this.hide();
			return true;
		}
	}
	//键盘事件-取消键
	this._pressEsc = function(){
		this.hide();
	}
	//隐藏结果
	this.hide = function(){
		this.dataFromServer = "";
		this.div.style.visibility = "hidden";
	}
	//显示结果
	this.show = function(){
		this._calcPosition();
		this.div.style.visibility = "visible";
	}
	
	// server call	
	this.showData = function(data){
		if(data == this.dataFromServer) return;
		this._setDivContent(data);
		this.dataFromServer = data;
	}
	
	//根据ajax后台返回结果进行前台展示组装并显示
	this._setDivContent = function(data){
		if(data == ""){
			this.div.innerHTML = "";
			this.dataFromServer = "";
			this.hide();
			return;
		}
		var tableContent = "";
		var tableAdd = "";
		/*
		var resultAry = data.split(",");
		for(var i = 0; i < resultAry.length; i++){
			var itemAry = resultAry[i].split("=");
			tableContent += "<tr onmouseover='" + this.id + "._rowOver(this);' onclick='" + this.id + "._rowClick(this);'><td>" + itemAry[0] + "</td><td align='right'>" + itemAry[1] + "</td></tr>";
		}
		var content = "<div align='right' style='float:right;padding-right:3px;width:100%'><a href='javascript:" + this.id + ".hide();'><img src='images/point_2.gif' border=0 height=9 width=9></a></div>";
		content += "<table width='100%' style='font-size: 12px; color: #999999;'><tbody id='" + this.tableId + "'>"
			+ tableContent + "</tbody></table>";
		*/
		var resultAry = data.split(",");
		
		if(resultAry.length > 0){
			if(resultAry[0] != 1){
				for(var i = 0; i < resultAry.length; i++){
					var itemAry = resultAry[i].split("|");
					if(itemAry.length > 0){					
							for(var j = 0; j < itemAry.length; j++){
								var listAry = itemAry[j].split("=");
								if(j == 0){
									tableContent += "<h2><span><a href='http://newcar.xcar.com.cn/price/b" + listAry[0] + "/' target='_blank'>进入品牌页</a></span><b><a href='http://newcar.xcar.com.cn/price/b" + listAry[0] + "/' target='_blank'>" + listAry[1] +"</a></b></h2>";
									if(listAry[2] > 0) tableAdd = "<div class='ss_p_bm2'><a href='http://www.xcar.com.cn/bbs/forumdisplay.php?fid=" + listAry[2] + "' target='_blank'>进入" + listAry[3] + "&gt;&gt;</a></div>";
									else tableAdd = "<div class='ss_p_bm2'>&nbsp;</div>";
								}else{
									if(j == 1){
										tableContent += "<div class='ss_p_ls2'>";
									}
									tableContent += "<a a href='http://newcar.xcar.com.cn/" + listAry[0] + "/' target='_blank'>" + listAry[1] +"</a>";
								}
							}
							tableContent += "</div>";					
					}					
				}
			}else{
				tableContent += "<h2><span><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/' target='_blank'>进入频道首页</a></span><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/' target='_blank'>" + resultAry[2] + "</a>";
				if(resultAry[14] == 1){
					tableContent += "<i>(停产)</i>";
				}else if(resultAry[15] == 2){
					tableContent += "<i>(未上市)</i>";
				}

				tableContent += "</h2><ul class='ss_p_ls1'>";

				if(resultAry[14] == 0){
					if(resultAry[3] > 0 && resultAry[4] > 0)
	    				tableContent += "<li onmouseover=\"this.className='active'\" onmouseout=\"this.className=''\"><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/price.htm' target='_blank'><span class='c'>(" + resultAry[4] + "万-" + resultAry[3] + "万)</span><i class='i1'></i>价格</a></li>";
				}else if(resultAry[14] == 1){
					tableContent += "<li onmouseover=\"this.className='active'\" onmouseout=\"this.className=''\"><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/price.htm' target='_blank'><span class='c'>(停产)</span><i class='i1'></i>价格</a></li>";
				}

				if(resultAry[5] > 0)
					tableContent += "<li onmouseover=\"this.className='active'\" onmouseout=\"this.className=''\"><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/photo.htm' target='_blank'><span>(" + resultAry[5] + "张)</span><i class='i2'></i>图片</a></li>";
				if(resultAry[6] > 0)
					tableContent += "<li onmouseover=\"this.className='active'\" onmouseout=\"this.className=''\"><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/config.htm' target='_blank'><span>(" + resultAry[6] + "款车型)</span><i class='i3'></i>参数配置</a></li>";
				if(resultAry[7] > 0)
					tableContent += "<li onmouseover=\"this.className='active'\" onmouseout=\"this.className=''\"><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/news.htm' target='_blank'><span>(" + resultAry[7] + "篇)</span><i class='i4'></i>文章</a></li>";
				if(resultAry[8] > 0)
					tableContent += "<li onmouseover=\"this.className='active'\" onmouseout=\"this.className=''\"><a href='http://newcar.xcar.com.cn/" + resultAry[1] +"/review.htm' target='_blank'><span>(" + resultAry[8] + "人说)</span><i class='i5'></i>说真的</a></li>";
				if(resultAry[9] > 0)
					tableContent += "<li class='ld' onmouseover=\"this.className='ld active'\" onmouseout=\"this.className='ld'\"><a href='http://www.xcar.com.cn/bbs/forumdisplay.php?fid=" + resultAry[9] +"' target='_blank'><span>(" + resultAry[10] + "帖子)</span><i class='i6'></i>论坛</a></li>";
				tableContent += "<!--<li class='nd'></li>--></ul><div class='ss_p_bm'>所属品牌：<a href='http://newcar.xcar.com.cn/price/b" + resultAry[11] +"/' target='_blank'>" + resultAry[12] + "</a><br />";
				if(resultAry[13] > -1 && resultAry[13] < 9){
					tableContent += "所属级别：<a href='http://newcar.xcar.com.cn/type/" + resultAry[13] +"/' target='_blank'>";
					switch(resultAry[13]){
						case '0' : tableContent += "微型车";break;
						case '1' : tableContent += "小型车";break;
						case '2' : tableContent += "紧凑型车";break;
						case '3' : tableContent += "中型车";break;
						case '4' : tableContent += "中大型车";break;
						case '5' : tableContent += "豪华车";break;
						case '6' : tableContent += "MPV";break;
						case '7' : tableContent += "SUV";break;
						case '8' : tableContent += "跑车";break;
						default : tableContent += "";break;
					}
					tableContent += "</a>";
				}else{
					tableContent += "所属级别：其他";
				}							
				tableContent += "</div>";
			}

			var content = "<div class='ss_p_box'>" + tableContent + tableAdd + "</div>"
			this.div.innerHTML = content;
			this.show();
		}
	}
	
	// private
	/*
	//鼠标滑动时,显示不同样式
	this._rowOver = function(row){
		var tbl = document.getElementById(this.tableId);
		this.currRow = row;
		for(i = 0; i < tbl.rows.length; i++){
			if(i == this.currRow.rowIndex){
				tbl.rows[i].className = this.div.className + "_row2";
			}
			else{
				tbl.rows[i].className = this.div.className + "_row1";;
			}
		}
	}
	
	//鼠标点击时,取点击的数值传递给input框
	this._rowClick = function(row){
		this.currRow = row;
		var value = row.cells[0].innerHTML;
		this.input.value = value;
		this.hide();
	}
	*/
	//样式
	this._createStyle = function(){
		document.write("<style type=\"text/css\">");
		document.write(".suggestStyle{z-index:200; position:absolute; visibility:hidden; background: #ffffff; text: left; font-size: 12px; color: #999999; line-height: 20px; text-align: left;}");
		document.write(".suggestStyle_row1{background-color:#fff; color:#999; padding-left:6px; padding-right:6px; font-size:12px;}");
		document.write(".suggestStyle_row2{background-color:#edf2fb; color:#000; padding-left:6px; padding-right:6px; font-size:12px;}");
		document.write("</style>");
	}
	
	this._createStyle();

	this._createDiv();
	this._captureEvent();

}
