// JavaScript Document

$(document).ready(function() { 
		
		var topNumberOfSlides = $('#slider ul.slides li').length;
		var topSlidewidth = 946;
		var topViewPort = 946;
		var topSliderWidth = topNumberOfSlides*topSlidewidth;
		var topLessThanViewPort = topSliderWidth-topViewPort;
		
		$('#slider ul.slides').css('width',topSliderWidth);
		
		var topFlag;
				
		//Navigation
		var topNumberOfSlidesNav = $('#slider .controls ul li').length;
		var topSlideNavDotWidth = $('#slider .controls ul li').outerWidth();
		var topSliderControlsWidth = topNumberOfSlidesNav*topSlideNavDotWidth;
		$('#slider .controls').css('width',topSliderControlsWidth);
		
		setInterval(function() { moveTopSlider(topSlidewidth, topLessThanViewPort) } ,10000);
		
		$('#slider .controls ul li a').click(function(){
		  
		  	if (!$('#slider ul.slides').is(':animated')) {
				
				$('#slider .controls ul li').removeClass('active')
				$(this).parent().addClass('active')
			
				var thisDotId = $(this).attr('id');
				thisDotId = thisDotId.replace('nav-', "");
				thisDotPos = thisDotId-1;
					
					$('#slider ul.slides').animate({
						'marginLeft': 	thisDotPos*(topSlidewidth*-1)							  
					},1000);
			}
				
		});
		
	
		
		
});


function moveTopSlider(topSlidewidth, topLessThanViewPort){
			
			var topLeftVal = $('#slider ul.slides').css('margin-left');
				topLeftVal = parseInt(topLeftVal);
				topLeftVal = (topLeftVal*-1); 
				
				if (topLeftVal==0){
					topFlag = 1;
				}
				
				if (topLeftVal<topLessThanViewPort && topFlag==1){
					$('#slider .controls ul li.active').removeClass('active').next().addClass('active');
					
					if (!$('#slider ul.slides').is(':animated')) {
					
						$('#slider ul.slides').animate({
								'marginLeft': '-='+topSlidewidth							  
						},1000)
					}
				}else if (topLeftVal>=0){
		
					$('#slider .controls ul li.active').removeClass('active').prev().addClass('active');
					if (!$('#slider ul.slides').is(':animated')) {
						$('#slider ul.slides').animate({
								'marginLeft': '+='+topSlidewidth									  
						},1000)
						topFlag = 0;
					}
				}
			
			
}
