$(function() {
	$.fn.confirmBox = function(message, settings) {
		var ac = arguments.callee;
		ac.hide();

		ac.show = function(id, box) {
			$(id).find(".conf_msg_btn_close").unbind("click");
			$(id).find(".conf_msg_btn_close").bind("click", function(e) {
					e.preventDefault();
					ac.hide();
					if(settings.failure) {
						settings.failure();
					}
				});
			$(id).find(".conf_msg_btn_yes").unbind("click");
			$(id).find(".conf_msg_btn_yes").bind("click", function(e) {
					e.preventDefault();
					ac.hide();
					if(settings.success) {
						settings.success();
					}
				});
			$(id).find(".conf_msg_message").html(message);
			$(id).
				css("top", box.top).
				css("left", box.left).
				show();
		}
		
		var el = $(this.get(0));
		var pos = {
			left: el.offset().left,
			top: el.offset().top,
			width: el.width(),
			height: el.height()
		};

		var container = {
			left: $(window).scrollLeft(),
			top: $(window).scrollTop(),
			width: $(window).width(),
			height: $(window).height(),
			contains: function(box) {
				if(box.left < this.left) {
					return false;
				}
				if(box.top < this.top) {
					return false;
				}
				if(box.left + box.width > this.left + this.width) {
					return false;
				}
				if(box.top + box.height > this.top + this.height) {
					return false;
				}
				return true;
			}
		};

		var boxTop = {
			left: pos.left - 10,
			top: pos.top - $("#conf_msg_top").height() - 4,
			width: $("#conf_msg_top").width(),
			height: $("#conf_msg_top").height()
		};

		if(container.contains(boxTop)) {
			ac.show("#conf_msg_top", boxTop);
			return;
		}

		var boxBottom = {
			left: pos.left - 10,
			top: pos.top + pos.height + 10,
			width: $("#conf_msg_bottom").width(),
			height: $("#conf_msg_bottom").height()
		};

		if(container.contains(boxBottom)) {
			ac.show("#conf_msg_bottom", boxBottom);
			return;
		}


		var boxLeft = {
			left: pos.left - $("#conf_msg_left").width() - 5,
			top: pos.top - 11,
			width: $("#conf_msg_left").width(),
			height: $("#conf_msg_left").height()
		};
		
		if(container.contains(boxLeft)) {
			ac.show("#conf_msg_left", boxLeft);
			return;
		}


		var boxRight = {
			left: pos.left + pos.width + 10,
			top: pos.top - 11,
			width: $("#conf_msg_right").width(),
			height: $("#conf_msg_right").height()
		};

		if(true) {
			ac.show("#conf_msg_right", boxRight);
		}
		
	};

	$.fn.confirmBox.hide = function() {
		$("#conf_msg_bottom").hide();
		$("#conf_msg_top").hide();
		$("#conf_msg_right").hide();
		$("#conf_msg_left").hide();
	};


});

