(function($){

	jQuery.fn.extend({
		
		"ezModal":function($opt)
		{
			jQuery.ezModal.worker.create(this, $opt);
			jQuery.ezModal.worker.open();
		}
		
	});
	jQuery.ezModal=function($data, $opt)
	{
		jQuery.ezModal.worker.create($data, $opt);
		jQuery.ezModal.worker.open();
	}

	jQuery.ezModal.worker={
		"defaults":{
			"OverlayID":"ezOverlay",
			"OverlayCSS":{},
			"OverlayTrans":30,
			"ContentID":"ezContent",
			"ContentCSS":{"width":"300px"},
			"ContentTrans":100,
			"onOpen":null,
			"onClose":null,
			"onAJAXLoad":null,
			"AjaxURL":null,
			"AutoBorder":true,
			"NowLoading":"Now Loading...",
			"NoScroll":true,
			"AutoTitle":'Warning',
			"Draggable":false,
			"DragOption":{},
			"Overflow":"hidden",
			"ZBase":3000
		},
		"overlay":null,
		"border":null,
		"content":null,
		"opt":null,
		"iframe":null,
		"btnClose":null,
		"overflowBak":'',
		"create":function($data, opt)
		{
			this.opt={};
			jQuery.extend(this.opt, this.defaults, opt);
			
			if(!$data && opt.AjaxURL)
				$data=this.opt.NowLoading;
	
			
			var w=jQuery(window).width();
			var h=jQuery(window).height();
			var opa=this.opt.ContentTrans/100;
			if(w<jQuery(document.body).innerWidth())w=jQuery(document.body).innerWidth();
			if(h<jQuery(document.body).innerHeight())h=jQuery(document.body).innerHeight();
			w+='px';
			h+='px';
	
			this.overlay=jQuery('<div>')
				.attr('id', this.opt.OverlayID)
				.css(jQuery.extend(
					{
						"background-color":"#000000",
						"opacity":this.opt.OverlayTrans/100,
						"position":"absolute",
						"cursor":"wait",
						"width":"100%",
						"height":h,
						"top":'0px',
						"left":'0px',
						"z-index":this.opt.ZBase
					},
					this.opt.OverlayCSS
				))
				.hide()
				.prependTo(document.body);
			this.border=jQuery('<div>').hide()
				.addClass(this.opt.ContentID)
				.css(jQuery.extend({
						"z-index":this.opt.ZBase+1,
						"opacity":opa,
						"position":"absolute",
						"top":"0px",
						"left":"0px"
					},
					this.opt.ContentCSS
				))
				.css('background-color','')
				.prependTo(document.body);
			this.content=jQuery('<div>')
				.attr('id',this.opt.ContentID)
				.css(jQuery.extend(
					{
						"opacity":opa,
						"overflow":this.opt.Overflow,
						"z-index":this.opt.ZBase+2
					},
					this.opt.ContentCSS
				))
				.hide().append(jQuery($data).clone())
				.appendTo(this.border);
			if(this.opt.AutoBorder)
			{
				this.border.css({
					"background-color":"#c0c0c0",
					"border-top":"1px solid #ffffff",
					"border-bottom":"1px solid #333333",
					"border-left":"1px solid #ffffff",
					"border-right":"1px solid #333333",
					"padding":"2px"
				});
				this.btnClose=jQuery('<div>')
					.css({
						"background-color":"#0000FF",
						"opacity":"1.0",
						"border":"0px",
						"padding":"1px",
						"top":"0px",
						"left":"0px",
						"width":"200px",
						"vertical-align":"middle"
					}).hide()
					.html('<table width="100%" height="16" cellspacing="0" cellpadding="0"><tr><td style="color:#ffffff;font-size:12px;">'+this.opt.AutoTitle+'</td><td width="18" align="center"><input type="button" value="X" style="font-size:9px;width:16px;height:16px;padding:0px;font-weight:bold;" onclick="jQuery.ezModal.close();return false;" /></td></tr></table>')
					.prependTo(this.border);
			}
			if(this.opt.Draggable)
			{
				if(this.opt.AutoBorder)
				{
					this.border.draggable(jQuery.extend(
						{
							"cursor":"move",
							"opacity":"0.7",
							'handle':this.btnClose
						},
						this.opt.DragOption
					));
					this.btnClose.css('cursor','move');
				}
				else
				{
					this.border.draggable(jQuery.extend(
						{
							"cursor":"move",
							"opacity":"0.7"
						},
						this.opt.DragOption
					));
				}
			}
			this.iframe=jQuery('<iframe src="javascript:false;">')
				.css({
					 "opacity":"0.0",
					 'position':'absolute',
					 'width':'100%',
					 'height':h,
					 'top':'0px',
					 'left':'0px',
					 "z-index":1
				})
				.hide()
				.appendTo(document.body);
		},
		"reposition":function()
		{
			var w=jQuery(window).width(),h=jQuery(window).height();
			var x=jQuery(window).scrollLeft(),y=jQuery(window).scrollTop();
			var a=this.border.width(),b=this.border.height();
			var pl=parseInt(this.border.css('padding-left'));
			var pr=parseInt(this.border.css('padding-right'));
			if(isNaN(pl)) pl=0;
			if(isNaN(pr)) pr=0;
			this.iframe.width(jQuery(document).width());
			this.iframe.height(jQuery(document).height());
			this.iframe.css('top',y);
			this.overlay.width(jQuery(document).width());
			this.overlay.height(jQuery(document).height());
			this.overlay.css('top',y);
			if(this.btnClose!=null)
			{
				this.btnClose.width(this.content.width());
				this.btnClose.height(18);
				this.btnClose.css('left', this.border.offset({border:true,padding:true}).left+1);
				this.btnClose.css('top', this.border.offset({border:true,padding:true}).top);
				this.content.css('margin-top','1px');
			}
			this.border.css({
				"left":Math.floor((w-a)/2+x),
				"top":Math.floor((h-b)/2+y)
				
			});
			if(this.opt.NoScroll)
			{
				jQuery('body').css('overflow','hidden');
			}
		},
		"bindEvent":function()
		{
			jQuery(window).bind('scroll', this, function(e){e.data.reposition();});
			
		},
		"unbindEvent":function()
		{
			jQuery(window).unbind('scroll');
		},
		"open":function(t)
		{
			if(this.opt.NoScroll)
			{
				jQuery('body').css('overflow','hidden');
			}

			if(typeof t!='number' || t!=1)
			{
				this.overlay.slideDown();
				this.iframe.show();
				this.border.show();
				this.content.show();
				if(this.btnClose!=null)this.btnClose.show();
				this.reposition();
				if(typeof this.opt.onOpen=='function')
					this.opt.onOpen(this);
			}
			if(this.opt.AjaxURL)
			{
				jQuery.get(this.opt.AjaxURL, {}, function(d){
					jQuery.ezModal.worker.content.empty().append(d);
					if(jQuery.ezModal.worker.btnClose) jQuery.ezModal.worker.btnClose.prependTo(jQuery.ezModal.worker.border);
					jQuery.ezModal.worker.reposition();
					if(typeof jQuery.ezModal.worker.opt.onAJAXLoad=='function')
						jQuery.ezModal.worker.opt.onAJAXLoad(jQuery.ezModal.worker, d);
				});
			}
			this.bindEvent();
		},
		"close":function()
		{
			jQuery('body').css('overflow', 'auto');
			this.unbindEvent();
			if(this.content!=null)
				this.content.remove();
			if(this.btnClose!=null) this.btnClose.remove();
			if(this.border!=null)
				this.border.remove();
			if(this.overlay!=null)
				this.overlay.remove();
			if(this.iframe!=null)
				this.iframe.remove();
			if(this.opt!=null)
			{
				if(typeof this.opt.onClose=='function')
					this.opt.onClose(this);
			}
			this.content=null;
			this.border=null;
			this.overlay=null;
			this.iframe=null;
			this.opt=null;
			this.btnClose=null;
		}
	};
	jQuery.ezModal.close=function(){jQuery.ezModal.worker.close();};


})(jQuery);