var suggest = function (){

	var $sug = {
		url: "/plus/ajax_search.php",
		key: "keyword",
		result: "suggest",
		button: "searchbutton",
		delay: 200,
		shortLi: 0,
		selectedclass:'selected',
	};
	var i, k, d= [] ,
	q = "",
	c = 0,	
	b=-1;
	var resultbg=$sug.result+'_bg';
	var playPage=false;
	function getsearch(k){   //执行get方法
			$.get($sug.url,
				  {
					  key:k
				  },
 					function(data){
						 formatsuggest(data, k);
					},
					'text'
				);
	
	}
	
	function formatli(j, q) {
		
		if (j.length == 0) return;
		//alert(j);
		//186|片花|爱情自有天意|完结|/dalu/186.html|/videos/186/0_1.html
		for (var c = j.split("|"), r = "", n = 0, b = [], d = 0; d < c.length;) {
			var id = c[d++],
				lastvolume = c[d++],
				title = c[d++],
				state = c[d++],
				titleurl = c[d++],
				playurl= c[d++],
				z = c[d++];
			b.push('<li id="ret' + id + '" rel="' + id + '" title="' + title + '" onclick=\'window.location.href="' + titleurl + "\"' >");
			
			var l = '<a href="'+playurl+'">' + lastvolume + '</a>',
				i = '<a href="' + titleurl + '">' + title.replace(new RegExp("(" + q + ")", "gi"), "<strong>$1</strong>") + "</a>";
			if ($sug.shortLi == 1) {
				b.push(i);
				b.push(" <span>[");
				b.push(l) 
				b.push("]</span> ");
			 
			} else {
				b.push("<span>更新至：");
				b.push(l);
				b.push("</span>");
				b.push(i) 
				 
			}
			b.push("</li>");
			
			n++;
		}
		 
		return b.join("")
	}
	function formatsuggest(f, g) {
		b = -1;
		
		$("#" + $sug.result).html("<ul>" + formatli(f, g) + "</ul>");
		$("#" + $sug.result + " li").mouseover(function() {
			$("#" + $sug.result + " li").removeClass("selected");
			$(this).addClass("selected");
			b = $(this).attr("id").replace("ret", "")
		});
		f.length > 0 ? sugshow() : sughide();
		c = $("#" + $sug.result + " li").length;
		d[g] = f;
		d.length > 8 && d.pop()
	}	
	function updown() {
		if (b < -1) b = c - 1;
		else if (b >= c) b = -1;
		$("#" + $sug.result + " li").removeClass($sug.selectedclass);
		if (b == -1) $("#" + $sug.key).val(k);
		else {
			var d = $("#ret" + b);
			$(d).addClass($sug.selectedclass);
			$("#" + $sug.key).val($(d).attr("title"))
		}
	}
	function gettime() {
		i && clearTimeout(i);
		
		i = window.setTimeout(function() {
			var keyword = $.trim($("#"+$sug.key).val());
			var keyword = $("#"+$sug.key).val();
			if (k != keyword) {
				
				k = keyword;
				if (keyword != "") {
					getsearch(keyword);
					sugshow();	
				}
				else {

					$("#" + $sug.result).html("")
				}
			}
			else{
				if (keyword != "") {
					sugshow();	
				}				
				}
		},$sug.delay)
	}


	function sughide(){
		playPage=false;
		$('#'+$sug.result).hide(200);
	}
	function sugshow(){
		playPage=true;
	    $('#'+$sug.result).show(200);
	}





	this.bind = function() {
		var d = this;		
		$("#" + $sug.key).keyup(function(d) {
			d.stopPropagation();
			if (d.keyCode == 13) {
				}
			else {
				var g = document.getElementById($sug.result).style.display != "none";
				if (g) switch (d.keyCode) {
				case 27:
					if (b != -1) {
						b = -1;
						updown();
					} else e();
					break;
				case 38:
					b--;
					updown();
					break;
				case 40:
					b++;
					updown();
					break;
				default:
					gettime()
				} else switch (d.keyCode) {
				case 38:
				case 40:
					c > 0 && j();
					break;
				default:
					gettime()
				}
			}
		});	
		
		
		$("#" + $sug.key).blur(function() {
			setTimeout(function() {
				sughide();
			}, 200)
		});
		$("#" + $sug.key).focus(function() {
			//g.append();
			//c > 0 && j()
		  
			
			gettime();
			
			
		});
		$("#" + $sug.button).click(function() {
			//l()
		})
	}
}
var sug=new suggest();sug.bind();