// JavaScript Document
// JavaScript Document
$(document).ready(
	function()
	{
		$('#divAjax').css('height', '350px');
		$('#divChart').draggable();
		
		$.ajaxSetup({ cache: 'false'});
		
		loadPage(12);
	} // end document ready function
);

function loadPage(limit)
{
	$.post('nymex_ajax.php', { timeLimit : limit }, 
		function(data)
		{
			$('div#divAjax').css('height', 'auto').html(data);
			$('#divLimit').show();
			$('tr.content:odd').addClass('oddRow');
			$('tr.content:even').addClass('evenRow');
			
			$('#btnLimit').click(
				function()
				{
					newLimit = $('#selLimit').val();
					if(newLimit != limit)
					{
						$('#divLimit').hide();
						$('#divAjax').css('height', '350px');
						$('#divAjax').html('<img src="../img/ajax-loader-thick.gif" alt="Loading..." />');
						loadPage(newLimit);
					}
					return false;
				} // end btnLimit click function
			); // end btnLimit click event

			$('a.btnChart').click(
				function()
				{
					var $thisId = $(this).attr('id');
					var offset = $(this).offset();
					if((offset.top + 450) >= $(document).height())
						var newTop = offset.top - 200;
					else
						var newTop = offset.top - 50;
					$('body').prepend('<div id="blackscreen"></div>');
					$('div#blackscreen').css({ width: $(document).width() + 'px', height: $(document).height() + 'px' }).css('opacity', .5);
					$('div#blackscreen').fadeIn(500);
					$('div#divChart').css({ position: 'absolute', top: newTop + 'px'}).animate({ opacity: '1', width: '300px', height: '200px'}, 500);
					$.post('retrievechart.php', { time: $thisId },
						function(data)
						{
							if(data.length < 1) {
								$('div#divChartContent').html('A chart is not available as the NYMEX is closed today.');
							} else {
							$('div#divChartContent').html('<img class="chartImg" src="' + data + '" />');
							}
							$('div#divChart').animate({ width: '650px', height: '450px' }, 500);
							$('a#btnHide').show();
						}
					);
					
					$(window).resize(
						function()
						{
							$('div#blackscreen').css('width', $(document).width()).css('height', $(document).height() + 'px');
						}
					);
					
				return false;

				} // end chart button click function
			);
			
			$('div#divChart').ajaxStart(
				function()
				{
					$('div#divChartContent').html('<img src="/img/ajax-loader-small.gif" alt="Loading..." /> Loading chart, please wait...');
					$('a#btnHide').hide();
				}
			);
			
			$('a#btnHide').click(
				function()
				{
					$('div#divChart').hide(500, function() { $(this).css('width', '0').css('height', '0').css('opacity', '0');  });
					$('div#blackscreen').fadeOut(500, function() { $(this).remove(); });
					$('div#divChartContent').html('');
					return false;
				}
			); // end btnHide click event
		}
	); // end post call
}