var currPanel = 0;
var stopAnimation = false;
var leftIntervalId = 0;
var rightIntervalId = 0;

function slidePanel( direction ) {
	
	if (stopAnimation) return false;

	var $slider = $('#full-slider');
	var $sliderPanels = $slider.children('.slide-panel');

	if (direction == 'right'){	
		currPanel--;
	
		// check if the new panel value is too small
		if ( currPanel < 0 ) currPanel = $sliderPanels.length - 1;
	}else{
		currPanel++;

		// check if the new panel value is too big
		if ( currPanel >= $sliderPanels.length ) currPanel = 0;

	}


	// define the offset of the slider obj, vis a vis the document
	var offsetLeft = $slider.offset().left;

	// offset required to hide the content off to the left / right
	var hideLeft = -1 * ( offsetLeft + $slider.width() );
	var hideRight = $(window).width() - offsetLeft;

	// change the current / next positions based on the direction of the animation
	if ( direction == 'left' ) {
		currPos = hideLeft;
		nextPos = hideRight;
	}else{
		currPos = hideRight;
		nextPos = hideLeft;
	}

	// slide out the current panel, then remove the active class
	$slider.children('.slide-panel.active').addClass('opacity');
	$slider.children('.slide-panel.active').animate({
		left: currPos
	}, 500, function() {
		$(this).removeClass('active');
		$slider.children('.slide-panel.active').removeClass('opacity');
	});

	// slide in the next panel after adding the active class
	$( $sliderPanels[currPanel] ).css('left', nextPos).addClass('active').animate({
		left: 0
	}, 500 );


	$('.jqNavMenu li').removeClass('active');
	$('#jqNavMenu' + currPanel).addClass('active');

}


function animateLeftMovement (){
	slidePanel('right');
}

function animateRightMovement (){
	slidePanel('left');
}


$(document).ready(function() {

	var $navLeft = $('.scrollLeft');
	var $navRight = $('.scrollRight, .next');

	$('#jqNavMenu0').addClass('active');


	$navLeft.click(function() {
		clearInterval(leftIntervalId);
		clearInterval(rightIntervalId);
		stopAnimation = false;
		animateLeftMovement();
		leftIntervalId = setInterval('animateLeftMovement()', 10000);
	});

	$navRight.click(function() {
		clearInterval(leftIntervalId);
		clearInterval(rightIntervalId);
		stopAnimation = false;
		animateRightMovement();
		rightIntervalId = setInterval('animateRightMovement()', 10000);
	});
	
	$('#full-slider').hover(function(){
		stopAnimation = true;
	}, function (){
		stopAnimation = false;
	 });

	rightIntervalId = setInterval('animateRightMovement()', 10000);

	var $slider = $('#full-slider');
	var $sliderPanels = $slider.children('.slide-panel');

	//Show the paging and activate its first link
	var totalSlides = $('#full-slider').children('.slide-panel').length;

	$('.jqNavMenu a').each(function(index, element){
		if (index < totalSlides){
			$(this).show();
			$(this).click(function(){
                console.log(index);
				clearInterval(leftIntervalId);
				clearInterval(rightIntervalId);
				stopAnimation = false;

				if (index > currPanel){
					currPanel = index - 1;
					animateRightMovement();
				}else{
					if (index < currPanel){
						currPanel = index + 1;
						animateLeftMovement();
					}
				}

			});
		}else{
			$(this).hide();
		}
	});

			
});

