(function($){
	var Modal=function(options,source){
		var self=this;
		this.defaults={
			id:'modal',
			content:'',
			overlayClose:true,
			effects:'simple'
		};
		this.templates={
			modal:'\
				<span class="modal-overlay"></span>\
				<div class="modal-holder">\
					<div class="modal-vcenter">\
						<table class="modal-hcenter"><tr>\
							<td class="modal-content"></td>\
						</tr></table>\
					</div>\
				</div>\
			'
		};
		this.effects={
			simple:{
				modalOpen:function(e,modal){
					$('.modal-overlay',modal.el)
						.animate({opacity:0},0)
						.animate({opacity:.8})
					;
					$('.modal-content',modal.el)
						.fadeOut(0)
						.fadeIn()
					;
				},
				modalClose:function(e,modal){
					$('.modal-overlay',modal.el).animate({opacity:0},function(){
						modal.close();
					});
					$('.modal-content',modal.el).fadeOut();
					return false;
				}
			}
		};
		this.availParams=['id','class'];
		this.open=function(){
			var content=$('.modal-content',this.el);
			content.append(options.content);
			if(typeof(options.xhr)!='undefined'){
				content
					.toggleClass('loading')
					.load(options.xhr,function(){
						$(this).toggleClass('loading');
						source.trigger('modalLoad',self);
					})
				;
			}
			if(options.overlayClose){			
				this.el.click(function(e){
	        var target=$(e.target);
					if(!(target.is('.modal-content') || target.parents('.modal-content').length)){
						self.closeTrigger();
					}
				});
			}
			if($.browser.msie && $.browser.version==6){
				self.fixIE6();
			}
			this.el.appendTo('body');
			source.trigger('modalOpen',this);
		};
		this.close=function(){
			source.trigger('modalUnload',self);
			self.el
				.remove()
				.html(self.templates.modal)
			;
		};
		this.closeTrigger=function(){
			var
				events=source.data('events').modalClose,
				value=true
			;
			if(typeof(events)!='undefined'){
				$.each(events,function(key,event){
					value=event.handler(event,self);
				});
			}
			if(value){
				this.el.queue(this.close);
			}
		};
		this.fixIE6=function(){
			$('.modal-overlay,.modal-holder',this.el).css({
				width:$(window).width(),
				height:$(window).height()
			});
			self.el.css('top',document.documentElement.scrollTop);
		};
		this.action=function(){
			var params={};
			if(typeof(source)=='undefined'){
				source=$(self);
			}else{
				source.click(function(){
					self.open();
					return false;
				});
			}
			if(typeof(options)!='object'){
				options={content:options};
			}
			options=$.extend(self.defaults,options);
			for(p in self.availParams){
				if(typeof(options[self.availParams[p]])!='undefined'){
					params[self.availParams[p]]=options[self.availParams[p]];
				}
			}
			self.el=$('<div/>',params);
			self.el.html(self.templates.modal);
			if($.browser.msie && $.browser.version==6){
				$(window).bind('resize scroll',self.fixIE6);
			}
			if(options.effects && typeof(self.effects[options.effects])!='undefined'){
				source.bind(self.effects[options.effects]);
			}
			if(options.closeHandler){
				$(options.closeHandler,'#'+self.el.attr('id')).live('click',function(){
					self.closeTrigger();
				});
			}
		}();
	};
	$.modal=function(options){
		return new Modal(options);
	};
	$.fn.modal=function(options){	
		return this.each(function(){
			new Modal(options,$(this));
		});
	};
})(jQuery);
