var template = 
{
	/**
	* @internal
	* intializes the template functions
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_init":function( )
	{
		this._nav( );
		this._contentSlideDown( '.slideDown', '.slideContent' )
	}, // end _init		


	/**
	* @internal
	* creates UI interface that slides down content when clicked on a DOM element
	*
	* @param
	* selector:string - the selector that controls the content slide up/down eg h5, a, .classname, #idname
	* content:string - the selector html DOM element that contains the content that needs to be slid down eg div, .classname, #idname
	*
	* @return
	* none
	*
	*/
	"_contentSlideDown":function( selector, content )
	{
		var needlePlus = /\[\+\]/i;
		var needleMinus = /\[-\]/i;
	
		// click handler for the show of content
		$( selector ).click( function( )
		{
			// grabs current clicked item
			var selectText = $( this ).html( );

			// if item is expanded, slide up content
			if( needleMinus.test( selectText ) )
			{
				$( this ).html( str_replace( '[-]', '[+]', selectText ) );
				$( this ).next( content ).hide( 'blind', 'fast' );
			} // end hide content
			
			// if item is hidden, slide down content
			else if( needlePlus.test( selectText ) )
			{
				// checks to see if other items are already on display to hide them
				$( selector ).each( function( )
				{
					var plus = $( this ).html( );
					
					// if content item is diplayed, hide it so that only one content item is displayed at a time
					if( needleMinus.test( plus ) )
					{
						$( this ).html( str_replace( '[-]', '[+]', plus ) );
						$( this ).next( content ).hide( 'blind', 'fast' );
					} // end if
				} ); // end check
			
				$( this ).html( str_replace( '[+]', '[-]', selectText ) );
				$( this ).parent( ).next( content ).show( 'blind', 'fast' );
			} // end show content
		} ); // end click handler
		
		/** if a close tag is found close the current DOM elements parent **/
		$( '.close' ).click( function( )
		{
			var indicator = $( this ).parent( content ).hide( ).prev( ).find( selector );
			
			var status = indicator.html( );
			
			indicator.html( str_replace( '[-]', '[+]', status ) );
		} ); // end close

	}, // end _contentSlideDown

	
	/**
	* @internal
	* controls main nav menu's effects, hovers, and dropdowns
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_nav":function( )
	{
		var urlPath = location.pathname;
		
		// splits url by '/' if it is not the homepage		
		urlPath = ( urlPath.length > 1 ) ? urlPath.split( '/' ) : urlPath;

		// creates search param for jquery find
		var hrefSearch = ( is_array( urlPath ) ) ? 'a[href^="/' + urlPath[ 1 ] + '/"]' : 'a[href="' + urlPath + '"]';

		// activates superfish with params
		$( "ul.sf-menu" ).superfish(
		{
			"delay":0,
			"speed":"fast",
			"dropShadows":false
		} ); // end superfish

		/** highlights the main menu tab depending on the page the user is at as the first indicator of navigation path tree **/
		var object = $( "ul.sf-menu" ).find( hrefSearch );
		var src = object.children( 'img' ).attr( 'src' );
		
		if( !empty( src ) )
		{
			src = ( src.indexOf( '_ro' ) > 0 ) ? src : str_replace( '.', '_ro.', src );
			object.children( 'img' ).removeAttr( 'onmouseout' ).attr( 'src', src );
			/** end navigation follow path tree highlight **/
		
			// keeps rollover status for main nav image rollover, due to non-template image for each section
			$( ".sub_sf-menu" ).hover( 
				function( )
				{
					var src = $( this ).parent( ).find( 'img' ).unbind( 'mouseenter mouseleave' ).attr( 'src' );
					src = ( src.indexOf( '_ro' ) > 0 ) ? src : str_replace( '.', '_ro.', src );
					$( this ).parent( ).find( 'img' ).attr( 'src', src );
				}, // end mouseenter state
				function( )
				{
					// if the item is not part of the navigation follow path tree highlight, activate mouseout effect
					if( object.attr( 'href' ) != $( this ).prev( ).attr( 'href' ) )
					{
						var src = $( this ).parent( ).find( 'img' ).attr( 'src' );
						src = str_replace( '_ro.', '.', src );
						$( this ).parent( ).find( 'img' ).attr( 'src', src );
					} // end if
				} // end mouseout state
			); // end hover effects
		}
	} // end _menu

} // end Template

$( function( )
{
	// activates any template functions
	var activate = template;
	activate._init( );
	
	
	// load countdown timer script and css
	generic.loadCSS( "/css/countdown.css" );
	generic.loadModule( "/lib/jquery/jquery.countdown/jquery.countdown.min.js", function( )
	{
		var targetDate = new Date( );		
		var userTime = new Date( );

		// gets the timezone offset in milliseconds
		userTime = userTime.getTimezoneOffset( ) * 60 * 1000;

		// sets the target date of countdown
		targetDate.setFullYear( 2012 );
		targetDate.setMonth( 7 );
		targetDate.setDate( 5 ); // plus one day for august 5 due to 4pm PST offset of +8 hours GMD time
		targetDate.setHours( 0 ); // 4pm PST offset of +8 hours GMT time set
		targetDate.setMinutes( 0 );
		targetDate.setSeconds( 0 );

		// calculates user timezone from GMT for countdown consistency
		targetDate = targetDate.getTime( ) - userTime;
		targetDate = new Date( targetDate );

		// creates countdown timers
		$( '.timer' ).countdown( { "until": targetDate, "significant":4, "format":"dHMS", "layout":'{d<}{dnn}  {sep}  {d>}{hnn}  {sep}  {mnn}{s<}  {sep}  {snn}{s>}',
			"alwaysExpire":true,
			"onExpiry":function( ) { $( this ).css( 'font-size', '18px' ).html( 'See you on the court!' ); }
		 });
	} ); // end countdown timer

	
	// load book a room module script
	generic.loadModule( "/lib/application/bookroom.js", function( )
	{
		var reservation = bookModule;
		reservation.bookRoomInitialize( );
	} ); // end book room module script

	
	// newletter module form input ui
	$( '#newsletter' ).bind( 'focusin', function( ) 
	{
		if( $( this ).val( ) == $( this ).attr( 'title' ) ){ $( this ).val( '' ); }
	} )
	.bind( 'focusout', function( )
	{ 
		if( $( this ).val( ).length == 0 ){ $( this ).val( $( this ).attr( 'title' ) ); } 
	} ); // end form input UI


	// newletter form validation
	$( '#subscribe' ).css({ "cursor":"pointer" }).bind( 'click', function( )
	{
		var validation = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var input = $.trim( $( '#newsletter' ).val( ) );

		// not valid email.
		if( !validation.test( input ) ){ alert( 'Please enter a valid e-mail address, ' + input + ' is not a valid e-mail address' ); }

		// submit form.
		else{ $( '#newsletter' ).parent( ).submit( ); }
	} ); // end validation


	$.ajax(
	{
		"type":"GET",
		"url":"/data/application/proxy.php?url=http://twitter.com/statuses/user_timeline/138120531.rss",
		"dataType":"xml",
		"fail":function( x )
		{
			// do error message here.
		},
		"success":function( data )
		{
			var $item = $( data ).find( 'item:first' );
			var time = generic.tweetTimeFormatter( $item.find( 'pubDate' ).text( ) );
			var text = $item.find( 'title' ).text( ).replace( 'Nikebball3on3:', '' );		

			$( 'div.tweet' ).html( text );
		}
	});






	/**
	
	FILTER_VALIDATE_URL
	
	// load twitter module.
	generic.loadModule( "/lib/jquery/twitterific/twitterific.js", function( )
	{
		console.log( $( '#twitter' ).twitterific );
	
		$( 'div.twitter' ).twitterific({ "url":"/data/application/proxy.php?twitter/news/json" });
	}); // end load twitter module.
	/**/
} );

