var eShadowBox = function ()
{
    var store = {};   
    var options = {overlayId   : 'eshadow_overlay',
                   contentId   : 'eshadow_content',
                   width       : null,
                   height      : null};
                                  
    var overlayEl = null, contentEl = null, contentName = null, topOffset = 0, contentWidth = null, contentHeight = null;
    
    function initOverlayDiv()
    {
        if (null === overlayEl) {
            addOverlayDiv();
            overlayEl.height($(document).height());
            bindScrollEvent();
            bindResizeEvent();
            bindClickEvent();
        }               
    }

    function initContentPosition(width, height)
    {
        var left = ($(window).width() > width) ? Math.round(($(window).width() - width) / 2) : 0;

        calculateTopOffset(width, height);
        var top = topOffset + $(window).scrollTop();                
        
        contentEl.css({top:top + 'px', left:left + 'px'});
    }

    function calculateTopOffset(width, height)
    {
        topOffset = ($(window).height() > height) ? Math.round(($(window).height() - height) / 2) : 0;
    }

    function bindScrollEvent()
    {
        $(window).scroll(function () {
            if (topOffset > 0) {
                var top = topOffset + $(window).scrollTop();
                
                contentEl.css({top:top});
            }
        });
    }
    
    function bindResizeEvent()
    {
        $(window).resize(function () {
            initContentPosition(contentWidth, contentHeight);
        });
    }
    
    function bindClickEvent()
    {
        overlayEl.click(function() {eShadowBox.close();});
    }
        
    function addOverlayDiv()
    {
        $('body').append('<div id="' + options.overlayId + '"></div>').append('<div id="' + options.contentId + '"><div class="content"></div><div class="close"><a href="#">Закрыть</a></div></div>');
        
        overlayEl = $('#' + options.overlayId);        
        contentEl = $('#' + options.contentId);
        $('div.close a', contentEl).click(function() {eShadowBox.close();});
    }
    
    function putHtmlToContentEl(header, html)
    {
        if (null !== header) {
            contentEl.prepend('<h3 class="header">' + header + '</h3>');
        }
        
        $('div.content:eq(0)', contentEl).html(html);
    }
    
    function setupSizes(name)
    {
        if (null !== store[name].options.width) {
            contentWidth = store[name].options.width;
        } else {
            contentWidth = contentEl.width();
        }
        
        if (null !== store[name].options.height) {
            contentHeight = store[name].options.height;
        } else {
            contentHeight = contentEl.height();                    
        }
    }

    return {
        setup : function(name, html, header, lOptions)
        {
            store[name] = {'html'    : html, 
                           'header'  : header,
                           'options' : $.extend(options, lOptions)}
        },
        
        open : function(name)
        {
            if (typeof(store[name]) != 'undefined') {
                initOverlayDiv();                
                
                if (contentName != name) {
                    putHtmlToContentEl(store[name].header, store[name].html);
                    setupSizes(name);
//                    contentEl.width(store[name].options.width).height(store[name].options.height);                    
                    contentName = name;
                }
                
                initContentPosition(contentWidth, contentHeight);
                
                overlayEl.show();
                contentEl.show();
            }
        },
        
        close : function()
        {
            if (null !== overlayEl) {
                overlayEl.hide();
            }
            
            if (null !== contentEl) {
                contentEl.hide();
            }
        },
        
        updateCurrentContent : function(header, html)
        {
            putHtmlToContentEl(header, html);
            setupSizes(contentName);
            calculateTopOffset(contentWidth, contentHeight);
        }
    }
} ();