/*
systym: the NIH-syndrome javascript library
copyright (c) 2009 Chris Rose-Mathew
*/

// TODO: This code is very old and inefficient...
// My first Javascript special effect. Must revisit....

var tabFadeTimer = null
var tabFadingIn = null
var tabFadingOut = null
var fadeAmount = 0.0
var firstTime = true
var tableArray = undefined;
var currentTab = null;

function fadeTabs(inTab, outTab)
{
	clearTimeout(tabFadeTimer)
	
	if (scene.lowCPU)
		fadeAmount += 0.23;
	else
		fadeAmount += 0.1;
	var ieFadeAmount = fadeAmount * 100
	if (fadeAmount >= 1.0)
	{
		fadeAmount = 0.0
		if (outTab)
			outTab.style.display = "none"
		if (inTab)
			inTab.style.display = "block"
	}
	else
	{
		//carry on fading
		if (scene.lowCPU)
			setTimeout("fadeTabs(tabFadingIn, tabFadingOut)", 70)
		else
			setTimeout("fadeTabs(tabFadingIn, tabFadingOut)", 50)
		
		//actually apply the fade amounts
		if (inTab)
		{
			inTab.style.opacity = fadeAmount;
			inTab.style.filter = "alpha(opacity=" + ieFadeAmount + ")"
		}
		if (outTab)
		{
			outTab.style.opacity = 1.0 - fadeAmount;
			outTab.style.filter = "alpha(opacity=" + (1.0 - ieFadeAmount) + ")"
		}
	}
}

function toggleTab(tabID)
{
	if (tabID != "tabPage3" && currentTab && tabID == currentTab)
		return;

	// Make sure splash image only on first tab
	if (tabID == "tabPage0" || tabID == "tabPage6")
	{
		//scene.splashSpriteTargetOpacity = 0.45;
		scene.splashSpriteTargetOpacity = 0.0;
	}
	else
	{
		scene.splashSpriteTargetOpacity = 0.0;
	}
	
	
	if (tabID == "tabPage1")
	{
		scene.showPointingHands();
		scene.leftPointingHandSprite.element.onclick = function() { toggleEventsProfiles(-1); };
		scene.rightPointingHandSprite.element.onclick = function() { toggleEventsProfiles(1); };
	}
	else if (tabID == "tabPage4")
	{
		//scene.showPointingHands();
		scene.leftPointingHandSprite.element.onclick = function() { toggleArtisteProfiles(0,-1); };
		scene.rightPointingHandSprite.element.onclick = function() { toggleArtisteProfiles(0,1); };
	}
	else
	{
		scene.hidePointingHands();
	}
	
	if (tableArray == undefined)
	{
		tableArray = document.getElementsByTagName("div")
	}
	HideTabs(tableArray)
	ShowTab(tableArray, tabID)
	
	if (tabID == "tabPage3")
	{
		// Going back to the galleries page.
		// We must remove any individual gallery from the page and
		// re-display the gallery collection (thumbnails representing galleries)
		
		// TODO: Not the ideal spot for this code
		// GalleryCollection object?
		// Remove any specific gallery elements from the tab, and reshow the
		// main gallery collection div
		if (scene.currentGallery && scene.currentGallery.element)
		{
			var galleryParent = scene.currentGallery.element.parentNode;
			if (galleryParent)
				galleryParent.removeChild(scene.currentGallery.element);
		}

		var galleryCollection = document.getElementById("galleryCollection");
		if (!galleryCollection)
		{
			alert("ERROR - Failed to find gallery collection element");
		}
		galleryCollection.style.display = "block";	
	}
	
	currentTab = tabID;
}

function HideTabs(tabArray) 
{
	for (i=0; i < tabArray.length; i++) 
	{
		if (tabArray[i].className == "tabPage")
		{
			if (firstTime)
			{
				tabArray[i].style.display = "none"
				//firstTime = false
			}
			
			//----fade out------
			if (tabArray[i].style.display == "block")
			{
				// store tab to fade out
				tabFadingOut = tabArray[i]
			}
		}
	}
}

function ShowTab(tabArray, tabID) 
{
	for (i=0; i < tabArray.length; i++) 
	{
		if (tabArray[i].id == tabID)
		{	
			//alert("Found tab to show" + tabID)
			tabArray[i].style.display = "block"
			
			tabArray[i].style.opacity = 0.0
			tabArray[i].style.filter = "alpha(opacity=0)"
			
			tabFadingIn = tabArray[i]
		}
	}
	setTimeout("fadeTabs(tabFadingIn, tabFadingOut)",50)
}

