// set this to the number of tabs available.
window.tabNumber = 4;

function hide_content_tabs()
{
	var tabDiv = null;
	for(var i=1; i<=window.tabNumber; i++)
	{		
		tabDiv = document.getElementById("main_tab"+i);
		if(tabDiv != null)
		{
			tabDiv.style.display = "none";
		}
	}
	// TODO: add code to set the style for all tabs to off state
}

/* Removes the highlight from the main tabs */
function clearMainTabs()
{
	var tabs = document.getElementById("tabsnavcontainer");
	if(tabs == null) return;
	var tabList = tabs.getElementsByTagName("div");
	for(var i=0; i < tabList.length ; i++)
	{
		if(tabList[i].className == "tabShadowOn")
		{
			tabList[i].className = "tabShadow";
		}
		else if(tabList[i].className == "tabContainer2_on")
		{
			tabList[i].className = "tabContainer2_off";
		}
		else if(tabList[i].className == "tabShadowEndOn")
		{
			tabList[i].className = "tabShadowEndOff";
		}		
	}
}

/* Generic tab functions */

/**
 * This method determines what the name of the cookie used for this page's
 * tab preference.
 * For each page that uses the tabs, a cookie name should be defined.
 */
function getPageCookieName()
{
	var cookieName = null;
	
	switch(window.pageName)
	{
		case 'Sports':
			cookieName = 'belo_sports_tab';
			break;
		case 'News':
			cookieName = 'belo_news_tab';
			break;
		case 'HomePage':
			cookieName = 'denton_main_tab';
			break;
		default:
			cookieName = 'belo_main_tab';
	}
	
	return cookieName;
}

/**
 * This function is called on page load and loads the default tab based on 
 * a cookie. If the cookie is not set, it returns 1.
 */
function getTabCookie()
{		
	var cookieName = getPageCookieName();			
	
	var cookieNum = getCookie(cookieName);
	if(cookieNum == "NaN" || cookieNum == null)
	{			
		return 1;
	}
	else
	{
		return cookieNum;
	}
}
	

function showTab(tabNumber)
{
	hide_content_tabs();
	clearMainTabs();
	// show the selected tab area
	var tabToShowDiv = document.getElementById("main_tab" + tabNumber);
	if (tabToShowDiv == null) return;
	tabToShowDiv.style.display = "block";
	
	// turn the selected tab image on
	var tabToShow = document.getElementById("headlinetab" + tabNumber);	
	
	if(tabNumber < window.tabNumber)
	{
		tabToShow.className = "tabShadowOn";		
	}
	else // this is the last tab
	{
		tabToShow.className = "tabShadowEndOn";		
	}
	
	var innerTabs = tabToShow.getElementsByTagName("div");
	for(var x=0; x < innerTabs.length; x++)
	{
		if(innerTabs[x].className == "tabContainer2_off")
		{
			innerTabs[x].className = "tabContainer2_on";
		}
	}
	
	// check or uncheck the default view checkbox.
	var checkbox = document.getElementById("checkbox_main_tab" + tabNumber);
	var cookieName = getPageCookieName();
	
	if(getCookie(cookieName) == tabNumber)
	{		
		checkbox.checked = true;
	}
	else
	{
		checkbox.checked = false;
	}
	
	// show the tab's default list
	showDefaultListTab();
	// TODO: turn on tab style
}

/**
 * Updates the cookie information for the user's selected favorite cookie.
 * If the method finds the user unchecked, the cookie will be removed. 
 **/
function updateTabCookie(tabNumber)
{
	// check the state of the checkbox clicked.
	var checkbox = document.getElementById("checkbox_main_tab" + tabNumber);
	if(checkbox == null) return;
	
	var cookieName = getPageCookieName();
	
	if(checkbox.checked)
	{
		setCookie(cookieName, tabNumber, 60);
	}
	else
	{
		clearCookie(cookieName);	
	}
}


/* List tabs */
function hide_video_list(listDiv)
{	
	// turn off tabs
	
	
	if (listDiv == null) return;
	
	var videoLists = listDiv.childNodes;
	
	var tempDiv = null;
	for(var i = 1; i <= videoLists.length  ; i++)
	{
		tempDiv = document.getElementById("videoList" + i);
		if (tempDiv == null)
		{
			return;
		}
		else
		{
			tempDiv.style.display = "none";
		}
	}
}

function showDefaultListTab()
{	
	clearListTabs();
	showListTab(1);
	var defaultListDiv = document.getElementById("videoList1");
	if(defaultListDiv != null)
	{		
		defaultListDiv.style.display = "block";
	}
}

function showListTab(tabNum)
{	
	var listToShow = document.getElementById("videoList" + tabNum);
	if(listToShow != null) 
	{
		hide_video_list(listToShow.parentNode);
	}
	
	clearListTabs();
	
	if(document.getElementById("listTabs") != null)
	{
		document.getElementById("listTabs").childNodes[tabNum - 1].className = "on";
	}
	
	if(listToShow != null)
	{
		listToShow.style.display = "block";
	}
}

// turns off video list tabs
function clearListTabs()
{
	var tabsContainer = document.getElementById("listTabs");	
	if (tabsContainer == null) return;
	var tabs = tabsContainer.childNodes;
	
	for(var i=0; i < tabs.length; i++)
	{
		tabs[i].className = "";
	}
}

window.videoWin = null;

function popVideo(videoURL)
{
	if(window.videoWin == null || window.videoWin.closed)
	{
		window.videoWin = window.open(videoURL, "beloVideoWindow", "width=400,height=500");
	}
	else
	{
		window.videoWin.focus();
	}
	
	return false;
	
}


