/**
 * Custom Built jQuery Dialog Box with iFrame Support
 */

(function($) {
	jQuery.widget("ui.dialog_box", jQuery.ui.dialog, {
		options:{
			modal:true,
			draggable:false,
			autoOpen:false,
			resizable:false,
			width: 650,
			height:485,
			iframe:true,
			debug:true,
			show:'scale',
			hide:'fade',
			position:'center',
			title:''
		},
		
	    _create: function()
	    { 
			if( this.option.debug ){  this.debug = true; }
			this.vURL = this.element.attr('href');
			if( this.element.attr('x-pop-width') ) this.options.width = this.element.attr('x-pop-width');
			if( this.element.attr('x-pop-height') ) this.options.height = this.element.attr('x-pop-height');
			
			this._debug('_create');
			this._debug(this);
			jQuery(this.element).bind('click',[this], this._click);
			if( this.options.autoOpen == true){ this._open(); }
		},
		
	    _dialog: function()
	    { 
			this._debug('_dialog');
			this.options.autoOpen = false;
			jQuery.ui.dialog.prototype._create.apply( this );
		},
		
		_open: function()
		{
			this._debug('_open');
			this.element = this._container();
			this._dialog();
			jQuery(this.uiDialog).find('div.ui-dialog-titlebar').removeClass('ui-widget-header');
			jQuery(this.uiDialog).find('.ui-dialog-content').css('padding','0px');
			this.open();
		},
		
		close: function(event)
		{
			var self = this;
			if (false === self._trigger('beforeclose', event)) { return;	}
	
			(self.overlay && self.overlay.destroy());
			
			self.uiDialog.unbind('keypress.ui-dialog');
	
			(self.options.hide
				? self.uiDialog.hide(self.options.hide, function() { self._trigger('close', event); })
				: self.uiDialog.hide() && self._trigger('close', event));
			$.ui.dialog.overlay.resize();
			self._isOpen = false;
		},
	    
		_click: function(event)
		{
			var self = event.data[0];
			self._debug('_click');
			self._open();
			event.stopPropagation();
			return false;
		},
		
	    _container: function()
	    {
			this._debug( '_container ');
			var container = jQuery('<div />').addClass('dialog_box');
			if( this.options.iframe )
			{
				myiframe = jQuery('<iframe />')
								.attr('src', this.vURL)
								.attr('style','border:0;padding:0;margin:0;')
								.height('100%')
								.width('100%')
								.addClass('dialog_box_iframe')
								.appendTo(container); 
			} else { 
				mydiv = jQuery('<div />')
							.addClass('dialog_box_div')
							.html( jQuery.ajax({url: this.vURL,async:false,dataType:'html'}).responseText )
							.appendTo(container); 
			}
			
			return container; 
		},
	    _debug: function(input){ if( this.debug && console )	console.log(input,arguments);	}
	});
})(jQuery);
