// #####################################
// Accordion constructor
function Accordion(globalReference, divId){
    this.name = globalReference;
	this.divId = divId;
    this.folds = Array();
    if (arguments.length > 2) { this.toggleState = arguments[2]; } else { this.toggleState = false; }
    if (arguments.length > 3) { this.toggleStateText = arguments[3]; } else { this.toggleStateText = false; }
}
// write html into div
Accordion.prototype.init = function() {
    // loop over folds, createHTML tags
    for (var i=0; i<this.folds.length; i++) {
        var foldElement = this.folds[i].createHTML();
        // attach to accordion div
        document.getElementById(this.divId).appendChild(foldElement); // the tab
        this.folds[i].attachEventsContentCSS(); // setup click events, grab content from temp divs, grab custom css
    }    
}
// write html into div
Accordion.prototype.addFold = function(foldObject) {
    // add this fold to the folds array
    this.folds.push(foldObject);
    // set this folds index num
    foldObject.indexNumber = this.folds.length-1;
    foldObject.parentAccordion = this;
}
// open fold command; arg: 'all'
Accordion.prototype.openAll = function() {
    // call appropriate folds click command    
    for (var i=0; i<this.folds.length; i++) {        
        if (!this.folds[i].opened) {
            this.folds[i].foldClick();
        }
    }
}
// close fold command; arg: 'all'
Accordion.prototype.closeAll = function() {
    // call appropriate folds click command
    for (var i=0; i<this.folds.length; i++) {        
        if (this.folds[i].opened) {
            this.folds[i].foldClick();
        }
    }
}

function beforeIE8(){
	return $.browser.msie && jQuery.browser.version < 7.999;
}

// #####################################
// AccordionFold constructor
function AccordionFold(globalReference, contentTempDivId, title) {
    //alert(arguments.length);
    this.name = globalReference;
    this.contentTempDivId = contentTempDivId;
    this.title = title;
    if (arguments.length > 3) { this.introHTML = arguments[3]; } else { this.introHTML = ''; }
    if (arguments.length > 4) { this.opened = arguments[4]; } else { this.opened = false; }
    this.tabDivId = null;
    this.holderDivId = null;
    this.contentDivId = null;
    this.introDivId = null;
    this.parentAccordion = null;
    this.indexNumber = null;
}
// create this folds HTML to be placed in accordion div;
AccordionFold.prototype.createHTML = function() {
    // fold wrapper
    var wrapDiv = document.createElement('div');
    wrapDiv.setAttribute('id',this.name+'_wrap');
    wrapDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'fold_wrap');    
       
    // create the tab
    var tabDiv = document.createElement('div');
    tabDiv.setAttribute('id',this.name+'_tab');
    tabDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'fold_tab');
    this.tabDivId = this.name+'_tab';
    
    var tabAnchor = document.createElement('a');
    tabAnchor.setAttribute('href','javascript:;');
    tabAnchor.setAttribute('id',this.name+'_link');
    
    var titleDiv = document.createElement('div');
    titleDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'title');
    var titleText = document.createTextNode(this.title);
    titleDiv.appendChild(titleText);
    
    tabAnchor.appendChild(titleDiv);
    
    var linkDiv = document.createElement('div');
    linkDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'link');
    var linkText = this.opened == true ? document.createTextNode('Hide') : document.createTextNode('Show');
    linkDiv.appendChild(linkText);
    
    if (this.parentAccordion.toggleStateText) {  // only attach if accordion is set to display this
        tabAnchor.appendChild(linkDiv);
    }
    
    tabDiv.appendChild(tabAnchor);    
    wrapDiv.appendChild(tabDiv); // add to the wrapper
    
    if (this.introHTML != '') {
        var introDiv = document.createElement('div');
        introDiv.setAttribute('id',this.name+'_intro');
        introDiv.setAttribute((beforeIE8() ? 'className' : 'class'), 'fold_intro');
        this.introDivId = this.name+'_intro';
        wrapDiv.appendChild(introDiv); // add to the wrapper
    }
    
    // create the holder
    var foldHolderDiv = document.createElement('div');
    foldHolderDiv.setAttribute('id',this.name+'_holder');
    foldHolderDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'fold_holder');
    this.holderDivId  = this.name+'_holder';
    
    var foldContentDiv = document.createElement('div');
    foldContentDiv.setAttribute('id',this.name+'_content');
	foldContentDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'fold_holder_inner');
    this.contentDivId = this.name+'_content';
    
    foldHolderDiv.appendChild(foldContentDiv);
    wrapDiv.appendChild(foldHolderDiv); // add to the wrapper
    
    // place the content
    return wrapDiv;
}
// create this folds HTML to be placed in accordion div;
AccordionFold.prototype.attachEventsContentCSS = function() {
    if (!this.opened) {
        // hide the content div
        eval("$('#"+this.name+"_holder').hide();");
    } else {
        // turn the arrow down
        this.setStateDisplay();
    }
    // create the onclick functions
    eval("$('#"+this.name+"_link').click(function () { "+this.name+".foldClick(); });");
    
    // grab the content from the holder div and put it in the holder div
    document.getElementById(this.contentDivId).innerHTML = document.getElementById(this.contentTempDivId).innerHTML;
    if (this.introHTML != ''){
        document.getElementById(this.introDivId).innerHTML = this.introHTML;
    }
	// removed the content from the temp id
	document.getElementById(this.contentTempDivId).innerHTML = '';
    
    // if the user has set a custom style for this fold apply that style to the content div
    var classes_string = eval("$('#"+this.contentTempDivId+"').attr('class');"); // get the classes of the fold container
    var classes = classes_string.split(' ');
    if (classes.length > 1) {
        for (var i=0; i<classes.length; i++) {
            if (classes[i] != 'fold_content'){
                eval("$('#"+this.contentDivId+"').addClass('"+classes[i]+"')"); // add these classes to the content div
            }
        }
    }
}
// open this fold
AccordionFold.prototype.foldClick = function() {
    if (this.opened) {
        // fade content out & slideUp the holder
        eval("$('#"+this.contentDivId+"').fadeTo('fast', 0.01, function(){ $('#"+this.holderDivId+"').slideUp('fast'); });");        
        // reset state
        this.opened = false;
        this.setStateDisplay(); // arrow
        this.setStateText(); // text
    } else {
        // fade slideDown & fade content in
        eval("$('#"+this.contentDivId+"').css('opacity', 0.01);");
        eval("$('#"+this.holderDivId+"').slideDown('fast', function(){ $('#"+this.contentDivId+"').fadeTo('fast', 1.0); });");
        // reset state
        this.opened = true;
        this.setStateDisplay(); // arrow
        this.setStateText(); // text
    }
}
// toggles 'open' class on the tab, a, title (class) div
AccordionFold.prototype.setStateDisplay = function() {
    if (this.parentAccordion.toggleState) {
        // change arrow orientation by adding 'open' class to the tab, link, title container
        if (this.opened) {
            eval("$('#"+this.tabDivId+"').addClass('open');");
        } else { 
            eval("$('#"+this.tabDivId+"').removeClass('open');");
        }
    }
}
// toggles 'Hide'/'Show' text in the tab, a, link (class) div
AccordionFold.prototype.setStateText = function() {
    if (this.parentAccordion.toggleStateText) {
        // change arrow orientation by adding 'open' class to the tab, link, title container
        if (this.opened) {
            // change right link text
            eval("$('#"+this.tabDivId+"').children('a').children('.link').empty('');");
            eval("$('#"+this.tabDivId+"').children('a').children('.link').append('Hide');");
        } else { 
            eval("$('#"+this.tabDivId+"').children('a').children('.link').empty('');");
            eval("$('#"+this.tabDivId+"').children('a').children('.link').append('Show');");
        }
    }
}

