(function ($,undefined) {

var shown,closing;

$.widget('ui.popuptab', {
	_create: function() {
		var self=this;
		if (self.options.navbar===undefined) self.options.navbar=self.element;
		var a=$('a[href]',self.element);
		var inline_contents=$.find(a.attr('href'));
		self['dlg']=$(inline_contents).dialog($.extend({autoOpen:false,closeOnEscape:false,minHeight:'20',minWidth:'40',width:'auto',dialogClass:'notitle '+self.options.tabclass+' body',focus:function(){shown=self;closing=undefined;}},self.options));
		self.dlg.dialog('widget').removeClass('ui-corner-all');
		$('.notitle div.ui-dialog-titlebar').hide();
		$('a',this.element).click(function(){
			self.show();
			return false;
		});
	},
	show: function()
	{
		if (shown) shown.close();
		this.dlg.dialog('open');closing=shown=this;
		this.dlg.dialog('widget').position({my:'right top',at:'right bottom',of:$(this.options.navbar),offset:'0 -1'});
		var head=$(this.element);
		w=head.innerWidth();
		h=head.innerHeight();
		head.addClass('ui-widget-content ui-corner-top '+this.options.tabclass+' head');
		head.outerWidth(w);
		head.outerHeight(h);
		head.css('z-index',3000);
		if (this.options.onopen)
			this.options.onopen();
	},
	close: function()
	{
		this.dlg.dialog('close');
		var head=$(this.element);
		w=head.outerWidth();
		h=head.outerHeight();
		head.removeClass('ui-widget-content ui-corner-top '+this.options.tabclass+' head');
		head.innerWidth(w);
		head.innerHeight(h);
	}

});

$(function(){
	$('body').click(function() {if (closing) closing.close();closing=shown;});
});


}) (jQuery);


