// JavaScript Document
//Function to animate product options
$(function () {
	//ready the first tab
	$('#ProductInfoTabs li').eq(0).addClass('selectedTab');
	$('#VisibleTab div').eq(0).addClass('selectedTab');
	//create click function for other tabs
	$('#ProductInfoTabs > ul > li').click(function () {
		var thingIndex = $('#ProductInfoTabs li').index(this);
		$('#ProductInfoTabs li').removeClass('selectedTab');
		$('#VisibleTab div').removeClass('selectedTab');
		$(this).addClass('selectedTab');
		$('#VisibleTab div').eq(thingIndex).addClass('selectedTab');
	});			
	
	//Shopping cart popup, wot wot!
	$('#UserShoppingCart span').click(function () {
		if ($('.cartPopup').css('display') == 'none') {
			$('.cartPopup').css('display','block');
		}
		else {
			$('.cartPopup').css('display','none');
		}		
	});
	$('#UserShoppingCart span span').click(function () {
		if ($('.cartPopup').css('display') == 'none') {
			$('.cartPopup').css('display','block');
		}
		else {
			$('.cartPopup').css('display','none');
		}		
	});
	$('.cartPopupClose').click(function () {
		$('.cartPopup').css('display','none');
	});
	
	//can I haz variable to keep track of when the element is clicked? Stupid double clicking!
	var hasClicked = 0
	
	//disable left scroll buttons
	$('#AdsScrollLeft').addClass('disabled');
	$('#BestSellersScrollLeft').addClass('disabled');
	
	//This is for scrolling the ads
	$('#AdsScrollLeft').click(function () {
		var scrollIncrement = $(this).next().children().children().eq(0).attr('clientWidth');
		var scrollLeftValue = $(this).next().attr('scrollLeft');
		
		if (scrollLeftValue > 0 && hasClicked == 0) {
			hasClicked = 1;
			$('#AdsScrollRight').removeClass('disabled');
			if (scrollLeftValue - scrollIncrement <= 0) {
				$(this).next().animate({scrollLeft: 0}, 1000, 'easeInOutQuad', function () {
					hasClicked = 0;
					$('#AdsScrollLeft').addClass('disabled');
				});
			}
			else {
				$(this).next().animate({scrollLeft: scrollLeftValue - scrollIncrement}, 1000, 'easeInOutQuad', function () {hasClicked = 0;});
			}
		}
	});
	$('#AdsScrollRight').click(function () {
		var scrollIncrement = $(this).prev().children().children().eq(0).attr('clientWidth');
		var scrollLeftValue = $(this).prev().attr('scrollLeft');
		var maxScroll = ($(this).prev().children().children().length-1) * scrollIncrement;

		if (scrollLeftValue + scrollIncrement < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#AdsScrollLeft').removeClass('disabled');
			$(this).prev().animate({scrollLeft: scrollLeftValue+ scrollIncrement}, 1000, 'easeInOutQuad', function () {hasClicked = 0;});
		}
		else if (scrollLeftValue < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#AdsScrollLeft').removeClass('disabled');
			$(this).prev().animate({scrollLeft: maxScroll}, 1000, 'easeInOutQuad', function () {
				hasClicked = 0;
				$('#AdsScrollRight').addClass('disabled');
			});
		}
	});
	
	
	//This is just for the home page best sellers section.
	$('#BestSellersScrollLeft').click(function () {
		var scrollIncrement = $(this).next().children().children('.productListItem').eq(0).attr('clientWidth')*5;
		var scrollLeftValue = $(this).next().attr('scrollLeft');
		
		if (scrollLeftValue > 0 && hasClicked == 0) {
			hasClicked = 1;
			$('#BestSellersScrollRight').removeClass('disabled');
			if (scrollLeftValue - scrollIncrement <= 0) {
				$(this).next().animate({scrollLeft: 0}, 1000, 'easeInOutQuart', function () {
					hasClicked = 0;
					$('#BestSellersScrollLeft').addClass('disabled');
				});
			}
			else {
				$(this).next().animate({scrollLeft: scrollLeftValue - scrollIncrement}, 1000, 'easeInOutQuart', function () {hasClicked = 0;});
			}
		}
	});
	$('#BestSellersScrollRight').click(function () {
		var scrollIncrement = $(this).prev().children().children('.productListItem').eq(0).attr('clientWidth')*5;
		var scrollLeftValue = $(this).prev().attr('scrollLeft');
		var maxScroll = ($(this).prev().children().children('.productListItem').length-5) * (scrollIncrement/5);
		
		if (scrollLeftValue + scrollIncrement < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#BestSellersScrollLeft').removeClass('disabled');
			$(this).prev().animate({scrollLeft: scrollLeftValue+ scrollIncrement}, 1000, 'easeInOutQuart', function () {hasClicked = 0;});
		}
		else if (scrollLeftValue < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#BestSellersScrollLeft').removeClass('disabled');
			$(this).prev().animate({scrollLeft: maxScroll}, 1000, 'easeInOutQuart', function () {
				hasClicked = 0;
				$('#BestSellersScrollRight').addClass('disabled');
			});
		}
	});
	
	//Email a friend
	$('#EmailFriendForm').prepend('<h3>Email a Friend <span>close</span></h3>');
	$('#EmailFriend').click(function() {
		$('#GreyFade').css({'height':$(document).height(), 'display':'block', 'opacity':0}).fadeTo('normal', 0.42, function () {
			$('#EmailFriendForm').fadeIn('fast');
		});
	});
	$('#EmailFriendForm h3 span').click(function () {
		$('#EmailFriendForm').fadeOut('fast', function () {
			$('#GreyFade').fadeOut('fast');
		});
	});	
});
