/* create a tabcontroller for each tab widget on the page */

nfl.ui.tabs.controllers	= [];

nfl.ui.tabs.initialize	= function(){

	var tabGroups		= document.getElementsByClassName("nfl-tabs-group");

	for(tg=0; tg < tabGroups.length; tg++){

		var tgTabs		= [];

		/* create tab object for each tab that belongs to the tab widget */

		var tgDomTabs	= document.getElementsByClassName("nfl-tabs-tab",tabGroups[tg]);

		var tgDomCont	= document.getElementsByClassName("nfl-tabs-content",tabGroups[tg])[0];

		for(tgTab=0; tgTab < tgDomTabs.length; tgTab++){

			var tabObj		= tgDomTabs[tgTab];			

			var contPath	= tgDomTabs[tgTab].getElementsByTagName('input')[0].value;

			var contParams	= null;

			if(contPath.indexOf('?') > -1){

				//content path contains parameters

				contParams	= contPath.toQueryParams();

				contPath	= contPath.substring(0,(contPath.indexOf('?')));

				contParams.widgetPath	= contPath;

			}

			console.log("tab src params are: "+ contParams);

			tgTabs[tgTabs.length]	= {'tab': tabObj, 'container': tgDomCont, 'contentPath': contPath, 'contentParams': contParams};

		}

		/* create new controller with tabs to control passed in as array of objects */

		nfl.ui.tabs.controllers[nfl.ui.tabs.controllers.length]	= new nfl.ui.tabs.controller(tgTabs);

	}

}



//tab controller

nfl.ui.tabs.controller	= Class.create();

nfl.ui.tabs.controller.prototype = {

	initialize: function(tabs){

		//set up internal properties

		this.activeTab	= 0;

		this.defaultTab	= 0;

		this.tabs		= new Array(); //internal array that holds tab objects

		this._tabRefs	= tabs || null;

		//create tab objects from args

		if(this._tabRefs != null){

			for(tr=0; tr < this._tabRefs.size(); tr++){

				this.tabs[this.tabs.length]	= this.createTabFromJSON(this._tabRefs[tr]);

				this.tabs[(this.tabs.length - 1)].observe('click',this.activate.bindAsEventListener(this));

			}

		}

		//extend object

		Object.Event.extend(this);

		

		//by default display the content matching default tab property

		try{

			this.tabs[this.defaultTab].show();

		}catch(e){}

	},

	createTabFromJSON: function(json){

		var tObj	= new nfl.ui.tabs.tab(json.tab,json.container,json.contentPath,json.contentParams);

		return tObj;

	},

	activate: function(event){

		/** 

		   this function is bound to the tab objects click event. 

		   loop through the internal collection and call the hide

		   method on all tab objects that are not displayed

		**/

		//alert('activate called on tab controller['+ this.tabs +']');

		for(u=0; u < this.tabs.length; u++){

			console.log('hide tab['+ u +']');

			this.tabs[u].hide();

		}

	}

}



//individual tab object

nfl.ui.tabs.tab = Class.create();

nfl.ui.tabs.tab.prototype = {

	initialize: function(tab,container,contentPath,contentParams){

		this.contentsrc			= contentPath 	|| null;

		this.contentparams		= contentParams	|| null;

		this.contentwrapper		= container		|| null;

		this.tabwrapper			= tab			|| null;

		this.content			= null;

		this.displayed			= false;

		Object.Event.extend(this);

		

		this.tabwrapper.observe('click',this.show.bindAsEventListener(this));

	},

	show: function(){

		if(!this.displayed){

			this.notify('click',{name:'nfl.ui.tabs.tab.click',obj:this.base});

			this.display(true);	

		}

	},

	hide: function(){

		this.display(false);

	},

	display: function(bool){

		if(bool == true){

			//show

			this.tabwrapper.addClassName('nfl-tabs-active');

			this.get();

			this.displayed		= true;

		}else{

			//hide

			this.tabwrapper.removeClassName('nfl-tabs-active');

			this.displayed		= false;

		}

	},

	get: function(){

		//get data via xmlhttp, using restful pattern

		nfl.log('nfl.ui.tabs.tab.get: '+ this.contentsrc);

		this.loading();

		console.log("fetch tab src: "+ this.contentsrc +" with params: \""+ this.contentparams +"\"");

		if(this.contentparams != null){

			this._au				= new Ajax.Updater(this.contentwrapper, "/ajax/widgets/wrapper", { method:'get', onComplete: this.onResponse.bind(this), parameters: this.contentparams});

		}else{

			this._au				= new Ajax.Updater(this.contentwrapper, "/ajax/widgets/wrapper", { method:'get'});

		}

	},

	loading: function(){

		//show neat transitional/loading effect

		nfl.log('tab loading..');

		//get current contentwrapper geometry, create loading object

		var contentDims	= {width: this.contentwrapper.getWidth(), height: this.contentwrapper.getHeight()}

		new Effect.Opacity(this.contentwrapper, {duration:0.5, from:1.0, to:0.5});

		//now position spinning wheel thingie front and center

				

		//this.contentwrapper.update("loading..");

	},

	onResponse: function(){

		//clear out the loading effect

		console.log("clear the loading effect.");

		new Effect.Opacity(this.contentwrapper, {duration:0.5, from:0.5, to:1.0});

	}

}





/**

 * getElement

 * LEGACY FUNCTION

 * will be deprecated in favor of object oriented approach

 */

function getElement(divprefix, number) {

    return $(divprefix + "_" + number);

}

/**

 * activate

 * updated 10/11/07

 */



function activate(tabid, number, numtabs, stylemod, ajax) {

	//if($(tabid)){

	try{

		var tabcontainer	= $(tabid).up();

		var tabs			= $(tabid).getElementsBySelector("li");

		var tabInc			= 0;

		var linkclass		= {on:'blackLinkNoUL', off: 'greyLink'};

		if(stylemod.toLowerCase() == "superbowl"){ linkclass.off = 'superBowlLink';}

		if(stylemod.toLowerCase() == "nfl_europe"){ linkclass.off = 'nfl_europeLink';}

		

		tabs.each(function(li){

			element			= $((tabid +'_'+ tabInc));

			if(tabInc != number){

				if(ajax != "ajax")	{element.setStyle({'display': 'none'})};

				if(tabInc == 0)		{li.className	= "special "+ linkclass.off; }else{ li.className	= linkclass.off }

			}else{

				if(ajax != "ajax"){element.setStyle({'display': 'block'})};

			}

			tabInc++;

		});

	    if (number == 0 && stylemod == "tabbed-blue") {

	        tabs[number].className = "special-current-blue " + linkclass.on;

	    } else if (number == 0) {

	        tabs[number].className = "special-current " + linkclass.on;

	    } else {

	        tabs[number].className = "norm-current " + linkclass.on;

	    }

	}catch(e){

		//alert(e);

	}

	//}else{

	//	throw "contentnotready";

	//}

}



/**

 * CONTENTS OF accordian_arrows.js

 * RELOCATED TO THIS FILE

 */



var visible_element;

// To change the plus or minus images, change these image file paths

var ACC_IMG_ON	= 'img/red_arrow.gif';

var ACC_IMG_OFF = 'img/light_grey_arrow.gif';



function toggleStateImage(stateImageSource) {

    for (var i = 0; i < visible_element.childNodes.length; i++) {

        if (visible_element.childNodes[i].nodeName == "IMG") {

            visible_element.childNodes[i].src = stateImageSource;

        }

    }

}





function setup_accordion_container(accordion_container_title)

{

    var accordion_container = $(accordion_container_title);

    if(accordion_container){

	    var visible_header		= null;

	    var visible_parent		= null;	    

	    accordion_container.getElementsBySelector("div.panelBody").each(function(element){

	    	element.setStyle({'display':'none'});

	    });

	    accordion_container.getElementsBySelector("h3.visible").each(function(element){

	    	visible_element		= element;

	    	visible_parent		= element.parentNode.id + "-body";

	    	$(visible_parent).setStyle({'display':'block'});

	    });

    }

}



function changeTabs(clicked_tab) {

    if (clicked_tab == "network") {

        activate("nflnetwork_gameclips_tabs", 0, 2, "tabbed");

        setup_accordion_container("nflnetwork_gameclips_tabs_0");

    } else if (clicked_tab == "clips") {

        activate("nflnetwork_gameclips_tabs", 1, 2, "tabbed");

        setup_accordion_container("nflnetwork_gameclips_tabs_1");

    }

}



function accordion(clicked_line_item, status_image) {

    if (visible_element == clicked_line_item) {

        var to_slide_up = visible_element.parentNode.id + "-body";

        new Effect.SlideUp(to_slide_up, {duration:0.3});

        toggleStateImage(ACC_IMG_OFF);

        visible_element.className = "";

        visible_element = null;

    } else if (visible_element) {

        var to_slide_down = clicked_line_item.parentNode.id + "-body";

        var to_slide_up = visible_element.parentNode.id + "-body";

        new Effect.Parallel([new Effect.SlideUp(to_slide_up), new Effect.SlideDown(to_slide_down)], {duration:0.3});

        toggleStateImage(ACC_IMG_OFF);

        visible_element.className = "";

        clicked_line_item.className = "visible";

        visible_element = clicked_line_item;

        status_image.setAttribute("src", ACC_IMG_ON);

    } else {

        var to_slide_down = clicked_line_item.parentNode.id + "-body";

        new Effect.SlideDown(to_slide_down, {duration:0.3});

        clicked_line_item.className = "visible";

        visible_element = clicked_line_item;

        status_image.setAttribute("src", ACC_IMG_ON);

    }

}



	/*

	 * legacy placeholders

	 * these should be removed when absolutely 

	 * sure nothing is referencing them.

	 */

	//var getElement	= nfl.ui.tabs.getElement;

	//var activate	= nfl.ui.tabs.activate;



if(nfl.global.eventModel != "prototype"){

	nfl.events.observe("contentloaded", function(){ nfl.ui.tabs.initialize(); nfl.events.notify('libTabsLoaded'); /* firing off libTabsLoaded after document load for backwards compatibility with pages */ });	

}else{

	document.observe("contentloaded", function(){ nfl.ui.tabs.initialize(); document.fire('libTabsLoaded'); /* firing off libTabsLoaded after document load for backwards compatibility with pages */ });

}// JavaScript Document