if (typeof ecnext == "undefined") ecnext = {};
ecnext.tabcontainers = {};
preloadClick=0;skip=0;clicks=0;skipDefault=0;alerts=0;
initTabsLoaded=0;
tabsReady="";

function inactive_tab(base_id){
	if (typeof ecnext.tabcontainers[base_id] != "undefined") {
		if (alerts==1){alert("Set Inactive: "+base_id)}
		var old_id = ecnext.tabcontainers[base_id];
		var old_title = document.getElementById(old_id + '-title');
		var old_content = document.getElementById(old_id + '-content');
		old_title.className = old_title.className.replace(/ ?m-tab-title-active ?/, '');
		old_content.className = old_content.className.replace(/ ?m-tab-content-active ?/, '');
	}
}

function DOMready(){tabsReady="complete"; return tabsReady;} //FireFox
if (document.addEventListener){
	document.addEventListener('DOMContentLoaded', function(){DOMready()}, false);
}
function recordmytabclick(){} //used on preload page evt
function activate_tab(e, id, tabid) {
	var base_id = id.replace(/-[^-]*$/, '');
	clicks++;

	/* check for "preloading of page" and click event */
	if (tabid){ // multipage
		if (alerts==1){alert("activate_tab("+id+","+tabid+")\nclicks: "+clicks);}
		if (!document.addEventListener){
			tabsReady=document.readyState // IE
		}
		if(e && tabsReady != "complete"){skipDefault=1;}
		if (tabsReady != "complete" && tabid!=defaultTab && e){
				preloadClick=1;skip=1;
				if(skipDefault==1 && tabid!=defaultTab){
					dojo.addOnLoad(function(){initTabs();})
					dojo.addOnLoad(function(){activate_tab(0,id,tabid)})
					if(alerts==1){alert('add preload tab event: '+base_id+'\ntabClick: '+tabid+'\nreadystate: ' + tabsReady + "\nEvtClick: "+e)}
				}
		} else if (preloadClick==1 && tabid==defaultTab) {
			// page is now loaded, trying to load the default tab, so skip it
				skip=1; //preloadClick=0; skipDefault=1;
				if(alerts==1){alert('skip default tab event: '+base_id+'\ntabClick: '+tabid+'\nreadystate: '+ tabsReady)}
		} else if (preloadClick == 1) {  // && document.readyState == "complete"
				if(alerts==1){alert('run preloaded tab event: '+base_id+'\ntabClick: '+tabid+'\nreadystate: '+ tabsReady)}
				preloadClick=0; skip=0;
		} else if (tabsReady == "complete"){
			skip=0; preloadClick=0;
		}
	}

	if (skip == 0){
		if(tabid){RightColumnDisplay(tabid)}
		if(initTabsLoaded==0 && tabid){initTabs()}
		if (preloadClick != 1 || !tabid) {inactive_tab(base_id)}
		if(alerts==1){alert("tabInits: "+tabid)}
		if(tabid){tabInits[tabid]()}

		/* Save the currently active tab. */
		ecnext.tabcontainers[base_id] = id;
		/* Make the new tab active. */
		var title = document.getElementById(id + '-title');
		var content = document.getElementById(id + '-content');
		if (!title.className.match(/m-tab-title-active/)) {
			title.className = title.className + ' m-tab-title-active';
		}
		if (!content.className.match(/m-tab-content-active/)) {
			content.className = content.className + ' m-tab-content-active';
		}
		/* Set width/height based on parent. */
		var container = content.parentNode;
		content.style.width = container.style.width;
		content.style.height = container.style.height;
	}
}
