
var section  = new Object();

var workarea;
var fontbox;

var wa_width   = -1;
var wa_height  = -1;
var wa_columns = -1;
var c_fontsize = -1;

var column_width = 220;
var column_gap   = 20;



function blockAppEventProcessor( ev ) {

    switch( ev.type ) {

        case "documentLoaded" :
            workarea = document.getElementById('content_scroller');
            fontbox = document.getElementById("copyright");
            SetResize();
            top.setInterval( SetResize, 10 );
            break;

    }

} // function blockEventProcessor()
top.registerAppEventProcessor( self, "blockAppEventProcessor" );


function SetResize() {

    new_fontsize  = fontbox.offsetHeight;
    new_wa_width  = workarea.offsetWidth;

    new_wa_columns = Math.floor(
        (workarea.offsetWidth - column_gap * 2 + 3) / (column_gap + column_width)
    );

    if(
           c_fontsize     != new_fontsize
        || new_wa_columns != wa_columns
    ) {

        wa_columns = new_wa_columns;
        SetWidth_MainBox();
        SetHeight_AllBoxes();
        c_fontsize = new_fontsize;
        wa_width   = new_wa_width;

    }

} // function SetResize()


function SetWidth_MainBox() {

    var change_width = false;
    for( var name in section ) {

        var sect = section[name];
        if( !sect.mainbox.mainbox ) continue;

        var freespace = wa_columns - (sect.a_smallbox.length > 0 ? 1 : 0);

        if( freespace < sect.mb_width_max ) {
            sect.mb_width = sect.mb_width_min;
        }

        if( freespace > sect.mb_width_min ) {
            sect.mb_width = sect.mb_width_max;
        }
        
        //
        // mainbox_no_resize = true, когда в статью вставлен большой плеер
        // значение задается в соответсвующем плагине tinyMCE - play2best
        //
        if( mainbox_no_resize == true ){
        	sect.mb_width = sect.mb_width_max;
        }

        if( sect.mainbox.mainbox.className != 'mainbox-w' + sect.mb_width ) {
            change_width = true;
        }
        
        sect.mainbox.mainbox.className = 
            'mainbox-w' + sect.mb_width;
        ;

    } // for( var name in section )

    if( change_width ) {
        dispatchAppEvent( { type : "mainboxChangeWidth" } );
    }
    

    
} // function SetWidth_MainBox()


function SetHeight_AllBoxes() {

    for( var name in section ) {

        var sect = section[name];

        if( sect.mainbox.mainbox ) {
            sect.mainbox.mainbox.style.height = '';
        }

        var line_top = -1;
        var line_height = -1;
        var a_smallbox = sect.a_smallbox;
        var smallbox, content;
        for( var i = 0; i < a_smallbox.length; i++ ) {

            smallbox = a_smallbox[i].smallbox;
            content  = a_smallbox[i].content;

            smallbox.style.height = '';

            if( smallbox.offsetTop > line_top ) {

                align_height();

                // begin new line
                var line_begin  = i;
                var line_height = -1;
                var line_top    = smallbox.offsetTop;

            } // if( smallbox.offsetTop > line_top )

            // get the max line height
            if( a_smallbox[i].smallbox.scrollHeight > line_height ) {
                line_height = a_smallbox[i].smallbox.scrollHeight;
            }

        } // for( i in a_smallbox )

        align_height();

    } // for( var name in section )


    function align_height() {

        // process the line
        if( i > 0 ) {

            // mainbox and smallboxes corrections
            if( sect.mainbox.mainbox ) {

                mb_bottom = sect.mainbox.mainbox.offsetTop + sect.mainbox.mainbox.offsetHeight;
                ll_bottom = a_smallbox[line_begin].smallbox.offsetTop + line_height;
                ll_top    = a_smallbox[line_begin].smallbox.offsetTop;

                if( ll_top < mb_bottom ) {

                    // проверяем что нижний конец smallbox немного выходит за высоту mainbox
                    // в этом случае mainbox дотягивается до линии smallboxes
                    if( ll_bottom > mb_bottom ) {

                        sect.mainbox.mainbox.style.height =
                            ll_top
                          + line_height
                          - sect.mainbox.mainbox.offsetTop
                          + 'px'
                        ;

                    } else if( mb_bottom < ll_bottom + (line_height / 2) ) {

                        line_height += (mb_bottom - ll_bottom);

                    }

                } // if( ll_top < mb_bottom )
                
            } // if( sect.mainbox.mainbox )

            // now we have max line height and line top
            for( var j = line_begin; j < i; j++ ) {
                a_smallbox[j].smallbox.style.height = line_height + 'px';
            }

        } // if( i > 0 )

    } // function align_height()


} // function SetHeight_AllBoxes()
