function popup(show, opt, body, opt2){
        
    if(typeof opt == 'string'){        
        var header = opt;
        var opt = {};
        opt.header = header;
    }
    
    if(typeof body != 'undefined'){
        opt.body = body;
    }
    
    if(typeof body == 'string'){
        $.extend(opt, opt2);       
    }
    
    var rand_number = rand(0, 10000000000);     
    
    if(typeof show == 'undefined') show = 1;
    if(typeof opt == 'undefined') opt = {};
    if(typeof opt.header == 'undefined') opt.header = '';
    if(typeof opt.body == 'undefined') opt.body = '';
    if(typeof opt.width == 'undefined') opt.width = 400;
    if(typeof opt.height == 'undefined') opt.height = 'auto';
    if(typeof opt.buttons == 'undefined') opt.buttons = 1;
    if(typeof opt.type == 'undefined') opt.type = false;
    if(typeof opt.buttonOk == 'undefined') opt.buttonOk = "Применить";
    if(typeof opt.closeButton   == 'undefined') opt.closeButton = true;
    if(typeof opt.transparent == 'undefined') opt.transparent = false; 
    if(typeof opt.overlay == 'undefined') opt.overlay = true;     
    if(typeof opt.id == 'undefined') opt.id = 'popup_'+rand_number;    
    if(typeof opt.overlayId == 'undefined') opt.overlayId = 'overlay-global-'+opt.id; 
    
    
    
    if(typeof opt.buttonCancel == 'undefined'){
        if(opt.buttons === 1){
            opt.buttonCancel = "Ok";
        }else{
            opt.buttonCancel = "Отмена";
        }
    }
    
    //if(typeof opt.buttonCallbackOk != 'function') opt.buttonCallbackOk = defaultcallback;
    //if(typeof opt.buttonCallbackCalcel != 'function') defaultcallback;


    
        
    var cls = '';
    if(opt.type == 'err') cls = 'popup-err';
    if(opt.type == 'ok') cls = 'popup-ok';
    if(opt.type == 'notice') cls = 'popup-notice';
    
    if(opt.transparent) cls += ' popup-transparent';
    
    
    var box = '\
    <div class="overlay-global" id="'+opt.overlayId+'"></div>\
    <div id="'+opt.id+'" class="popup-bibil '+cls+'">';
    if(opt.closeButton)  box += '<div class="popup-bibil-close" title="Закрыть"></div>';
    
    box += '<h2 class="popup-header">'+opt.header+'</h2>\
        <div class="popup-body">'+opt.body+'</div>';
    
    if(opt.buttons === true || opt.buttons === 1)
    {
        box += '<div class="popup-buttons">';
        if(opt.buttons === true) box += '<input type="button" value="'+opt.buttonOk+'" class="button button-ok"/> ';
        box += '<input type="button" value="'+opt.buttonCancel+'" class="button button-cancel"/>'
        box += '</div>';
    }
        
    box +='</div>';
    
    var popup = null;
    var popupExists = false;
    
    if($("#"+opt.id).size() > 0){            
        //$("#popup").remove();        
        popup = $("#"+opt.id);
        popupExists = true;
    }else{
        $("body").append(box);
        popup = $("#"+opt.id);
    }    
    
    var ov = $("#"+opt.overlayId).eq(0);
    
    var close = function(){
       popup.hide();
       overlay(0); 
    }      
    
    if(show && !popupExists)
    {                
        var defaultCallbackOk = function(popup, opt){
            if(typeof opt.buttonCallbackOk == 'function'){
                return function(){                    
                    opt.buttonCallbackOk.call(popup, opt);
                }                                
            }else{
                return function(){                    
                    window.popup(0, {id: opt.id});
                }
            }
        }(popup, opt)
            
        
        var defaultCallbackCancel = function(options){
            if(typeof opt.buttonCallbackCancel == 'function'){
                return function(){                                        
                    opt.buttonCallbackCancel.call(options);
                }                                
            }else{              
                return function(){                                        
                    window.popup(0, {id: options.id})
                }
            }
        }(opt);    
    
      
        popup.find(".popup-header").eq(0).html(opt.header);
        popup.find(".popup-body").eq(0).html(opt.body);                
        
        popup.show();        
        if(opt.buttons){
            popup.find(".button-ok").eq(0).bind('click', defaultCallbackOk);            
            popup.find(".button-cancel").eq(0).bind('click', defaultCallbackCancel)            
        }
        var windowSize = getPageSize();        
        var wind_w = windowSize[0];
        var wind_h = windowSize[1];
        var wind_hh = windowSize[3];
        
        var scrolls = getPageScroll();                    
        var curtop = parseInt(popup.css("top"));        
        var newtop = scrolls[1] + 100;
        
        popup.css({maxWidth: opt.width+"px"});
        
        
        var popup_width = opt.width;
        var popup_height = opt.height;
        
        var popup_width = popup.width();        
        var popup_height = popup.height();        
        
        
        
        if(popup_height < wind_h){
            
        }
        
        newtop = parseInt(((wind_hh/2)) - (popup_height/2) + scrolls[1]);
        
        var left = parseInt((wind_w/2) - (popup_width/2));                
        popup.css({top: newtop+"px", left: left+"px"});
        setPopupEvents();                        
        if(opt.overlay) overlay(ov, 1);
        
    }else if(!show)
    {
        popup.remove();
        overlay(ov, 0);
    }
    
    return {
        popup: popup
    }
}

function setPopupEvents()
{
    var els = ".popup-bibil";
    //$(els).unbind();
    $(els).each(function(){
        var closeButton = $(this).find('.popup-bibil-close').eq(0);
        if(closeButton.size() > 0)
        {
            var id = $(this).attr('id');            
            closeButton.click(function(){                                        
                popup(0, {id: id});
            })        
        }
    })
}


function overlay(ov, show)
{
      
    
    //var ov = $(".overlay-global");    
    var me = this;

    if(typeof me.zindex == 'undefined'){
        me.zindex = 130;
    }else{
        me.zindex++;
    }
    
    if(show)
    {
        
        var scrolls = getPageScroll();
        var windowSize = getPageSize();
        var w = windowSize[0];
        var h = windowSize[1];
        h = h + 1000;
        
        $(".overlay-global").each(function(i, el){
            
            var zIndex = $(el).css("z-index");
            if(typeof zIndex != 'undefined'){
                //alert(me.zindex);
                //me.zindex = parseInt(zIndex) + 1;
            }
        })       
        
        ov.css({
                backgroundColor:    "#000",
                opacity:            0.7,                
                width:                w,
                height:               h,
                'z-index': me.zindex
            }).fadeIn();
        ov.show();
    }else
    {
        ov.remove();
    }
}


$(function(){
    
    
    $(document).keypress(function(ev){
        
        var code = ev.keyCode ? ev.keyCode : ev.which ? ev.which : null;
        if(code)
        {            
            if($("#popup").css("display") != 'none' && (ev.keyCode == 27))
            {
                popup(0);
            }
        }
    })  
    
})

