var _kikyuuAttachments = {
	"below":		function(x, y, w, h, m){return {left: x,			top: y + h + m};},
	"right":		function(x, y, w, h, m){return {left: x + w + m,	top: y};}
};

var _kikyuuInited=false;

function _kikyuuClose(e)
{
    if (!e) e = window.event;
    if (!$(e.target).parents().andSelf().filter('.kikyuu').length) $(".kikyuu").remove();
}

$.kikyuu=function(_opt){
	var opt=$.extend({
		content:	'気球',
		class:		'',
		attachTo:	null,
		attach:		'below',
		left:		0,
		top:		0,
		margin:		5,
		fadeIn:		"fast",
		timeout:	false
	}, _opt);
	
	// Generate basic balloon
	var kikyuuEl = $("<div>").html(opt.content).addClass("kikyuu");
	if(opt.class) kikyuuEl.addClass(opt.class);
	
	// Position it
	var pos = {};
	if(opt.attachTo)
	{
		var attEl = $(opt.attachTo);
		var attLoc = attEl.offset();
		var attacher = _kikyuuAttachments[opt.attach] || _kikyuuAttachments["below"];
		pos = attacher(attLoc.left, attLoc.top, attEl.width(), attEl.height(), opt.margin);
		kikyuuEl.addClass("kikyuu-"+opt.attach);
	}
	else
	{
		pos = {left: opt.left, top: opt.top};
	}
	pos["position"]="absolute";
	pos["z-index"]=9001;
	kikyuuEl.css(pos);
	
	// And add it to the page.
	
	kikyuuEl.appendTo($("body"));
	if(opt.fadeIn)
	{
		kikyuuEl.hide().fadeIn(opt.fadeIn);
	}
	if(opt.timeout)
	{
		setTimeout(function(){
			kikyuuEl.fadeOut('fast',function(){$(this).remove();});
		}, opt.timeout);
	}
	
	if(!_kikyuuInited)
	{
		$(document).click(_kikyuuClose);
		_kikyuuInited = true;
	}
};

