/*
 * Get a map of all the named anchors
 */
function getNamedAnchors() {
	var allAnchors = document.getElementsByTagName('a');
	var namedAnchors = Object();

	for(var i=0;i<allAnchors.length;i++) {
		if(allAnchors[i].name) {
			namedAnchors[allAnchors[i].name] = allAnchors[i];
		} else if(allAnchors[i].id) {
			namedAnchors[allAnchors[i].id] = allAnchors[i];
		}
	}
	return namedAnchors;
}


function onload_init_tabstrip() {
	var i,j,anchorName;	

	var allItems = document.getElementsByTagName('ul');
	var namedAnchors = getNamedAnchors();

	for(i=0;i<allItems.length;i++) {
		if(allItems[i].className.indexOf('tabstrip') != -1) {
			initTabstrip(allItems[i], namedAnchors);
		}
	}
}

function initTabstrip(tabstrip, namedAnchors) {
	var i, anchor, container, anchorName, li;
	var childAnchors = tabstrip.getElementsByTagName('a');
	var previousTab = null;
	
	// Detect a current tab
	var base, curTab = null, curURL = window.location.href;
	if(curURL.indexOf('#') == -1) {
		base = curURL.length;
	} else {
		base = curURL.indexOf('#');
		curTab = curURL.substr(curURL.indexOf('#')+1);
	}
	
	for(i=0;i<childAnchors.length;i++) {
		// Detect an anchor reference
		if(childAnchors[i].href.substr(base,1) == '#') {
			anchorName = childAnchors[i].href.substr(base+1);
			li = childAnchors[i].parentNode;
			if(namedAnchors[anchorName])
				container = namedAnchors[anchorName].parentNode;
			else
				container = null;
				
			tabstrip_initTab(childAnchors[i], namedAnchors, anchorName, tabstrip);

			// Hook up previousTab / nextTab suppoort
			if(previousTab) {				
				previousTab.nextTab = li;
				li.previousTab = previousTab;
			}
			previousTab = li;
			
			// Default to showing the first tab
			if(curTab == null) curTab = anchorName;
			
			// Show current tab
			if(curTab && (anchorName == curTab || (curTab + '.').substr(0,anchorName.length+1) == anchorName + '.')) {
				tabstrip.currentlyShowing = li;
				addClass(li, 'current');
				if(container) {
					container.style.display = '';
					if(updateRequired && !tabstrip.getAttribute('noRequiredFields')) {
						var ownerForm = findParentForm(container);
						if(ownerForm) updateRequired(ownerForm);
					}
				}
			} else {
				if(container) container.style.display = 'none';
			}
		}
	}
	
	// Add nextTab() and previousTab() functions to the tabstrip
	
	tabstrip.openNextTab = tabstrip_openNextTab;
	tabstrip.openPreviousTab = tabstrip_openPreviousTab;
}

function tabstrip_initTab(a, namedAnchors, anchorName, tabstrip) {
	var anchor = namedAnchors[anchorName];
	var container;
	
	if(anchor != null) {				
		// Find the attached container and hide it
		container = anchor.parentNode;
		
		// Hook up information / events
		a.onclick = function() { return false; };
		
		li = a.parentNode;
		li.container = container;
		li.anchorName = anchorName;
		li.tabstrip = tabstrip;
		li.onclick = tabstrip_showTab;
		li.onmouseover = tabstrip_mouseOver;
		li.onmouseout = tabstrip_mouseOut;
	}
}

/*
 * Returns the form object that the given element is
 * inside; or null if it's not inside a form
 */
function findParentForm(el) {
	var ownerForm = el.parentNode, tn;
	while((tn = ownerForm.tagName.toLowerCase()) != "body" && tn != "form") ownerForm = ownerForm.parentNode;
	if(tn == "form") return ownerForm;
	else return null;
}


function tabstrip_showTab() {
	var runCheckRequired = true;
	
	if(this.parentNode.getAttribute('noRequiredFields'))
		runCheckRequired = false;
	
	// Check if we're inside a form
	var ownerForm = null;
	
	if(runCheckRequired) ownerForm = findParentForm(this.tabstrip.currentlyShowing.container);
	
	// Perform required field validation
	if(ownerForm && checkRequired && !checkRequired(ownerForm.name ? ownerForm.name : ownerForm.id)) return false;

	if(this.tabstrip.currentlyShowing) {
		this.tabstrip.currentlyShowing.container.style.display = 'none';
		removeClass(this.tabstrip.currentlyShowing, 'current');		
	}
	
	this.container.style.display = '';
	addClass(this, 'current');

	this.tabstrip.currentlyShowing = this;
	
	setHashLink(this.anchorName);

	// Show the required field marks
	if(ownerForm) updateRequired(ownerForm.name ? ownerForm.name : ownerForm.id);
	
	return false;
}

/*
 * Redirect to the given hash link
 * It won't actually reload the page, but it will update the current URL
 */
function setHashLink(hashLink) {
	// Mac/IE5 cannot handle this
	if(navigator.userAgent.indexOf("Mac") > -1 && navigator.userAgent.indexOf("MSIE") > -1)
		return;
	
	if(window.location.href.indexOf('#') == -1)
		window.location.href += '#' + hashLink;
	else
		window.location.href = window.location.href.replace(/#.*$/, '#' + hashLink);
}


function tabstrip_mouseOver() {
	addClass(this, 'over');
}

function tabstrip_mouseOut() {
	removeClass(this, 'over');
}

function tabstrip_openNextTab() {
	if(this.currentlyShowing && this.currentlyShowing.nextTab)
		this.currentlyShowing.nextTab.onclick();
}
function tabstrip_openPreviousTab() {
	if(this.currentlyShowing && this.currentlyShowing.previousTab)
		this.currentlyShowing.previousTab.onclick();
}



appendLoader(onload_init_tabstrip);
