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


// Tabs
cms.tabcontainers = [];
/**
* TabContainer. HTML must contain the tabs in a UL and the tab content
* in DIV tags. The order of the tabs must correspond to the order of 
* the tab content.
*/
cms.TabContainer = function(containerDiv) {
    this.container = containerDiv;
    this.tabItemList = [];
    this.tabs = [];
    this.tabItemIndexes = {};
	this.doPersistence = !this.container.className.match(/tc_no_persist_selection/) && !!this.container.id;
	this.showCallback = i2rd.bindAsEventListener(this.showTab, this);
	this.mouseOverCallback = i2rd.bindAsEventListener(this.mouseOver, this);
   	this.mouseOutCallback = i2rd.bindAsEventListener(this.mouseOut, this);
   	this.foundUL = false;
};
cms.TabContainer.prototype ={
    displayFirst: function () {
        if(this.tabs.length === 0) {return;}
        if(this.tabItemList.length != this.tabs.length) {
            log4js.logger.error("TabContainer is mismatched." + this.tabItemList.length + " != " + this.tabs.length);
		}
        var i, index = null;

        // URL tab selection 
        if (location.hash.length > 1) {
            try {
                var tid, loc, locations = location.hash.substring(1).split(",");
                for (i = 0; i < locations.length; i++) {
                    loc = locations[i];
                    tid = loc.split(":");
                    if (tid[0] == this.container.id) {
                        index = this.tabItemIndexes[tid[1]];
                        if (index != null) {
                            this.setActiveTab(index, false);
                            return;
                        }
                    }
                }
            } catch (e) {
                log4js.logger.error("Unable to set tab from location hash", e);
            }
        }
        // Content specified tab selection
        for(i = 0; i < this.tabs.length; i++) {
        	if(this.tabs[i].className.match(/tab-selected/)) {
        		this.setActiveTab(i, false);
        		return;
        	}
        	if(this.tabItemList[i].className.match(/tab-selected/)) {
                this.setActiveTab(i, false);
        		return;
        	}
        }
        // Cookie specified tab selection
        if (this.doPersistence) {
            index = i2rd.getCookie(this.container.id + "-selectedtab");
            if (index) {
                try {
                    index = parseInt(index);
                    if (this.tabItemList.length > index) {
                        this.setActiveTab(index, false);
                        return;
                    }
                } catch (e) {}
            }
        }
        // Default tab selection
        this.setActiveTab(0, false);
    },
    setActiveTabByID: function(id, persist) {
        this.setActiveTab(this.tabItemIndexes[id], persist);
    },
    setActiveTab: function(index, persist) {
        var tabItem = this.tabItemList[index];
        if(!tabItem) tabItem = this.tabItemList[0];
        this._show(tabItem, persist);
    },
    persistSelection: function(index){
        if(!this.container.id || this.container.id.length == 0) return;
        var time = new Date();
		time.setHours(time.getHours() + 4);
		i2rd.setCookie(this.container.id + "-selectedtab", index, time, "/");
    },
    /** Add an anchor to the tab container. */
    addTabItem:  function (element) {
        if (element.id) {
            this.tabItemIndexes[element.id] = this.tabItemList.length;
            var idx = element.id.indexOf("_tab");
            if (idx != -1 && idx == element.id.length - 4) {
                this.tabItemIndexes[element.id.substring(0, idx)] = this.tabItemList.length;
            }
        }
        this.tabItemList.push(element);
        i2rd.addEvent(element, 'click', this.showCallback);
        i2rd.addEvent(element, 'mouseover', this.mouseOverCallback);
        i2rd.addEvent(element, 'mouseout', this.mouseOutCallback);
    },
    /** Add a div to the tab container. */
    addTab: function (element) {
        this.tabs.push(element);
    },
	getEventSource: function(evt, expectedTag) {
		var selected = i2rd.eventElement(evt);
		while(selected && selected.tagName && selected.tagName.toLowerCase() != expectedTag) {
			selected = selected.parentNode;
		}
		return selected;
	},
	showElement: function(el) {
		el.style.display = 'block';
	},
	hideElement: function(el) {
		el.style.display = 'none';
	},
    _show: function(tabItem, persist) {
        var h, t, ti;
        for (h = 0; h < this.tabItemList.length; h++) {
            ti = this.tabItemList[h];
            t = this.tabs[h];
            i2rd.removeClassName(ti, "tab-selected");
            if(ti == tabItem) {
                if(persist) this.persistSelection(h);
				i2rd.addClassName(ti, "tab-selected");
                this.showElement(t);
            }
            else this.hideElement(t);
        }
    },
    /** Show the tab. */
    showTab: function(evt) {
        var selectedTabItem = this.getEventSource(evt, "li");
        this._show(selectedTabItem, this.doPersistence);
        return false;
    },
   	mouseOut: function(evt) {
   	    for(var h = 0; h < this.tabItemList.length; h++) {
	        var ti = this.tabItemList[h];
	        i2rd.removeClassName(ti, "over");
	    }
   	},
   	mouseOver: function(evt) {
   	    this.mouseOut(evt);
   		var overTabItem = this.getEventSource(evt, "li");
	    i2rd.addClassName(overTabItem, "over");
   	},
	lookupTabItems: function(node) {
		var elements = i2rd.getElementsByTagName("li", node);
		var li, j;
		for (j = 0; (li = elements[j]); j++) {
			this.addTabItem(li);
		}
	},
	processNode: function(node) {
		var cc = true, ncn = node.className;
		if(ncn ) {
			// Nested tab container
			var nn = node.nodeName.toLowerCase();
			if(ncn.match(/tab-container/)) {
				return;
	        } else if (!this.foundUL && nn == "ul" && ncn.match(/tab-items/)) {
	            this.foundUL = true;
				this.lookupTabItems(node);
				cc = false;
	        } else if(nn == "div" && ncn.match(/tab-content/)) {
	           	//log4js.logger.info("Found tab-content");
	            this.addTab(node);
	            node.style.display = 'none';
	            cc = false;
	        }				
		}
		if(cc)  {
	        if(this.foundUL && this.tabItemList.length <= this.tabs.length) {return;}
	        var children = node.childNodes;
	        var el, h;
	        for(h = 0; (el = children[h]); h++) {
		        if(el.nodeType != 1) {continue;}
	        	this.processNode(el);
	        }
        }
	}
};
cms.initTC = function(div){
    if(typeof div == 'string') div = document.getElementById(div);
    if(!div) { return; }
	if(typeof div.tabinited != 'undefined') {return;}
	div.tabinited = true;
	var el, tc = new cms.TabContainer(div);
    cms.tabcontainers.push(tc);
	
    /* Find the tab items and the tab content. */
	var children = div.childNodes;
    for(var h = 0; (el = children[h]); h++) {
    	if(el.nodeType != 1) {continue;}
		tc.processNode(el);
    } 
    tc.displayFirst();
};
cms.checkTCDom = function(start) {
	start = start || i2rd.getBody();
	var div, list = i2rd.getElements("div.tab-container", start);
	while((div = list.pop())) {
		cms.initTC(div);		
	}
};
// This probably isn't needed after the component editor UI rewrite
cms.executeOnContentLoadOrAfter(function() {
	i2rd.addEvent(window, __i2rd_domupdate_event, cms.checkTCDom);
});

}// End conditional eval.
