var ContentWidth;
var CurrentIndex = 0;
var intval="";
var TotalSlides = 0;
function SetContent(Resize){
	TotalSlides = $("#ContentNav > li").size();
	ContentWidth = $('#ContentContainerWrapper').width();
	$('.ContentBlock').addClass('Block');
	//determine number of content div
	var ContentCount = $('#ContentContainer > div.ContentBlock').length;
	//set elngth of the container
	$('#ContentContainer').css('width',ContentWidth * ContentCount);
	$('.Block').css('width',ContentWidth);
	//keep current slide in place on resize
	PositionContent(CurrentIndex);
	//
	//Add event to navigation
	$('.ContentNavButton').click(function(e){
		e.preventDefault();
		//var ContainerPos = 	$('#ContentContainer').position();		  
		CurrentIndex = $(this).parent().index();
		ChangeContent(CurrentIndex,true);
		
	});
	
	$('.NextContent').click(function(e){
		e.preventDefault();		  
		CurrentIndex = $(this).parent().parent().index() + 1;
		ChangeContent(CurrentIndex,true);
		
	});
	$('.PrevContent').click(function(e){
		e.preventDefault();		  
		CurrentIndex = $(this).parent().parent().index() - 1;
		ChangeContent(CurrentIndex,true);
		
	});
	
	if(Resize != true){
	$('#ContentNav li:eq(' + 0 +')').addClass('active');
	} else{
		if(intval!=""){
        window.clearInterval(intval);
        intval="";
    }
	}
	intval = window.setTimeout( "RotateContent()", 5000 );

	
	
}

function RotateContent(){
	CurrentIndex+=1;
	if (CurrentIndex >= TotalSlides)
	{
		CurrentIndex=0;
	}
	ChangeContent(CurrentIndex,false);
}



function ChangeContent(index,clicked){
	if(intval!=""){
        window.clearInterval(intval);
        intval="";
    }
	var timer = 6500;
	$('#ContentNav').children().removeClass('active');
	$('#ContentNav li:eq(' + index +')').addClass('active');
	var newPos = (ContentWidth * (-1)) * index;
	//$('#ContentContainer').css('left',newPos);
	$('#ContentContainer').stop().animate({
    left: newPos
  }, 1000, function() {
    // Animation complete.
  })
	if(clicked == true){
		timer = 10000;
	}
	intval = window.setTimeout( "RotateContent()", timer );
}

function PositionContent(index){
	var newPos = (ContentWidth * (-1)) * index;
	$('#ContentContainer').css('left',newPos);
}

function InitContentBlock(){
	$('#ContentNav').css('display','block');
	$('.ContentBlock').css('display','block');
}

$(document).ready(function(){
	InitContentBlock();SetContent();
	$(window).resize(function(){
		SetContent(true);							   
	});	
	
});
