/*
	Pearson Canada Corporate Site JS Enhancements
	Author: Alexander Drobyshev @ Pearson Canada
	Date: July 5, 2011
*/

// FOUC fix
$( 'html' ).hide();

var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

$( function() {
	
	// FOUC fix
	$( 'html' ).show();
	
	// Remove external link icon from linked images
	$( '#content > section a[href^="http://"]:has(img), #content > section a[href^="https://"]:has(img)' )
		.css( 'background-image', 'none' )
		.css( 'padding-right', 'inherit' );
	
	// Primary nav enhancements
	if ( !$( 'body' ).hasClass( 'high-contrast' ) )
	{
		var mainNav = $( '#main-nav' );
		if ( mainNav.find( 'li' ).hasClass( 'current' ) )
		{
			mainNav.lavaLamp( { speed: 650 } );
		}
		else if ( $( 'html' ).hasClass( 'ie7' ) )
		{
			mainNav.lavaLamp(
				{
					returnHome: true,
					homeLeft: 0,
					homeTop: -100,
					homeWidth: 720,
					homeHeight: 18,
					speed: 650
				}
			);
		}
		else
		{
			mainNav.lavaLamp(
				{
					returnHome: true,
					homeLeft: -100,
					homeTop: -25,
					homeWidth: 720,
					homeHeight: 18,
					speed: 650
				}
			);
		}
		
		var currentColor = mainNav.find( 'li.current a' ).css( 'color' );
		
		// Fix current link color when hovered over other links
		mainNav.find( 'li a' ).hover(
			function()
			{
				if ( !$( this ).parent().hasClass( 'current' ) ) {
					mainNav.find( 'li.current a' ).css( 'color', 'inherit' );
				}
			}, 
			function()
			{
				if ( !$( this ).parent().hasClass( 'current' ) ) {
					mainNav.find( 'li.current a' ).css( 'color', currentColor );
				}
			}
		);
	}
	
	// Smooth scroll for anchor links
	var anchorLinks = $( '.anchor-link' );
	anchorLinks.click( function( $e )
	{
		$e.preventDefault();
		$.scrollTo( $( this ).attr( 'href' ), 700 );
	} );
	
	// Footer comment link
	var commentForm = $( '#footer-comment-form' );
	var commentFormHtml = commentForm.html();
	var commentSent = false;
	var commentFormWidth = commentForm.width();
	var commentFormHeight = commentForm.height();
	
	// Slide form in/out
	$( '#footer-comment-link' ).click( function( $e ) {
		
		$e.preventDefault();
	
		commentForm.slideToggle( 'slow', function() {
			
			if ( commentSent ) {
				commentForm.html( commentFormHtml );
				commentForm.css( 'height', commentFormHeight + 'px' );
				commentSent = false;
			}
			
			// Scroll to form and focus on first input field
			if ( commentForm.is( ':visible' ) ) {
				$.scrollTo( commentForm, 1000 );
				commentForm.find( 'input:visible:first' ).focus();
				commentForm.attr( 'aria-hidden', true );
			} else {
				commentForm.attr( 'aria-hidden', false );
			}
			
		} );
		
	} );
	
	// Process form
	commentForm.submit( function( $e ) {

		// Error checking
		var emailField = commentForm.find( '#footer-comment-email' );
		var commentField = commentForm.find( '#footer-comment' );
		
		if ( !( emailFilter.test( emailField.val() ) ) ) {
			alert( 'Please enter your email address' );
			emailField.focus();
			return false;
		}
		
		if ( !$.trim( commentField.val() ).length ) {
			alert( 'Please enter your comments');
			commentField.focus();
			return false;
		}
		
		// Send form
		var formVals = {
			'ajax'          : '1',
			'comment-email' : emailField.val(),
			'comment' : commentField.val()
		};
		
		var success = function( data )
		{
			// Set form dimensions to prevent animation artifacts
			commentForm.css( 'height', commentFormHeight + 'px' );
			commentForm.css( 'width',  commentFormWidth + 'px' );
			
			commentForm.find( 'div' ).fadeOut( 'slow', function() {
				commentForm.find( 'div' ).html( '<h4>Thank You!</h4><p>We received your submission.</p>' );
				commentForm.find( 'div' ).fadeIn( 'slow', function() {
					
					commentForm.animate( { height: commentForm.find( 'div' ).height() } );
					
					} );
				commentSent = true;
			} );
		}
		
		$.post( commentForm.attr( 'action' ), formVals, success );
		
		return false;
	} );
	
} );
