var site = {
	init: function() {
		site.initBanner();
	},
	contactForm: function() {
		$('nav a[href="/contact"], nav a[href="/request-a-meeting"]').click(
			function(e) {
				e.preventDefault();
				$.get($(this).attr('href'), function(data) { $(data).modal({opacity: 80, onShow: site.initModal}); });
			}
		);
	},
	
	initModal: function() {
		Cufon.now();
		cufonInit();
		required_fields.init();
	},

	initBanner: function() {
		// Check to see if there are multiple banners
		if($('#banners li').length > 1) {
			var banners = $('#banners li');
			// Hide all of banners
			banners.hide();
			// Set the first one as current and show it
			$(banners[0]).show().addClass('current');
			// Mark the second one as the next banner
			$(banners[1]).addClass('next');
			// Start the timer
			site.bannerInterval = window.setInterval('site.crossfadeBanners()', 6000);
			
			// Create an ordered list for the controls
			var controls = $('<ol id="controls">');
			$('#banner').append(controls);
			// Add a control for each banner
			banners.each(function() {
				controls.append('<li>' + $(this).index() + '</li>');
			});
			// Mark the first control as current
			controls.find('li').first().addClass('current');
			// Set up an onclick callback to stop the timer and change banners
			controls.find('li').click(function() {
				window.clearInterval(site.bannerInterval);
				$('#banners li.next').removeClass('next');
				$(banners[$(this).index()]).addClass('next');
				site.crossfadeBanners();
			});
		}
	},
	
	crossfadeBanners: function() {
		// This is the banner which will fade in
		var next_banner = $('#banner li.next');
		// Fade out the old banner
		$('#banners li.current').fadeOut(500).removeClass('current');
		// Fade in the new banner
		next_banner.fadeIn(500).addClass('current').removeClass('next');
		// Find the next banner and mark it as next
		if(next_banner.next().length > 0) {
			next_banner.next().addClass('next');
		} else {
			$('#banners li:first-child').addClass('next');
		}
		// Mark the new control as current
		$('#controls li.current').removeClass('current');
		$($('#controls li')[next_banner.index()]).addClass('current');
	}

};
jQuery(site.init);
