(function($){
	$.widget('ui.rc_dialog',$.ui.dialog,{
		_create:function(){
		$.ui.rc_dialog.instances.push(this);
		this.options=$.extend(this.options,{
			wrap:'<div class="d-shadow-wrap"></div>',
			before:'',
			after:'<div class="d-sh-cn d-sh-bl"></div><div class="d-sh-cn d-sh-br"></div>',
			afterContent:'<div class="d-sh-cn d-sh-tl"></div><div class="d-sh-cn d-sh-tr"></div>',
			beforeContent:''
		})
		$.ui.dialog.prototype._create.call(this);
		var uiDialogContent=$('<div class="dialog-content"></div>'),
			options=this.options
		this.uiDialogTitlebar.appendTo(uiDialogContent);
		this.element.appendTo(uiDialogContent);
		this.uiDialog.wrapInner(uiDialogContent);
		this.uiDialog.wrapInner(options.wrap);
		this.uiDialog.append(options.after);
		uiDialogContent.before(options.beforeContent);
		uiDialogContent.after(options.afterContent);
		if(options.openButton){
			this._openButton(options.openButton)
		}
	},
	_openButton:function(openButton){
		var button=openButton.button,
			position=openButton.position;
			button=$(button);
		var self=this;
		
		this.openButton=button;
		button.click(function(e){
			var $this=$(this);
			e.preventDefault();
			if (!self.isOpen()){
				self._activeButton=$this;
				self.open();
			}
			else{
				if ($.inArray(this,self._activeButton)>=0){
					self.close();
					return
				}else{
					self._activeButton=$this;
					self._setButtonClass();
					self._setPosition(self._activeButton);
				}
			}
			
		})
	},
	_setPosition:function(of){
		var defaultPosition={of:this._activeButton,
				at:'right bottom',
				my:'right top'};
		this.uiDialog.position($.extend(
			defaultPosition,
			this.options.openButton.position));
	},
	open:function(e){
			$.ui.dialog.prototype.open.call(this,e);
			if(this.openButton){
				this._setButtonClass();
				this._setPosition();
			}
		},
	_setButtonClass:function(){
			this.openButton.removeClass('button-open');
			this._activeButton.addClass('button-open');
		},
	close:function(e){
			$.ui.dialog.prototype.close.call(this,e);
			if(this.openButton){
				this.openButton.removeClass('button-open');
			}
		},
	getActiveButton:function(){
			return this._activeButton;
		}
	})
	$.extend($.ui.rc_dialog,{instances:[]});
})(jQuery)
