if(typeof cmscollapsiblecontainer == 'undefined') {
cmscollapsiblecontainer = true;
if(typeof cms == 'undefined') {cms = {};}


/** CollapsibleContainer. */
cms.Collapsible = function(div) {
    this.outer = div;
    this.group = null;
   	this.hidden = false;
    this.persistent = false;
    this.shownContent = [];
    this.hiddenContent = [];
    this.hideshowcontrol = null;
    this.hidecontrol = null;
    this.showcontrol = null;
    this.singlecontrol = true;

    var cls = this.outer.className || "";
    this.persistent = cls.match(this.pat_collapsible_persistent);

    var grp = cls.match(this.pat_collapsible_group);
    if (grp) this.group = grp[1];

    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);
           	}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 = i2rd.bindAsEventListener(this.collapseExpand, this);
    if(this.hidecontrol && this.showcontrol) {
        this.singlecontrol = false;
        i2rd.addEvent(this.hidecontrol, 'click', ceFunc);
        i2rd.addEvent(this.showcontrol, 'click', ceFunc);
        this.updateControls();
    }
    else {
    	i2rd.addEvent(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");
    	//log4js.logger.info("Hidden? " + hidden + ", State: " + state);
    	if( (state == "0" && !this.hidden)
    		|| (state == "1" && this.hidden)) {
    		//log4js.logger.info("Toggling");
    		this._collapseExpand();
    	}
        if (this.group && !this.hidden) {
    		// log4js.logger.info("Initializing group:"+ this.group +":" + this.outer.id);
            cms.collapsibleGroups[this.group] = this;
        }
   	}
};
cms.Collapsible.prototype = {
	pat_collapsible_control_hide : /collapsible-control-hide/,
	pat_collapsible_control_show : /collapsible-control-show/,
	pat_collapsible_control : /collapsible-control/,
    pat_collapsible_persistent : /collapsible-persistent/,
    pat_collapsible_group : /collapsible-group-(\w*)/,
    setClassName: function() {
        i2rd.removeClassName(this.outer, "shown");
        i2rd.removeClassName(this.outer, "hidden");
        i2rd.addClassName(this.outer, (this.hidden ? "hidden" : "shown"));
        if(this.singlecontrol) {
        	// Compat
	       	i2rd.removeClassName(this.hideshowcontrol, "hidden");
	       	i2rd.removeClassName(this.hideshowcontrol, "shown");
	       	if(this.hidden){ i2rd.addClassName(this.hideshowcontrol, "hidden");
	       	}else{ i2rd.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);
			}
		}
    },
    showElement : function(el) {
    	el.style.display = "block";
    },
    hideElement : function(el) {
    	el.style.display = "none";
    },
    _collapseExpand: function() {
    	this.hidden = !this.hidden;
//    	log4js.logger.info("Making hidden? " + this.hidden);
		var h, el, fn;
		fn = (this.hidden) ? this.showElement : this.hideElement;
   		for (h = 0; (el = this.hiddenContent[h]); h++) {fn(el);}
		fn = (this.hidden) ? this.hideElement : this.showElement;
   		for(h = 0; (el = this.shownContent[h]); h++){fn(el);}
		this.updateControls();
		this.setClassName();
    },
    updateGroup: function() {
        if (this.group != null) {
            var current = cms.collapsibleGroups[this.group];
            if (current) {
                if (current == this) {
                    if (this.hidden) {
                        cms.collapsibleGroups[this.group] = null;
                    } else {
                        cms.collapsibleGroups[this.group] = this;
                    }
                } else {
                    if (!this.hidden) {
                        current._collapseExpand();
                        current.persistState();
                        cms.collapsibleGroups[this.group] = this;
                    }
                }
            } else {
                if (!this.hidden) {
                    cms.collapsibleGroups[this.group] = this;
                }
            }
        }
    },
    persistState: function() {
       	if (this.outer.id) {
            var cname = "cc-" + this.outer.id + "-state";
            if (this.persistent) {
                var time = new Date();
                time.setHours(time.getHours() + 4);
                var value = (this.hidden ? "0" : "1");
                // log4js.logger.info("Setting state: " + value);
                i2rd.setCookie(cname, value, time, document.location.pathname);
            } else if (i2rd.getCookie(cname)) {
                // log4js.logger.info("Removing stale cookie: " + cname);
                i2rd.deleteCookie(cname, document.location.pathname);
            }
        }
    },
    collapseExpand: function(evt) {
	    this._collapseExpand();
        this.persistState();
        this.updateGroup();
        this.ensureVisible();
    },
    ensureVisible: function(){
        if (this.hidden) return;
        var elementTop = this.outer.offsetTop;
        var scrollTop = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        if (elementTop < scrollTop) {
            if (document.documentElement.scrollTop) {
                document.documentElement.scrollTop = elementTop;
            } else {
                document.body.scrollTop = elementTop;
            }
        }
    }
};
cms.collapsibleGroups = {};
cms.initCollapsible = function(div) {
    if(typeof div == 'string') div = document.getElementById(div);
	if(typeof div.initedCollapsible == 'undefined') {
		div.initedCollapsible = true;
		new cms.Collapsible(div);
	}
};
function cms_checkCCDom (start) {
    if(typeof start == 'string') start = document.getElementById(start);
	start = start || i2rd.getBody();
	var div, list = i2rd.getElements("div.collapsible",start);
	while((div = list.pop())) {
		cms.initCollapsible(div);
	}
};
cms.executeOnContentLoadOrAfter(function() {
	var div, list = i2rd.getElements("div.collapsible");
	while((div = list.pop())) {
		cms.initCollapsible(div);
	}
	i2rd.addEvent(window, __i2rd_domupdate_event, cms_checkCCDom);
});

}//End conditional eval
