﻿/* 
File:   myjqplugin.searchtips.js
Title:  搜索框文本提示插件 基于jQuery
Author: bladecamper#gmail.com
Log:    Creat 20110901
20111012 xiaozj 将生成的提示文本置入body区域最后，避免因文本框margin产生的位置偏差
*/
(function($) {
	$.fn.searchTips = function(option) {
		var setting = {
			fontsize : "12px",
			fontcolor : "#ccc"
		};
		return this.each(function() {
			var $this = $(this);
			if (option) {
				var opts = $.extend({}, setting, option);
			}
			var t = $this.attr("tips");
			if (!t) return; // 没有tips属性则退出
			var _l = $this.offset().left;
			var _t = $this.offset().top;
			$("body").append("<p id=\"j_searchTips\">" + t + "</p>");
			var $tips = $("#j_searchTips");
			if ($.trim($this.val())) $tips.hide(); // 防止Firefox刷新记忆功能
			$tips.css({
				"position" : "absolute",
				"left" : parseInt(_l) + parseInt($this.css("border-left-width")) + 2,
				"top" : _t,
				"height" : $this.outerHeight() + "px",
				"line-height" : $this.outerHeight() + "px",
				"color" : opts.fontcolor,
				"font-size" : opts.fontsize,
				"font-family" : "\\5b8b\\4f53"
			});
			$this.bind({
				focus : function() {
					$tips.hide();
				},
				blur : function() {
					if (!$.trim($this.val())) $tips.show(); // 失去焦点时判断是否有输入，没有则恢复TIPS
				}
			});
			$tips.bind("click focus", function() {
				$this.focus(); // 解决IE点击TIPS时文本框未获得焦点
			});
		});
	}
})(jQuery);
