// Onload: Background Resize
// Void the image src before binding onload (http://code.google.com/p/chromium/issues/detail?id=7731)
background_image_src = $('#background-image').attr('src');
$('#background-image').attr('src','');
$('#background-image').bind("load", function() {
	bgResize();
	
	// Reset the body background
	$('body').css({backgroundColor: 'transparent'});	
}, false);
$('#background-image').attr('src',background_image_src);


// Onready
$(document).ready(function() {
	// Background Resize
	$(window).resize(function() {
		bgResize();
	});	
	
	// Setup the about dialog
	setupAboutDialog();
	
	// Setup footer featured events
	setupFooterFeaturedEvents();
	
	// Animate the Event Bar
	if (eventTotal > 1) {
		setTimeout("animateEventBar()", 5000);
	}
});

// Resize the background

var bgResize = function() {
	var bgImg = $('#background-image'),
		newWidth,
		imgWidth = bgImg.width(),
		imgHeight = bgImg.height(),
		winWidth = $(window).width(),
		widthRatio = winWidth / imgWidth,
		heightRatio = $(window).height() / imgHeight;
		
	if (widthRatio < heightRatio) {
		widthRatio = heightRatio;
	}
	
	newWidth = widthRatio * imgWidth;
		
	bgImg.css({
		width:  newWidth + 'px',
		height: (widthRatio * imgHeight) + 'px',
		padding: 0,
		left: newWidth > winWidth ? '-' + (newWidth - winWidth) / 2 + 'px' : 0
	});
}

// Animate the Event Bar
function animateEventBar() {
    $("#animated-event-bar").animate({
		opacity: 0
	}, {duration: 1000, complete: function(){				
		// Change the event
		eventIndex = (eventIndex == eventTotal - 1) ? 0 : eventIndex + 1;			
		$('#animated-event-bar a').attr('href', eventUrls[eventIndex]);
		$('#animated-event-bar a').html(eventTitles[eventIndex]);	
		$('#animated-event-bar').animate({
			opacity: 1
		}, {duration: 1000, complete: function () {
			setTimeout("animateEventBar()", 5000);
		}}
		);			
	}});
}

// Setup about dialog
function setupAboutDialog() {
	$('.footer-links a').hover(
	  // On mouse enter
	  function () {
	    $('#about-dlg').fadeIn();
	  }
	);
	
	
	$('#about-dlg').hover(
	  // On mouse enter
	  function () {
	    $('#about-dlg').show();
	  }, 
	  // On mouse leave
	  function () {
	    $('#about-dlg').fadeOut();	
	  }		
	);
	
	// Setup video
	$("#about-dlg .video a").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'fade',
			'title'			: this.title,
			'width'			: 960,
			'height'		: 540,
			'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
			'type'			: 'swf',
			'overlayShow': true,
			'overlayOpacity': 0.75,
			'overlayColor': '#000'
		});

		return false;
	});		
}

// Setup footer featured events
function setupFooterFeaturedEvents() {
	$('#carousel a').hover(
		// On mouse enter
		function () {			
			featured_event = $(this).children('span');
			featured_event.css('display', 'inline-block');	
			
			// Calculate width of featured event span
			var final_width = featured_event.width();			
			featured_event.css('width', 0);			
			featured_event.animate({
				width: final_width
			}, 500);
		},
		
		// On mouse leave
		function () {
			featured_event = $(this).children('span');
			featured_event.animate({
				width: 0
				},
				{duration: 250, complete: function() {
					$(this).css({
						'display': 'none',
						'width': 'auto'
					});					
				}}
			);
		}		
	);
}


