/**
 * Primary controller for ScionRacing navigation and handling for deep linking.  Actual browser URL manipulation and interaction
 * is handled by swfaddress.  
 *
 * See swfaddress.js
 *
 */
NavigationManager = Class.create();
Object.extend(NavigationManager.prototype, {

	/**
	*	define member vars for this class
	*
	*/
	initialize: function() {
		this.topSection = "";	// topSection is the primary navigational level: 'drag', 'drift', 'road', 'shows', 'project' (possibly 'news', 'schedule' as well)... topSection elements 
								// do not affect deep links because all topSections have their own HTML pages for SEO purposes
								
		this.teamSection = "";	// teamSection is the specific team, if one exists: 'titan', 'world', 'jotech', 'rsr', 'mpme', 'dgspec', etc... teamSection elements do not affect deep links because
								// all teamSections have their own HTML pages for SEO purposes
								
		this.levelOne = "";		// levelOne is the 'tabs' section: '', 'images', 'video', 'askTheDriver', 'gear'... note that the 'about' section is the default and therefore does not have a 
								// levelOne value.  Only team-specific pages have levelOne elements
		
		this.levelTwo = ""; 	// levelTwo is the tabs sub section.  An example is the images 'gallery' section.  Though a string, the value is probably a string version of an int, reflecting 
								// the gallery number.  This is only the case in video and images sections.  The levelTwo var may also represent the sub-section of the Ask The Driver tab, in 
								// which case it will be a string value like: 'main', 'roadDiary', or 'archive'
								
		this.levelThree = "";	// levelThree is the specific gallery asset. Though a string, the value is probably a string version of an int, reflecting the gallery number.  This is only used
								// in video and images sections
	},
	
	
	/**
	*	Should be called when page is loading.  Sets the default state of the page's
	*	deep link
	*
	*/
	setInitState: function(top, team) {
		// BIKO: Add "top" argument to setLocalDeepLinkValues function to pass its value
		this.setLocalDeepLinkValues(top);
		this.setData(top, team, this.levelOne, this.levelTwo, this.levelThree);
		this.setupAnalyticsCall();
		this.requestInitContent();
		//BIKO: Add new call to set active states of top and side nav menu items depending on what section you are in
		this.setActiveMenuItems(top, team);
	},
	
	/**
	*	Pulls the deep link value from SWFAddress.  Breaks that down into section variables from which
	*	the local levelOne, levelTwo, levelThree vars are set.
	*
	*	BIKO: Add "top" argument to setLocalDeepLinkValues function to pass its value
	*/
	setLocalDeepLinkValues: function(top) {
		var deepLinkValue = SWFAddress.getValue();
		// remove preceding and trailing slashes
		if( deepLinkValue.startsWith("/") ) deepLinkValue = deepLinkValue.substr(1, deepLinkValue.length);
		if( deepLinkValue.endsWith("/") ) deepLinkValue = deepLinkValue.substr(0, deepLinkValue.length-1);
		var sections = deepLinkValue.split("/");
		
		// update local vars
		this.levelOne = sections[0];
		if( sections.length > 1) { this.levelTwo = sections[1] } else { this.levelTwo = "" };
		if( sections.length > 2) { this.levelThree = sections[2] } else { this.levelThree = "" };
		
		if(this.levelOne != "")
		{
			var showTab = !(((this.levelOne == 'video') && $('videoTabMarker')) || ((this.levelOne == 'images') && $('imageTabMarker')));
			if(showTab){
				//BIKO: Add "top" argument to pass its value
				this.setPageTabDisplay(this.levelOne, top);	
			}
			if(this.levelTwo !== "") {
				this.setPopupDisplay();
			}
		} else {
			//BIKO: Add "top" argument to pass its value
			this.setPageTabDisplay('about', top);
		}
	},
	
	
	/**
	*	Sets the navigation's default position variables.
	*	Examples of the different tiers (first, second, etc) would be:
	*	[ 'drag', 'titan', 'askTheDriver', 'roadDiary' ]
	*	[ 'drag', '', 'video', '' ]
	*	
	*	Or, in the following case, the numbers are reflective of the gallery number and the asset number:
	*	[ 'drag', 'world', 'images', '3', '12' ]
	*
	*/
	setData: function(top, team, one, two, three) {
		this.topSection = top;
		this.teamSection = team;
		this.levelOne = one;
		this.levelTwo = two;
		this.levelThree = three;
	},
	
	
	/**
	*	SWFAddress should inform us if the browser URL changes
	*
	*/
	handleBrowserUrlChange: function() {
		
		//BIKO: Add "this.topSection" argument to pass its value
		this.setLocalDeepLinkValues(this.topSection);
		
		this.setupAnalyticsCall();
		
		// get the new content and update display
		this.requestContent();
	},
	
	
	/**
	*	Based on local page name information, relays a call to the AnalyticsManager
	*
	*/
	setupAnalyticsCall: function() {
		var pageName = "sci: slash: scion racing";
		if(this.topSection != "")
		{
			pageName += ": " + this.topSection;
		}
		if(this.teamSection != "")
		{
			pageName += ": " + this.teamSection;
		}
		if(this.levelOne != "")
		{
			pageName += ": " + this.levelOne;
		} 
		AnalyticsManager.callOmniture(pageName);
	},
	
	
	/**
	*	Sets the 'selected' state appearance on the main content area tabs
	*
	*/
	setPageTabDisplay: function(tabName, top) {
		// BIKO: Ignore tabs for news, schedule, and search results pages by checking "top" value
		if( ( top != 'news' ) && ( top != 'schedule' ) && ( top != 'searchresults' ) ) {
			// BIKO: Set new tabs for Project Cars and Shows & Events sections
			if( ( top == 'cars' ) || ( top == 'shows' ) ) {
				var tabs = ['about','images','video'];
			//} if else ( top == 'archive' ) {
				//var tabs = ['about','images','video','askTheDriver','wallpaper', 'news' ];
			} else {
				var tabs = ['about','images','video','askTheDriver','gear', "news"];
			}
			tabs.each(function(name, index) {
				var tab = name + 'Tab';
				if(tabName == name)
				{
					$(tab).className = 'active';
				} else {
					if ($(tab) != null) {
						$(tab).className = '';
					}
				}
			});
		}
	},
	
	/**
	*	Displays the popup for videos or images and starts playing a video if it was specified
	*
	*/
	setPopupDisplay: function() {
		var selectedMedia = {galleryId: this.levelTwo, itemId: this.levelThree};
		eventsFlashGallery.showGallery(selectedMedia, this.levelOne);
	},

	/**
	*	Contacts the ContentManager to request the full page content based on the current navigational state
	*
	*/
	requestInitContent: function() {
		var showTab = !(((this.levelOne == 'video') && $('videoTabMarker')) || ((this.levelOne == 'images') && $('imageTabMarker')));
		if(showTab){
			ContentManager.getInitContent( this.topSection, this.teamSection, this.levelOne, this.levelTwo, this.levelThree );
		}
	},
	
	/**
	*	Contacts the ContentManager to request just the new 'tab section' content based on the current navigational state
	*
	*/
	requestContent: function() {
		var showTab = !(((this.levelOne == 'video') && $('videoTabMarker')) || ((this.levelOne == 'images') && $('imageTabMarker')));
		if(showTab){
			ContentManager.getSectionContent( this.topSection, this.teamSection, this.levelOne, this.levelTwo, this.levelThree );
		}
	},
	
	/**
	*	BIKO: Set active states of top and side nav menu items depending on what section you are in
	*
	*/
	setActiveMenuItems: function(top, team) {
		if( ( top == 'drag' ) && ( team == '' ) ){
			$('menu-1').immediateDescendants().first().addClassName('active');
			$('side-menu').immediateDescendants().first().addClassName('active');
		} else if( ( top == 'drag' ) && ( team == 'titan' ) ){
			$('menu-1').immediateDescendants().first().addClassName('active');
			$('side-menu-drag').immediateDescendants()[0].addClassName('active');
		} else if( ( top == 'drag' ) && ( team == 'world' ) ){
			$('menu-1').immediateDescendants().first().addClassName('active');
			$('side-menu-drag').immediateDescendants()[1].addClassName('active');
		} else if( ( top == 'drag' ) && ( team == 'scottkelley' ) ){
			$('menu-1').immediateDescendants().first().addClassName('active');
			$('side-menu-drag').immediateDescendants()[2].addClassName('active');
		} else if( ( top == 'drift' ) && ( team == '' ) ){
			$('menu-1').immediateDescendants()[1].addClassName('active');
			$('side-menu').immediateDescendants()[1].addClassName('active');
		} else if( ( top == 'drift' ) && ( team == 'rsr' ) ){
			$('menu-1').immediateDescendants()[1].addClassName('active');
			$('side-menu-drift').immediateDescendants()[0].addClassName('active');
		} else if( ( top == 'drift' ) && ( team == 'nfs' ) ){
			$('menu-1').immediateDescendants()[1].addClassName('active');
			$('side-menu-drift').immediateDescendants()[1].addClassName('active');
		} else if( ( top == 'road' ) && ( team == '' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu').immediateDescendants()[2].addClassName('active');
		} else if( ( top == 'road' ) && ( team == 'crawford' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu-road').immediateDescendants()[0].addClassName('active');
		} else if( ( top == 'road' ) && ( team == 'dgspec' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu-road').immediateDescendants()[1].addClassName('active');
		} else if( ( top == 'road' ) && ( team == 'sparco' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu-road').immediateDescendants()[2].addClassName('active');
		} else if( ( top == 'road' ) && ( team == 'greddy' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu-road').immediateDescendants()[3].addClassName('active');
		} else if( ( top == 'road' ) && ( team == 'world' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu-road').immediateDescendants()[4].addClassName('active');
		} else if( ( top == 'road' ) && ( team == 'mpme' ) ){
			$('menu-1').immediateDescendants()[2].addClassName('active');
			$('side-menu-road').immediateDescendants()[5].addClassName('active');
		} else if( top == 'news' ){
			$('menu-1').immediateDescendants()[3].addClassName('active');
		} else if( top == 'schedule' ){
			$('menu-1').immediateDescendants()[4].addClassName('active');
		} else if( top == 'shows' ){
			$('menu-2').immediateDescendants()[0].addClassName('active');
		} else if( top == 'cars' ){
			$('menu-2').immediateDescendants()[1].addClassName('active');
		}
	}
	
});
