(function($) {
	$.activeModal=null;
	$.modalSettings={};
	$.extend($.fn, {
		showModal: function(settings) {
			$.modalSettings = $.extend({
				closeButton: true,
				autoOpen: false,
				onShow:function(el){},
				onClose:function(el){}
			}, settings);
			$(this).each(function() {
				if($.activeModal==null)
				{
					var el = $(this);
					$.activeModal=el.attr('id');
				    el.resizeModal($.activeModal);
				    $(document).bind('keydown.modal', function (e) {
						if (e.keyCode === 27) { // ESC
							e.preventDefault();
							el.closeModal();
						}
					});
					$(window).bind('resize.modal', function () {
						el.resizeModal($.activeModal);
					});	
					$(document).bind('scroll.modal', function () {
						el.resizeModal($.activeModal);
					});
					$('.close_modal').bind('click',function(e){
						e.preventDefault();
						el.closeModal();
				    });
				    $('#'+$.activeModal).fadeIn(500);
				    $('#modal_mask').css({ 'display' : 'block', opacity : 0});
				    $('#modal_mask').fadeTo(500,0.6);
				    $.modalSettings.onShow(el)
				}
			});
			return this;
		},
		modal : function(settings) {
			$.modalSettings = $.extend({
				closeButton: true,
				autoOpen: false,
				onShow:function(){}
			}, settings);
			if($('#modal_mask').length<1){
				$('body').append('<div id="modal_mask" class="close_modal"></div>');//.bind('click',function(){ $('#'+$.activeModal).closeModal();return false;});
			};
			$(this).each(function() {
				var el = $(this);
				if($.modalSettings.closeButton){
					var closeButton=$('<a class="close_modal_btn" href=""></a>').bind('click',function(){ $(this).parent().closeModal();return false;});
					el.addClass('modal_window').prepend(closeButton);
				}
				if($.modalSettings.autoOpen && $.activeModal==null)
				{
					el.showModal();
				}
			});
			return this;
		},
		closeModal:function(){
		    $('#modal_mask').fadeOut(500);
		    $('#'+$.activeModal).fadeOut(500);
		    $.modalSettings.onClose($('#'+$.activeModal));
			$(document).unbind('keydown.modal');
			$(window).unbind('resize.modal');
			$(document).unbind('scroll.modal');
			$('.close_modal').unbind('click');
			$.activeModal=null;
			
			return this;
		},
		resizeModal:function(id){
			$('#'+id).each(function(){
				var window_width = $(window).width();
				var window_height = $(window).height();
				var modal_height = $(this).outerHeight();
				var modal_width = $(this).outerWidth();
				var top = (window_height-modal_height)/2+$(window).scrollTop();
				var left = (window_width-modal_width)/2+$(window).scrollLeft();
				$(this).css({'top' : top , 'left' : left});
				$('#modal_mask').css({'height' : $(document).height() , 'width' : $(document).width()});
			});
			return this;
		}				
	});
})(jQuery);	


/* /* modal * /
$(document).ready(function(){
	$('body').append('<div id="modal_mask" class="close_modal"></div>');
   $('.modal_window').prepend('<a class="close_modal_btn" href=""></a>');
   $('.activate_modal').click(function(e){
		  e.preventDefault();
          var modal_id = $(this).attr('rel');
          $('#'+modal_id).appendTo('body');
          if($(this).attr('data-js')){
        	  eval($(this).attr('data-js'));
          }
          show_modal(modal_id);
    });
    $('.close_modal,.close_modal_btn').click(function(e){
		e.preventDefault();
        close_modal();
    });
    
	
});
function resizeModal(id){
	$('#'+id).each(function(){
		var window_width = $(window).width();
		var window_height = $(window).height();
		var modal_height = $(this).outerHeight();
		var modal_width = $(this).outerWidth();
		var top = (window_height-modal_height)/2+$(window).scrollTop();
		var left = (window_width-modal_width)/2+$(window).scrollLeft();
		$(this).css({'top' : top , 'left' : left});
		$('#modal_mask').css({'height' : $(document).height() , 'width' : $(document).width()});
	});
}
function close_modal(){
	activeModal=null;
    $('#modal_mask').fadeOut(500);
    $('.modal_window').fadeOut(500);
	$(document).unbind('keydown.modal');
	$(window).unbind('resize.modal');
	$(document).unbind('scroll.modal');
}
var activeModal;
function show_modal(modal_id){
	activeModal=modal_id;
    $('#modal_mask').css({ 'display' : 'block', opacity : 0});
    $('#modal_mask').fadeTo(500,0.5);
	resizeModal(modal_id);
    // bind keydown events
	$(document).bind('keydown.modal', function (e) {
		if (e.keyCode === 27) { // ESC
			e.preventDefault();
			close_modal();
		}
	});
	$(window).bind('resize.modal', function () {
		resizeModal(activeModal);
	});	
	$(document).bind('scroll.modal', function () {
		resizeModal(activeModal);
	});
    $('#'+modal_id).fadeIn(500);
}
*/
