(function($){
    $.modalWindow_create = function(options)
    {        
        var defaults =
            {
                width: 200,
                height: 200,
                path: '/modules/modal/',
                callback: function(){},
                html: '',
                id: 'id',
                title: 'title',
                show_btn_close: true,
                btn_close_click: function(){
                    $('#' + settings.id).modalWindow_remove();
                }
            },
        settings = $.extend({}, defaults, options);
    
        
        $.get(settings.path + 'modal.html', function(html){
            if($('body #modal_mask').length == 0 ){
                var $modal_mask = $('<div id="modal_mask"></div>');
                
                var new_height = 0;
                if($('#jedo').height() > $(window).height())                
                    new_height = $('#jedo').height();
                else
                    new_height = $(window).height();
                

                $modal_mask.css({
                    height: new_height + 'px'
                });
                
                $modal_mask.appendTo('body');
            }
            else if($('body #modal_mask').length == 1){
                var $modal_mask = $('body #modal_mask');
                
                var new_height = 0;
                if($('#jedo').height() > $(window).height())                
                    new_height = $('#jedo').height();
                else
                    new_height = $(window).height();
                

                $modal_mask.css({
                    height: new_height + 'px'
                });
            }
                        
            html = html.replace('[BODY]', settings.html);
            html = html.replace('[TITLE]', settings.title);

            if(settings.show_btn_close)
                html = html.replace('[BUTTONS]',
                    '<img id="btn_close" class=img_button src="' + settings.path + 'img/close.png" />');
            else
                html = html.replace('[BUTTONS]','');

            $('body').append(html);            
            $('#new_modal_window').attr('id', settings.id);
            var modalWindow = $('#' + settings.id);     
            
       
            modalWindow.css('width', (settings.width + 48) + 'px');
            modalWindow.css('height', (settings.height + 48) + 'px');


            $('#center_center', modalWindow).css('height', settings.height + 'px');
            $('#center_center', modalWindow).css('width', settings.width + 'px');
            $('#center_left', modalWindow).css('height', $('#center_center', modalWindow).outerHeight());
            $('#center_right', modalWindow).css('height', $('#center_center', modalWindow).outerHeight());
            $('#top_center', modalWindow).css('width', $('#center_center', modalWindow).outerWidth());
            $('#bottom_center', modalWindow).css('width', $('#center_center', modalWindow).outerWidth());
            
            modalWindow.hide();


            if(settings.show_btn_close)
                $('#btn_close', modalWindow).click(function(){
                    settings.btn_close_click();
                });

            settings.callback();
        });
    }

    $.fn.modalWindow_show = function(){
        var modalWindow = $(this);
       //modalWindow.show();
        
        
      
        center_element(window, '#' + modalWindow.attr('id'));
		
		
        modalWindow.css({
            top: (parseFloat(modalWindow.css('top')) + $(window).scrollTop()) + 'px',
			display: 'block'
        });        
		
		
		var $modal_mask = $('body #modal_mask');
		
		var new_height = 0;
		//if($('#jedo').outerHeight() > $(window).height())                
		//	new_height = $('#jedo').outerHeight();
		//else
			new_height = $(window).height() + $(window).scrollTop();
		

		$modal_mask.css({
			height: new_height*2 + 'px'
		});
		
		//return this;
    }

    $.fn.modalWindow_remove = function(){      
        $(this).remove();      
        if($('body .modal').length == 0)
            $('body #modal_mask').remove();
    }

    $.fn.modalWindow_disable = function()
    {
        $('<div class="modal_disable"></div>').appendTo($('#center_center', $(this)));
    }

    $.fn.modalWindow_enable = function()
    {
        $('.modal_disable', $('#center_center', $(this))).remove();
    }

    $.fn.modalWindow_load = function()
    {
        $('<div class="modal_load"></div>').appendTo($('#center_center', $(this)));
    }

    $.fn.modalWindow_loaded = function()
    {
        $('.modal_load',$('#center_center', $(this))).remove();
    }
	
	$.fn.modalWindow_resize = function(new_height, new_width, window_id)
    {
		var modalWindow = $('#' + window_id); 
		alert($('body').outerHeight() + ' - ' + $(window).outerHeight())
		$('body #modal_mask').css({
			height: $('body').outerHeight()
		});
	
        modalWindow.css('width', (new_width + 48) + 'px');
        modalWindow.css('height', (new_height + 48) + 'px');


        $('#center_center', modalWindow).css('height', new_height + 'px');
        $('#center_center', modalWindow).css('width', new_width + 'px');
        $('#center_left', modalWindow).css('height', $('#center_center', modalWindow).outerHeight());
        $('#center_right', modalWindow).css('height', $('#center_center', modalWindow).outerHeight());
        $('#top_center', modalWindow).css('width', $('#center_center', modalWindow).outerWidth());
        $('#bottom_center', modalWindow).css('width', $('#center_center', modalWindow).outerWidth());            
    }

})(jQuery);

