/**
 *	Initialise the whole she-bang
 */
if( document.getElementById && jQuery ){
	$(document).ready(
		function(){
			Video.init();
			DayTabs.init();
		}
	);
}

/**
 *	Allows for the swapping of videos in the Maven player
 *
 *	@author	gordongroup marketing + communication
 */
var Video = {
	/**
	 *	Holds the Id of Maven player
	 *	@access	public
	 *	@var	string
	 */
	mavenId : 'tvpage',
	
	/**
	 *	Holds the Id of Maven player playlist
	 *	@access	public
	 *	@var	string
	 */
	mavenPlaylistId : '86e0ab804447bf3e3ec235a9ca86f46fc254e72f',

	/**
	 *	Holds the Id of the div Maven is placed in
	 *	@access	public
	 *	@var	string
	 */
	videoId : 'mediaplayer',
	
	/**
	 *	Holds the class to look for and attach the swap video event to
	 *	@access	public
	 *	@var	string
	 */
	swapClass : 'swap-video',
	
	/**
	 *	Holds the class to look for and attach the pop-up video event to
	 *	@access	public
	 *	@var	string
	 */
	popupClass : 'popup-video',
	
	/**
	 *	Initialises all the links with the class to swap videos
	 *
	 *	@access	public
	 *	@return	void
	 */
	init : function(){
		
		Video.initSwap();
		Video.initPopup();
		
		$( '.maven' ).html( '<div id="'+Video.videoId+'"><!-- --></div><p class="popup"><a href="maven.html" class="popUpLink">Launch Popup Player</a></p>' );
		Video.swap();
		$('.popUpLink').click(
			function(){
				return Video.popup( $(this).attr('href') );
			}
		);
		
	}, // END PROPERTY: init
	
	
	/**
	 *	Initialises the swap video links
	 *
	 *	@access	public
	 *	@return	void
	 */
	initSwap : function(){
		$( '.'+Video.swapClass ).click(
			function(){
				return Video.swap( $(this).attr('href') );
			}
		);	
	},
	
	
	/**
	 *	Initialises the pop up video links
	 *
	 *	@access	public
	 *	@return	void
	 */
	initPopup : function(){
		$( '.'+Video.popupClass ).click(
			function(){
				return Video.popup( $(this).attr('href') );
			}
		);
	},
	
	
	/**
	 *	Swaps the video by emptying the div and replacing it's contents with new flash
	 *
	 *	@access	public
	 *	@param	string	p_id
	 *	@return	false 
	 */
	swap : function( p_id ){

		if( p_id != '' && p_id != null ){
			var uniqueId = Video.cleanId( p_id );
		}

		$( '#'+Video.videoId ).html('');
		
		var swfObj = new SWFObject( streamPath, 'maven-swap', '360', '400', '8', '#535353' );
		swfObj.addParam( 'allowScriptAccess', 'always' );
		swfObj.addVariable( 'playerId', Video.mavenId );
		swfObj.addVariable( 'playlistId', Video.mavenPlaylistId );

		if( p_id != '' && p_id != null ){
			swfObj.addVariable( 'referralObject', uniqueId );
		}
		
		swfObj.addParam( 'quality', 'high' );
		swfObj.write( Video.videoId );
		
		return false;
	}, // END PROPERTY: swap


	/**
	 *	Pops up a new window and displays the selected video in the pop-up window
	 *
	 *	@access	public
	 *	@param	string	p_href
	 *	@return	false
	 */
	popup : function( p_href ){
		window.open( p_href, 'mavenpop', 'width=360,height=400,scrollbars=0,resizable=1');
		return false;	
	}, // END PROPERTY: popup
	
	
	/**
	 *	Returns the cleaned id
	 *
	 *	@access	public
	 *	@param	string	p_id
	 *	@return	string
	 */
	cleanId : function( p_id ){
		var patt = /^(?:.+)?#(\d+)$/;
		var matches = p_id.match( patt );
		return matches[1];
	} // END PROPERTY: cleanId
	
} // END OBJECT: Video



/**
 *	Creates Ajax bindings for the DayTabs
 *	
 *	@author	gordongroup marketing + communications
 */
var DayTabs = {
	/**
	 *	Holds the class to look for and attach the daytabs events to
	 *	@access	public
	 *	@var	string
	 */
	tabClass : 'day-tabs',
	
	/**
	 *	The code to generate a loader
	 *	@access	public
	 *	@var	string
	 */
	loaderTemplate: '<div class="loader">Loading&#8230;</div>',
	
	
	/**
	 *	Initialises the events on each of the links
	 *
	 *	@access	public
	 *	@return	void
	 */
	init : function(){
		$( 'ul.'+DayTabs.tabClass+' li a' ).each(
			function( i, val ){
				$(this).click(
					function(){
						DayTabs.loadDay( $(this).attr('href'), i );
						return false;
					}
				);
			}
		)
	}, // END METHOD: init
	
	
	/**
	 *	Loads the new day from Ajax, displays loading messages in the proper spaces
	 *
	 *	@access	public
	 *	@param	string	p_href
	 *	@param	string	p_int
	 *	@return	void
	 */
	loadDay : function( p_href, p_int ){
		
		var dateOffset = p_int - 1;
		var theDate = new Date();
		theDate.setDate( theDate.getDate() + dateOffset );
		var days = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' );
		var months = new Array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
		var newDate = days[ theDate.getDay() ]+', '+months[ theDate.getMonth() ]+' '+theDate.getDate()+', '+theDate.getFullYear();
		$('p.date').html( newDate );

		$('.loader').remove();
		
		DayTabs.setActive( p_href );
		$('#body').html( DayTabs.loaderTemplate );
		$('#sidebar-extra').html( DayTabs.loaderTemplate );
		$('#sidebar-feature').html( DayTabs.loaderTemplate );
		$('.primetime').html( DayTabs.loaderTemplate );
		
		$.get(
			'get-day.ajax.php',
			{ d:p_href },
			function( data ){
				$('#body').html( data );
				Video.initSwap();
				Video.initPopup();
			}
		);
		
		$.get(
			'get-primetime.ajax.php',
			{ d:p_href },
			function( data ){
				$('.primetime').html( data );
				$('.primetime .loader').remove();
			}
		);

		$.get(
			'get-also-on.ajax.php',
			{ d:p_href },
			function( data ){
				$('#sidebar-extra').html( data );
			}
		);
		
		$.get(
			'get-feature.ajax.php',
			{ d:p_href },
			function( data ){
				$('#sidebar-feature').html( data );
			}
		);
		
	}, // END METHOD: loadDay
	
	
	/**
	 *	Sets the correct tab active, and applys the previous and next classes
	 *	
	 *	@access	public
	 *	@param	string	p_href
	 *	@return	void
	 */
	setActive : function( p_href ){
		
		var active;
		
		$('ul.'+DayTabs.tabClass+' li').removeClass('active');
		$('ul.'+DayTabs.tabClass+' li').removeClass('previous');
		$('ul.'+DayTabs.tabClass+' li').removeClass('next');
		
		$('ul.'+DayTabs.tabClass+' li').each(
			function( i, val ){
				
				if( $(this).find('a').attr('href') == p_href ){
					active = i;
					$(this).addClass('active');		
				}
				
			}
		);
		
		var previous = active - 1;
		if( previous < 0 ){
			previous = 6;
		}
		
		var next = active + 1;
		if( next > 6 ){
			next = 0;
		}
		
		$('ul.'+DayTabs.tabClass+' li').eq(next).addClass('next');
		$('ul.'+DayTabs.tabClass+' li').eq(previous).addClass('previous');
		
	}
	
} // END OBJECT: DayTabs

