﻿/*
* Search DropdownList 0.1
* Copyright (c) 2011 Zhaojiping http://lastidea.net/
* Date: 2011-03-1
* Search DropdownList是一个模似google 搜索下拉菜单的Jquery插件
*/ 
(function($) { 
	
	$.fn.lastidea_net_search = function(options){
		var opts = $.extend({}, $.fn.lastidea_net_search.defaults, options); 
		
		$(this).val(opts.text.tipword).css("color",opts.text.b_color)
		
		$(this).focus(function(){ 
			if($(this).val() == opts.text.tipword){
				$(this).val("").css("color",opts.text.f_color);
			}
			if($.trim($(this).val()) != ""){
				$("#lastidea_dropdownlist").show();
			}
		});
		
		$(this).click(function(){ 
			$(this).focus();
		});
		
		$(this).blur(function(){ 
			if($.trim($(this).val()) == ""){
				$(this).val(opts.text.tipword).css("color",opts.text.b_color);
			}
			$("#lastidea_dropdownlist").hide();
		});
		
		$(this).keyup(function(){
			if($(this).val()==""){ 
				$("#lastidea_dropdownlist").hide();
				return false;
			}
			
			if( ! $("#lastidea_dropdownlist").length)
			{
				$(this).after("<div id='lastidea_dropdownlist'>" + opts.dropdownlist.waiting + "</div>");
				
				$("#lastidea_dropdownlist").css({
					color: opts.dropdownlist.color,
					background: opts.dropdownlist.background,
					border: opts.dropdownlist.border,
					"border-top":0,
					
					top: opts.dropdownlist.top + parseInt($(this).offset().top) + parseInt($(this).outerHeight()),
					left: opts.dropdownlist.left + parseInt($(this).offset().left),
					width: opts.dropdownlist.width  + $(this).width(),
					height: opts.dropdownlist.height,
					
					position:"absolute",
					"z-index": "9999",
					"overflow-y":"auto",
					"overflow-x":"hidden"
				}); 
				
			}else{
				$("#lastidea_dropdownlist").html(opts.dropdownlist.waiting)
				$("#lastidea_dropdownlist").show();
			}
			
			if( $.trim(opts.ajaxURL) == ""){
				alert("lastidea_net_search错误:ajaxURL需要定义!");
			}
			$.ajax({
				url:opts.ajaxURL + escape($("#txtKeyWord").val()), 
				cache: false,
				success: function(data){
					//alert(data)
					$("#lastidea_dropdownlist").html(data);
				}
			});
		});  
	};

	$.fn.lastidea_net_search.defaults = {
		ajaxURL : "" ,
		dropdownlist:{
			waiting: "加载中...",
			color: "black", 
			background: "white",
			border: "2px #ddd solid",
			top: 0,
			left: 0,
			width: 0,
			height: 210
		},
		text:{
			tipword: "请输入查询关键字",
			b_color: "#aaa",
			f_color: "#333"
		}
	};        
})(jQuery);	   

