

function slideSwitch() {
	if($("#headerImage > img").size() > 2)
	{
		var $active = $('#headerImage img.active');
		if ( $active.length == 0 ) $active = $('#headerImage img:last');
	
		// use this to pull the images in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()
			: $('#headerImage img:first');
	
		// uncomment the 3 lines below to pull the images in random order
		
		// var $sibs  = $active.siblings();
		// var rndNum = Math.floor(Math.random() * $sibs.length );
		// var $next  = $( $sibs[ rndNum ] );
	
		$active.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	}
}


$(document).ready(function() {
	
	/**
	 * Homepage quick links
	*/
	$("#courseTypes a").hover(function() {
		$(this).children("img.courseTypesText").stop().animate({ 
		top: 50
	  }, 200 );
		$(this).children("img.courseTypesImage").stop().animate({ 
		opacity: 0.6
	  }, 200 );
	}, function() {
		$(this).children("img.courseTypesText").stop().animate({ 
		top: 55
	  }, 600 );
		$(this).children("img.courseTypesImage").stop().animate({ 
		opacity: 1
	  }, 600 );
	});
	 
	
	/**
	 * Header fade
	 */
	setInterval( "slideSwitch()", 7000 );
	
	/**
	 * Application form pages 
	 */
	(function() {
		
		// Setup some variables..
		var form_pages = $('#application-form .form-page');
		var tallest_form = 0;
		form_pages.each(function() {
			if($(this).height() > tallest_form) {
				tallest_form = $(this).height();
			}
		});
		
		$('#application-form').css('min-height', (tallest_form + 144));
		
		//console.log('tallest_form: ' + tallest_form);
		
		
		/**
		 * Change page
		 */
		$(window).bind('hashchange', function() {
			
			// Setup some more variables..
			var hash = window.location.hash || '#step1';
			var page_num = parseInt(hash.substr(5, 1));
			var page_num_previous = page_num - 1;
			var page_num_next = page_num + 1;
			var page_uri = window.location.pathname.substr(5);
			var total_pages = form_pages.size();
			
			$.pageVars = {
				hash: hash,
				page_num: page_num,
				total_pages: total_pages
			};
			
			//console.log('////////////////////////////////////////');
			//console.log($.pageVars);
			/*console.log('hash: ' + hash);
			console.log('page_num: ' + page_num);
			console.log('page_num_previous: ' + page_num_previous);
			console.log('page_num_next: ' + page_num_next);
			console.log('page_uri: ' + page_uri);
			console.log('total_pages: ' + total_pages);
			console.log('////////////////////////////////////////');*/
			
			// Hide the pages
			form_pages.hide().removeClass('current');
			
			// Show the current page
			form_pages.filter(hash).show().addClass('current');
			
			// Update the next and previous buttons
			$('#application-form a.previous').attr('href', page_uri + '#step' + page_num_previous).show();
			$('#application-form a.next').attr('href', page_uri + '#step' + page_num_next).show();
			
			$('#application-form .form-header h2').html('Step ' + page_num + ' of ' + total_pages);
			
			if(page_num_previous < 1) {
				$('#application-form a.previous').hide();
			}
			
			if(page_num_next > total_pages) {
				$('#application-form a.next').hide();
			}
			
			
		});
		
		/**
		 * Trigger a change of page as soon as the script loads
		 */
		$(window).trigger('hashchange');
		
		/**
		 * Form Validation
		 */
		function form_validation(e) {
			var inputs = $('#application-form ' + $.pageVars.hash + ' .required input[type=text], #application-form ' + $.pageVars.hash + ' .required textarea');
			
			// Clear previous errors
			$('#application-form #errors ul').html('');
			$('#application-form #errors').hide();
			
			// Scroll to the top
			$('html, body').animate({ scrollTop: 0 }, 'slow');
			
			inputs.each(function() {
				var val = $.trim($(this).val());
				var label = $(this).siblings('label').html();
				if(val == '') {
					$(this).addClass('error');
					$('#application-form #errors ul').append('<li>' + label + ' is required.</li>');
				}
				else {
					$(this).removeClass('error');
				}
			});
			
			// Are there errors?
			if($('#application-form #errors ul').html()) {
				e.preventDefault();
				$('#application-form #errors').show();
			}
		}
		
		
		/**
		 * Next page..
		 */
		$('#application-form a.next').click(function(e) {
			form_validation(e);
		});
		
		
		/**
		 * Form submit..
		 */
		$('#application-form form').submit(function(e) {
			form_validation(e);
			// If it's not the final page..
			if($.pageVars.page_num != $.pageVars.total_pages) {
				$('#application-form a.next').click();
				e.preventDefault();
			}
		});
		
	})();
	
	
	
});
