var durationTopStory = 10000;  //TIME IN MILLISECONDS BETWEEN TOP STORY ROTATION
var intervalIdTopStory = '';  //ID TO USE FOR ROTATION SETINTERVAL
var isRotating = false;

//ARRAY OF IMAGES FOR PRELOADING
var preImgs = new Array('/images/09/backgrounds/bg_btn_on.gif', '/images/09/backgrounds/bg_menu.png');

$(function() 
{
	$('#ny_logo').click(function()
	{
		location.href = "/index2.html";
	});
	
	preloadImages(preImgs);
	
	//NAVIGATION
	$('#navigation > li').hover(function()
	{
		$(this).addClass('current').find('div.menu').show();
	}, function()
	{
		$(this).removeClass('current').find('div.menu').hide();
	});
	
	$('#navigation .subnav li:last-child').addClass('last');
	
	//HP TOP STORY CONTROLS SETUP
	var $hpControls = $('#hp_top_stories .hp_control li');
	var numHpStories = $hpControls.filter('li.num').size();
	
	$hpControls.mouseover(function()
	{
		$(this).addClass('over');
	}).mouseout(function()
	{
		$(this).removeClass('over');
	});
	
	$hpControls.bind('click', { numHpStories: numHpStories, $hpControls : $hpControls }, changeHpStory);
	
   	// Start Top Story Rotation if the div exists
   	$('#hp_top_stories').each(function() 
	   {
			intervalIdTopStory = setInterval(rotateStory, durationTopStory);
			isRotating = true;
			$hpControls.filter('li:eq(0)').trigger('click');
	   }
	);

	//SETUP VIDEO BUTTONS TO PLAY VIDEO IN THICKBOX
	
	
	$('#hp_top_stories a.thickbox').each(function(index)
	{
		$(this).thickbox(
			{
			macFFBgHack: '/images/09/backgrounds/macFFBgHack.png',
			loadingImage: '/images/09/loadingIcon.gif',
			width: 576,
		        height: 366,
		        inlineId: 'vid_' + index
			}
		);
	});
	
	if (document.getElementById('hp_game'))
	{
		var now = new Date();
		var seasonStart = new Date();
		var seasonEnd = new Date();

		//(yyyy,m,dd) - month is zero based, so 8 = september
		//TO TEST A SPECIFIC DATE, UNCOMMENT THE NEXT LINE
		//now.setFullYear(2009,11,29);
		seasonStart.setFullYear(2009,8,14);
		seasonEnd.setFullYear(2009,11,29);
		var gameWeek = 3;
				
		if (now <= seasonStart)
		{
			gameWeek = 0;
		}
		else if (now >= seasonEnd)
		{
			gameWeek = 15;
		}
		else
		{
			gameWeek = getGameWeek(now);
		}
		
		//HP GAME MODULE CONTROLS SETUP
		var $HpGameControls = $('#hp_game .game_controls li a');
		$HpGameControls.click(changeHpGame);
		$HpGameControls.filter('a:eq('+ gameWeek +')').trigger('click');
	
		//HP STANDINGS MOD
		$('div.standings_controls a').bind('click', swapStanding);
		$('div.standings_controls a:eq(4)').trigger('click');
	}
	
	//HP TICKER
	if (document.getElementById('ticker'))
	{
		$('#ticker').liScroll({travelocity: 0.03}).css('visibility', 'visible');
	}

	//INITIATE KEYWORD POPUPS
	//$('a.keywordPop').keywordPopup();

	//NFL DROPDOWN HOVER
	$('.show_NFL', '#header').hover(showNFL, hideNFL);

	//TAB SWAP
	$('.tabbar a').click(showTab);
	
	//MM PAGE STARTING TAB
	$('#mm_videos').each(function()
	{
		$('#mm_tabs_09 ul li:first-child a').trigger('click');
	});
	
	//HIDE IMAGES ON ARTICLE PAGES UNTIL READY
	$('div.story_img_left').add('div.story_img_right').addClass('invisible');

});

//RUN FOLLOWING AFTER ALL IMAGES ARE AVAILABLE
$(window).load(function() 
{
	//FIX STORY IMG WIDTHS
	$('div.story_img_left').add('div.story_img_right').each(function()
	{
		var imgWidth = $(this).find('img:first-child').width();
		$(this).width(imgWidth).removeClass('invisible');
	});
	
});

var showNFL = function() 
{
	$('#NFL_network').show();
};

var hideNFL = function() 
{
	$('#NFL_network').hide();
};

var showTab = function()
{
	var theTab = $(this).attr('rel');
	$(this).parent().removeClass('tabinact').addClass('tabactive')
					.siblings().removeClass('tabactive').addClass('tabinact');
	$('.tabcontent').hide();
	$('.' + theTab).show();
	return false;
};

//OLD FUNCTION FROM CUT
// var swapStanding = function(e)
// {
// 	var theTable = $(this).attr('rel');
// 	$(this).addClass('current').siblings().removeClass('current');
// 	$('.standings_table').hide();
// 	$('table.' + theTable).css('display', 'table');
// 	return false;
// };

var swapStanding = function(e)
{
	var theTable = $(this).attr('rel');
	$(this).addClass('current').siblings().removeClass('current');
	$('#standings_go_here').load('/includes/ajax/homepage_standings/'+theTable+'.txt', 
		function(){
			$('table.standings_table > tbody > tr:even').addClass('altrow');
		});
	return false;
};


var changeHpStory = function(e)
{
	var storyNum = e.data.$hpControls.index(this);
	var $curStory = e.data.$hpControls.filter('li.current');
	var curStory = e.data.$hpControls.index($curStory);
		
	//IF THIS IS A NUMBER, SET THE STORY TO THE STORY CLICKED
	if (storyNum < e.data.numHpStories)
	{
		setHpStory(storyNum, e.data.$hpControls, e.data.numHpStories);
		if (isRotating)
		{
			resetStoryRotate();
		}
	}
	else if ($(this).hasClass('prev'))
	{
		setHpStory((curStory - 1), e.data.$hpControls, e.data.numHpStories);
		if (isRotating)
		{
			resetStoryRotate();
		}
	}
	else if ($(this).hasClass('next'))
	{
		setHpStory((curStory + 1), e.data.$hpControls, e.data.numHpStories);
		if (isRotating)
		{
			resetStoryRotate();
		}
	}
	else
	{
		if($(this).hasClass('play'))
		{
			$(this).removeClass('play')
					.addClass('stop')
					.find('img')
					.attr('src', '/images/09/buttons/hp_control_stop.png')
					.attr('alt', 'stop');
			intervalIdTopStory = setInterval(rotateStory, durationTopStory);
			isRotating = true;
		}
		else if($(this).hasClass('stop'))
		{
			$(this).removeClass('stop')
					.addClass('play')
					.find('img')
					.attr('src', '/images/09/buttons/hp_control_play.png')
					.attr('alt', 'play');
			window.clearInterval(intervalIdTopStory);
			isRotating = false;
		}
	}
};

var rotateStory = function()
{
	$('#hp_top_stories .hp_control li.next').trigger('click');
};

var resetStoryRotate = function()
{
	window.clearInterval(intervalIdTopStory);
	intervalIdTopStory = setInterval(rotateStory, durationTopStory);
	isRotating = true;
};


var setHpStory = function(storyNum, $hpControls, numHpStories)
{
	var currentStoryNum;
	$hpControls.removeClass('current');
	
	//IF THIS WILL SET THE STORY TO SOMETHING OUTSITE THE NUMBER OF STORIES, ADJUST storyNum
	if ((parseInt(storyNum) + 1) > parseInt(numHpStories))
	{
		storyNum = 0;
	}
	else if (parseInt(storyNum) < 0)
	{
		storyNum = (parseInt(numHpStories) - 1);
	}
	
	$hpControls.filter('li:eq(' + storyNum + ')').addClass('current');
	
	//SET THE HP STORY TO STORY (storyNum + 1)
	var topImg = $('#hp_top_stories .hp_story').hide()
		.filter('#hp_story_' + (parseInt(storyNum) + 1))
		.show()
			.find('#TopStoryImagePath' + (parseInt(storyNum) + 1))
			.val();

	document.getElementById('top_img').src = topImg;
	
	currentStoryNum = storyNum + 1;

};

var changeHpGame = function()
{
	var gameNum =$('#hp_game .game_controls li').index($(this).parent());
	var gameDate = document.getElementById("GAME_DATE" + (gameNum + 1)).value;

	$(this).parent().addClass('active').siblings().removeClass('active');

	$('div.hp_game_info').hide();
	$('#hp_game_' + (gameNum + 1)).show();

	eventCountDown(gameDate, (gameNum + 1));

	return false;
};

var preloadImages = function (urls)
{
	var img = new Array();
	for (var i=0; i<urls.length; i++)
	{
		img[img.length] = new Image();
		img[img.length - 1].src = urls[i];
	}
};


//jQUERY TICKER FUNCTION
jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
			var $strip = jQuery(this);
			$strip.addClass("newsticker")
			var stripWidth = 0;
			var $mask = $strip.wrap("<div class='mask'></div>");
			var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
			var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
			$strip.find("li").each(function(i){
			stripWidth += jQuery(this, i).width();
			});
			$strip.width((stripWidth + 20));			
			var defTiming = stripWidth/settings.travelocity;
			var totalTravel = stripWidth+containerWidth;								
			function scrollnews(spazio, tempo){
			$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
			}
			scrollnews(totalTravel, defTiming);				
			$strip.hover(function(){
			jQuery(this).stop();
			},
			function(){
			var offset = jQuery(this).offset();
			var residualSpace = offset.left + stripWidth;
			var residualTime = residualSpace/settings.travelocity;
			scrollnews(residualSpace, residualTime);
			});			
		});	
};

//This is where all the game dates are set.
//the dates are set to one day after the game - so the hp doesn't switch over for a day after the game
var getGameWeek = function(now)
{
	var game15 = new Date();
	game15.setFullYear(2009,11,28);
	if (now > game15) { return 15; }
	
	var game14 = new Date();
	game14.setFullYear(2009,11,22);
	if (now > game14) { return 14; }

	var game13 = new Date();
	game13.setFullYear(2009,11,14);
	if (now > game13) { return 13; }

	var game12 = new Date();
	game12.setFullYear(2009,11,7);
	if (now > game12) { return 12; }

	var game11 = new Date();
	game11.setFullYear(2009,10,27);
	if (now > game11) { return 11; }

	var game10 = new Date();
	game10.setFullYear(2009,10,23);
	if (now > game10) { return 10; }

	var game9 = new Date();
	game9.setFullYear(2009,10,9);
	if (now > game9) { return 9; }

	var game8 = new Date();
	game8.setFullYear(2009,10,2);
	if (now > game8) { return 8; }

	var game7 = new Date();
	game7.setFullYear(2009,9,26);
	if (now > game7) { return 7; }

	var game6 = new Date();
	game6.setFullYear(2009,9,19);
	if (now > game6) { return 6; }

	var game5 = new Date();
	game5.setFullYear(2009,9,12);
	if (now > game5) { return 5; }

	var game4 = new Date();
	game4.setFullYear(2009,9,5);
	if (now > game4) { return 4; }

	var game3 = new Date();
	game3.setFullYear(2009,8,28);
	if (now > game3) { return 3; }

	var game2 = new Date();
	game2.setFullYear(2009,8,21);
	if (now > game2) { return 2; }

	return 1;
};