﻿var BlockDocReady = false;

try{
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
catch(ex) {
    BlockDocReady = true;
}
    
function BeginRequestHandler(sender, args){
    //if (args.get_error() == undefined) {
        BlockDocReady = false;
    //}
}
function EndRequestHandler(sender, args) {
if (args.get_error() == undefined) {
    BlockDocReady = true;
}
}


function ResizeBlock(type, id, width, height) {

    if(BlockDocReady)
    {
        switch(type){
            case serverVars.TypeBlockContent : 
                var bc = serverVars.Controls["block_cont_div_" + id].getJQ();
                var  cntWidth = bc.outerWidth();
                var cntHeight = bc.outerHeight();
                if(bc.width() <= 0 || bc.height()<=0)
                    break;
                bc.width(width);
                bc.height(height);
                var children = bc.children();
                jQuery.each(children, function(){
                     var w = jQuery(this).outerWidth();
                     var h = jQuery(this).outerHeight();
                     if(w > cntWidth)
                        cntWidth = w;
                     if(h > cntHeight)
                        cntHeight = h;
                });
                
                bc.jScrollPane({contentHeight:cntHeight, contentWidth:cntWidth, scrollbarMargin:0,scrollbarWidth:8,scrollbarHeight:8,scrollbarMargin:3});
            break;
            case serverVars.TypeBlockDesigned : 
                var bd = serverVars.Controls["block_des_div_" + id].getJQ();
                if(bd.height() <= 0)
                    break;
                bd.height(height - 50);
                var pd = serverVars.Controls["block_des_p_" + id].getJQ();
                if(pd.width() <= 0 || pd.height() <= 0)
                    break;
                pd.width(width - 5);
                pd.height(height - 145);
                pd.jScrollPane({scrollbarMargin:0,scrollbarWidth:8,scrollbarHeight:8,scrollbarMargin:3});
            break;
            case serverVars.TypeBlockFiles : 
                var bf = serverVars.Controls["block_files_div_" + id].getJQ();
                if(bf.width() <= 0 && bf.height() <= 0)
                    break;
                bf.height(height - 30);
                bf.width(width);
                bf.jScrollPane({scrollbarMargin:0,scrollbarWidth:8,scrollbarHeight:8,scrollbarMargin:3});
            break;
        }
    }
    else
    {
        setTimeout(function(){ ResizeBlock(type, id, width, height);}, '20');
    }
}