var tabSelected, displaySelectedMenu, displaySelectedContent, movieNum, t, tSel, tId, content, awardSel, commSel, commercialsHolder;
var firstTab = false;
var useContent = new Array();
var moviePlaying="Commercials1Content";

// creates tabs based on tabs array passed from the page that called the function
// each tab has two corresponding DIV's: a menu, which displays with the tab, 
// and content, which displays elsewhere on the page. the array should contain
// unique names. the menu DIV should have an id of nameMenu, and the page
// content DIV should have an idea of nameContent.
//
// the tSel argument is the id of the tab that should be open on page load.
// if the tSel argument is not passed, the first tab will open by default.

function createTabs(t,tSel) {
	for (var a=0; a<t.length; a++) {
		if ($(t[a]+"1Content") != null) {
			useContent.push(t[a]);
		}
	}

	if (useContent.length > 0) {
		var showTabs = '';
		var tabWidth = 225/t.length + "px";
		var tabClass;
		
		if (tabSelected == null) {
			_getSelected(useContent, tSel);
		}
		
		// Only displays tabs with content, just in case invalid arguments are passed
		for (var a=0; a<useContent.length; a++) {
			if (tSel != null) {
				if (useContent[a] == tSel) {tabClass = 'tabSel gEmphasis';}
				else {tabClass = 'tab gEmphasis'}
			} else {
				if ($(useContent[a]+"1Content") != null && firstTab == false) {tabClass = 'tabSel gEmphasis'; firstTab = true;}
				else {tabClass = 'tab gEmphasis'}
			}
			if (useContent.length > 1) {
				showTabs += '<div id="'+useContent[a]+'" class="'+tabClass+' gEmphasis"><a href="javascript:toggleTab(\''+useContent[a]+'\')" class="tTab gEmphasis">'+useContent[a]+'</a></div>';
			} else if (useContent.length == 1) {
				showTabs += '<div id="'+useContent[a]+'" class="tTabFull gEmphasis">'+useContent[a]+'</div>'
			}
		}
		setHtml("tabsContainer", showTabs);
	} else {_noDisplay()}
}

function _noDisplay() {
	$("tTabsHolder").style.display = 'none';
	$("tTabsContentHolder").style.display = 'none';
}

// opens and closes tabs and all related content
function toggleTab(tId) {
	tab = $(tId);
	if (tId == "Awards") {
		commercialsHolder = $(displaySelectedContent).innerHTML;
		$(displaySelectedContent).innerHTML = '';
	} else {
		if (commercialsHolder != null) {
			$(tId+"1Content").innerHTML = commercialsHolder;
		}
		fireEvent(commEvent1);
		$('ufoVLP').setAttribute("ufoflashvars", "videoUrl="+moviePath);
		initUFO('ufoVLP','ufoVLPMovie');
	}
	$(tabSelected).className = 'tab';
	tab.className = 'tabSel';
	$(displaySelectedMenu).style.display = 'none';
	$(tId+"Menu").style.display = 'block';
	$(displaySelectedContent).style.display = 'none';
	$(tId+"1Content").style.display = 'block';
	tabSelected = tId;
	displaySelectedMenu = tId+"Menu";
	displaySelectedContent = tId + "1Content";
	permOutline('AwdThumbDiv1','Award');
	permOutline('ComThumbDiv1','Commercial');
}

// tells the page to display menu and contents when the page loads
function displayFirst(showThis) {
	if (useContent.length > 0) {
		$(displaySelectedContent).style.display = 'block';
		$(displaySelectedMenu).style.display = 'block';
	}
}

// sets the tabSelected, displaySelectedMenu, and displaySelectedContent variables 
// when the page loads
function _getSelected(t, tSel) {
	if (tSel != null) {
		tabSelected = tSel;
	} else { 
		for (a=0; a<t.length; a++) { 
			if ($(t[a]+"1Content") != null && tabSelected == null) {
				tabSelected = t[a];
				break;
			} 
		}	
	}
	displaySelectedMenu = tabSelected + "Menu";
	displaySelectedContent = tabSelected + "1Content";
}

function contentSwitch(content,path) {
	if ($(content + "Content") != null) {
		if (content.substring(0,3) == "Com") {
			var tagNumbers=(path.indexOf("_commercial_")>=0)?[4,5]:[2,3];
			movieNum = content.substr(11);
			$('ufoVLP').setAttribute("ufoflashvars", "videoUrl={0}&fvStartEvent={1}&fvEndEvent={2}&enEspanol=1".format(path,tagNumbers[0],tagNumbers[1]));
			initUFO('ufoVLP','ufoVLPMovie');
		} else {
			$(displaySelectedContent).style.display = 'none';
			displaySelectedContent = content + "Content";
			$(displaySelectedContent).style.display = 'block';
		}
	} 
}


function outlineImage(imgID) {
	$(imgID).className = 'tAwdThumbImgBorder';
}
function removeOutline(imgID) {
	$(imgID).className = 'tAwdThumbImg';
}

function initOutline(imgID,cType) {
	if ($(imgID) != null && $(imgID) != undefined) {
		$(imgID).className = 'tAwdThumbImgBorder';
	}
	if (cType == 'Award') {
		awardSel = imgID;
	} else if (cType == 'Commercial') {
		commSel = imgID;
	}
}

function outlineImage(imgID) {
	$(imgID).className = 'tAwdThumbImgBorder';
}

function removeOutline(imgID,cType) {
	if (cType == 'Award') {
		if (imgID != awardSel) {
			$(imgID).className = 'tAwdThumbImg';
		}
	} else if (cType == 'Commercial') {
		if (imgID != commSel) {
			$(imgID).className = 'tAwdThumbImg';
		}
	}
}

function permOutline(imgID,cType) {
	if (cType == 'Award') {
		var sel = awardSel;
	} else if (cType == 'Commercial') {
		var sel = commSel;
	}
	if (imgID != sel) {
		$(sel).className = 'tAwdThumbImg';
	} 
	$(imgID).className = 'tAwdThumbImgBorder';
	if (cType == "Award") {
		awardSel = imgID;
	}
	if (cType == "Commercial") {
		commSel = imgID;
	}
}

