
var fx_slide_base = new Class({

    /**
     * Stores panel sliding effect objects
     */
    obj_panel_slides: null,
    arr_blocks: null,
    
    /** 
     * Initialises panel sliding features
     */
    initialize: function(str_block_selector) {
        
        // global
        this.obj_panel_slides = new Array();
            
        // retrieve data blocks to attach ourselves to
        var arr_blocks = this.arr_blocks = $(document.body).getElements(str_block_selector);

        // create a new slide effect for each block
        for(var i = 0, ilen = arr_blocks.length; i < ilen; i++) {
            
            var elm_block = arr_blocks[i];
            var elm_title = elm_block.getElement('div.title');
            var elm_data = elm_block.getElement('div.content');

            // create slide effect - onComplete we toggle the parent class
            this.obj_panel_slides[elm_block.id] = new Fx.Slide(elm_data, {
                onStart: elm_block.toggleClass.bind(elm_block, 'closed') 
            });

            // do we need to close this immediately?
            if(elm_block.hasClass('closed')) {

                // note: slideOut() as opposed to hide() due to IE spacing issues bug
                this.obj_panel_slides[elm_block.id].slideOut();
                
                // note: because of the above, we need to switch the class back to closed
                elm_block.addClass('closed');
            }
            
            // add the click event to the title area
            elm_title.addEvent('click', this._event_panel_click.bind(this, [elm_block.id, i]));
        }
    },
    
    /**
     * Callback for when a panel title bar is clicked on. Either opens or closes
     */
    _event_panel_click: function(str_id, int_index) { 

        // retrieve the slide effect object associated with this data block
        var obj_slide;
        
        if(typeof(str_id) == 'object') { // Chrome returns an array for some reason
        
           obj_slide = this.obj_panel_slides[str_id[0]];
        }
        else {
           obj_slide = this.obj_panel_slides[str_id];
        }
        
        // toggle the open/closed class for the title and the sliding of the data block
        obj_slide.toggle();
    },
    
    /**
     * Expands or collapses all panels depending on the passed boolean
     */
    expand_all_panels: function(boo) {
        
        // loop through each panel slide effect (the key is the id)
        for(var id in this.obj_panel_slides) {
        
            var elm = $(id);
            
            if(elm) {
                
                // expand all panels
                if(boo == true) {
                    
                    // only expand if it's closed currently
                    if(elm.hasClass('closed')) {
                        this.obj_panel_slides[id].slideIn();
                    }
                }
                
                // collapse all panels
                else {
                    
                    // only collapse if it's open currently
                    if(!elm.hasClass('closed')) {
                        this.obj_panel_slides[id].slideOut();
                    }
                }
            }
        }
    }

});