/**
** Author: Tongo M
** this adds an accordion effect to all elements with a class of accordion_toggle
** That toggle div should also have a first child of a div with a plus(+) sign in it and a second child div with a minus sign in it
** The content div should be the next sibbling to the toggle div see the onload events defined below for adding content to the divs if its to be done through ajax(recommended to reduce first load page size)
**/

/*-----------------------------------------------------------------------------------------------*/

if (typeof Effect == 'undefined') 
    throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create({
    // set up variables
    duration :0.5,
    actionIsOpen   :null,
    toggleActiveClass: 'accordion_toggle_active',
    accordionVisibleClass:'accordion_content_active',
    
    // Our main handler
    handleToggle: function(ele){
  //      alert(ele.identify);
        if(!$(ele)){
            throw(ele + ' does not exist!!');
        }
        // we are only interested in the next sibling of this element
        var currentAccordion = $(ele).next();
        if(!currentAccordion){
            throw('content for Accordion ' + ele + ' not found');
        }
        if($(ele).hasClassName(this.toggleActiveClass)){
            this.actionIsOpen = false; // show the content
        }else{
            this.actionIsOpen = true // hide the content
        }
        
        // we now have all we need, handle the accordion
        var performAction = this.processAccordion($(ele), currentAccordion);
    }, 
    
    // do something !!!!
    processAccordion: function(toggler, currentAccordion){            
        // toggle the cta
        togglerImg = $$('#' + toggler.identify() + ' img.togglerImg')[0];
        if (togglerImg && togglerImg.tagName.toUpperCase() == 'IMG') {
            togglerImg.toggleClassName('ctaGreyArrowDown');
            togglerImg.toggleClassName('ctaGreyArrowUp');
        }

        if (this.actionIsOpen){
            // reset the toggle
            toggler.addClassName(this.toggleActiveClass);
            // open it up
            new Effect.BlindDown(currentAccordion, {duration: this.duration,transition: Effect.Transitions.linear});
            // @TODO : consider checking the height so that we can enforce a max-height that IE6 will also listen to
        } else {
            // we want the toggle active, if that means anything
            toggler.removeClassName(this.toggleActiveClass);
            // shut it down
            new Effect.BlindUp(currentAccordion, {duration:this.duration,transition: Effect.Transitions.linear});
        }
    }
});
//  On load, lets add some event listers to load the data into the accordions so we don't have to pre-load!
// 
Event.observe(window, 'load', loadAccordions, false);
var frontPageAccordion;
//
//  Set up all accordions
//
function loadAccordions() {
    frontPageAccordion = new accordion();
    // set the onclick for each accordion to the ajax to load the data. The onclick event is removed in each respective js file when the data is loaded..
    // file admin tools
    if ($('fileAdminToolsContainer')){
        Event.observe('adminToolsToggle', 'click', callFileAdminTools);
    }
    // file reviews
    if ($('reviewsContainer')){
        Event.observe('fileReviewsToggle', 'click', callFileReviews);
    }
    // storyboard
    if ($('storyBoardContainer')){
        Event.observe('fileStoryBoardToggle', 'click', callFileStoryBoard);
    }
    // exif data
    if ($('fileExifDataContainer')){
        Event.observe('exifToggle', 'click', callFileExifData);
    }
    // file process queue
    if ($('fileProcessQueueToolContainer')){
        Event.observe('fileProcessToggle', 'click', callFileProcessQueue);
    }
    // set the accordion onclick events afterwards so that data starts loading a bit before the accordion opens, one can only hope
    var accordions = $$('.accordion_toggle');
        accordions.each(function(accordion) {
            Event.observe(accordion, 'click', function(){frontPageAccordion.handleToggle(accordion);}, false);
            accordion.next().setStyle({
                display: 'none'
            });
        }.bind(this));
    
}
// Functions to grab the Ajax for the contents of the Accordions if we ever toggle any of them
function callFileReviews(){
    FileReviewsAjax.getFileReviews(fcu_fileID);
}
function callFileExifData(){
    FileExifDataAjax.getFileExifData(fcu_fileID);
}
function callFileAdminTools(){
    FileAdminToolsAjax.getFileAdminTools(fcu_fileID);
}
function callFileProcessQueue(){
    FileProcessQueueToolAjax.getFileProcessQueueTool(fcu_fileID);
}
function callFileStoryBoard(){
    FileStoryBoardAjax.getFileStoryBoard(fcu_fileID);
}

