/*External Links*/

$(document).ready(function() {
	if($('.external')[0]) {
		$('.external').click(function() {
			open(this.href);
			return false;
		});
	}
});

/*Table Stripes*/

$(document).ready(function() {
	if($('.table-content')[0]) {
		if ($('.table-content')) {
			$('.table-content tbody tr:odd').addClass('even');
			$('.table-content tbody td:first-child').addClass('nesn');
		}
	}
});

/*Video Thumbs*/

$(document).ready(function() {
	if($('#videos')[0] || $('#video-center')[0]) {
		$('.video-hover').hover(
			function() {
				$(this).prepend('<img src="http://cdn.nesn.com/video-hover-35.png" class="play-button" />');
			},
			function() {
			$(this).find('.play-button').remove();
			}
		);
	}
});

/*Countdown Clocks*/

$(document).ready(function() {
	if($('#hockey-countdown')[0]) {
		$('#winter-classic-countdown').countdown({until: new Date(2010, 0, 1, 13),  format: 'dhM', compact: true, timezone: -5});
		$('#legends-classic-countdown').countdown({until: new Date(2010, 0, 2, 14),  format: 'dhM', compact: true, timezone: -5});
		$('#frozen-fenway-countdown').countdown({until: new Date(2010, 0, 8, 16),  format: 'dhM', compact: true, timezone: -5});
	}
});

/*Photo Gallery*/

$(document).ready(function() {
	if($('#gallery-container-single')[0]) {
		$('#gallery-container-single #thumbs a').each(function() {
			if ($('#gallery-container-single #slideshow img').attr('alt') == $(this).attr('title')) {
				$(this).parent().addClass('selected');
			};
		});
		$('#gallery-container-single #thumbs li:not(.selected)').find('img').css('opacity', '0.67');
		$('#gallery-container-single #thumbs li:not(.selected)').hover(
			function() {
				$(this).find('img').fadeTo('fast', 1);
			},
			function() {
				$(this).find('img').fadeTo('fast', 0.67);
			}
		);
		$('#gallery-container-single .selected img').css('opacity', 1);
		var prevUrl = $('#gallery-container-single .selected').prev().find('a').attr('href');
		if(prevUrl == undefined) {prevUrl = $('#gallery-container-single .thumbs li:last').find('a').attr('href');}
		var nextUrl = $('#gallery-container-single .selected').next().find('a').attr('href');
		if(nextUrl == undefined) {nextUrl = $('#gallery-container-single .thumbs li:first').find('a').attr('href');}
		$('#gallery-container-single .image-wrapper').prepend('<div id="controls" class="controls"><div class="nav-controls"> <a class="prev" title="Previous Photo" rel="history" href="' + prevUrl + '">&laquo; Previous Photo</a><a class="next" title="Next Photo" rel="history" href="' + nextUrl + '">Next Photo &raquo;</a></div></div>').append('<div id="controls" class="controls"><div class="nav-controls"> <a class="prev" title="Previous Photo" rel="history" href="' + prevUrl + '">&laquo; Previous Photo</a><a class="next" title="Next Photo" rel="history" href="' + nextUrl + '">Next Photo &raquo;</a></div></div>');
		$('#gallery-container-single #slideshow img').wrap('<a href="' + nextUrl + '"></a>');
	}
});

/*Quiz Engine*/

$(document).ready(function() {
	if($('#question')[0] || $('#answer')[0]) {
		var args = new Object();
		var query = location.search.substring(1);
		var pairs = query.split('&');
		$.each(pairs, function(i,pair) {
			var pos = pair.indexOf('=');
			var argname = pair.substring(0,pos);
			var value = pair.substring(pos+1);
			args[argname] = unescape(value);
			return args;
		});
		var quizname = location.pathname.split('.')[0].split('/')[1].split('-')[0];
		var quiznum = location.pathname.split('.')[0].split('/')[1].split('-')[1].substr(1) * 1;
		var pagenum = location.pathname.split('.')[0].split('/')[1].split('-')[2].substr(1) * 1;
	}
	if($('#question')[0]) {
		$('#question :radio').click(function() {
			var ans = $('#question :radio:checked[value]').val();
			url = quizname + '-q' + quiznum + '-a' + pagenum + '.html?ans=' + ans + '&ansCor=' + args.ansCor;
			window.location = url;
		});
	}
	if($('#answer')[0]) {
		if (args.ans == ans) {
			$('#answer').append('Correct! You\'re now ' + ((args.ansCor * 1) + 1) + ' for ' + pagenum + '.');
			$('#submit').click(function() {
				url = quizname + '-q' + quiznum + '-q' + (pagenum + 1) + '.html?ansCor=' + ((args.ansCor * 1) + 1);
				window.location = url;
			});
		} else {
			$('#answer').append('Wrong! You\'re now ' + args.ansCor + ' for ' + pagenum + '.');
			$('#submit').click(function() {
				url = quizname + '-q' + quiznum + '-q' + (pagenum + 1) + '.html?ansCor=' + args.ansCor;
				window.location = url;
			});
		}
	}
});