// globals
var current_menu = false;
var current_drop = false;
var drop_active = false;
var supporter_logos = new Array("aliant.jpg","casino.jpg","coorslight.jpg","doolys.jpg","homedepot.jpg","molson.jpg","pepsi.jpg","telus.jpg","timhortons.jpg","wendys.jpg");
	
$(document).ready(function()
{

	// insert dropdown menus
	$.get('/home/js_menu', function(data){
		// don't show to IE6
		if($.browser.msie && $.browser.version<7) return;
		// load submenu
		$('#content .wrap').prepend(data);
		// menu li mouseover
		$('.menu li a').hover(
			function(){
				$('.dropdown:visible').hide();
				current_menu = $(this).parent('li').attr('class');
				current_drop = 'drop'+current_menu.replace(/menu/, "");
				$('#'+current_drop).show();
			},
			function(){
				current_menu = $(this).parent('li').attr('class');
				current_drop = 'drop'+current_menu.replace(/menu/, "");
				setTimeout("hide_drop('"+current_drop+"')",10);
			}
		);
		// dropdown mouseover
		$('.dropdown').hover(
			function(){
				drop_active = true;
				$('.'+current_menu+' a').addClass('current');
			},
			function(){
				drop_active = false;
				$('.'+current_menu+' a').removeClass('current');
				$(this).hide();
			}
		);
	});

	// setup gameday nav
	if($('#gameday').get(0))
	{
		// add title links
		if($('div.gameday-upcoming').get(0)) $('#gameday').prepend('<h3><a href="#upcoming" rel="gameday-upcoming">Upcoming</a></h3>');
		if($('div.gameday-next').get(0)) $('#gameday').prepend('<h3><a href="#next" rel="gameday-next">Next</a></h3>');
		if($('div.gameday-latest').get(0))$('#gameday').prepend('<h3><a href="#latest" rel="gameday-latest">Latest</a></h3>');
		// highlight first
		$('#gameday h3:first').addClass('current');
		// hide rest
		$('#gameday div:not(:first)').hide();
		// swap on click
		$('#gameday h3 a').click(function(){
			$('#gameday div').hide();
			$('#gameday h3.current').removeClass('current');
			var gdclicked = $(this).attr('rel');
			$('.'+gdclicked).show();
			$(this).parent('h3').addClass('current');
			return false;
		});
	}
	
	// rotate home news, show numbers
	if($('body.home').get(0))
	{
		newsTimer = setTimeout("news_next()",10000);
		$('div.latest').after('<div class="latest-numbers"><ul><li class="current"><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li></ul></div>');
		$('div.latest-numbers li a').click(function(){
			news_next( $('div.latest-numbers li a').index(this) );
			return false;
		});
	}

	// rotate home supporters
	if($('.supporter .logo').get(0))
	{
		setTimeout("supporter_next(0)",5000);
	}

	// rotate home sponsors
	if($('.sponsor').get(0))
	{
		setTimeout("random_sponsor()",5000);
	}

	// players fan photos
	if($('body.team').get(0) && $('.related-photos').get(0))
	{
		$('.related-photos dd a.relate').click(function(){
			// get clicked thumbnail id
			clicked_filename = $(this).attr('href').replace("/media/images/fanphotos/", "");
			// change photo
			$('.media-photo span').fadeOut('slow', function(){
				$('.media-photo span img').remove();
				$('.media-photo span').append('<img src="/media/images/fanphotos/'+clicked_filename+'" alt="" />');
				$('.media-photo span img').error(function(){$(this).hide();}); // hide missing images
				$('.media-photo span').fadeIn('slow');
			});
			return false;
		});
		$('.related-photos dd a.original').click(function(){
			// get clicked thumbnail id
			clicked_filename = $(this).attr('href').replace("/media/images/players/", "");
			// change photo
			$('.media-photo span').fadeOut('slow', function(){
				$('.media-photo span img').remove();
				$('.media-photo span').append('<img src="/media/images/players/'+clicked_filename+'" alt="" />');
				$('.media-photo span img').error(function(){$(this).hide();}); // hide missing images
				$('.media-photo span').fadeIn('slow');
			});
			return false;
		});
	}

	// confirm external links
	//$('a[@rel=external]').click(function(){
	//	return confirm('You are now leaving the Eagles website. Are you sure you wish to continue?');
	//});

});

// utilities

function hide_drop(drop)
{
	if(!drop_active) $('#'+drop).hide();
}

function supporter_next(current)
{
	current++;
	if(current==supporter_logos.length) current = 0;
	$('.supporter .logo span').fadeOut('slow', function(){
		$('.supporter .logo span img').remove();
		$('.supporter .logo span').append('<img src="/media/images/supporters/'+supporter_logos[current]+'" width="175" alt="" />');
		$('.supporter .logo span').fadeIn('slow');
		setTimeout("supporter_next("+current+")",5000);
	});
}

function random_sponsor()
{
	var randomnumber=Math.floor(Math.random()*1121);
	$('div.sponsor').fadeOut('slow', function(){
		$('div.sponsor').load('/home/random_sponsor/'+randomnumber, null, function(){$('div.sponsor').fadeIn('slow')});
	});	
	setTimeout("random_sponsor()",5000);
}

function news_next(news_current)
{
	clearTimeout(newsTimer);
	if(news_current==null)
	{	
		// determine next
		news_current = news_previous = $('ol.latest li').index($('ol.latest li.current').get(0));
		news_total   = $('ol.latest li').size();
		news_current++;
		if(news_current==news_total) news_current = 0;
	}
	// get next data
	news_obj = $('ol.latest li').get(news_current);
	next_id = $(news_obj).find('a').attr('href').replace("/news/view/", "");
	next_intro = $(news_obj).find('a').attr('title');
	next_h3 = $(news_obj).find('a').html();
	next_photo = ($(news_obj).find('a[rel="photo"]').length>0) ? true : false;
	// change photo
	$('.latest-photo span').fadeOut('slow', function(){
		$('.latest-photo span img').remove();
		if(next_photo) $('.latest-photo span').append('<img src="/media/images/news/'+next_id+'.jpg" width="435" alt="" />');
		$('.latest-photo a').attr('href', '/news/view/'+next_id);
		$('.latest-photo span img').error(function(){$(this).hide();}); // hide missing images
		$('.latest-photo span').fadeIn('slow');
	});
	// change feature
	$('div.latest h3 a').html(next_h3);
	$('div.latest h3 a').attr('href', '/news/view/'+next_id);
	$('div.latest p span').html(next_intro);
	$('div.latest p a').attr('href', '/news/view/'+next_id);
	// move current highlight
	$('ol.latest li.current').removeClass('current');
	$(news_obj).addClass('current');
	// move number highlight
	$('.latest-numbers li.current').removeClass('current');
	$('.latest-numbers li:eq('+news_current+')').addClass('current');
	// restart
	newsTimer = setTimeout("news_next()",10000);
}