/**
* global.js
*
* @author
* KKIM_( at )_aeg_world_wide_( dot )_com
* 11/04/2010
*
* @internal
* js file that will be used by all pages globally
*
*/
var template = 
{
	/**
	* @internal
	* intializes the template functions
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_init":function( )
	{
		this._nav( );
		this._highliteSubNav( );
		this._imageNavRollover( );
		this._navToggle( );
		this._contentSlideDown( '.slideDown', '.slideContent' );
		
		if( $( '.filterOptions' ).length > 0 )
		{
			$( '.filterOptions' ).select_skin();
		}
	}, // end _init		


	/**
	* @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


	/**
	* @internal
	* functionality for any navigation module for hover dropdown menu
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_navModule":function( )
	{
		$( 'ul.nav-Hover' ).find( 'ul' ).hide( );
		
		$( 'ul.nav-Hover' ).children( ).hover
		( 
			function( )
			{	
				$( this ).find( 'ul' ).show(  );
			},
			function( )
			{
				$( this ).find( 'ul' ).hide(  );
			}
		);
	},
	
	
	/**
	* @internal
	* functionality for any navigation module for a toggle dropdown menu
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_navToggle":function( )
	{
		// match the current page with the right nav img href to get the img object
		var rightNavObj = $( 'ul.nav-Toggle' ).find( 'ul' ).find( 'li' ).find( 'a[href="' + location.pathname + '"]' ).parent( ).parent( ).parent( ).children( 'img' );

		// check if the right sub-nav has another sub nav, if it does highlight the parent nav
		if( !empty( rightNavObj.attr( 'src' ) ) )
		{
			// use the nav obj to get the src image url
			var imgSrc = rightNavObj.attr( 'src' );

			// replaced src image with rollover image
			imgSrc = imgSrc.indexOf( '_ro' ) > 0 ? imgSrc : imgSrc.replace( '.', '_ro.' );		

			// unbinds mouse out behavior
			rightNavObj.removeAttr( 'onmouseout' );
		
			// set onState image	
			rightNavObj.attr( 'src', imgSrc );
		} // end right sub sub nav
		
		// highlights current right side navigation 
		else
		{
			rightNavObj = $( 'ul.nav-Toggle' ).children( 'li' ).find( 'a[href="' + location.pathname + '"]' ).children( 'img' );

			if( !empty( rightNavObj.attr( 'src' ) ) )
			{
				// use the nav obj to get the src image url
				var imgSrc = rightNavObj.attr( 'src' );

				// replaced src image with rollover image
				imgSrc = imgSrc.indexOf( '_ro' ) > 0 ? imgSrc : imgSrc.replace( '.', '_ro.' );		

				// unbinds mouse out behavior
				rightNavObj.removeAttr( 'onmouseout' );
		
				// set onState image	
				rightNavObj.attr( 'src', imgSrc );
			}
		}
		
	}, // end _navToggle
	
	
	/**
	* @internal
	* highlights sub nav's menu according to which page the user is on
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_highliteSubNav":function( )
	{
		var urlPath = location.pathname;

		urlPath = urlPath.split( '/' );

		// gets section name from the url path
		section = urlPath[ urlPath.length - 2 ];

		// highlights appropriate subnav item
		$( '.subNav' ).each( function( )
		{
			var temp = $( this ).find( 'img' ).attr( 'src' );

			if( temp.match( section ) )
			{
				var src = $( this ).find( 'img' ).attr( 'src' );
				
				src = str_replace( section + '.', section + '_ro.', src );
				
				$( this ).find( 'img' ).attr( 'src', src );
				$( this ).find( 'img' ).removeAttr( 'onmouseout' ); 
			} // end if
		} ); // end searching through subNav class items
	}, // end _highliteSubMenu


	/**
	* @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 ).parent( ).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 ).parent( ).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
		
		$( '.close' ).click( function( )
		{
			var indicator = $( this ).parent( content ).hide( ).parent( ).find( selector );
			
			var status = indicator.html( );
			
			indicator.html( str_replace( '[-]', '[+]', status ) );
		} );

	}, // end _contentSlideDown
	
	
	/**
	* @internal
	* rollover effect for section list such as the list of arenas, stadiums, sports, and etc, and right side navigation module
	*
	* @param
	* none
	*
	* @return
	* none
	*
	*/
	"_imageNavRollover":function( )
	{
		// checks for which list to rollover, due to the fact only one type listing of a section will be displayed on a paged
		var hoverItem = ( $( '.listingContainer' ).children( ).length > 0 ) ? $( '.listingContainer' ) : $( '.rightNaviContainer' );

		// hover effect, onmouseover change css for title to white background black font, and vice versa for onmouseout
		$( hoverItem ).hover
		( 
			// mouseover effect
			function( )
			{
				$( this ).find( 'span' ).eq( 0 ).css
				( 
					{
						"background-image":"url(/media/images/bg_white50.png)",
						"color":"black"
					} 
				);
			}, // end mouseover
			
			// mouseout effect
			function( )
			{
				$( this ).find( 'span' ).eq( 0 ).css
				( 
					{
						"background-image":"url(/media/images/bg_black.png)",
						"color":"white"
					}
				);
			} // end mouseout
		); // end hover effect
	} // end _listingRollover

} // end Template


$( function( ) 
{
	var activate = template;
	activate._init( );
		
	// search module form input ui
	$( '.searchInput' ).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
});

