(function($){
	var Modal=function(source,options){
		var self=this;
		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('xhrLoaded',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)
				.data('modal',this)
			;
		};
		this.close=function(){
			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={};
			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]);
			}
		}();
	};
	$.fn.modal=function(j){
		var options={};
		if(typeof(j)!='undefined'){
			if(typeof(j)=='object'){
				options=j;
			}else{
				options.content=j;
			}
		}
		return this.each(function(){
			var m=new Modal(this,$.extend({
				id:'modal',
				'class':$(this).attr('id'),
				content:'',
				overlayClose:true,
				effects:'simple'
			},options));
			$(this).click(function(){
				m.open();
				return false;
			});
		});
	};
})(jQuery);