var BORDER_WIDTH = 1;

var LATEST_NEWS_TICKER_TIMEOUT = 7000;
var LATEST_NEWS_FADER_TIMEOUT = 1000;
var timeoutThreadId = 0;


$( document ).ready( function()
{
	/**
	 * MAIN NAV
	 */
	//resizeNavElm( "nav_bar", 1 );

	resizeNavBar();
	
	/**
	 * FOOTER LINKS
	 */
	//$('#footer_links > ul').css('padding','0');
	//resizeNavElm( "footer_links" );

	//$('#footer_links ul > li').css('width','24%');
	/*$('#footer_links, #footer_links ul').css('margin','0').css('padding','0');*/
	
	/**
	 * BOTTOM GREYBAR
	 */
	//resizeNavElm( "bottom_greybar_mid" );

	/**
	 * SEARCH BUTTON ROLLOVER
	 */
	$( "#search_submit" ).mouseover( function()
	{
		$( "#search_submit" ).attr( "src", "/images/search_btn_active.gif" );
	});
	$( "#search_submit" ).mouseout( function()
	{
		$( "#search_submit" ).attr( "src", "/images/search_btn.gif" );
	});
	
	/**
	 * CLEAR SEARCH FIELD WHEN CLICKED
	 */
	$( "#search_keywords" ).click( function()
	{
		$( "#search_keywords" ).val( "" );
	});
	
	/**
	 * Set pdf links to open in new window, unless target specified
	 */
	var elms = $('a[@href$=.pdf]');
	elms.each(
		function()
		{
			if (! (this.target.length > 0))
			{
				$(this).attr('target', '_blank');
			}
		}
	);

	/**
	 * LATEST NEWS
	 */
	initialiseLatestNews();
	$( "#prev_news" ).click( getPrevNews );
	$( "#next_news" ).click( getNextNews );
	//timeoutThreadId = setTimeout( cycleNews, LATEST_NEWS_TICKER_TIMEOUT );
	timeoutThreadId = setTimeout( getNextNews, LATEST_NEWS_TICKER_TIMEOUT );
	
	
	//	Report filter
	var $elm = $('#report_filter');
	if ($elm.length > 0) {
		$elm.bind('click',
			function() {
				report_sort($(this).val());
			}
		);
	}
});


function adjustPods()
{
	var leftSidebarHeight = $( '#left_sidebar' ).height();

	/**
	 * CHECK IF BANNER SLIDESHOW EXISTS, IF IT DOES THEN USE ITS HEIGHT ELSE USE CONTENT HEIGHT
	 */
	if( $( "*" ).index( $( '#image_banner' )[0] ) < 0 )
		var midHeight = $( '#mid' ).height();
	else
		var midHeight = bannerImgHeight;
	
	var rightSidebarHeight =  $( '#right_sidebar' ).height();
	
	var stretcherHeight = getMaxHeight( leftSidebarHeight, midHeight, rightSidebarHeight );
	
	var rightPanels = $( '#right_sidebar .panel' );
	rightPanels.each( function()
	{
		//alert( "height: " + $( this ).height() + ", margin-bottom: " + $( this ).css( "margin-bottom" ) );
		stretcherHeight -= parseInt( $( this ).height() ) - parseInt( $( this ).css( "margin-bottom" ) );
	} );

	$( '#stretcher' ).height( stretcherHeight );
}


function getMaxHeight()
{
	var maxHeight = 0;

	if( arguments.length > 0 )
	{
		for( var i = 0; i < arguments.length; i++ )
		{
			if( arguments[i] > maxHeight )
				maxHeight = arguments[i];
		}
	}
	
	return maxHeight;
}

function resizeNavElm( navBarId, withBorder )
{
	// $( "#" + navBarId ) != undefined
	if( $( "#" + navBarId ).length > 0 )
	{
		//var navBarWidth = $( "#" + navBarId ).width();
		var navBarWidth = 995;
		var navElms = $( "#" + navBarId + " ul" ).children( "li" );	
		
		if( navElms.length > 0 )
		{
			var totalBorderWidth = ( withBorder != undefined ) ? ( navElms.length - 1 ) * BORDER_WIDTH : 0;
			navElmWidth = Math.floor( ( navBarWidth - totalBorderWidth ) / navElms.length );
			$( "#" + navBarId + " ul li" ).width( navElmWidth );
		}
	}
}


function initialiseLatestNews()
{
	var latestNews = $( ".panelNews" );
	if( latestNews.length > 1 )
	{
		var max_height = 0;
		latestNews.each( function()
		{
			if( $( this ).height() > max_height )
				max_height = $( this ).height();
		});
		
		latestNews.height( max_height );
	}
}


function getPrevNews()
{
	var latestNews = $( ".panelNews" );
	if( latestNews.length > 1 )
	{
		var visibleNewsIdx = -1;
		var i = 0;
		latestNews.each( function()
		{
			if( $( this ).attr( "class" ).indexOf( "panelNewsInvisible" ) < 0 )
				visibleNewsIdx = i;

			i++;
		} );

		if( visibleNewsIdx >= 0 )
		{
			/**
			 * We need to kill the current timeout thread and make a new one.
			 * We also need an extra time to fade out, so add that time in timeout
			 */
			if( timeoutThreadId > 0 )
				clearTimeout( timeoutThreadId );
			timeoutThreadId = setTimeout( getNextNews, LATEST_NEWS_TICKER_TIMEOUT + LATEST_NEWS_FADER_TIMEOUT );

			var visibleNews = latestNews[visibleNewsIdx];

			$( visibleNews ).fadeOut( LATEST_NEWS_FADER_TIMEOUT, function()
			{
				$( visibleNews ).addClass( "panelNewsInvisible" );

				if( visibleNewsIdx > 0 )	// if this is not the first news
				{
					$( visibleNews ).prev().fadeIn( 0, function()
					{
						$( visibleNews ).prev().removeClass( "panelNewsInvisible" );
					} );
				}
				else				// this is the first news hence get the last news
				{
					var lastNews = latestNews[latestNews.length - 1];
					$( lastNews ).fadeIn( 0, function()
					{
						$( lastNews ).removeClass( "panelNewsInvisible" );
					} );
				}
			} );
		}
	}
}


function getNextNews()
{
	var latestNews = $( ".panelNews" );
	if( latestNews.length > 1 )
	{
		var visibleNewsIdx = -1;
		var i = 0;
		latestNews.each( function()
		{
			if( $( this ).attr( "class" ).indexOf( "panelNewsInvisible" ) < 0 )
				visibleNewsIdx = i;

			i++;
		} );

		if( visibleNewsIdx >= 0 )
		{
			/**
			 * We need to kill the current timeout thread and make a new one.
			 * We also need an extra time to fade out, so add that time in timeout
			 */
			if( timeoutThreadId > 0 )
				clearTimeout( timeoutThreadId );
			timeoutThreadId = setTimeout( getNextNews, LATEST_NEWS_TICKER_TIMEOUT + LATEST_NEWS_FADER_TIMEOUT );


			var visibleNews = latestNews[visibleNewsIdx];

			$( visibleNews ).fadeOut( LATEST_NEWS_FADER_TIMEOUT, function()
			{
				$( visibleNews ).addClass( "panelNewsInvisible" );

				if( visibleNewsIdx < ( latestNews.length - 1 ) )	// if this is not the last news
				{
					$( visibleNews ).next().fadeIn( 0, function()
					{
						$( visibleNews ).next().removeClass( "panelNewsInvisible" );
					} );
				}
				else							// this is the last news hence get the first news
				{
					var firstNews = latestNews[0];
					$( firstNews ).fadeIn( 0, function()
					{
						$( firstNews ).removeClass( "panelNewsInvisible" );
					} );
				}
			} );
		}
	}
}

function resizeNavBar() {
	var elms = $('#nav_bar > .sf-menu > li');
	var pageWidth = 994;
	var count = elms.length;
	var width = Math.floor(pageWidth / count);
	var leftoverWidth = pageWidth - (count * width);
	elms.width(width);
	$('#nav_bar > .sf-menu > li:last').width(width + leftoverWidth);
}

/**
 * deprecated, cycle through getNextNews instead
 */
/*
function cycleNews()
{
	getNextNews();
	if( timeoutThreadId > 0 )
		clearTimeout( timeoutThreadId );
	timeoutThreadId = setTimeout( cycleNews, LATEST_NEWS_TICKER_TIMEOUT );
}
*/