﻿function AthenaAccordian(elemID, settings) {
    this.accordianDiv = document.getElementById(elemID);
    
    if (settings) {
        this.activeState = (settings.nohoverActiveState || "");
        this.inactiveState = (settings.nohoverInactiveState || "");
        this.hoverActiveState = (settings.hoverActiveState || "");
        this.hoverInactiveState = (settings.hoverInactiveState || "");
		this.onShowContent = (settings.onShowContent || null);
		this.onHideContent = (settings.onHideContent || null);
    } else return;

    if (!this.accordianDiv) return;
    if (!this.accordianDiv.hasChildNodes()) return;

    var stateStr = getCookie(this.accordianDiv.id);
    this.state = new stateStorage(stateStr);

    this.frameList = new Array();

    var children = this.accordianDiv.childNodes;
    var frame;
    for (var i = 0; i < children.length; i++) {
        if ((children[i].nodeType == 1) && (children[i].nodeName.toLowerCase() == "div")) {
            frame = children[i];
            this.frameList.push(new AthenaAccordianFrame(this,frame));
        }
    }
    
    for (var j = 0; j < this.frameList.length; j++) {
        this.frameList[j].initialize(this.state);
    }
    
    this.saveState = function () {
        for (var i = 0; i < this.frameList.length; i++) {
            if (this.frameList[i].active) {
                this.state.storage[this.frameList[i].header.id] = "e";
            } else {
                this.state.storage[this.frameList[i].header.id] = "c";
            }
        }
        setCookie(this.accordianDiv.id, this.state.getString(), 3650);
    };
    
    this.showFrame = function(headerID) {
        for (var i = 0; i < this.frameList.length; i++) {
            if (this.frameList[i].header.id == headerID) {
                this.frameList[i].setActive(true);
                break;
            }
        }
    }
    
    this.hideFrame = function(headerID) {
        for (var i = 0; i < this.frameList.length; i++) {
            if (this.frameList[i].header.id == headerID) {
                this.frameList[i].setActive(false);
                break;
            }
        }
    }
    
    this.toggleFrame = function(headerID) {
        for (var i = 0; i < this.frameList.length; i++) {
            if (this.frameList[i].header.id == headerID) {
                this.frameList[i].toggleActive();
                break;
            }
        }
    }
}

function AthenaAccordianFrame(accordian, frame) {
    this.mouseover = false;
    this.active = true;
    this.accordian = accordian;
	var children = frame.childNodes;
	var gotHeader = false;
	for (var i = 0; i < children.length; i++) {
		if ((children[i].nodeName.toLowerCase() == "div") && (children[i].nodeType == 1))
			if (gotHeader) {
				this.content = children[i];
				break;
			} else {
				this.header = children[i];
				gotHeader = true;
			}
	}

    this.doEvent = function() {
        if (this.active) {
            if (this.accordian.onShowContent)
                this.accordian.onShowContent(this.header, this.content);
        } else {
            if (this.accordian.onHideContent)
                this.accordian.onHideContent(this.header, this.content);
        }
    }

    this.setActive = function(newActive) {
        if (newActive == this.active) return;
        this.active = newActive;
        this.doEvent();
        this.updateState();
    }
    this.toggleActive = function() {
        this.active = !this.active;
        this.doEvent();
        this.updateState();
    }

    this.updateState = function() {
		if ((this.content) && (this.content.style)) {
	        if (this.active) {
	            this.content.style.display = "block";
	        } else {
	            this.content.style.display = "none";
	        }
	    }

        if (this.mouseover) {
            if (this.active) this.header.className = this.accordian.hoverActiveState;
            else this.header.className = this.accordian.hoverInactiveState;
        } else {
            if (this.active) this.header.className = this.accordian.activeState;
            else this.header.className = this.accordian.inactiveState;
        }
    };

    var cthis = this;
    if (!this.header.onmouseover) this.header.onmouseover = function() {
        cthis.mouseover = true;
        cthis.updateState();
    };
    if (!this.header.onmouseout) this.header.onmouseout = function() {
        cthis.mouseover = false;
        cthis.updateState();
    };
    if (!this.header.onclick) this.header.onclick = function() {
        cthis.toggleActive();
        cthis.accordian.saveState();
    };
    
    this.initialize = function(state) {
        var stateStr = state.getState(this.header.id);
        if (stateStr == "e") {
            this.active = true;
        } else {
            this.active = false;
        }
        this.updateState();
    };
}

