/************************************************/
/* IPB3 Javascript								*/
/* -------------------------------------------- */
/* ips.customBlocks.js -C Custom Blocks			*/
/* (c) http://www.invisionmodding.com			*/
/* -------------------------------------------- */
/* Author: Vadim Vincent Gabriel				*/
/************************************************/

var _customBlocks = window.IPBoard;

_customBlocks.prototype.customBlocks = {
    /*------------------------------*/
    /* Constructor 					*/
    init: function()
    {
        Debug.write("Initializing ipb_customBlocks.js");

        document.observe("dom:loaded", function(){


            // If we can't hide it then show it and exit;
            if( allowhideleft )
            {
                ipb.customBlocks.initLeftColumn();
            }
            else
            {
                ipb.customBlocks.hideUnhideSetting('left');
            }


            // If we can't hide it then show it and exit;
            if( allowhideright )
            {
                ipb.customBlocks.initRightColumn();
            }
            else
            {
                ipb.customBlocks.hideUnhideSetting('right');
            }

        });

    },

    initLeftColumn: function()
    {

        if( $('left_column').getStyle('display') == 'none' )
        {
            $('close_left_menu').hide();
            $('open_left_menu').show();
        }
        else
        {
            $('close_left_menu').show();
            $('open_left_menu').hide();
        }

        if( $('open_left_column') )
        {
            $('open_left_column').observe( 'click', ipb.customBlocks.ToggleLeft );
        }

        if( $('close_left_column') )
        {
            $('close_left_column').observe( 'click', ipb.customBlocks.ToggleLeft );
        }

        if( !leftexists )
        {
            $('close_left_menu').hide();
            $('open_left_menu').hide();
        }

    },
    ToggleLeft: function(e)
    {
        Effect.toggle( $('left_column'), 'blind', { duration: 0.4, afterFinish: function(e){
            if( $('left_column').getStyle('display') == 'none' )
            {
                ipb.customBlocks.hideUnhide('left', 0);
            }
            else
            {
                ipb.customBlocks.hideUnhide('left', 1);
            }

        } } );

        Event.stop(e);
    },

    initRightColumn: function()
    {

        if( $('right_column').getStyle('display') == 'none' )
        {
            $('close_right_menu').hide();
            $('open_right_menu').show();
        }
        else
        {
            $('close_right_menu').show();
            $('open_right_menu').hide();
        }

        if( $('open_right_column') )
        {
            $('open_right_column').observe( 'click', ipb.customBlocks.ToggleRight );
        }

        if( $('close_right_column') )
        {
            $('close_right_column').observe( 'click', ipb.customBlocks.ToggleRight );
        }

        if( !rightexists )
        {
            $('open_right_column').hide();
            $('close_right_menu').hide();
        }

    },
    ToggleRight: function(e)
    {
        Effect.toggle( $('right_column'), 'blind', { duration: 0.4, afterFinish: function(e){
            if( $('right_column').getStyle('display') == 'none' )
            {
                ipb.customBlocks.hideUnhide('right', 0);
            }
            else
            {
                ipb.customBlocks.hideUnhide('right', 1);
            }

        } } );

        Event.stop(e);
    },

    makeWidths: function( )
    {
        // By default they are all set to their default values
        new_middle_width = middle_width;
        new_right_width = needed_right_width;
        new_left_width = needed_left_width;

        top_middle_width = middle_width_set;
        top_right_width = right_width_set;
        top_left_width = left_width_set;

        max_middle_width = top_middle_width + top_right_width + top_left_width;

        //-------------------------------------------

        // Make sure both columns exists
        if( leftexists && rightexists )
        {
            // Both are shown
            if( $('right_column').getStyle('display') == 'none' )
            {
                new_right_width = 0;
                new_middle_width += needed_right_width;
            }
            else
            {
                new_right_width = top_right_width;
                new_middle_width += needed_left_width;
            }

            if( $('left_column').getStyle('display') == 'none' )
            {
                new_left_width = 0;
                new_middle_width += needed_left_width;
            }
            else
            {
                new_left_width = top_left_width;
                new_middle_width += needed_right_width;
            }
            
            if( $('left_column').getStyle('display') != 'none' && $('right_column').getStyle('display') != 'none' )
            {
                new_left_width = top_left_width;
                new_right_width = top_right_width;
                new_middle_width = top_middle_width;
            }

        }
        else if( leftexists == 0 && rightexists)
        {
            // Just the right column shown
            if( $('right_column').getStyle('display') == 'none' )
            {
                // right is hidden
                new_middle_width += needed_right_width + needed_left_width;
                new_right_width = 0;
                new_left_width = 0;
            }
            else
            {
                // Right is shown
                new_middle_width += needed_left_width;
                new_right_width = top_right_width;
                new_left_width = 0;
            }

        }
        else if( rightexists == 0 && leftexists)
        {
            // Just the left column shown
            if( $('left_column').getStyle('display') == 'none' )
            {
                // left is hidden
                new_middle_width += needed_right_width + needed_left_width;
                new_right_width = 0;
                new_left_width = 0;
            }
            else
            {
                // left is shown
                new_middle_width += needed_right_width;
                new_right_width = 0;
                new_left_width = top_left_width;
            }
        }
        else
        {
            // Both hidden
            new_middle_width += needed_right_width + needed_left_width;
            new_right_width = 0;
            new_left_width = 0;
        }

        // Make sure the middle is not over 100 when the two blocks are hidden
        if(new_middle_width > 100)
        {
            new_middle_width = 100;
        }
        
        if( new_right_width > 0 && new_middle_width == 100 )
        {
            new_middle_width -= needed_right_width;
        }
        
        if( new_left_width > 0 && new_middle_width == 100 )
        {
            new_middle_width -= needed_left_width;
        }

        // Set styles
        $('middle_column').setStyle( { width: parseInt( new_middle_width ) + '%' } );
        $('right_column').setStyle( { width: parseInt( new_right_width ) + '%' } );
        $('left_column').setStyle( { width: parseInt( new_left_width ) + '%' } );
    },

    hideUnhide: function( type, show )
    {
        if( show == 1 )
        {
            $('close_' + type + '_menu').show();
            $('open_' + type + '_menu').hide();
            //$( type + '_column_sep').show();
            ipb.Cookie.set('hide_' + type + '_column', '0', 1);
        }
        else
        {
            $('close_' + type +'_menu').hide();
            $('open_' + type + '_menu').show();
            //$( type + '_column_sep').hide();
            ipb.Cookie.set('hide_' + type + '_column', '1', 1);
        }

        // Sort widths
        ipb.customBlocks.makeWidths();
    },

    hideUnhideSetting: function( type )
    {
        $('close_' + type +'_menu').hide();
        $('open_' + type + '_menu').hide();
        $( type + '_column').show();

        // Sort widths
        ipb.customBlocks.makeWidths();
    }
}

ipb.customBlocks.init();