var slideCurr=[0,0,0,0,0,0];
var slideTotal=[0,0,0,0,0,0];
var trackingControl = 0;
	// 0 is off    1 is on


$(document).ready(function(){

	var trackScroll;
	var btnSide;
	//var curSlide = 0;
	var nextSlide;
	var isAnimating = 0;
	var slideFade;
	var slideTime = 600;
	var focusedContainer;
	
// global used to animate the products container
	var productNum = 0;
	
	
// count containers to see how many slides are in them.
	var containerAmount = $('#mainContainer').children().length;
	
	
	for(var i=0; i<containerAmount; i++){
	
		// counts how many main slides are in each container   steps thru About, Featured, Products (always 1), Advice, Store
		slideTotal[i]= $('#container'+i+' .mainContent').children().length;
		
	}
		
		

// set a attribute for current slide - depricated to slide array
	$("#container0, #container1, #container2, #container3").attr('cs', 0);

	 $("#takeBack").fadeOut();
	 
	 $('#bul00, #bul10, #bul20, #bul30').css("color", "#333333");
	 $('.slide0').css('display','block');

	 $('.scrollHere').click(function() {
	 var elementClicked = $(this).attr("href");
	 
	 if (elementClicked == "#newarrivals") container = "Featured Products";
	 if (elementClicked == "#products") container = "Products";
	 if (elementClicked == "#advice") container = "Advice";
	 if (elementClicked == "#store") container = "Store";
	 
	 if( trackingControl == 1){
	 	_gaq.push(['_trackEvent', 'nav', 'main' , container]);
	   }
	   
	   var destination = $(elementClicked).offset().top;
	   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-35}, 1000, 'easeInOutCubic' );
	  
	   return false;
	});

	$("#takeBack").click(function() {
			
		 if( trackingControl == 1){
			_gaq.push(['_trackEvent', 'nav', 'button' , 'top']);
			}
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 1200, 'easeInOutCubic' );
			//$("#takeBack").animate({bottom: -100}, 500);
		}).mouseenter(function(){
			$('#takeBack').css('color','#ffcb08');
		}).mouseleave(function(){
			$('#takeBack').css('color','#fff');
		});
		
		
	$(window).scroll(function () { 
		var trackScroll = $(window).scrollTop();
		$('#track').html(trackScroll);
		
		if(trackScroll > 250){
			if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)){
					$("#takeBack").css('display','none');
				}else{
					$("#takeBack").fadeIn(500);
				}

			 }	  
		if(trackScroll < 250){
				if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)){
					$("#takeBack").css('display','none');
				}else{
					$("#takeBack").fadeOut(500);
				}
				
			 }	


	});
	

	
	
	$(".leftArrow, .rightArrow").bind("click touchend MozTouchUp", function() {

		var arrowClicked = $(this).attr("id");
		var arrowSide = $(this).attr("class");
		focusedContainer = arrowClicked.substring(4,5);
		btnSide = arrowSide.substring(0,1);
		var myNum = parseInt($("#container"+focusedContainer).attr("cs"));
		curSlide=myNum;
		
			if( isAnimating == 0 && focusedContainer != 2){
				focusedContainerHandler(focusedContainer);
			}
			
			// products logos
			if( isAnimating == 0 && focusedContainer == 2){
				// not direct navigation
				productHandler(-1);
			}
			
			
			var container = arrowClicked.substring(4, 5);
			if (container == 0) container = "About Us";
			if (container == 1) container = "Featured Products";
			if (container == 2) container = "Products";
			if (container == 3) container = "Advice";
			
			//alert(container);
			 if( trackingControl == 1){
				_gaq.push(['_trackEvent', 'nav', 'arrow' , container]);
				}
			//console.log('This is how we log our day - doo da - doo da');
		});
		
		
	$(".bullet, .bullet2").bind("click touchend MozTouchUp", function() {

		var bulletClicked = $(this).attr("id");
		var focusedContainer = bulletClicked.substring(3,4);
		var bulletNum = parseInt(bulletClicked.substring(4,5));
		var myNum = parseInt($("#container"+focusedContainer).attr("cs"));
		curSlide=myNum;
		
			if( isAnimating == 0 && focusedContainer != 2){
			
				var container = "";
				if (focusedContainer == 0) container = "About Us";
				if (focusedContainer == 1) container = "Featured Products";
				if (focusedContainer == 2) container = "Products";
				if (focusedContainer == 3) container = "Advice";
			
				//alert(container);
				 if( trackingControl == 1){
					_gaq.push(['_trackEvent', 'nav', 'bullet' , container]);
					}
					
				slideBullettHandler(bulletNum, focusedContainer);
			}
		
		
		
			// products logos, direct navigatoin
			if( isAnimating == 0 && focusedContainer == 2){
				
				var container = "Products";
				 if( trackingControl == 1){
					_gaq.push(['_trackEvent', 'nav', 'bullet' , container]);
					}
				productHandler(bulletNum);
			}
	
		});
		
		
		
		
// map click event

		$(".mapPage").bind("click touchend MozTouchUp", function() {
			 if( trackingControl == 1){
				_gaq.push(['_trackEvent', 'nav', 'button' , 'map']);
				}
				
			window.location="http://maps.google.com/maps?q=Mike+Lynne's+Tennis+Shop+&hl=en&sll=37.0625,-95.677068&sspn=51.887315,87.099609&z=13&iwloc=A";
		
		});
	
	

	function focusedContainerHandler(focusedContainer){
		isAnimating=1;
		slideFade = '#container'+focusedContainer+' .slide' +slideCurr[focusedContainer];
		$(slideFade).css( 'z-index', '11');
		if(btnSide=='r'){
	
			var t = slideCurr[focusedContainer];
			t++;
			if (t == slideTotal[focusedContainer]) {
				t = 0;
			}

			var slideNext = '#container'+focusedContainer+' .slide' +t;
			$(slideFade).animate({'margin-left': '-900px'}, slideTime, 'easeInOutQuart');
			$(slideNext).css({'margin-left' : '900px', display : 'block', opacity : '1', 'z-index' : '15'});
			$(slideNext).animate({'margin-left': '0px'}, slideTime, 'easeInOutQuart', function() {
					slideComplete(focusedContainer, t);
				});	

			
		}
		
		if(btnSide=='l'){

			var t = slideCurr[focusedContainer];
			t--;
			if (t == -1) {
				t = (slideTotal[focusedContainer]-1);
			}

			var slideNext = '#container'+focusedContainer+' .slide' +t;
			$(slideNext).css({ 'margin-left' : '-900px', display : 'block', opacity : '1', 'z-index' : '15'});
			$(slideFade).animate({'margin-left': '900px'}, slideTime, 'easeInOutQuart');
			$(slideNext).animate({ 'margin-left': '0px'}, slideTime, 'easeInOutQuart', function() {
					slideComplete(focusedContainer, t)
				});	
		}

		}
		
	function slideBullettHandler(i, focusedContainer){

		isAnimating=1;
		slideFade = '#container'+focusedContainer+' .slide' +slideCurr[focusedContainer];
		$(slideFade).css( 'z-index', '11');
			
		nextSlide = i;
		var slideNext = '#container'+focusedContainer+' .slide' +nextSlide;
		
		if(nextSlide>slideCurr[focusedContainer]){
		
			$(slideNext).css({'margin-left' : '900px', display : 'block', opacity : '1', 'z-index' : '15'});
			$(slideFade).animate({'margin-left': '-900px'}, slideTime, 'easeInOutQuart');
			$(slideNext).animate({'margin-left': '0px'}, slideTime, 'easeInOutQuart', function() {
						slideComplete(focusedContainer, i)
					});	
		}
		
		if(nextSlide<slideCurr[focusedContainer]){
			$(slideNext).css({'margin-left' : '-900px', display : 'block', opacity : '1', 'z-index' : '15'});
			$(slideFade).animate({'margin-left': '900px'}, slideTime, 'easeInOutQuart' );
			$(slideNext).animate({'margin-left': '0px'}, slideTime, 'easeInOutQuart', function() {
						slideComplete(focusedContainer, i)
					});	
		}
			
	}
		
		
		function slideComplete(focusedContainer, t){
		
			isAnimating = 0;
			$(slideFade).css({ display : 'none'});
			var bulletChange = '#bul'+focusedContainer+t;
			$(bulletChange).css("color", "#333333");
			var bulletChange = '#bul'+focusedContainer+slideCurr[focusedContainer];
			$(bulletChange).css("color", "#cccccc");
		
			slideCurr[focusedContainer] = t;

			
		}
	


	// handler for products section (brand logos)
	
	function productHandler(type){
	
		var numOfBrands = $('.brand-list').children().length;
		var numOfMoves = Math.ceil(numOfBrands/8);
		var slideMoves = numOfMoves-1;
		var slideDistance = 880;
		
		var slideAll = slideDistance*slideMoves;
		isAnimating=1;
		
		// if type is a direct navigation, type var contains the destination slide
		if (type > -1){
		
			var delta = Math.abs(type - productNum);
			
			if (type < productNum){
				//alert("request a move left");
				btnSide='l';
			}
				
			if (type > productNum){
				//alert("request a move right");
				btnSide='r';
			}
			
			// no move
			if (type == productNum){
				btnSide='x';
				isAnimating=0;
			}
			
			slideDistance = slideDistance * delta;
			
		}
		
		//alert("type " + type + "  productNum" + productNum + "  delta" + delta);
		
		if(btnSide=='r'){
			if( productNum<slideMoves){
				$('.brand-list').animate({'margin-left': '-='+slideDistance+'px'}, slideTime, 'easeInOutQuart', function() {
				
						if (type == -1) productNum = productNum+1;
						if (type > -1) productNum = type;
						
						productComplete(productNum);
					});	
			}
			
			if(productNum==slideMoves){
					$('.brand-list').animate({'margin-left': '+='+slideAll+'px'}, slideTime, 'easeInOutQuart', function() {
						productNum = 0;
						productComplete(productNum);
					});	
			}
		}
		
		if(btnSide=='l'){
			if( productNum>0){
				$('.brand-list').animate({'margin-left': '+='+slideDistance+'px'}, slideTime, 'easeInOutQuart', function() {
				
						if (type == -1) productNum = productNum-1;
						if (type > -1) productNum = type;
						
						productComplete(productNum);
					});	
			}
			if(productNum==0){
					$('.brand-list').animate({'margin-left': '-='+slideAll+'px'}, slideTime, 'easeInOutQuart', function() {
						productNum = slideMoves;
						productComplete(productNum);
					});	
			}
		}
		
	}
	
	
	function productComplete(productNum){
		isAnimating = 0;
		
		if(productNum==3){
			$('.brand-message p').html('In addition to tennis gear, we also carry a full line<br />of racquetball and platform tennis equipment.');
		}
		if(productNum!=3){
			$('.brand-message p').html('We carry more tennis gear and apparel than anyone in the region. We would love to help you find exactly what you are looking for.');
		}
		
		var temp = 20 + Number(productNum);
		
		for(var i=20; i<24; i++){
			var bulletChange = '#bul'+i;
			$(bulletChange).css("color", "#cccccc");
			
		}
		
		var bulletChange = '#bul'+temp;
		$(bulletChange).css("color", "#333333");
	
		
	}


	



///////   END DOCUMENT READY FUNCTION  ///////
});
