

function displayCurrentTab(checkCookie, ignoreConditions)
{
	if ( checkCookie && !ignoreConditions ) {
		ignoreConditions = { getVars: ['ID'],
							referrers: ['/index.php', '/'] 
							};
	}
	
	var currentTab = getQueryVariable("_CurrentTab");
	
	if ( currentTab > 1 ) {
		displayTab(currentTab);	
	}
	else if ( checkCookie && prototypeAvailable() ) {
		currentTab = fetchTabState(ignoreConditions);
		
		if ( currentTab > 1 ) {
			displayTab(currentTab);
		}
	}
}

function fetchTabState(ignoreConditions)
{	
	
	if ( !prototypeAvailable() ) return null;
	
	referrer = stripDomain(getURI(document.referrer));
				
	// Ignore cookie if referrer is null
	if ( !document.referrer ) return null;
	
	// Ignore cookie if referrer is list of referrers to ignore.
	if ( ignoreConditions.referrers ) {
		for ( i = 0; i < ignoreConditions.referrers.length; i++ ) {
			if ( referrer == ignoreConditions.referrers[i] ) return null;
		}
	}
	
	var tabState = decodeTabStateCookie(stripDomain(getURI()));
	
	if ( !tabState ) return null;
		
	// Ignore cookie if one of the key get variables has changed.
	if ( ignoreConditions.getVars ) {
		for ( i = 0; i < ignoreConditions.getVars.length; i++ ) {
			varName = ignoreConditions.getVars[i];
			if ( getQueryVariable(varName) != getQueryVariable(varName, tabState.queryString) ) {
				return null;	
			}
		}
	}
	
	return tabState.currentTab;
}


function decodeTabStateCookie(uri)
{
	if ( !prototypeAvailable() ) return;
	
	var cookieResult = getCookie('AutoTabState');
	var allTabsState = cookieResult ? $H(cookieResult.evalJSON()) : $H();
	
	if ( uri ) {
		return allTabsState.get(uri);
	}
	else {
		return allTabsState;		
	}
}


function saveTabState(number)
{
	if ( !prototypeAvailable() ) return;
	
	var allTabsState = decodeTabStateCookie();
	allTabsState.set(stripDomain(getURI()), { currentTab: number,
							queryString: window.location.search.substring(window.location.search.indexOf('?'))
							});
	
	setCookie('AutoTabState', Object.toJSON(allTabsState), 0.1);
}

function displayTab(number, tabIdName, contentContainerName)
{
	// Save state only if default containers used.
	if ( !tabIdName && !contentContainerName ) {
		saveTabState(number);
	}
	
	if ( !contentContainerName ) contentContainerName = 'admin_body';		
	if ( !tabIdName ) tabIdName = 'Tab';
	
	var contentContainer = document.getElementById(contentContainerName);
	var contentTags = contentContainer.getElementsByTagName('div');
	
	for ( var i = 0; i < contentTags.length; i++ ) {
		
		if ( contentTags[i].id.substring(0,tabIdName.length) == tabIdName ) {
			var num = contentTags[i].id.substring(tabIdName.length);
			
			currentTab = document.getElementById(tabIdName + 'Link' + num);
			
			if ( contentTags[i].id.substring(tabIdName.length) == number ) {
				contentTags[i].style.display = '';
				
				// Replace here to avoid duplicate class names.
				currentTab.className = currentTab.className.replace(/selected/, '');
				currentTab.className = 'selected ' + currentTab.className;
				
				resizeTinyMceIfNeeded(contentTags[i].id);
				
				// Set the currently selected tab cookie.
			}
			else {
				contentTags[i].style.display = 'none';
				
				// Ensure we don't overwrite any additional classes
				currentTab.className = currentTab.className.replace(/selected/, '');
			}
		}
	}
	
	function resizeTinyMceIfNeeded(contentTagId)
	{	
		if ( prototypeAvailable() ) {
			
			var tinymceEditors = $$('#' + contentTagId + ' .text_editor');
			
			for ( j = 0; j < tinymceEditors.length; j++ ) {
				dimensions = tinymceEditors[j].getDimensions();
				
				iframeElement = $(tinymceEditors[j].id + "_ifr")
				
				if ( !iframeElement ) {
					
					iframeElements = $$('#' + contentTagId + ' .mceEditorIframe');
					
					if ( iframeElements[0] ) {
						iframeElement = iframeElements[0];
					}
				}
				
				if ( iframeElement ) {
					iframeElement.setStyle({
						width: dimensions.width,
						height: dimensions.height
					});
				}
			}
		}
	}
}


function toggleElementDisplay(elementId)
{
	var elementIdArray = elementId.split(",");
	
	for ( i = 0; i < elementIdArray.length; i++ ) {
		var elem = document.getElementById(elementIdArray[i]);
		
		if ( elem ) {
			elem.style.display = elem.style.display == 'none' ? '' : 'none';
		}
	}
	
	return false;
}

function toggleElementDisplayByClass(className, triggerElement, showText, hideText)
{
	var elements = getElementsByClass(className);
	
	for ( i = 0; i < elements.length; i++ ) {
		elements[i].style.display = triggerElement.value == showText ? '' : 'none';
	}
	
	triggerElement.value = triggerElement.value == showText ? hideText : showText;
}