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");
        return;
	}
    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() + 48);
                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();
    },
    // If we can, we should delete ensureVisible.
    getComputedStyle: function (el, style) {
    	if (document.defaultView && document.defaultView.getComputedStyle)
    		return document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
    	else if (el.currentStyle) return el.currentStyle[style];
    	else return "";
    },
    cumulativeOffset: function(el, scroll){
    	scroll = !!scroll;
    	var y = 0, x = 0, t = scroll ? 'scroll' : 'offset', wk = navigator.userAgent.indexOf('AppleWebKit/') > -1, b = document.body;
    	if (el.parentNode) {
    	  do {
    	    y += el[t + 'Top']  || 0;
    	    x += el[t + 'Left'] || 0;
    	    el = scroll ? el.parentNode : el.offsetParent;
            if (!scroll && wk && el == b && this.getComputedStyle(el, 'position') == 'absolute')
            	break;
    	  } while (el);
    	}
    	return {0:x,1:y,x:x,y:y,left:x,top:y};
    },
    ensureVisible: function(){
        if (this.hidden) return;
        var elementTop = this.cumulativeOffset(this.outer).y, d=document, de=d.documentElement, db= d.body;
        var scrollTop = (de.scrollTop) ? de.scrollTop : db.scrollTop;
        if (elementTop < scrollTop) {
            if (de.scrollTop) {
                de.scrollTop = elementTop;
            } else {
                db.scrollTop = elementTop;
            }
        }
    }
};
cms.collapsibleGroups = {};
cms.initCollapsible = function(div) {
    if(typeof div == 'string') div = document.getElementById(div);
	if(!div.initedCollapsible) {
		div.initedCollapsible = true;
		new cms.Collapsible(div);
	}
};
function cms_checkCCDom (start) {
    if(typeof start == 'string') 
    	start = document.getElementById(start);
    else if(start && i2rd.eventElement(start)) 
    	start = i2rd.eventElement(start);
	start = start || document.body;
	var i=0, e, list = (start.querySelectorAll) ? start.querySelectorAll("div.collapsible") : start.getElementsByTagName("div");
	for(;e=list[i];i++){
		if(i2rd.hasClassName(e, "collapsible"))
			cms.initCollapsible(e);
	}
};
i2rd.addEvent(document, 'dom:loaded', function() {
	cms_checkCCDom();
	i2rd.addEvent(window, __i2rd_domupdate_event, cms_checkCCDom);
});

}//End conditional eval

