/*
 * jQuery Autocomplete plugin 1.1
 *
 * Copyright (c) 2009 Jörn Zaefferer
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $
 */

;(function($) {
	
	$.fn.extend({
		autocomplete: function(urlOrData, options) {
			var inputwidth = $(this).width();
			var inputheight = $(this).height();
			
			//alert(inputheight);
			if (document.getElementById("input_search_menu") != undefined){
				var input_search_menu = document.getElementById("input_search_menu");
				document.body.removeChild(input_search_menu);
			}
			var input_div = document.createElement('div');
			input_div.id = "input_search_menu";
			input_div.style.zIndex = 1000;
			//input_div.style.height = inputheight+"px";
			input_div.style.width = (parseInt(inputwidth)-6)+"px";
			input_div.style.height = "250px";
			
			input_div.style.backgroundColor = "#e9e9e9";
			input_div.style.border = "1px solid #CCCCCC";
			input_div.style.overflowY = "scroll";
			input_div.style.overflowX = "visible";
			//alert($(this).attr("value"));
			var offset=$(this).offset();
			input_div.style.position="absolute";
			input_div.style.left = offset.left+"px";
			input_div.style.top = (parseInt(offset.top)+parseInt(inputheight)+3)+"px";
			input_div.style.padding = "3px";
			document.body.appendChild(input_div);
			
			var str = "";
			
			var showdivwidth = (parseInt(input_div.style.width) - 18);
			var leftdivwidth = (showdivwidth/3);
			var rightdivwidth = (showdivwidth/3)*2;
			
			var inputid = $(this).attr("id"); 
			
			
			var data_input_value = $(this).attr("value");
			for ( var i = 0; i < urlOrData.length; i++){
				//str += "<li>"+urlOrData[i][0]+"</li>";	
				if (urlOrData[i][0] != undefined){
					if (urlOrData[i].length > 1){
						var select_value = urlOrData[i][1]+"||||"+urlOrData[i][0];
						str += "<div style=\"float: left; width: "+showdivwidth+"px; cursor: pointer;\" onmouseover=\"this.style.backgroundColor='#FF9900'\" onmouseout=\"this.style.backgroundColor=''\" onclick=\"selectoption('"+inputid+"','"+select_value+"')\">";
					} else {
						str += "<div style=\"float: left; width: "+showdivwidth+"px; cursor: pointer;\" onmouseover=\"this.style.backgroundColor='#FF9900'\" onmouseout=\"this.style.backgroundColor=''\" onclick=\"selectoption('"+inputid+"','"+urlOrData[i][0]+"'\">";
					}
					
					if (urlOrData[i].length > 1){
						var data_json_0 = urlOrData[i][0];						
						data_json_0_get = data_json_0.replace(eval("/("+data_input_value+")/ig"), "<font color=\"#00aa00\">$1</font>");
						
						var data_json_1 = urlOrData[i][1];						
						data_json_1_get = data_json_1.replace(eval("/("+data_input_value+")/ig"), "<font color=\"#00aa00\">$1</font>");
						//alert(data_json_0_get);
						str += "<div style=\"float: left; width: "+leftdivwidth+"px; text-align: left; line-height: 16px;\">"+data_json_0_get+"</div>";
						str += "<div style=\"float: right; width: "+rightdivwidth+"px; text-align: right; line-height: 16px;\">"+data_json_1_get+"</div>";
					} else {
						str += "<div>"+urlOrData[i][0]+"</div>";
					}
					str += "</div>";
				}
			}
			if (options["pagecount"] != undefined){
				if (options["pagecount"] > 1){
					var this_page = parseInt(options["pageno"]);
					var this_pagecount = parseInt(options["pagecount"]);
					str += "<div style=\"float: left; width: "+showdivwidth+"px; cursor: pointer;\">";
					for (var page = this_page - 3; page < this_page + 4; page++){
						if (page > 0 && page < this_pagecount){
							str += "&nbsp;<span style=\"cursor: pointer;\" onclick=\"eval("+options["fun"]+"('"+options["funtype"]+"',"+page+"))\">"+page+"</span>&nbsp;";
						}
					}
					str += "</div>";
				}
				if (options["pagecount"] == 0){
					str += "<div style=\"float: left; width: "+showdivwidth+"px; cursor: pointer;\">";
					str += "无法找到相符合的条件";
					str += "</div>";
				}
			}
			input_div.innerHTML = str;
			
			$("body").bind("click", menu_add_event);
			//alert(offset.left);
			/*
			var isUrl = typeof urlOrData == "string";
			options = $.extend({}, $.Autocompleter.defaults, {
				url: isUrl ? urlOrData : null,
				data: isUrl ? null : urlOrData,
				delay: isUrl ? $.Autocompleter.defaults.delay : 10,
				max: options && !options.scroll ? 10 : 150
			}, options);
			
			// if highlight is set to false, replace it with a do-nothing function
			options.highlight = options.highlight || function(value) { return value; };
			
			// if the formatMatch option is not specified, then use formatItem for backwards compatibility
			options.formatMatch = options.formatMatch || options.formatItem;
			
			return this.each(function() {
				new $.Autocompleter(this, options);
			});
			*/
			//alert(this.value);
			//alert(urlOrData.length);
			/*
			return this.each(function() {
				new $.Autocompleter(this, options);
			});
			*/
			/*
			var getvalue = this.attr("value");
			alert(getvalue);
			*/
		}
	});
})(jQuery);
	
function menu_add_event(event){
	if (document.getElementById("input_search_menu") != undefined){
		var mouse_x = event.pageX;
		var mouse_y = event.pageY;
		var offset=$("#input_search_menu").offset();
		var search_div_width = $("#input_search_menu").width();
		var search_div_height = $("#input_search_menu").height();
		var serach_div_right = offset.left+search_div_width;
		var serach_div_bottom = offset.top+search_div_height;
		if (mouse_x < offset.left || mouse_x > offset.left+search_div_width || mouse_y < offset.top || mouse_y > serach_div_bottom){
			var input_search_menu = document.getElementById("input_search_menu");
			document.body.removeChild(input_search_menu);
			$("body").unbind("click", menu_add_event);
		}
	}
}
	
function selectoption(id,selectvalue){
	if (document.getElementById("input_search_menu") != undefined){
		var input_search_menu = document.getElementById("input_search_menu");
		document.body.removeChild(input_search_menu);
		$("body").unbind("click", menu_add_event);
	}
	var thisarr = selectvalue.split("||||")
	$("#"+id).attr("value",thisarr[0]);
	if (thisarr.length > 1){
		if (document.getElementById(id+"_txt") != undefined){
			document.getElementById(id+"_txt").innerHTML = thisarr[1];
		}
	}
	//alert(thisarr[0]);
	//alert(typeof(selectvalue));
	/*
	
	
	*/
	//document.getElementById(id).value = selectvalue;
}
