$( function( )
{
	// init sub-nav animation with params
	$('.sliding_banner').kwicks(
	{  
		max : 275,  
		spacing : 0 
	}); // end init sub-nav animation


	/** Loads Twitter App **/
	generic.loadModule( 'lib/jquery/twitterific/twitterific.js', function( )
	{
		$( '#twitter' ).twitterific( 
		{ 
			"url":"data/application/proxy.php?twitter/news/json", 
			"stopEvent":".idTabs a[href!=\"#tweets\"]" 
		} );
	} ); // end twitter app


	/**  Load Movie App **/
	generic.loadModule( "/lib/swfobject.js", function( )
	{
		if( swfobject.hasFlashPlayerVersion( "9.0.0" ) )
		{
			var flashvars = {};
			var params = {};
			params.scale = "noscale";
			params.salign = "tl";
			params.wmode = "transparent";
			flashvars.folderPath = "/media/video/";
			var attributes = {};
			swfobject.embedSWF("/media/swf/player.swf", "PlayerDiv", "457", "400", "9.0.0", false, flashvars, params, attributes);
		}
	} ); // end load movie app
	

	/** Loads Scrollbar Wallpaper **/	
	generic.loadModule( "/lib/jquery/scrollbarpaper/jquery.scrollbarpaper.js", function( )
	{
		$( '#twitter' ).scrollbarPaper( );
	} ); // end scrollbar wallpaper
	
	
	/** Tab re-enabling hover, highlight/onState clicked tab, and loading data to tabs through ajax **/
	$( '.idTabs' ).find( 'img' ).click( function ( )
	{
		/** unhighlight old selectd tab **/
		var tabObject = $( '.idTabs' ).find( '.selected' ).find( 'img' );
		var imgSrc = tabObject.attr( 'src' );

		imgSrc = imgSrc.replace( '_ro.', '.' ); 
		tabObject.bind( 'mouseleave', function( ){ $( this ).attr( 'src', imgSrc ); } );
		tabObject.attr( 'src', imgSrc );
		
		/** sets onstate/highlight of clicked item **/
		tabObject = $( this );
		tabObject.removeAttr( 'onmouseout' );
		tabObject.unbind( 'mouseleave' );
		
		var clickedImgSrc = tabObject.attr( 'src' );
		clickedImgSrc = ( clickedImgSrc.match( '_ro.' ) ) ? clickedImgSrc : clickedImgSrc.replace( '.', '_ro.' );
		tabObject.attr( 'src', clickedImgSrc );
		
		/** dynamic data generation and writing **/
		var tab = $( this ).parent( 'a' ).attr( 'href' );

		// loads data to the right tab on the right container
		switch( tab )
		{
			// load events in the event tab container
			case "#events" :
			{
				var loaded = $( tab ).find( '.loading' );
				loaded = loaded.length > 0 ? loaded : $( tab ).find( '.error' );
				
				// check if data is already loaded, if it isn't load the data
				if( loaded.length > 0)
				{
					/** Loads Events Application **/
					generic.loadModule( '/lib/application/eventsApp.js', function( )
					{
						var eventsObj = new eventList( );
		
						var request = [ '[0,' + eventsObj.config.get( 'HOME_EVENT_LIST_ITEMS' ) + ']' ];
		
						var list = eventsObj.serviceCall
						(
							{
								"data"	: { "type" : "Event:homepageEvents", "request" : request },
								"url"	: eventsObj.config.get( 'EVENT_SERVICE' )
				   			}
				   		); // end framework.serviceCall	
				   		
						// event list template
						var template = 	'<div class="homeeventRow">\
							<div style="float:left;width:410px;"><a href="<!--||EVENT_INFO_LINK||-->" class="homeeventTitle" target="_blank"><!--||EVENT_TITLE||--></a></div>\
							<div style="float:left;width:410px;"><span class="homeeventDate"><!--||EVENT_DATE||--> | <!--||EVENT_VENUE||--></span></div>\
							</div>'

						var html = '';


						// checks if events list exist
				   		if( !empty( list ) && !empty( list.data ) && is_array( list.data ) )
				   		{
				   			list = list.data;
			

							html = eventsObj.generateEvents( { "htmlTemplate":template, "listObj":list, "maxItemDisp":eventsObj.config.get( 'HOME_EVENT_LIST_ITEMS' ) } );

							$( '#eventsList' ).html( html );
				   		} // end if

				   		
				   		// error message if event list is missing
				   		else { $( '#eventsList' ).html( '<p class="error">We apologize for the inconvenience but there was an error with your request.  Please refresh the page, or try again later.</p>' ); } 
				   		
					} ); // end Loads Events Application
				} // end check if data is already loaded
				
				break;			
			} // end events tab load
			
			// load news in the event tab container
			case "#news" :
			{
				var loaded = $( tab ).find( '.loading' );
				loaded = loaded.length > 0 ? loaded : $( tab ).find( '.error' );
				
				// check if data is already loaded, if it isn't load the data
				if( loaded.length > 0)
				{
					/** Loads News Application **/
					generic.loadModule( '/lib/application/newsApp.js', function( )
					{
						var newsObj = new newsList( );
		
						var request = [ '[0,' + newsObj.config.get( 'HOME_NEWS_LIST_ITEMS' ) + ']' ];
		
						var list = newsObj.serviceCall
						(
							{
								"data"	: { "type" : "News:newsList", "request" : request },
								"url"	: newsObj.config.get( 'NEWS_SERVICE' )
				   			}
				   		); // end framework.serviceCall	
				   		
						// event list template
						var template = 	'<div class="homenewsRow"><a href="<!--||NEWS_LINK||-->" class="homenewsTitle"><!--||NEWS_TITLE||--></a><span class="homenewsDate"><!--||NEWS_DATE||--> | <!--||NEWS_TYPE||--></span></div>';

						var html = '';

						// checks if news list exist
				   		if( !empty( list ) && !empty( list.data ) && is_array( list.data ) )
				   		{
				   			list = list.data;
			
							html = newsObj.generateNews( { "htmlTemplate":template, "listObj":list, "maxItemDisp":newsObj.config.get( 'HOME_NEWS_LIST_ITEMS' ) } );

							$( '#newsListing' ).html( html );
				   		} // end if
				   		
						// error message if event list is missing
				   		else { $( '#newsListing' ).html( '<p class="error">We apologize for the inconvenience but there was an error with your request.  Please refresh the page, or try again later.</p>' ); }
				   		
					} ); // end Loads News Application
				} // end check if data is already loaded
				
				break;			
			} // end news tab load
			
			// load tweets in the event tab container
			case "#tweets" :
			{
				var loaded = $( tab ).find( '.loading' );
				loaded = loaded.length > 0 ? loaded : $( tab ).find( '.error' );
				
				var tweetCount = $( tab ).find( '.jta-tweet-list-item' ).length;
				
				/**
				* Needs to remove all instances of the twitter div with the events and write a new div for the
				* scrollpaper to work correctly with the new twitter instance
				*
				* Will only activate if the tab is currently not on twitter tab
				*
				**/ 
				if( !$( this ).parent( 'a' ).attr( 'class' ) )
				{
					// removes the current twitter html and any related handlers
					$( '#twitter' ).remove( );
				
					// loads new loading html for the twitter
					$( tab ).html
					( 
						'<div id="twitter"><div class="loading"><p><img src="/media/images/loading.gif" alt="Loading..." /><br />loading...</p></div></div>\
						<div class="axsTwitter">\
							<img src="/media/images/twitter_axs.png" align="right" style="padding-top:10px;" />\
						</div>'
					);
				
					/** loads new instance of twitter with scrollbar **/
					$( '#twitter' ).twitterific( { "url":"data/application/proxy.php?twitter/news/json", "stopEvent":".idTabs a[href!=\"#tweets\"]" } );
					$( '#twitter' ).scrollbarPaper( );
				} // end if
				break;			
			} // end tweets tab load
		} // end switch statement of loading tabs with data
	} ); // end tab highlight on click and loading data to tabs through ajax
	
	
	if( !( navigator.appVersion.indexOf( 'MSIE 7.' ) == -1 ) ){ generic.fixzIndex( ); }
}); 

