g_formLastVisible = false;

function switchTab(tabname, first, form_name){
	if(g_formLastVisible == false) g_formLastVisible = first;
	if(tabname == g_formLastVisible) return;
	
	document.getElementById("link_"+g_formLastVisible).className = '';
	document.getElementById(g_formLastVisible).style.display = 'none';
		
	document.getElementById("link_"+tabname).className = 'selected';
	document.getElementById(tabname).style.display = 'block';
	document.getElementById(tabname).style.visibility = 'visible';
	
	if(tabname == "text" && tinyMCE != null && tinyMCE.isMSIE){
		tinyMCE.execCommand("mceCleanup");
	}
	
	// add the anchor into the form action - so it gets perserved until next page display
	if(form_name) formAddAnchor(form_name, "tabs-" + tabname);
	
	g_formLastVisible = tabname;
}
$(function(){
	
	var masters = $("div.tabs");
	for(var x = 0; x < masters.length; x++){
		var tab = masters.get(x);
		var id = tab.id;
			
		var elem = tab;
			
		// find appropriate form
		var forms = elem.getElementsByTagName('form');
		if(forms && forms[0]) var form = forms[0];
		
		// get set of tabs
		var tabs = new Array();
		var names = new Array();
		$(elem).find("div.tabs-tab").each(function(index){
			tabs[index] = $(this).attr("id");
			names[index] = $(this).attr("title");
		});
		
		if(elem && names && tabs.length){
			
			// now insert the tabs
			var ul = document.createElement("ul");
			ul.id = 'form-tabs';
			elem.insertBefore(ul, elem.firstChild);
			var firstPart = tabs[0];
			
			
			var firstTab;
			// if selected tab is provided in the address, get the part of the hash after "tabs-" prefix
			if(window.location.hash.indexOf("#tabs-") == 0) firstTab = window.location.hash.substr(6);
			// else use the first one
			else firstTab = firstPart;
			
			if(form) formAddAnchor(form.name, "tabs-" + firstTab);
			
			for (var index in tabs){
				part = tabs[index];
				var li = document.createElement("li");
				li.id = 'link_' + part;
				
				// selected tab
				if(firstTab == part) li.className = 'selected';
				// else - hide the tab!
				else document.getElementById(part).style.display = 'none';
				
				var span = document.createElement('span');
				span.className = 'underline';
				span.appendChild(document.createTextNode(names[index]));
				
				var span2 = document.createElement('span');
				span2.className = 'inside';
				span2.appendChild(span);
				
				var a = document.createElement('a');
				a.href = '#tabs-'+part;
				a.onclick = new Function("", "switchTab('"+part+"', '"+firstTab+"', "+(form ? "'"+form.name+"'" : "null")+");");
				a.appendChild(span2);
				
				li.appendChild(a);						
				ul.appendChild(li);
			}
			
			tabsLink = document.getElementById(id+"_link");
			if(tabsLink){
				
				var a = document.createElement('a');
				a.href = tabsLink.firstChild.nodeValue;
				a.appendChild(document.createTextNode(tabsLink.firstChild.nextSibling.nodeValue));
				tabsLink.insertBefore(a, tabsLink.firstChild);
			}
		}
	}
	
	var links = $("div.form-tabize-on");
	for(var x = 0; x < links.length; x++){
		var link = links.get(x);
		var a = document.createElement('a');
		a.href = link.firstChild.nodeValue;
		a.appendChild(document.createTextNode(link.firstChild.nextSibling.nodeValue));
		link.insertBefore(a, link.firstChild);
	}
});
