(function($) {
	$.fn.balloon = function(options) {
		$.fn.balloon.defaults = {
			balloonedClass: 'balloon',
			balloonClass: 'balloon-1',
			balloonShadowEnabled: !($.browser.msie && $.browser.version == 6),
			balloonArrowOffset: {x: 0, y: 0},
			balloonShadowOffset: {x: -5, y: 0},
			float: false
		};
		var opts = $.extend({}, $.fn.balloon.defaults, options);
		
		return this.each(function(){
			var ul = $(this);
			ul.addClass(opts.balloonedClass).children("li").hover(
        		function(){ if(!$(this).hasClass("inactive"))$(this).addClass("hover");},
        		function(){ $(this).removeClass("hover");}
    		).click(function() {
				if ($(this).hasClass("inactive") || $(this).hasClass("active")) return;
				//---CloseBubble(el);
				var balloonWrapper = $('<div class="' + opts.balloonClass + '-wrapper"></div>');
				var balloon = $('<div class="' + opts.balloonClass + '"><a href="#" class="close">Close</a></div>').css('z-index', '5000');
				var shadow;
				if (opts.balloonShadowEnabled) shadow = $('<div class="' + opts.balloonClass + ' shadow"><div class="upper" /></div>').css('z-index', '4999');
				balloon.find("a.close").click(function() {
					var il = ul.children("li.inactive").removeClass("inactive");
				    //if ($.browser.msie) il.css("opacity", 1);
				    //else
				    il.fadeTo("fast", 1);
				    if ($.browser.msie) {
						balloon.hide();
						if (shadow) shadow.hide();
					} else {
						balloon.fadeOut("fast");
						if (shadow) shadow.fadeOut("fast");
					}
				    balloon.children(".balloon-content").appendTo(ul.children("li.active").removeClass("active"));
					balloon.remove();
					if (shadow) shadow.remove();
					return false;
				});
				$(this).children('.balloon-content').appendTo(balloon);
				//$('<div class="balloon-content"><p>This is a test.</p><p>The quick brown fox jumps over the lazy dogs.</p></div>').appendTo(balloon);
				balloon.insertAfter(ul);
				if (shadow) {
					shadow.insertAfter(balloon);
					shadow.css({
						height: (balloon.outerHeight(true) + 15) + "px"/*,
						width: balloon.innerWidth(false) + "px"*/
					}).children(".upper").css({height: (balloon.outerHeight(true) - 5) + "px" });
				}
				var hotspot = $(this).attr("hotspot");
				if (hotspot) hotspot = hotspot.split(",");
				else hotspot = [$(this).width(),0];
				var link = $(this);
				$(this).addClass("active").siblings().addClass("inactive");
				//if ($.browser.msie) $(this).siblings().css("opacity", 0.5);
				//else
				$(this).siblings().fadeTo("fast", 0.5);
				balloon.css({
					left: ($(link).position().left + opts.balloonArrowOffset.x + parseInt(hotspot[0])) + 'px',
					top: ($(link).position().top + opts.balloonArrowOffset.y + parseInt(hotspot[1]))  + 'px'
				});
				if (shadow)
					shadow.css({
						left: ($(link).position().left + opts.balloonArrowOffset.x + parseInt(hotspot[0]) + opts.balloonShadowOffset.x) + 'px',
						top: ($(link).position().top + opts.balloonArrowOffset.y + parseInt(hotspot[1]) + opts.balloonShadowOffset.y)  + 'px'
					});
				if ($.browser.msie) {
					balloon.show();
					if (shadow) shadow.show();
				} else {
					balloon.fadeIn("fast");
					if (shadow) shadow.fadeIn("normal");
				}
			});
			ul.wrap('<div style="position:relative;width:' + ul.outerWidth() + 'px;overflow:visible' + (opts.float ? ';float:' + opts.float : '') + '"></div>');
			
			//$(".bubble a.close").click(function() {
			//	CloseBubble(el);
			//	return false;
			//});
		});
	};
})(jQuery);
