/*
 * tabs.js, functions for tabs, requires corrib.js 
 */

// currently selected element
var selectedTabElement = null;
var selectedUnloadScript = null;
var current_hash = window.location.hash;

function start_loading() {
	if (document.getElementById('info_loading') == null) {
		info = null;
	    td = document.getElementById("_tabsdiv");
	    if (td != null) {
			info = document.createElement('div');
			info.setAttribute('id', 'info_loading');
			info.setAttribute('style', 'background-color: orange; margin-top: 23px; padding: 2px 6px 2px 6px; ' + 
				'font-size: 10px; font-weight: bold; right: 0; position: absolute; z-index: 2;');
			info.innerHTML = "Loading...";
			td.appendChild(info); 
	    }
	}
}

function end_loading() {
	d = document.getElementById('info_loading');
	if (d != null)
		d.parentNode.removeChild(d); 
}

function getHashParam(name,def) {
	re = new RegExp(".*" + name + "=([a-zA-Z0-9\.,]*)");
	if (re.test(window.location.hash))
		return RegExp.$1;
	else
		return def;
}

function setHashParam(name, value) {
        re = new RegExp(".*" + name + "=([a-zA-Z0-9\.,\-]*)");
        re.test(window.location.hash);
        if (RegExp.$1 != null && RegExp.$1 != "") 
                window.location.hash = window.location.hash.replace(name + "=" + RegExp.$1, name + "=" + value);
        else
                if (window.location.hash != null && window.location.hash != "")
                        window.location.hash = window.location.hash + ";" + name + "=" + value;
                else
                        window.location.hash = name + "=" + value;
        current_hash = window.location.hash;
}

// change the tab and loads the content of the tab
// tabElementId - the id of the element to get focus
// containerElementId - the id of the element to get new content
// url of the new content to be loaded into the containerElementId
// where - element on which the function was fired
function changeTab(tabId, tabElementId, containerElementId, url, onloadscript, onunloadscript) {
    tabElement = document.getElementById(tabElementId);
    
	if (selectedTabElement != null)
		selectedTabElement.className = '';
	tabElement.className = 'selected';
	selectedTabElement = tabElement;

    setHashParam("t",tabId);
    
    loadDynamicContent(containerElementId, url, onloadscript, selectedUnloadScript, 
		function() {
			start_loading();
		},
    	function() {
    		if (onloadscript == null || onloadscript == '') {
				end_loading();
    		}
    	}
    );
    selectedUnloadScript = onunloadscript;
}

// reloads the current tab according to the information from the location hash
function reloadCurrentTab() {
	// /^.*t=([a-zA-Z0-9]*)$/.test(window.location.hash);
	//e = document.getElementById(RegExp.$1);
	
	e = document.getElementById(getHashParam("t"));
	
	if (e == null)
    	e = document.getElementById('tabul').childNodes.item(0).childNodes.item(0);

	if (typeof e.onclick == "function") {
    	e.onclick.apply(e);
    }
	adjustPageHeight();		
}

function check_hash() {
    if ( window.location.hash != current_hash ) {
        reloadCurrentTab();
    }
}

function initializeTabs() {
	reloadCurrentTab();
	hashCheck = setInterval( "check_hash()", 200 );
}