if(typeof cmscollapsiblecontainer == 'undefined') {
cmscollapsiblecontainer = true;
if(typeof cms == 'undefined') {cms = {};}


/** CollapsibleContainer. */
cms.Collapsible = function(div) {
    this.outer = div;
   	this.hidden = false;
    this.shownContent = [];
    this.hiddenContent = [];
    this.hideshowcontrol = null;
    this.hidecontrol = null;
    this.showcontrol = null;
    this.singlecontrol = true;
    var i, node, list = this.outer.childNodes;
    for(i = 0; (node = list[i]); i++) {
        if(node.nodeName.toLowerCase() == "div") {
            var cn = node.className || "";
            if(cn.match(this.pat_collapsible_control_hide)){
                this.hidecontrol = node;
            }else if(cn.match(this.pat_collapsible_control_show)){
                this.showcontrol = node;
            }else if(cn.match(this.pat_collapsible_control)){
                this.hideshowcontrol = node;
            }else if(cn.match(/collapsible-hidden/)) {
            	this.hiddenContent.push(node);
            	node.style.display = "none";
           	}else if(cn.match(/collapsible-shown/) 
              || cn.match(/collapsible-container/) ) // Compat. 
              {
            	this.shownContent.push(node);
            	this.hidden = this.hidden || (node.style.display == "none");
			}
        }
    }
    this.hidden = !this.hidden; // we will toggle the state to init the elements.
	//log4js.logger.info("Hidden? " + this.hidden + " for " + this.outer.id);
    if( !(this.hideshowcontrol || (this.hidecontrol && this.showcontrol)) ){
        log4js.logger.error("Unable to find collapsible control");
	}
    var ceFunc = this.collapseExpand.bindAsEventListener(this);
    if(this.hidecontrol && this.showcontrol) {
        this.singlecontrol = false;
        Event.observe(this.hidecontrol, 'click', ceFunc);
        Event.observe(this.showcontrol, 'click', ceFunc);
        this.updateControls();
    }
    else {
        Event.observe(this.hideshowcontrol, 'click', ceFunc);
    }
    this._collapseExpand(); // Init
    if(this.outer.id) {  // Reset to saved state if different than default.
    	var state = i2rd.getCookie("cc-" + this.outer.id + "-state");
    	var hidden = this.hidden;
    	//log4js.logger.info("Hidden? " + hidden + ", State: " + state);
    	if( (state == "0" && !hidden)
    		|| (state == "1" && hidden)) {
    		//log4js.logger.info("Toggling");
    		this._collapseExpand();
    	}
   	}
};
cms.Collapsible.prototype = {
	pat_collapsible_control_hide : /collapsible-control-hide/,
	pat_collapsible_control_show : /collapsible-control-show/,
	pat_collapsible_control : /collapsible-control/,
    setClassName: function() {
        Element.removeClassName(this.outer, "shown");
        Element.removeClassName(this.outer, "hidden");
        Element.addClassName(this.outer, (this.hidden ? "hidden" : "shown"));
        if(this.singlecontrol) {
        	// Compat
	       	Element.removeClassName(this.hideshowcontrol, "hidden");
	       	Element.removeClassName(this.hideshowcontrol, "shown");
	       	if(this.hidden){ Element.addClassName(this.hideshowcontrol, "hidden");
	       	}else{ Element.addClassName(this.hideshowcontrol, "shown");}
        }
    },
    updateControls: function() {
		if(!this.singlecontrol) {
			if(this.hidden) {
			    this.hidecontrol.style.display = "none";
			    this.showcontrol.style.display = "block";
			    //Element.scrollTo(this.showcontrol);
			} else {
			    this.hidecontrol.style.display = "block";
			    this.showcontrol.style.display = "none";
			    //Element.scrollTo(this.hidecontrol);
			}
		}
    },
    _collapseExpand: function() {
    	this.hidden = !this.hidden;
    	//log4js.logger.info("Making hidden? " + this.hidden);
		var h, el, display;
		display = (this.hidden ? "block" : "none");
   		for(h = 0; (el = this.hiddenContent[h]); h++){el.style.display = display;}
   		display = (this.hidden ? "none" : "block");
   		for(h = 0; (el = this.shownContent[h]); h++){el.style.display = display;}
		this.updateControls();
		this.setClassName();
		
    },
    collapseExpand: function(evt) {
	    this._collapseExpand();
       	if(this.outer.id) {
           	var time = new Date();
			time.setHours(time.getHours() + 4);
			var value = (this.hidden ? "0" : "1");
			//log4js.logger.info("Setting state: " + value);
			i2rd.setCookie("cc-" + this.outer.id + "-state", value, time);
       	}
    }
};
cms.initCollapsible = function(div) {
	if(typeof div.initedCollapsible == 'undefined') {
		div.initedCollapsible = true;
		new cms.Collapsible(div);
	}
};
cms.checkCCDom = function(start) {
	start = start || i2rd.getBody();
	var div, list = Element.getElementsBySelector(start,"div.collapsible");
	while((div = list.pop())) {
		cms.initCollapsible(div);
	}
};
cms.executeOnContentLoadOrAfter(function() {
	var div, list = $$("div.collapsible");
	while((div = list.pop())) {
		cms.initCollapsible(div);
	}
	Event.observe(window, __i2rd_domupdate_event, cms.checkCCDom);
});

}//End conditional eval
