$(document).ready(function() {	
	
	var featureCount = 0;
	var currentFeature = 0;
	var rotateTimer;
	
	// Homepage Fader
	if( $(".homepage_content").length > 0 ) {
		featureCount = $('.featured_products').children('li').length -1;		
		rotateTimer = setTimeout(rotateHomepageProduct, 9000);
		
		// build our featured menu
		for($i=0;$i<featureCount+1;$i++) {
			if($i==0) {
				$(".featured_menu").append('<li class="active item-'+$i+'"><a href="#">view</a></li>');
			}else{
				$(".featured_menu").append('<li class="item-'+$i+'"><a href="#">view</a></li>');
			}			
		}
		
		// Apply a proper width and center it under the image
		var menuWidth = (featureCount+1) * 15
		$('.featured_menu').width(menuWidth);
		$('.featured_menu').css('left',225 - (menuWidth/2));
		
	}
	
	function rotateHomepageProduct() {	
		if(featureCount > 0) {
			if(currentFeature == featureCount) {
				// reset the counter				
				currentFeature = 0;				
				
				$('.featured_products li:visible').fadeOut(300);
				$('.feature-'+currentFeature).fadeIn(500);			
			}else{
				// add one to the counter
				currentFeature++;
				$('.featured_products li:visible').fadeOut(300);
				$('.feature-'+currentFeature).fadeIn(500);				
			}
			$('.featured_menu li').removeClass('active');
			$('.featured_menu .item-'+currentFeature).addClass('active');
			rotateTimer = setTimeout(rotateHomepageProduct, 9000);
		}				
	}
	
	$('.featured_menu a').live('click', function(e) {
		$('.featured_menu li').removeClass('active');
		var clickedItem = $(this).parent('li')
		clickedItem.addClass('active');		
		// get the index of the clicked item
		var index = $('.featured_menu li').index(clickedItem);
		
		if( $(".featured_products li:eq("+index+")").is(':visible') ) {
			return false;
		}else{
			$('.featured_products li:visible').fadeOut(300, function() {
				$('.featured_products li:eq('+index+')').fadeIn(500);
			});										
		}
		
		clearTimeout(rotateTimer);
		
		return false;
	});
	
	
	
	
	
	
	// Product Slider
	var productCount = 0;
	var sliderCount = 0;
	var sliderPosition = 0;
	var lastSlideCount = 0;
	var currentProduct = '';
	
	// Product List
	if( $("ul.productList").length > 0 ) {
		productCount = $(".productList li").length;
		sliderCount = Math.floor(productCount / 8);
		if( productCount % 8 != 0) {
			lastSlideCount = productCount % 8;
			sliderCount++;
		}
		$('.productSliderWrapper').scrollTo('li:eq(0)',0);
	};
	
	// Ring slider on the taxonomy page
	$('.productSlider .next').click(function() {
		if((sliderPosition+1) != sliderCount) {
			sliderPosition++;
			if(lastSlideCount == 0) {
				$('.productSliderWrapper').scrollTo('li:eq('+ sliderPosition * 8 +')',500);
			}else{
				var newIndex = sliderPosition * 8 - lastSlideCount;
				$('.productSliderWrapper').scrollTo('li:eq('+ newIndex +')',500);
			}
		}
		return false;
	});
	
	$('.productSlider .prev').click(function() {
		if((sliderPosition+1) > 1) {
			sliderPosition--;
			$('.productSliderWrapper').scrollTo('li:eq('+ sliderPosition * 8 +')',500);
		}
		return false;
	});
	
	/* Individual Product Pages */
	if($('.productGalleryWrapper').length > 0) {
		$('.productThumbnail li').hover(function() {
			var imageIndex = $(this).index();
			$('.productImages li').hide();
			$('.productImages li:eq('+imageIndex+')').show();
			}, function() {
		});
	}
	
	/* Prevent default behavoir when gallery links are clicked */
	$('.productThumbnail a').click(function() {
		return false;
	});
	
	/* Scroll to the proper slide for individual product pages */
	if( $('.single .productList').length != 0 ) {
		currentProductIndex = $('.productList a[href="'+window.location+'"]').parent('li').index();
		sliderPosition = Math.floor(currentProductIndex / 8);
		if(sliderPosition != 0) {
			// scroll to the proper location
			if(lastSlideCount == 0) {
				$('.productSliderWrapper').scrollTo('li:eq('+ sliderPosition * 8 +')',500);
			}else{
				var newIndex = sliderPosition * 8 - lastSlideCount;
				$('.productSliderWrapper').scrollTo('li:eq('+ newIndex +')',500);
			}
		}
	};
	
	
	
});
