// #####################################
// Accordion constructor
function Accordion(globalReference, divId){
    this.name = globalReference;
	this.divId = divId;
    this.folds = Array();
    this.openFoldArrayIndex = null;
    if (arguments.length > 2) { this.toggleState = arguments[2]; } else { this.toggleState = false; }
    if (arguments.length > 3) { this.toggleStateText = arguments[3]; } else { this.toggleStateText = false; }
	this.fadeEffect = false;
	this.expanded = "";
	this.hasOneOpened = false;	// use this is expanded is blank
}
// write html into div
Accordion.prototype.init = function() {
	// Reset the hasOneOpened if expanded was set since it has presedence
	if (this.expanded != "") {
		this.hasOneOpened = false;
		this.openFoldArrayIndex = null;
	}
	
    // loop over folds, createHTML tags
    for (var i=0; i<this.folds.length; i++) {
		// Expand the first by default if expanded is NOT set
		if (this.expanded == "") {
			if (this.hasOneOpened) {
				if (this.folds[i].opened) {
					this.expanded = this.folds[i].contentTempDivId;
				}
			} else if (i == 0) {
				this.expanded = this.folds[i].contentTempDivId;
			}
		}
		
		// If this is the element to expand, expand it
		if (this.folds[i].contentTempDivId == this.expanded) {
			this.folds[i].opened = true;
			this.openFoldArrayIndex = this.folds[i].indexNumber;
		}
		
        var foldElement = this.folds[i].createHTML(i);
        // 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;
	
	// Trevor 02/08/08 - Ensure that only 1 option is opened which will be the upper option set to true
	if (foldObject.opened) {
		if (this.hasOneOpened) {
			foldObject.opened = false;
		} else {
			this.hasOneOpened = true;
			this.openFoldArrayIndex = foldObject.indexNumber;
		}
	}
}

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

// #####################################    
// AccordionFold constructor
function AccordionFold(globalReference, contentTempDivId, title, icon, opened) {
    //alert(arguments.length);
    this.name = globalReference;
    this.contentTempDivId = contentTempDivId;
	this.url = "";
	this.opened = opened;

	this.title = title;
	this.iconImage = icon;

    this.tabDivId = null;
    this.holderDivId = null;
    this.contentDivId = null;
    this.introDivId = null;
    this.parentAccordion = null;
    this.indexNumber = null;
    this.animating = false;
}

// create this folds HTML to be placed in accordion div;
AccordionFold.prototype.createHTML = function(count) {
    // fold wrapper
    var wrapDiv = document.createElement('div');
    wrapDiv.setAttribute('id',this.name+'_wrap');
	
	//var content = $.trim(document.getElementById(this.contentTempDivId).innerHTML);
	
	// Set the first menu's margin-top to 0
    if (count == 0) {
		wrapDiv.setAttribute((beforeIE8() ? 'className' : 'class'), 'fold_wrap first');
	} else {
		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';
    
	// Is this a stand alone link?
	var url = $(this.title).attr("href");
	if (url) {
		this.url = url;
		this.title = $(this.title).html();
	}
	
	// Create the anchor element
    var tabAnchor = document.createElement('a');
    tabAnchor.setAttribute('href','javascript:;');
    tabAnchor.setAttribute('id',this.name+'_link');
	
	// Create the title element
	var titleDiv = document.createElement('div');
    titleDiv.setAttribute((beforeIE8() ? 'className' : 'class'),'title');
	$(titleDiv).html(this.title);
	tabAnchor.appendChild(titleDiv);
	
	tabDiv.appendChild(tabAnchor);
    wrapDiv.appendChild(tabDiv); // 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 action functions
    if (this.url == "") {
		eval("$('#" + this.name + "_link').hoverIntent(function () { " + this.name + ".foldClick(); }, function () {} );");
	} else {
		$("#" + this.name + "_link .title").removeClass("title");
		$("a#" + this.name + "_link").addClass("contact_button");
		$("a#" + this.name + "_link").attr('href',this.url);
	}

    // 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 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() { // argument of true overrides
    if (!this.animating) {
        var currentlyOpen = this.parentAccordion.openFoldArrayIndex == this.indexNumber;
        var forceClose = arguments.length > 0;        
        
        if (forceClose) { // close
            //traceThis('close '+this.name);
            // hide content out & slideUp the holder
            this.animating = true;
            //eval("$('#"+this.contentDivId+"').css('opacity', '0.01');");
            //eval("$('#"+this.contentDivId+"').slideUp('slow');");
			if (this.parentAccordion.fadeEffect) {
				eval("$('#"+this.contentDivId+"').fadeTo('fast', 0.01);");
			}
			eval("$('#"+this.holderDivId+"').slideUp('fast');");
            this.animating = false;
            // reset state
            this.opened = false;
            this.setStateDisplay(); // arrow
            this.setStateText(); // text
        } else if (!currentlyOpen) { // open
            //traceThis('open '+this.name);
            // tell currently open to close if one is opened!
            if (this.parentAccordion.openFoldArrayIndex != null) {
				this.parentAccordion.folds[this.parentAccordion.openFoldArrayIndex].foldClick(true);
			}
            // fade slideDown & display content
            this.animating = true;
            //eval("$('#"+this.contentDivId+"').css('opacity', 1.0);");
            //eval("$('#"+this.contentDivId+"').css('display', 'block');");
            //eval("$('#"+this.holderDivId+"').slideDown('slow');");
			if (this.parentAccordion.fadeEffect) {
				eval("$('#"+this.contentDivId+"').css('opacity', 0.01);");
			}			
	        eval("$('#"+this.holderDivId+"').slideDown('fast');");
			if (this.parentAccordion.fadeEffect) {
				eval("$('#"+this.contentDivId+"').fadeTo('fast', 1.0);");
			}
            this.animating = false;
            // reset state
            this.opened = true;
            this.setStateDisplay(); // arrow
            this.setStateText(); // text
            this.parentAccordion.openFoldArrayIndex = this.indexNumber;
        }
    }
}
// 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');");
        }
    }
}
