﻿
function LoadModal(mURL, mModal)
{
    var width = 500;
        
    switch (mModal)
    {
        case 'calculater':
            width = 600;    
                        
            break;
            
        case 'feedback':
            width = 460;    
                        
            break;
    }
    
    $('#modalDarkPanel').height($(document).height());
    
    $('#modalMainPanel').css('width', width);
    
    $.ajax({ 
            type : "GET",  
            url : mURL,
            contentType : "application/html; charset=windows-1251",
            beforeSend : function beforeSendLoadModal()
                {
                    SetPosition($('#modalLoadingPanel'), null, null);
                    
                    $('#modalLoadingPanel_body').empty();
                    $('#modalLoadingPanel_body').html('Загружается...');
                      
                    $('#modalDarkPanel').show();
                    $('#modalLoadingPanel').show();
                } ,
            success : function successLoadModal(data)
                {
                    $('#modalMainPanel').empty();
                    $('#modalMainPanel').html(data);
                        
                    $('#modalLoadingPanel').hide();
                    
                    $('#modalMainPanel').show();
                    
                    if ($('#modalMainPanel').height() >= $(window).height())
                    {
                        SetPosition($('#modalMainPanel'), '5%', null);
                    }
                    else
                    {
                        SetPosition($('#modalMainPanel'), null, null);
                    }
                        
                    var widthWindow = $('#modalMainPanel').width();
                    var heightWindow = $('#modalMainPanel').height();
    
                    $('#modalHeader').css('width', widthWindow);
                    $('#modalHeaderBody').css('width', widthWindow - 20);
    
                    $('#modalFooter').css('width', widthWindow);
                    $('#modalFooterBody').css('width', widthWindow - 20);
                },
            error : function errorLoadModal()
                {
                    $('#modalLoadingPanel').hide();
                    $('#modalMainPanel').hide();
                    $('#modalDarkPanel').hide();
                }
            });
}
    
function CloseModal()
{
    $('#modalMainPanel').hide();
    $('#modalLoadingPanel').hide();
    $('#modalDarkPanel').hide();
}

function SetPosition(mElement, top, left)
{
    if (top == null)
    {
        top = $(window).height()/2 - mElement.height()/2;
    }
    
    if (left == null)
    {
        left = $(window).width()/2 - mElement.width()/2;
    }

    mElement.css('top', top);
    mElement.css('left', left);                 
}

function SetPositionByScroll(mElement, top, left)
{
    if (top == null)
    {
        top = ($(window).height()/2 - mElement.height()/2) + $(document).scrollTop();
    }

    if (left == null)
    {
        left = $(document).width()/2 - mElement.width()/2;
    }

    mElement.css('top', top);
    mElement.css('left', left);                 
}

function ShowSmallModal(message, width)
{
    if (width == null)
    {
        width = 320;
    }
    
    $('#modalSmallPanel').width(width);
    
    SetPositionByScroll($('#modalSmallPanel'), null, null);
    
    $('#modalSmallMessage').empty();
    $('#modalSmallMessage').html(message);
    
    $('#modalSmallDarkPanel').height($(document).height());
    
    $('#modalSmallDarkPanel').show();
    $('#modalSmallPanel').show();

    return false;
}

function HideSmallPanel()
{
    $('#modalSmallPanel').hide();
    $('#modalSmallDarkPanel').hide();
    
    return false;
}