// JavaScript Document
var $indianPromoPanel = jQuery.noConflict();

//Location of elements
var $indianPromoPanelId = '#indian-promo-panel'
var $indianPromoPanelSlidesId = '#indian-promo-panel-slides'
var $indianPromoPanelSlidesChildren = '#indian-promo-panel-slides > *'
var $indianPromoPanelRightClickId = '#indian-promo-panel-slides-right-click'
var $indianPromoPanelLeftClickId = '#indian-promo-panel-slides-left-click'
var $indianPromoPanelControlsId = '#indian-promo-panel-controls'

//Modifiable variables
var $indianPromoPanelAnimationSpeed = 1000;
var $indianPromoPanelFadeAnimationSpeed = 200;
var $indianPromoPanelInitialFadeAnimationSpeed = 1000;
var $indianPromoPanelStartingSlide = 0;
var $indianPromoPanelSlidingSpeedInSeconds = 10;

//Do Not Touch variables
var $indianPromoPanelDefaultLeft;
var $indianPromoPanelAnimating = false;
var $indianPromoPanelWidth;
var $indianPromoPanelSize;
var $indianPromoPanelSlidingTimer;

$indianPromoPanel(document).ready(indianPromoPanel_READY);
$indianPromoPanel(window).load(indianPromoPanel_ONLOAD);

//Main
function indianPromoPanel_READY(){
	indianPromoPanel_SET_GLOBALS();
	indianPromoPanel_MANIP_DOM();
	indianPromoPanel_BIND_EVENTS();
}

//Onload
function indianPromoPanel_ONLOAD(){

	if( indianPromoPanel_detect_ie_8() )
		$indianPromoPanel( $indianPromoPanelId ).css({'visibility':'visible'});
	else
		$indianPromoPanel( $indianPromoPanelId ).css({'visibility':'visible'}).hide().fadeIn( $indianPromoPanelInitialFadeAnimationSpeed );
		
	$indianPromoPanelSlidingTimer = setInterval( indianPromoPanel_Right_Click, $indianPromoPanelSlidingSpeedInSeconds * 1000 ); 	
}

/* Main Functions */
function indianPromoPanel_MANIP_DOM(){
	$indianPromoPanel( $indianPromoPanelId ).css( 'visibility', 'hidden' );
	$indianPromoPanel( $indianPromoPanelSlidesChildren ).css( 'left', $indianPromoPanelDefaultLeft ); 
	$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $indianPromoPanelStartingSlide ).css('left', 0 ).addClass( 'current' );
	
	//Do not show controls if only one slide
	if( $indianPromoPanelSize > 1 ){
		indianPromoPanel_Create_Control_Panel();
		$indianPromoPanel( $indianPromoPanelControlsId + ' > ul > li:first-child' ).eq( $indianPromoPanelStartingSlide ).addClass('current');
	}
}

function indianPromoPanel_SET_GLOBALS(){
	$indianPromoPanelSize = $indianPromoPanel( $indianPromoPanelSlidesChildren ).size();
	$indianPromoPanelWidth = $indianPromoPanel( $indianPromoPanelSlidesId ).width();
	$indianPromoPanelDefaultLeft = $indianPromoPanelWidth * 2;
}

function indianPromoPanel_BIND_EVENTS(){
	//$indianPromoPanel( $indianPromoPanelId  ).hover( indianPromoPanel_Show_Arrows, indianPromoPanel_Hide_Arrows );		
	$indianPromoPanel( $indianPromoPanelLeftClickId ).click( indianPromoPanel_Left_Click );		
	$indianPromoPanel( $indianPromoPanelRightClickId ).click( indianPromoPanel_Right_Click_Stage );		
	$indianPromoPanel( $indianPromoPanelControlsId + ' > ul > li.dot' ).click( indianPromoPanel_Control_Dot_Click );		
}

/* Supporting Functions */

function indianPromoPanel_changeTimer(){
	if(	$indianPromoPanel( this ).hasClass( 'play' ) )
		indianPromoPanel_startTimer();
	else
		indianPromoPanel_pauseTimer();	
}

//First, Slide the panel
//Second, set the interval
//Third, change the button
function indianPromoPanel_startTimer(){
	indianPromoPanel_Right_Click();
	$indianPromoPanelSlidingTimer = setInterval( indianPromoPanel_Right_Click, $indianPromoPanelSlidingSpeedInSeconds * 1000 ); 	
}

//First, clear the interval
//Second, change the button
function indianPromoPanel_pauseTimer(){
	clearInterval( $indianPromoPanelSlidingTimer ); 	
}

//Create Controls
//First, create the left and right arrows outside of the promo content
//Second, create the controls below the promo content
function indianPromoPanel_Create_Control_Panel(){
	
	var $indianLeftRightControls = '<div id="' + $indianPromoPanelLeftClickId.substring(1) + '"></div>';
	$indianLeftRightControls = $indianLeftRightControls + '<div id="' + $indianPromoPanelRightClickId.substring(1) + '"></div>';
	
	$indianPromoPanel( $indianPromoPanelId ).append( $indianLeftRightControls );
	
	$controlsHTML = '<div id="indian-promo-panel-controls-container-parent"><div id="indian-promo-panel-controls-container"><div id="indian-promo-panel-controls-container-left"></div><div id="indian-promo-panel-controls-container-right"></div><div id="' + $indianPromoPanelControlsId.substring(1) + '"><ul>';
	
	for( $i=0; $i < $indianPromoPanelSize; $i++ )
		$controlsHTML = $controlsHTML + '<li class="dot">' + $i + '</li>';

	$controlsHTML = $controlsHTML + '</ul></div></div></div>';
	$indianPromoPanel( $indianPromoPanelId ).append( $controlsHTML );	
	
	
	$indianPromoPanel( '#indian-promo-panel-controls-container' ).width( $indianPromoPanel( $indianPromoPanelControlsId ).width() + $indianPromoPanel( '#indian-promo-panel-controls-container-left' ).width() + $indianPromoPanel( '#indian-promo-panel-controls-container-right' ).width() - 4 );
}

//Show Arrows on MouseEnter
function indianPromoPanel_Show_Arrows(){
	$indianPromoPanel($indianPromoPanelRightClickId + ',' + $indianPromoPanelLeftClickId ).stop(true, true).fadeIn( $indianPromoPanelFadeAnimationSpeed);
}

//Hide Arrows on MouseLeave
function indianPromoPanel_Hide_Arrows(){
	$indianPromoPanel($indianPromoPanelRightClickId + ',' + $indianPromoPanelLeftClickId ).fadeOut( $indianPromoPanelFadeAnimationSpeed );
}

//First, check if there's only 2 panels.  If so, move the non focused one to the left.
//Second, slide left to right.
function indianPromoPanel_Left_Click(){
	indianPromoPanel_pauseTimer();
	$current_index = indianPromoPanel_get_current();
	$previousSlideIndex = indianPromoPanel_get_previous($current_index);	
	
	$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $previousSlideIndex ).css( 'left', ( $indianPromoPanelWidth * -1 ) );
		
	indianPromoPanel_Slide_Left_to_Right();
}

function indianPromoPanel_Right_Click_Stage(){
	indianPromoPanel_pauseTimer();
	indianPromoPanel_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 indianPromoPanel_Right_Click(){		
	$current_index = indianPromoPanel_get_current();
	$nextSlideIndex = indianPromoPanel_get_next($current_index);	
	
	$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $nextSlideIndex ).css( 'left', ( $indianPromoPanelWidth ) );
		
	indianPromoPanel_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 indianPromoPanel_Control_Dot_Click(){
	indianPromoPanel_pauseTimer();
	$currentSlide = indianPromoPanel_get_current();
	$selectedSlide = $indianPromoPanel( this ).index();
	
	if( !$indianPromoPanelAnimating && $selectedSlide != $currentSlide ){
		if( $selectedSlide < $currentSlide ){			
			$previousSlideIndex = indianPromoPanel_get_previous( $currentSlide );
			$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $previousSlideIndex ).css( 'left', $indianPromoPanelDefaultLeft );
			$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $selectedSlide ).css( 'left', ( $indianPromoPanelWidth * -1 ) );
			
			indianPromoPanel_Slide_Left_to_Right();
		}
		else{
			$nextSlideIndex = indianPromoPanel_get_next( $currentSlide );
			$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $nextSlideIndex ).css( 'left', $indianPromoPanelDefaultLeft );
			$indianPromoPanel( $indianPromoPanelSlidesChildren ).eq( $selectedSlide ).css( 'left', $indianPromoPanelWidth );
			
			indianPromoPanel_Slide_Right_to_Left();
		}
	}
}

//Right Click
function indianPromoPanel_Slide_Right_to_Left(){
	
	if( !$indianPromoPanelAnimating ){
		
		$indianPromoPanelAnimating = true;
		var $slide_count_left = 0;
		
		$indianPromoPanel( $indianPromoPanelSlidesChildren ).removeClass( 'current' );
		$indianPromoPanel( $indianPromoPanelSlidesChildren ).each( function(){
			
			if( parseInt( $indianPromoPanel(this).css('left') ) != $indianPromoPanelDefaultLeft ){
				//$indianPromoPanel( '#indian-promo-panel-slides > * > div > *' ).css( 'z-index', '1' );
				$indianPromoPanel( this ).show().animate( {'left': ( parseInt( $indianPromoPanel(this).css('left') ) -  $indianPromoPanelWidth ) }, $indianPromoPanelAnimationSpeed, function(){
					
					//Set the current position
					if( parseInt( $indianPromoPanel( this ).css( 'left' ) ) == 0 ){
						$indianPromoPanel( this ).addClass( 'current' );
						$indianPromoPanel( $indianPromoPanelControlsId + ' > ul > li' ).removeClass('current');	
						$indianPromoPanel( $indianPromoPanelControlsId + " > ul > li" ).eq( $indianPromoPanel( this ).index() ).addClass( 'current' );	
						$current_index = $indianPromoPanel( this ).index();
					}
					
					$slide_count_left++;
					
					//Reorder the panel according to the current panel, then release the animation
					if( $slide_count_left == 2 ){
						indianPromoPanel_Reorder_Slides( $current_index );
						$indianPromoPanel( '#indian-promo-panel-slides > * > div > *' ).css( 'z-index', '2' );
						$indianPromoPanelAnimating = false;
					}
				});
			}
		});
	}
}

//Left Click
function indianPromoPanel_Slide_Left_to_Right(){	

	if( !$indianPromoPanelAnimating ){
		
		$indianPromoPanelAnimating = true;
		var $slide_count_right = 0;
		
		$indianPromoPanel( $indianPromoPanelSlidesChildren ).removeClass( 'current' );
		
		$indianPromoPanel( $indianPromoPanelSlidesId ).children().each( function(){
			if( parseInt( $indianPromoPanel(this).css('left') ) != $indianPromoPanelDefaultLeft ){
				//$indianPromoPanel( '#indian-promo-panel-slides > * > div > *' ).css( 'z-index', '1' );
				$indianPromoPanel( this ).show().animate( {'left': ( parseInt( $indianPromoPanel(this).css('left') ) + $indianPromoPanelWidth ) + 'px'}, $indianPromoPanelAnimationSpeed, function(){
	
					//Set the current position
					if( parseInt( $indianPromoPanel( this ).css( 'left' ) ) == 0 ){
						$indianPromoPanel( this ).addClass( 'current' );
						$indianPromoPanel( $indianPromoPanelControlsId + ' > ul > li' ).removeClass('current');	
						$indianPromoPanel( $indianPromoPanelControlsId + " > ul > li" ).eq( $indianPromoPanel( this ).index() ).addClass( 'current' );	
						$current_index = $indianPromoPanel( this ).index();
					}			
						
					$slide_count_right++;
					
					//Reorder the panel according to the current panel, then release the animation
					if( $slide_count_right == 2 ){
						indianPromoPanel_Reorder_Slides( $current_index );
						$indianPromoPanel( '#indian-promo-panel-slides > * > div > *' ).css( 'z-index', '2' );
						$indianPromoPanelAnimating = 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 indianPromoPanel_Reorder_Slides( $current_index ){

	$previous = indianPromoPanel_get_previous( $current_index );
	$next = indianPromoPanel_get_next( $current_index );
	
	$indianPromoPanel( $indianPromoPanelSlidesChildren ).each( function(){
		
		if( $indianPromoPanel( this ).index() == $current_index )
			$leftPosition = 0;
		else
			$leftPosition = $indianPromoPanelDefaultLeft;
			
		$indianPromoPanel( this ).css( 'left', $leftPosition );
	});
}

function indianPromoPanel_get_current(){
	return $indianPromoPanel( $indianPromoPanelSlidesChildren + '.current' ).index();
}

function indianPromoPanel_get_previous( $current_index ){
	return ( $current_index - 1 + $indianPromoPanelSize ) % $indianPromoPanelSize;
}

function indianPromoPanel_get_next( $current_index ){
	return ( $current_index + 1 + $indianPromoPanelSize ) % $indianPromoPanelSize;
}

function indianPromoPanel_detect_ie_8(){
	
	$result = false;
	if($indianPromoPanel.browser.msie){
  		ieVersion = $indianPromoPanel.browser.version;
		if(ieVersion == 8)
			$result = true;
	}
	return $result;
}
