/************************************************/
/* IPB3 Javascript								*/
/* -------------------------------------------- */
/* ips.board.js - Board index code				*/
/* (c) IPS, Inc 2008							*/
/* -------------------------------------------- */
/* Author: Rikki Tissier						*/
/************************************************/

var _idx = window.IPBoard;

_idx.prototype.redhookcafboard = {
    _statusClick: 0,
    _statusDefaultValue: '',

    /*------------------------------*/
    /* Constructor 					*/
    init: function()
    {
        Debug.write("Initializing redhook.caf.board.js");

        document.observe("dom:loaded", function(){
            ipb.redhookcafboard.redhook_initCollapseableforums();

        });
    },

    redhook_initCollapseableforums: function()
    {
        $$('.ipb_table tr').each( function(tr)
        {
            $( tr ).identify();
            if ( ( tr.hasClassName('row1') || tr.hasClassName('row2') ) && !tr.hasClassName('redirect_forum') && !tr.hasClassName('redhook_caf_container') ) {
                $( tr ).addClassName( 'redhook_caf_full' );
                /* insert the image */
                $( tr ).down('td',3).update( '<a onclick="blur()" href="#" class="redhook_caf_toggle_forums right">-</a>' +  $( tr ).down('td', 3).innerHTML );
            }
        });

        cookie = ipb.Cookie.get('redhooks_caf_forums');

        if( cookie )
        {
            var cookies		= cookie.split( ',' );
            var newCookies	= new Array();

            //-------------------------
            // Little fun for you...
            //-------------------------

            for( var abcdefg=0; abcdefg < cookies.length; abcdefg++ )
            {
                if( cookies[ abcdefg ] )
                {
                    if( $( 'redhook_caf_' + cookies[ abcdefg ] ) )
                    {
                        /* Test for known class name */
                        var top   = $( 'redhook_caf_' + cookies[ abcdefg ] ).up('tr');
                        /* Get the cat id */
                        catid = top.up('div .category_block').down('h3').id.replace('category_', '');
                        /* Get the forums id */
                        forumsid = top.down('a').id.replace('redhook_caf_', '');

                        // Get the Container
                        container = $('redhook_caf_cat_'+catid);
                        // Gather some informations
                        title = top.down('td',1).down('h4').innerHTML;
                        iconsrc = top.down('td').down('img').src;
                        iconalt = top.down('td').down('img').alt;
                        icontitle = top.down('td').down('img').title;
                        icon = '<img src="' + iconsrc + '" alt="' + iconalt + '" style="padding-right: 5px; width:16px;" />';
                        insertdata = '<p id="minimized_forum_' + forumsid + '" class="redhook_caf_entry">' + icon + title + '&nbsp;<a onclick="blur()" href="#" class="redhook_caf_toggle_entry" style=" vertical-align:middle;">&nbsp;&nbsp;&nbsp;&nbsp;</a></p>';
                        // Add the new collapsed forum to the container
                        container.insert( insertdata );

                        // Add observer
                        ipb.delegate.register(".redhook_caf_toggle_entry", ipb.redhookcafboard.redhook_caf_showforum);

                        // Hide the forumsrow
                        top.addClassName('hide');

                        // Make the container visible (if not already)
                        if ( container.up('tr').hasClassName('hide') ) {
                            container.up('tr').removeClassName('hide');
                        }
                    }
                }
            }
        }


        ipb.delegate.register(".redhook_caf_toggle_forums", ipb.redhookcafboard.redhook_caf_collapseforum);
    },
    redhook_caf_showforum: function(e, elem ) {
        Event.stop(e);
        elem.identify();

        // Get some ids
        catid = elem.up('div .category_block').down('h3').id.replace('category_', '');
        /* Get the forums id */
        forumsid = elem.up('p').id.replace('minimized_forum_', '');
        var remove = $A();
        cookie = ipb.Cookie.get('redhooks_caf_forums');

        if( cookie == null )
        {
            cookie = $A();
        }
        else
        {
            cookie = cookie.split(',');
        }

        container = $('redhook_caf_cat_'+catid).up('tr');
        forumsrow = $('redhook_caf_'+forumsid).up('tr');
        // Remove the entry from the minimized forums listing
        $('minimized_forum_'+forumsid).remove();

        // Show the forumsrow
        forumsrow.removeClassName('hide');

        // Check the container
        if ( !container.down('p') || container.down('p').className == "" || container.down('p').className != 'redhook_caf_entry'  ) {
            container.addClassName('hide');
        }
        remove.push( forumsid);
        cookie = "," + cookie.uniq().without( remove ).join(',') + ",";

        ipb.Cookie.set('redhooks_caf_forums', cookie, 1);
        Event.stop( e );

    },
    redhook_caf_collapseforum: function(e, elem) {
        Event.stop(e);
        elem.identify();

        var remove = $A();
        cookie = ipb.Cookie.get('redhooks_caf_forums');

        if( cookie == null )
        {
            cookie = $A();
        }
        else
        {
            cookie = cookie.split(',');
        }

        /* Test for known class name */
        var top   = elem.up('tr');
        /* Get the cat id */
        catid = top.up('div .category_block').down('h3').id.replace('category_', '');
        /* Get the forums id */
        forumsid = top.down('a').id.replace('redhook_caf_', '');

        // Get the Container
        container = $('redhook_caf_cat_'+catid);
        // Gather some informations
        title = top.down('td',1).down('h4').innerHTML;
        iconsrc = top.down('td').down('img').src;
        iconalt = top.down('td').down('img').alt;
        icontitle = top.down('td').down('img').title;
        icon = '<img src="' + iconsrc + '" alt="' + iconalt + '" style="padding-right: 5px; width:16px;" />';
        insertdata = '<p id="minimized_forum_' + forumsid + '" class="redhook_caf_entry">' + icon + title + '&nbsp;<a onclick="blur()" href="#" class="redhook_caf_toggle_entry">&nbsp;&nbsp;&nbsp;&nbsp;</a></p>';
        // Add the new collapsed forum to the container
        container.insert( insertdata );
            
        // Add observer
        ipb.delegate.register(".redhook_caf_toggle_entry", ipb.redhookcafboard.redhook_caf_showforum);

        // Hide the forumsrow
        top.addClassName('hide');

        // Make the container visible (if not already)
        if ( container.up('tr').hasClassName('hide') ) {
            container.up('tr').removeClassName('hide');
        }

        cookie.push( forumsid );
        cookie = "," + cookie.uniq().without( remove ).join(',') + ",";

        ipb.Cookie.set('redhooks_caf_forums', cookie, 1);
        Event.stop( e );

    }
}
ipb.redhookcafboard.init();
