var newsReelTimer = 0;

function changenewsreel(oldDom, newDom)
{
	var oldDomImageId = oldDom.html();
	var newDomImageId = newDom.html();
	oldDom.removeClass('newsactive');
	newDom.addClass('newsactive');
	$('img#newsimg' + oldDomImageId).addClass('disabled');
	$('img#newsimg' + newDomImageId).removeClass('disabled');
}

function advancenewsreel()
{
	var activeNav = $('ul.newsnav').children('.newsactive');
	var imageNum = parseInt(activeNav.html());
	var nextImageNum = (imageNum == 5) ? 1 : imageNum + 1;
	var newNav = $('ul.newsnav li:nth-child(' + nextImageNum + ')');
	changenewsreel(activeNav, newNav);
	newsReelTimer = setTimeout('advancenewsreel()', 5000);
}

$(document).ready(function()
{
	newsReelTimer = setTimeout('advancenewsreel()', 5000);

	$('li.newsnav').click(function()
	{
		clearTimeout(newsReelTimer);
		newsReelTimer = setTimeout('advancenewsreel()', 5000);
	
		var thisNav = $(this);
		if (!thisNav.hasClass('newsactive'))
		{
			var activeNav = $('ul.newsnav').children('.newsactive');
			if (!parseInt(thisNav.html()))
			{
				var imageNum = parseInt(activeNav.html());
				var nextImageNum = (imageNum == 5) ? 1 : imageNum + 1;
				thisNav = $('ul.newsnav li:nth-child(' + nextImageNum + ')');
			}
			changenewsreel(activeNav, thisNav);
		}
	});

	$('li.rankingtabs').click(function()
	{
		var thisTab = $(this);
		if (!thisTab.hasClass('tabactive'))
		{
			var activeTab = $('ul.rankingtabs').children('.tabactive');
			var activeTabId = activeTab.attr('id');
			var thisTabId = thisTab.attr('id');
			activeTab.removeClass('tabactive');
			thisTab.addClass('tabactive');
			
			var activeTableId = 'table' + activeTabId.substr(3);
			$('table#' + activeTableId).addClass('disabled');
			
			var thisTableId = 'table' + thisTabId.substr(3);
			var thisTable = $('table#' + thisTableId);
			thisTable.removeClass('disabled');
			if (!thisTable.hasClass('hasdata'))
			{
				var requestId = thisTabId.substr(3);
				thisTable.addClass('hasdata');		
				$.post('/beta/index_process.php',
				{
					action: 'frontpagedata',
					requestid: requestId
				},
				function(xmlResult)
				{
					var rankResult = $('iresult', xmlResult);
					var rankData = rankResult.attr('data');					
					if (rankData)
						thisTable.html(rankData);
					else
						thisTable.removeClass('hasdata');
				});
			}
		}
	});
});