/*
 *  Wizzard Bulgaria
 *  Mask Plugin created by Wizzard
 *  http://www.wizzard.bg
 *  
 *  @ Plamen Nikolov
 *  
 *  Version 1.0
 */

(function($){

	$.fn.wizzard_mask = function(options) {
            var opts = $.extend({}, $.fn.wizzard_mask.defaults, options);  
            
            
            return this.each(function() {
      			  $(this).click(function(){
          		    init(this);
          		    $(this).blur();
      				
      				return false;
			    });   
		    });
            
            
            function init(obj) {
				
				opts.before_display()
            
                var mask_id = 'wizzad_modal_mask_' + $(obj).index();
                var mask = $('<div />').attr({id: mask_id, "class": "mask"}).css({
                    display: 'none',
                    background: opts.bgcolor,
                    opacity: opts.opacity,
                    position: 'fixed',
                    top: 0,
                    left: 0,
                    width: '100%',
                    height: '100%',
                    zIndex: opts.zIndex
                })
                
                mask.appendTo('body').fadeIn(opts.speed, opts.after_display)
                mask.click(close_all_modals)
                
                $('#' + opts.close_handler_id).click(function() {
                    mask.fadeOut(opts.speed, function() { $(this).remove()})
                    $('.' + opts.modal_window_class).hide()
                    $(document).unbind('keypress')
                })
           };
           
            
         function close_all_modals() {
            $('.modals').hide()
            $('.mask').fadeOut(opts.speed, function() { $(this).remove()})
          };
    };
        
    $.fn.wizzard_mask.defaults = {
    		bgcolor: '#000000',
            opacity: 0.5,
            speed: 1000,
            zIndex: 10,
            modal_window_class: 'modals',
            close_handler_id: 'modal_close',
			before_display: function() {},
            after_display: function() {}
    };
    
     $(document).keypress(function(e){  
    	   if(e.keyCode == 27) {
                $('.mask').fadeOut(200, function() { $(this).remove()})
                $('.modals').hide()
                $(document).unbind('keypress')
    	    }  
        });
})(jQuery)
