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

$( function() {
	
	var contrastFlag = '';
	if ( $( 'body' ).hasClass( 'high-contrast' ) ) {
		contrastFlag = '-bw';
	}
	
	/********************************************************************
	* TIMER CLASS -- START
	********************************************************************/
	function Timer( callback, milliseconds, context )
	{
		this.milliseconds = milliseconds;
		this.callback = callback;
		this.context = context;
		if ( !this.context ) this.context = this;
		this.handle = null;
	}
	 
	Timer.prototype.start = function()
	{
		var timer = this;
		var context = this.context;
		var timeUp = function()
		{
			timer.callback( context );
		}

		this.handle = setInterval( timeUp, this.milliseconds );
	}
	 
	Timer.prototype.stop = function()
	{
		clearInterval( this.handle );
	}
	
	/********************************************************************
	* TIMER CLASS -- END
	********************************************************************/
	
	/********************************************************************
	* FEATURED SLIDER CLASS -- START
	********************************************************************/
	
	// Constructor
	function FeaturedSlider( target )
	{
		this.cur               = 1;
		this.next              = 2;
		this.prev              = 0;
		this.fadeDuration      = 1400;
		this.timeBetweenSlides = 5000; 
		this.items             = target.find( 'li' );
		
		// Initialize rotation timer
		this.timer = new Timer( this.showNext, this.timeBetweenSlides, this );

		// Controller images
		this.controllerImgOn  = '/images/homepage/slide-btn-on' + contrastFlag + '.png';
		this.controllerImgOff = '/images/homepage/slide-btn-off.png';
		
		// Create and configure slideshow controller
		var instance = this;
		var navLi = '<li><a href="1"><img src="' + this.controllerImgOn + '" alt="Slide 1 [button]"></a></li>';
		for ( var i = 2; i <= this.items.length; i++ ) {
			navLi += '<li><a href="' + i + '"><img src="' + this.controllerImgOff + '" alt="Slide ' + i + ' [button]"></a></li>';
		}
		target.parent().append( '<div><ul>' + navLi + '</ul></div>' );
		
		this.controls = target.parent().find( 'div ul li' );
		 
		this.controls.find( 'a' ).click( function( $e ) 
		{
			$e.preventDefault();
			
			// Stop auto-rotation
			instance.timer = null;
			
			var urlParts = $( this ).attr( 'href' ).split( '/' );	// Need this due to IE7 bug
			instance.showItem( parseInt( urlParts[ urlParts.length - 1 ] ) );
		} );
		
		// Stop rotation on mouseover, resume on mouseout
		target.mouseover( function() { instance.pause(); } );
		target.mouseout( function() { instance.rotate(); } );
		target.parent().find( 'div' ).mouseover( function() { instance.pause(); } );
		target.parent().find( 'div' ).mouseout( function() { instance.rotate(); } );
		
		// Auto-start slideshow
		this.rotate();
	}
	
	FeaturedSlider.prototype.showNext = function( instance )
	{
		var nextItem = ( instance.cur == instance.items.length ) ? 1 : instance.cur + 1;
		instance.showItem( nextItem );
	}
	
	FeaturedSlider.prototype.rotate = function( )
	{
		if ( this.timer ) { this.timer.start(); }
	}
	
	FeaturedSlider.prototype.pause = function( )
	{
		if ( this.timer ) { this.timer.stop(); }
	}
	
	FeaturedSlider.prototype.showItem = function( itemNum )
	{

		var instance = this;
		
		// TODO: Stop rotation if active
		
		if ( this.cur != itemNum ) {
			this.next = itemNum;
			this.prev = this.cur;
			this.items.eq( this.cur - 1 ).stop( true, true ).fadeOut( instance.fadeDuration );
			this.items.eq( this.next - 1 ).stop( true, true ).fadeIn( 
				instance.fadeDuration,
				function()
				{
				}
			);
			instance.controls.find( 'img' ).attr( 'src', instance.controllerImgOff );
			instance.controls.eq( instance.next - 1 ).find( 'img' ).attr( 'src', instance.controllerImgOn );
			
			this.cur = this.next;
		}
	}
	
	/********************************************************************
	* FEATURED SLIDER CLASS -- END
	********************************************************************/
	
	// Activate featured content slider
	var featuredObj = new FeaturedSlider( $( '#featured-content > ol' ) );
	
	// Activate news ticker
	var curNewsListPos = 0;
	var newsSlideWidth = 810;
	
	var newsTicker = $( '#news-ticker' );
	var newsList = newsTicker.find( '#news-list li' );
	
	// Add enclosing div to news list
	newsList.parent().wrap( '<div id="news-wrap" />' );
	
	// Set news list container width
	newsListWidth = newsList.length * newsSlideWidth;
	newsList.parent().css( 'width', newsListWidth + 'px' );
	
	// Add controls
	var newsControls = '<li><a href="#" title="Play"><img src="/images/homepage/news-play' + contrastFlag + '.png" alt="Play Control [icon]"></a></li>' + 
	                   '<li><a href="#" title="Pause"><img src="/images/homepage/news-pause' + contrastFlag + '.png" alt="Pause [icon]"></a></li>' + 
					   '<li><a href="#" title="Previous"><img src="/images/homepage/news-prev' + contrastFlag + '.png" alt="Previous [icon]"></a></li>' +
					   '<li><a href="#" title="Next"><img src="/images/homepage/news-next' + contrastFlag + '.png" alt="Next [icon]"></a></li>';
	
	newsTicker.append( '<div id="news-nav"><ul>' + newsControls + '</ul></div>');
	
	// Hide 'play' control
	newsTicker.find( '#news-nav ul li' ).eq( 0 ).addClass( 'screenreader' );
	newsTicker.find( '#news-nav ul li' ).eq( 1 ).addClass( 'screenreader' );	// temporarily disable rotation
	
	// De-activate 'previous' control
	newsTicker.find( '#news-nav ul li' ).eq( 2 ).addClass( 'inactive' );
	
	newsTicker.find( '#news-nav ul li a[title="Previous"], #news-nav ul li a[title="Next"]' ).click( function( $e ) {
		
		$e.preventDefault();
		
		if ( !$( this ).parent().hasClass( 'inactive' ) ) {
		
			curNewsListPos = $( this ).attr( 'title' ) == 'Next' ? curNewsListPos + 1 : curNewsListPos - 1;
			
			newsList.parent().animate( { 
				'left' : newsSlideWidth * ( -curNewsListPos )
			} );
		}
		
		// De-activate/re-activate controls at end points
		if ( curNewsListPos == 0 ) {		// beginning
		
			newsTicker.find( '#news-nav ul li a[title="Previous"]' ).parent().addClass( 'inactive' );
			newsTicker.find( '#news-nav ul li a[title="Next"]' ).parent().removeClass( 'inactive' );
			
		} else if ( curNewsListPos == newsList.length - 1 ) {	// end
		
			newsTicker.find( '#news-nav ul li a[title="Next"]' ).parent().addClass( 'inactive' );
			newsTicker.find( '#news-nav ul li a[title="Previous"]' ).parent().removeClass( 'inactive' );
			
		} else {
			newsTicker.find( '#news-nav ul li a[title="Next"]' ).parent().removeClass( 'inactive' );
			newsTicker.find( '#news-nav ul li a[title="Previous"]' ).parent().removeClass( 'inactive' );
		}
		
	} );
	
	// Animate division select menu
	var divisionLinks = $( '#divisions li a');
	var divisionPs = divisionLinks.find( 'p' );

	divisionPs.css( 'visibility', 'visible' ).css( 'display', 'none' );
	
	var slideDown = function()
	{
		$( this ).find( 'p' ).stop( true, true ).slideDown( 'fast', 'easeOutBounce' );
	};
	
	var slideUp = function()
	{
		$( this ).find( 'p' ).stop( true, true ).slideUp( 'fast' );
	};
	
	divisionLinks.hover( slideDown, slideUp );
	divisionLinks.focus( slideDown );
	divisionLinks.blur( slideUp );
	
	// Enable Flow Player for videos
	var lightboxVideos = $( '#content ul.extra li a[href$=".mp4"]' );
	
	lightboxVideos.each( function( index ) {
		
		var playerNum = index + 1;
		$( this ).attr( 'href', 'pearson-video-' + playerNum + '.html' );
		
	} );
	
	lightboxVideos.fancybox( {
			'width'              : 496, 
			'height'             : 270,
			'hideOnContentClick' : true,
			'overlayOpacity'     : 0.5,
			'overlayColor'       : '#000',
			'type'               : 'iframe',
			'scrolling'          : 'no',
			'onStart'            : function() { featuredObj.pause(); }
		});
} );


// Activate news slider
