﻿// JavaScript Document
var $corpPromoPanel = jQuery.noConflict();

//Location of elements
var $corpPromoPanelId = '#polaris-promo-panel'
var $corpPromoPanelSlidesId = '#polaris-promo-panel-slides'
var $corpPromoPanelSlidesChildren = '#polaris-promo-panel-slides > *'
var $corpPromoPanelRightClickId = '#polaris-promo-panel-slides-right-click'
var $corpPromoPanelLeftClickId = '#polaris-promo-panel-slides-left-click'
var $corpPromoPanelControlsId = '#polaris-promo-panel-controls'

//Modifiable variables
var $corpPromoPanelAnimationSpeed = 1000;
var $corpPromoPanelFadeAnimationSpeed = 200;
var $corpPromoPanelInitialFadeAnimationSpeed = 1000;
var $corpPromoPanelStartingSlide = 0;
var $corpPromoPanelSlidingSpeedInSeconds = 7;

//Do Not Touch variables
var $corpPromoPanelDefaultLeft;
var $corpPromoPanelAnimating = false;
var $corpPromoPanelWidth;
var $corpPromoPanelSize;
var $corpPromoPanelSlidingTimer;

$corpPromoPanel(document).ready(corpPromoPanel_READY);
$corpPromoPanel(window).load(corpPromoPanel_ONLOAD);

//Main
function corpPromoPanel_READY(){
	corpPromoPanel_SET_GLOBALS();
	corpPromoPanel_MANIP_DOM();
	corpPromoPanel_BIND_EVENTS();
}

//Onload
function corpPromoPanel_ONLOAD(){
	$corpPromoPanel( $corpPromoPanelSlidesChildren + " > img" ).each( function(){
		$corpPromoPanel( this ).attr( 'src', $corpPromoPanel( this ).attr( 'title' ) );
	});
	
	if( corpPromoPanel_detect_ie_8() )
		$corpPromoPanel( $corpPromoPanelId ).css({'visibility':'visible'});
	else
		$corpPromoPanel( $corpPromoPanelId ).css({'visibility':'visible'}).hide().fadeIn( $corpPromoPanelInitialFadeAnimationSpeed );
		
	$corpPromoPanelSlidingTimer = setInterval( corpPromoPanel_Right_Click, $corpPromoPanelSlidingSpeedInSeconds * 1000 ); 	
}

/* Main Functions */
function corpPromoPanel_MANIP_DOM(){
	$corpPromoPanel( $corpPromoPanelId ).css( 'visibility', 'hidden' );
	$corpPromoPanel( $corpPromoPanelSlidesChildren ).css( 'left', $corpPromoPanelDefaultLeft ); 
	$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $corpPromoPanelStartingSlide ).css('left', 0 ).addClass( 'current' );
	
	//Do not show controls if only one slide
	if( $corpPromoPanelSize > 1 ){
		corpPromoPanel_Create_Control_Panel();
		$corpPromoPanel( $corpPromoPanelControlsId + ' > ul > li:first-child' ).eq( $corpPromoPanelStartingSlide ).addClass('current');
	}
	
	var $slides = $corpPromoPanel('#polaris-promo-panel-slides > .panel > a');
	$corpPromoPanel($slides).each(function() {
		if ($corpPromoPanel(this).attr('eventTag') != null && $corpPromoPanel(this).attr('eventTag').length > 0) {
			var myLink = $corpPromoPanel(this).attr('href');
			$corpPromoPanel(this).attr('href', '#');
			$corpPromoPanel(this).attr('clickAction', myLink);
			
			if ($corpPromoPanel(this).attr('target').length > 0) {
				$target = $corpPromoPanel(this).attr('target');
				$corpPromoPanel(this).removeAttr('target');
				$corpPromoPanel(this).attr('pageTarget', $target);
			}
			//var myTag = "WTExternalBridge('dcsMultiTrack', " + $corpPromoPanel(this).attr('eventTag') + ")";
			//$corpPromoPanel(this).attr('onClick', myTag);
			$corpPromoPanel(this).bind('click', fireEventLevelTagAndForwardToDestination);
		}
	});
}

function corpPromoPanel_SET_GLOBALS(){
	$corpPromoPanelSize = $corpPromoPanel( $corpPromoPanelSlidesChildren ).size();
	$corpPromoPanelWidth = $corpPromoPanel( $corpPromoPanelId ).width();
	$corpPromoPanelDefaultLeft = $corpPromoPanelWidth * 2;
}

function corpPromoPanel_BIND_EVENTS(){
	$corpPromoPanel( $corpPromoPanelId  ).hover( corpPromoPanel_Show_Arrows, corpPromoPanel_Hide_Arrows );		
	$corpPromoPanel( $corpPromoPanelLeftClickId ).click( corpPromoPanel_Left_Click );		
	$corpPromoPanel( $corpPromoPanelRightClickId ).click( corpPromoPanel_Right_Click_Stage );		
	$corpPromoPanel( $corpPromoPanelControlsId + ' > ul > li.dot' ).click( corpPromoPanel_Control_Dot_Click );		
	$corpPromoPanel( '#polaris-promo-panel-controls-play-pause' ).click( corpPromoPanel_changeTimer );		
}

/* Supporting Functions */

function corpPromoPanel_changeTimer(){
	if(	$corpPromoPanel( this ).hasClass( 'play' ) )
		corpPromoPanel_startTimer();
	else
		corpPromoPanel_pauseTimer();	
}

//First, Slide the panel
//Second, set the interval
//Third, change the button
function corpPromoPanel_startTimer(){
	corpPromoPanel_Right_Click();
	$corpPromoPanelSlidingTimer = setInterval( corpPromoPanel_Right_Click, $corpPromoPanelSlidingSpeedInSeconds * 1000 ); 	
	$corpPromoPanel( '#polaris-promo-panel-controls-play-pause'  ).removeClass( 'play' ).addClass( 'pause' );
}

//First, clear the interval
//Second, change the button
function corpPromoPanel_pauseTimer(){
	clearInterval( $corpPromoPanelSlidingTimer ); 	
	$corpPromoPanel( '#polaris-promo-panel-controls-play-pause'  ).removeClass( 'pause' ).addClass( 'play' );
}

//Create Controls
//First, create the left and right arrows outside of the promo content
//Second, create the controls below the promo content
function corpPromoPanel_Create_Control_Panel(){
	$corpPromoPanel( $corpPromoPanelId ).append( '<div id="' + $corpPromoPanelLeftClickId.substring(1) + '"></div>' );
	$corpPromoPanel( $corpPromoPanelId ).append( '<div id="' + $corpPromoPanelRightClickId.substring(1) + '"></div>' );
	
	$controlsHTML = '<div id="' + $corpPromoPanelControlsId.substring(1) + '"><ul>';
	
	for( $i=0; $i < $corpPromoPanelSize; $i++ )
		$controlsHTML = $controlsHTML + '<li class="dot">' + $i + '</li>';

	$controlsHTML = $controlsHTML + '<li id="polaris-promo-panel-controls-play-pause" class="pause">Play Pause Controls</li>';
	$controlsHTML = $controlsHTML + '</ul></div>';
	$corpPromoPanel( $corpPromoPanelId ).append( $controlsHTML );	
}

//Show Arrows on MouseEnter
function corpPromoPanel_Show_Arrows(){
	$corpPromoPanel($corpPromoPanelRightClickId + ',' + $corpPromoPanelLeftClickId ).stop(true, true).fadeIn( $corpPromoPanelFadeAnimationSpeed);
}

//Hide Arrows on MouseLeave
function corpPromoPanel_Hide_Arrows(){
	$corpPromoPanel($corpPromoPanelRightClickId + ',' + $corpPromoPanelLeftClickId ).fadeOut( $corpPromoPanelFadeAnimationSpeed );
}

//First, check if there's only 2 panels.  If so, move the non focused one to the left.
//Second, slide left to right.
function corpPromoPanel_Left_Click(){
	corpPromoPanel_pauseTimer();
	$current_index = corpPromoPanel_get_current();
	$previousSlideIndex = corpPromoPanel_get_previous($current_index);	
	
	$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $previousSlideIndex ).css( 'left', ( $corpPromoPanelWidth * -1 ) );
		
	corpPromoPanel_Slide_Left_to_Right();
}

function corpPromoPanel_Right_Click_Stage(){
	corpPromoPanel_pauseTimer();
	corpPromoPanel_Right_Click();
}
//First, check if there's only 2 panels.  If so, move the non focused one to the right. 
//Second, slide right to left.
function corpPromoPanel_Right_Click(){		
	$current_index = corpPromoPanel_get_current();
	$nextSlideIndex = corpPromoPanel_get_next($current_index);	
	
	$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $nextSlideIndex ).css( 'left', ( $corpPromoPanelWidth ) );
		
	corpPromoPanel_Slide_Right_to_Left();
}

//First, check if panel is sliding and if current slide.
//Second, determine slide direction
//Thrid, move selected panel to the appropriate spot.
//Fouth, make the panel slide.
function corpPromoPanel_Control_Dot_Click(){
	corpPromoPanel_pauseTimer();
	$currentSlide = corpPromoPanel_get_current();
	$selectedSlide = $corpPromoPanel( this ).index();
	
	if( !$corpPromoPanelAnimating && $selectedSlide != $currentSlide ){
		if( $selectedSlide < $currentSlide ){			
			$previousSlideIndex = corpPromoPanel_get_previous( $currentSlide );
			$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $previousSlideIndex ).css( 'left', $corpPromoPanelDefaultLeft );
			$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $selectedSlide ).css( 'left', ( $corpPromoPanelWidth * -1 ) );
			
			corpPromoPanel_Slide_Left_to_Right();
		}
		else{
			$nextSlideIndex = corpPromoPanel_get_next( $currentSlide );
			$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $nextSlideIndex ).css( 'left', $corpPromoPanelDefaultLeft );
			$corpPromoPanel( $corpPromoPanelSlidesChildren ).eq( $selectedSlide ).css( 'left', $corpPromoPanelWidth );
			
			corpPromoPanel_Slide_Right_to_Left();
		}
	}
}

//Right Click
function corpPromoPanel_Slide_Right_to_Left(){
	
	if( !$corpPromoPanelAnimating ){
		
		$corpPromoPanelAnimating = true;
		var $slide_count_left = 0;
		
		$corpPromoPanel( $corpPromoPanelSlidesChildren ).removeClass( 'current' );
		$corpPromoPanel( $corpPromoPanelSlidesChildren ).each( function(){
			
			if( parseInt( $corpPromoPanel(this).css('left') ) != $corpPromoPanelDefaultLeft ){
				$corpPromoPanel( this ).animate( {'left': ( parseInt( $corpPromoPanel(this).css('left') ) -  $corpPromoPanelWidth ) }, $corpPromoPanelAnimationSpeed, function(){
					
					//Set the current position
					if( parseInt( $corpPromoPanel( this ).css( 'left' ) ) == 0 ){
						$corpPromoPanel( this ).addClass( 'current' );
						$corpPromoPanel( $corpPromoPanelControlsId + ' > ul > li' ).removeClass('current');	
						$corpPromoPanel( $corpPromoPanelControlsId + " > ul > li" ).eq( $corpPromoPanel( this ).index() ).addClass( 'current' );	
						$current_index = $corpPromoPanel( this ).index();
					}
					
					$slide_count_left++;
					
					//Reorder the panel according to the current panel, then release the animation
					if( $slide_count_left == 2 ){
						corpPromoPanel_Reorder_Slides( $current_index );
						$corpPromoPanelAnimating = false;
					}
				});
			}
		});
	}
}

//Left Click
function corpPromoPanel_Slide_Left_to_Right(){	

	if( !$corpPromoPanelAnimating ){
		
		$corpPromoPanelAnimating = true;
		var $slide_count_right = 0;
		
		$corpPromoPanel( $corpPromoPanelSlidesChildren ).removeClass( 'current' );
		
		$corpPromoPanel( $corpPromoPanelSlidesId ).children().each( function(){
			if( parseInt( $corpPromoPanel(this).css('left') ) != $corpPromoPanelDefaultLeft ){
				$corpPromoPanel( this ).animate( {'left': ( parseInt( $corpPromoPanel(this).css('left') ) + $corpPromoPanelWidth ) + 'px'}, $corpPromoPanelAnimationSpeed, function(){
	
					//Set the current position
					if( parseInt( $corpPromoPanel( this ).css( 'left' ) ) == 0 ){
						$corpPromoPanel( this ).addClass( 'current' );
						$corpPromoPanel( $corpPromoPanelControlsId + ' > ul > li' ).removeClass('current');	
						$corpPromoPanel( $corpPromoPanelControlsId + " > ul > li" ).eq( $corpPromoPanel( this ).index() ).addClass( 'current' );	
						$current_index = $corpPromoPanel( this ).index();
					}			
						
					$slide_count_right++;
					
					//Reorder the panel according to the current panel, then release the animation
					if( $slide_count_right == 2 ){
						corpPromoPanel_Reorder_Slides( $current_index );
						$corpPromoPanelAnimating = false;
					}
				});
			}
		});
	}
}

//First, find the next and previous slides.
//Second, set the left position for the next, previous and current slides.  Set all others to the left position default.
function corpPromoPanel_Reorder_Slides( $current_index ){

	$previous = corpPromoPanel_get_previous( $current_index );
	$next = corpPromoPanel_get_next( $current_index );
	
	$corpPromoPanel( $corpPromoPanelSlidesChildren ).each( function(){
		
		if( $corpPromoPanel( this ).index() == $current_index )
			$leftPosition = 0;
		else
			$leftPosition = $corpPromoPanelDefaultLeft;
			
		$corpPromoPanel( this ).css( 'left', $leftPosition );
	});
}

function corpPromoPanel_get_current(){
	return $corpPromoPanel( $corpPromoPanelSlidesChildren + '.current' ).index();
}

function corpPromoPanel_get_previous( $current_index ){
	return ( $current_index - 1 + $corpPromoPanelSize ) % $corpPromoPanelSize;
}

function corpPromoPanel_get_next( $current_index ){
	return ( $current_index + 1 + $corpPromoPanelSize ) % $corpPromoPanelSize;
}

function corpPromoPanel_detect_ie_8(){
	
	$result = false;
	if($corpPromoPanel.browser.msie){
  		ieVersion = $corpPromoPanel.browser.version;
		if(ieVersion == 8)
			$result = true;
	}
	return $result;
}

function fireEventLevelTagAndForwardToDestination() {
	$tag = $corpPromoPanel(this).attr('eventTag');
	WTExternalBridge('dcsMultiTrack', $tag);
	if ($corpPromoPanel(this).attr('pageTarget') == "_blank") {
		window.open($corpPromoPanel(this).attr('clickAction'), "_blank");
	} else {
		window.location = $corpPromoPanel(this).attr('clickAction');
	}
}



