window.nbaSelectedTab = null;
window.isPageClean = false;
window.isURLDefault = false;
window.Fabtabs = Class.create();
window.tabHash = getTabHash();


Fabtabs.prototype = {
	initialize : function(element, tDefault) {
		try
		{
			this.explicitDefault = tDefault;
			this.element = $(element);
			var options = Object.extend({}, arguments[1] || {});
			this.menu = $A(this.element.getElementsByTagName('a'));
			this.menu.each(this.setupTab.bind(this));
			
			var defaultTabObj = this.getInitialTab();	
				
			if (defaultTabObj.defaultSource == "noDefault"){
				this.show(defaultTabObj.defaultElm);
				setTabStateEX(defaultState);				
			}else{
				//alert(defaultTabObj.defaultSource);
				isURLDefault = true;
				this.show(defaultTabObj.defaultElm);
			}			
			
		}catch(err){
			//do nothing
		}
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.show(elm);
		nbaSelectedTab = elm;
		this.menu.without(elm).each(this.hide.bind(this));
	},
	hide : function(elm) {
		try{
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
		}
		catch(err)
		{
		}
	},
	show : function(elm) {
		try{
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');
		if (nbaOmEvent && nbaOmEvent.onActiveTab && nbaOmEvent.tabCall) {
			nbaOmEvent.activeTab=elm;
			nbaOmEvent.onActiveTab();
		}
		}
		catch(err)
		{
		}
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getExplicitDefault : function(){
		return this.explicitDefault;
	},
	getInitialTab : function() {
		var elm = null;
		var loc = '';
		var initialTab = new Object();
			initialTab.defaultSource = "noDefault";
			initialTab.defaultElm = this.menu.first();
			
		var isUrlAnchor = false;
		if(document.location.href.match(/#(\w.+)/)) {
			loc = RegExp.$1;
			elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			if (elm){
				initialTab.defaultSource = "urlDefault";
				initialTab.defaultElm = elm;
			}
		}
		//if not url defaults then check to see if there is an explicit default passed in
		if (initialTab.defaultSource == "noDefault"){
			loc = this.getExplicitDefault();
			elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc });
			if(elm){
				initialTab.defaultSource = "explicitDefault";
				initialTab.defaultElm = elm;			
			}
		}
		nbaSelectedTab = initialTab.defaultElm;
		return initialTab;
	}
}
function setTabStateEX(oSituation)
{
	var curTabElm = nbaSelectedTab;
	var curTabHref = curTabElm.href.match(/#(\w.+)/)[1];
	
	oSituation = "situation" + oSituation;
	//alert(curTabHref);
	switch(oSituation)
	{
		case "situation1":
				/*
					SITUATION 1: Before the game starts and before the preview is available
					DEFAULT TAB: TV & RADIO
					AVAILABLE TABS: TV & RADIO
					UNAVAILABLE TABS: GAME STORY, BOX SCORE, PLAY-BY-PLAY
				*/
				
				hideTabsAll();					//First Hide All
				if (!isPageClean){					
					showTab("nbaGIRadio", true);
					isPageClean = true;
				}else{					
					if (!isTabDefault(curTabHref, ["nbaGIRadio"])){					
						showTab("nbaGIRadio", true);	//Open TV & RADIO : Default		
					}else{
						showTab("nbaGIRadio", false);
					}	
				}
				
				break;
		case "situation2":
				/*
					SITUATION 2: Before the game starts, a preview comes in
				  	DEFAULT TAB: GAME STORY 
				  	AVAILABLE TABS: GAME STORY, TV & RADIO
				 	UNAVAILABLE TABS: BOX SCORE, PLAY-BY-PLAY
				*/
				
				hideTabsAll();					//First Hide All	
				if (!isPageClean){
					showTab("nbaGIlive", true);					
					isPageClean = true;
				}else{	
					if (!isTabDefault(curTabHref, ["nbaGIlive","nbaGIRadio"])){				
						showTab("nbaGIlive", true); 	//Open GAME STORY : Default		
					}else{
						showTab("nbaGIlive", false)
					}		
				}			
				showTab("nbaGIRadio", false)	//Open TV & RADIO						
				break;
				
		case "situation3":
				/*
					SITUATION 3: First box score comes in (usually just before game starts)
				  	DEFAULT TAB: BOX SCORE
				  	AVAILABLE TABS: GAME STORY, BOX SCORE, TV & RADIO
				  	UNAVAILABLE TABS: PLAY-BY-PLAY
				*/
				
				showTabsAll();
				hideTabs(["nbaGIPlay"]);
				if (!isPageClean && !isURLDefault){
					showTab("nbaGIboxscore", true);
					isPageClean = true;
				}else{				//First Hide All	
					if (!isTabDefault(curTabHref, ["nbaGIlive","nbaGIboxscore","nbaGIRadio"])){			
						showTab("nbaGIboxscore", true); //Open BOX SCORE : Default	
					}else{
						showTab("nbaGIboxscore", false)
					}		
				}		
				
				break;
				
		case "situation4":
				/*
					SITUATION 4: First play by play comes in
				  	DEFAULT TAB: BOX SCORE
				  	AVAILABLE TABS: GAME STORY, BOX SCORE, PLAY-BY-PLAY, TV & RADIO
				  	UNAVAILABLE TABS: NONE
				*/
				
				showTabsAll();	
				if (!isURLDefault){
					if (!isPageClean){
						showTab("nbaGIboxscore", true);
						isPageClean = true;
					}
				}else{
					isURLDefault = false;
					isPageClean = true;
				}						
				break;
		case "situation5":
				/*
					SITUATION 5: Game is ended and recap comes in
				  	DEFAULT TAB: GAME STORY
				  	AVAILABLE TABS: GAME STORY, BOX SCORE, PLAY-BY-PLAY, TV & RADIO
				  	UNAVAILABLE TABS: NONE
				*/
				
				showTabsAll();	
				if (!isURLDefault){
					if (!isPageClean){
						showTab("nbaGIlive", true);
						isPageClean = true;
					}	
				}else{
					isURLDefault = false;
					isPageClean = true;
				}
				break;		
		
				
		default:
				showTabsAll();
	}
}

function getTabHash()
{
	var tabsIDs = { 
				nbaGIlive		:{activeid:"nbaGIliveLIactive", 		inactiveid:"nbaGIliveLIinactive"},
				nbaGIboxscore	:{activeid:"nbaGIboxscoreLIactive", 	inactiveid:"nbaGIboxscoreLIinactive"},
				nbaGIPlay		:{activeid:"nbaGIPlayLIactive", 		inactiveid:"nbaGIPlayLIinactive"},					
				nbaGIRadio		:{activeid:"nbaGIRadioLIactive", 		inactiveid:"nbaGIRadioLIinactive"}			
			}
	return tabsIDs
}

function hideTabsAll()
{
	for (var oKey in tabHash)
	{
		$(tabHash[oKey].activeid).firstDescendant().addClassName("hide")
		$(tabHash[oKey].inactiveid).removeClassName("hide");
	}
}
function hideTabs(oTabsArry)
{
	for (var i = 0; i < oTabsArry.length; i++){
		$(tabHash[oTabsArry[i]].activeid).firstDescendant().addClassName("hide")
		$(tabHash[oTabsArry[i]].inactiveid).removeClassName("hide");
	}
}
function showTabsAll()
{
	var tabHash = getTabHash();
	for (var oKey in tabHash)
	{
		$(tabHash[oKey].activeid).firstDescendant().removeClassName("hide");
		$(tabHash[oKey].inactiveid).addClassName("hide");
	}
}
function showTab(oTab, isClicked)
{
	var defActive	= null;
	var defInactive = null;
	
	defActive 	= $(tabHash[oTab].activeid).firstDescendant();
	defInactive = $(tabHash[oTab].inactiveid)
	
	defActive.removeClassName("hide");				
	defInactive.addClassName("hide");
	if (isClicked){
		nbaOmEvent.tabCall=true;
		defActive.click();
	}
} 

function isTabDefault(oCurTab, oTabArry)
{
	for (var i = 0; i < oTabArry.length; i++){
		if (oCurTab == oTabArry[i]){
			return true;
		}
	}
	return false;
}
// for Mozilla browsers - adds a click() function to the prototype for HTMLAnchorElement if there isn't already one there
if (document.addEventListener) {
	if (!HTMLAnchorElement.prototype.click) {
	  HTMLAnchorElement.prototype.click = function() {
	    var ev = document.createEvent('MouseEvents');
	    ev.initEvent('click',true,true);
	    if (this.dispatchEvent(ev) !== false) {
	      //safari will have already done this, but I'm not sniffing safari
	      //just in case they might in the future fix it;
	      document.location.href = this.href;
	    }
	  }
	}
}

function togglePreview(option){
	if(option=="nbaGIPreview"){
		$('nbaGIPreview').setStyle({ display: 'block' })
		$('nbaGIRecap2').setStyle({ display: 'none' })	
	} else {	
		$('nbaGIPreview').setStyle({ display: 'none' })
		$('nbaGIRecap2').setStyle({ display: 'block' })	
	}
}
function activeLink(objLink){ 
var list = document.getElementById('nbaGIStoryPreLks').getElementsByTagName('a'); 
for (var i = list.length - 1; i >= 0; i--){ list[i].className='nonactive'; 
}; objLink.className= 'active'; 
} 

Event.observe(window,'load',function(){ new Fabtabs('tabs', ''); },false);