jQuery(document).ready(function()
{
	jQuery( '#accordion' ).dcAccordion( {
	        eventType: 'click',
	        menuClose: false,
	        autoClose: true,
	        speed: 'fast',
					disableLink: false,
					showCount: false, 
	        saveState: false,
					closeOnStart: false
	} );
	
	var document_nav = jQuery( '#document_accordion' );
	if ( document_nav && document_nav.length > 0 )
	{
		document_nav.dcAccordion( {
		        eventType: 'click',
		        menuClose: false,
		        autoClose: true,
		        speed: 'fast',
						disableLink: false,
						showCount: false, 
		        saveState: false,
						closeOnStart: false
		} );
	}
	
	if ( window.location.href.indexOf( '/a-day-at-the-races/' ) != -1 )
	{
		var link = $( '#a-day-at-the-races_link' );
		if ( link && link.length > 0 )
		{
			link.click();
			var items = $( '#a-day-at-the-races_sub_menu' );
			if ( items && items.length > 0 )
			{
				var links = $( items ).children( 'li' ).children( 'a' ); 
				if ( links && links.length > 0 )
				{
					$( links ).each( function( index, link )
					{
					  if ( $( link ).attr( 'href' ) === window.location.pathname ) 
							$( link ).addClass( 'active' );
					} );
				}
			}
		}
	}	
	
	if ( window.location.href.indexOf( '/showcase-awards-2011/' ) != -1 )
	{
		var link = $( '#showcase-awards-2011_link' );
		if ( link && link.length > 0 )
		{
			link.click();
			var items = $( '#showcase-awards-2011_sub_menu' );
			if ( items && items.length > 0 )
			{
				var links = $( items ).children( 'li' ).children( 'a' ); 
				if ( links && links.length > 0 )
				{
					$( links ).each( function( index, link )
					{
					  if ( $( link ).attr( 'href' ) === window.location.pathname ) 
							$( link ).addClass( 'active' );
					} );
				}
			}
		}
	}
	
	if ( window.location.href.indexOf( '/chairmens-news/' ) != -1 )
	{
		setTimeout( 'openChairmensNewsMenu()', 250 );
	}	
	
	if ( window.location.href.indexOf( '/news/' ) != -1 )
	{
		setTimeout( 'openNewsMenu()', 250 );
	}	
	
	jQuery( '#page_back_link' ).click( function( event ) 
	{
		event.preventDefault();
		history.go(-1);
	} );
} );

function getParameterByName(name)
{
	var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
	return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function openNewsMenu()
{
	var link = $( '#news_link' );
	if ( link && link.length > 0 )
	{
		if ( window.location.pathname != '/news/' )
			link.click();
			
		if ( window.location.pathname == '/news/latest-news' )
		{
			$( '#latest_news_list_item_link' ).addClass( 'active' );
			$( '#news_list_item' ).addClass( 'active' );
		}
			
		var year = getParameterByName( 'y' );
		var month = getParameterByName( 'm' );
		
		if ( year && month && year != '' && month != '' )
		{
			var year_menu = $( '#year_' + year + '_link' );
			if ( year_menu && year_menu.length > 0 )
			{
				year_menu.click();
				
				var month_item = $( '#' + year + '_' + month + '_link' );
				if ( month_item && month_item.length > 0 ) 
					$( month_item ).addClass( 'active' );
			}
		}
	}
}

function openChairmensNewsMenu()
{
	var link = $( '#chairmens-news_link' );
	if ( link && link.length > 0 )
	{
		if ( window.location.pathname != '/chairmens-news/' )
			link.click();
			
		if ( window.location.pathname == '/chairmens-news/latest-news' )
		{
			$( '#chairmens_latest_news_list_item_link' ).addClass( 'active' );
			$( '#chairmens-news_list_item' ).addClass( 'active' );
		}
			
		var year = getParameterByName( 'y' );
		var month = getParameterByName( 'm' );
		
		if ( year && month && year != '' && month != '' )
		{
			var year_menu = $( '#chairmens_year_' + year + '_link' );
			if ( year_menu && year_menu.length > 0 )
			{
				year_menu.click();
				
				var month_item = $( '#chairmens_' + year + '_' + month + '_link' );
				if ( month_item && month_item.length > 0 ) 
					$( month_item ).addClass( 'active' );
			}
		}
	}
}
