(function($) {
	$.fn.extend({
				dailyPrice : function(urlOrData, params, options) {
					var isUrl = typeof urlOrData == "string";
					options = $.extend({}, $.DailyPrice.defaults, {
								url : isUrl ? urlOrData : null,
								data : isUrl ? null : urlOrData
							}, options);

					return this.each(function() {
								$.DailyPrice(this, options, params);
							});
				}
			});

	$.DailyPrice = function(input, options, params) {
		var $input = $(input);
		var offset = $input.offset();
		var data = options.data;
		if (!$.DailyPrice.element)
			init();

		$input.hover((function() {
			$.DailyPrice.isHover = true;
			$.DailyPrice.timer = setTimeout(function() {
						if (data && data.length) {
							if ($.DailyPrice.isHover) {
								showPrice();
							}
						} else if ((typeof options.url == "string")
								&& (options.url.length > 0)) {
							var propId = $input.attr("propId");
							var ratePlan = $input.attr("ratePlan");
							var rateType = $input.attr("rateType");
							var userfrom = $input.attr("userfrom");
							params = {
								hotelId : propId,
								planCode : ratePlan,
								rateType : rateType,
								userfrom : userfrom
							};
							var extraParams = {
								timestamp : +new Date()
							};
							$.extend(extraParams, params);
							$.getJSON(options.url + '?jsoncallback=?',
									extraParams, function(json) {
										data = json;
										if ($.DailyPrice.isHover) {
											showPrice();
										}
									})
						}
					}, 200);
			return false;
		}), function() {
			clearTimeout($.DailyPrice.timer);
			$.DailyPrice.isHover = false;
			if ($.DailyPrice.element != null) {
				$.DailyPrice.element.hide();
			}
			return false;
		}).mousemove(function(e) {
			var target = $(e.target);
			if (target.is("a") || target.is("u")) {
				var left = target.offset().left;
				var top = target.offset().top;
				var width = target.width();
				var height = target.height();
				if ((e.pageX < left - 2) || (e.pageX > left + width + 2)
						|| (e.pageY < top - 2) || (e.pageY > top + height + 2)) {
					if ($.DailyPrice.isHover) {
						if ($.DailyPrice.element != null) {
							$.DailyPrice.element.hide();
						}
						$.DailyPrice.isHover = false;
					}
				} else {
					$.DailyPrice.isHover = true;
				}
			} else {
				if ($.DailyPrice.isHover) {
					if ($.DailyPrice.element != null) {
						$.DailyPrice.element.hide();
					}
					$.DailyPrice.isHover = false;
				}
			}
		}).focus(function() {
					$input.trigger("mouseover");
				}).click(function() {
					$input.trigger("mouseover");
				});

		/*
		 * function showPrice() { html = "<table border='0' cellpadding='0'
		 * cellspacing='0' class='tablePrice'><tbody>"; html += "<tr>"; for
		 * (j = 0;j < 7; j++) { html += "<th>" + data[0][j].week + "</th>"; }
		 * html += "</tr>"; for (i = 0;i < data.length; i++) { html += "<tr>";
		 * for (j = 0;j < 7; j++) { // html += "<td>"+
		 * options.data[i][j].price +"</td>"; price = data[i][j].price != "" ?
		 * data[i][j].price : "&nbsp;" html += "<td>" + price + "</td>"; }
		 * html += "</tr>"; } html += "</tbody></table>";
		 * element.html(html).css( { top : offset.top + input.offsetHeight, left :
		 * offset.left }).show(); }
		 */
		function showPrice() {
			var html = "<table  border='0' cellpadding='0' cellspacing='0' class='tablePrice'><tbody>";
			html += "<tr><th>&nbsp;</th>";
			for (j = 0; j < 7; j++) {
				html += "<th>" + data[0][j].week + "</th>";
			}
			html += "</tr>";
			for (i = 0; i < data.length; i++) {
				html += "<tr><td>第" + (i + 1) + "周</td>";
				for (j = 0; j < 7; j++) {
					// html += "<td>"+ options.data[i][j].price +"</td>";
					var breakfast = data[i][j].breakfast || "";
					var price = data[i][j].price != ""
							? data[i][j].price
							: "&nbsp;"
					var roomStatusStr = data[i][j].roomStatusStr;
					html += "<td title='" + data[i][j].date + "'>" + price
							+ "<br />" + roomStatusStr + "<br />" + breakfast
							+ "</td>";
				}
				html += "</tr>";
			}
			html += "</tbody></table>";
			if ($.DailyPrice.isHover) {
				$.DailyPrice.element.html(html).css({
					top : $(input).offset().top + $(input).height() + 5,
					right : document.body.clientWidth - offset.left
							- input.offsetWidth
				}).show();
			}
			if ($.browser.msie) {
				if ($.browser.version == 6.0) {
					data = null;
				}
			}
		}

		function init() {
			$.DailyPrice.element = $("<div/>").hide().css("position",
					"absolute").css("background-color", "#fff")
					.appendTo(document.body);
			// element.mousedown(function() {
			// config.mouseDownOnSelect = true;
			// }).mouseup(function() {
			// config.mouseDownOnSelect = false;
			// }).click(function(event) {
			// // TODO provide option to avoid setting focus again
			// // after selection? useful for cleanup-on-focus
			// return false;
			// }).mouseout(function(event) {
			// config.mouseDownOnSelect = false;
			// input.focus();
			// return false;
			// });

		}
	};
	$.DailyPrice.defaults = {
		width : 200,
		scroll : true,
		scrollHeight : 180
	};
})(jQuery);