
var slideInterval = 6; // seconds

$(document).ready(function() {
	
	// Preload product images
	var pl = ['01_sousvide-professional', '02_sousvide-thermal-circulator', '03_sousvide-thermal-circulating-baths',
			  '04_the-smoking-gun', '05_the-anti-griddle', '06_chamber-vacuum-sealer', '07_rotary-evaporator'];
	$.each(pl, function(i, v) {
		$('body').append('<img class="preloaded" alt="Preloaded Image - Ignore This" src="_image/_product-bar/large/' + v + '.png" />');
	});


	if ($.browser.msie) {
		$('.quote h1, .quote p').css('color', '#222');
		$('.quote').fadeTo(1, .8);
	}

	$('#slide-control').data({slideIndex: 0, userClicked: false});
	
	setInterval(function() {
		
		// Remove next line if you don't want clicking to cancel auto slideshow
		// if ($('#slide-control').data('userClicked') == true) return;
		
		var i = $('#slide-control').data('slideIndex');
		i++;
		$($('#slide-control a')[i]).trigger('click');
		if (i == $('#slide-control a').length-1) i = -1;	
		$('#slide-control').data('slideIndex', i);
	}, slideInterval*1000);

	$('#slide-control a').click(function() {
		// $('#slide-control').data('userClicked', true);
		
		if (!$(this).is('.sel')) {
			$('#slide-control a').removeClass('sel');
			$(this).addClass('sel');

			var showSlideIndex = $(this).attr('href').replace('#slide', '') - 1;
			var $showSlide = $($('#home-slides .slide')[showSlideIndex]);
			$showSlide.show();

			$('#home-slides .current').fadeOut('slow', function() {
				$(this).removeClass('current');
				$showSlide.addClass('current');
			});

		}

		return false;
	});
	
	
	// PRODUCT BAR
	if (!($.browser.msie && $.browser.version < 7)) // I HATE IE!!
	$('#product-line .product').hover(
		function() {
			var $p = $(this), leftOffset, bigWidth;
			if ($p.hasClass('thin')) {
				leftOffset = 0;
				bigWidth = 86;
			}
			else {
				leftOffset = -2;
				bigWidth = 144;
			}

			// $p.animate({fontSize: '12px'}, 100, 'swing');
			$p.find('a').animate({ color: '#222' });

			// I don't like to chain because I heard there's a performance hit
			var $pp = $p.find('img');
			$pp.attr('src', function(i, src) {
				return src.replace('small', 'large'); // oh that feels so good
			});
			$pp.addClass('hovered');
			$pp.animate({
				left: leftOffset,
				width: bigWidth,
				height: 120
			}, 100, 'linear');
		},
		function() {
			var $p = $(this), smallWidth;
			if ($p.hasClass('thin')) smallWidth = 72;
			else smallWidth = 120;

			// $p.animate({fontSize: '11px'}, 100, 'swing');
			$p.find('a').animate({color: '#888'});

			var $pp = $p.find('img');
			$pp.removeClass('hovered');
			$pp.animate({
				left: 0,
				width: smallWidth,
				height: 100
			}, 200, 'linear', function() {
				$pp.attr('src', function(i, src) {
					return src.replace('large', 'small');
				});
			});
		}
	);
	
});

function changeSlide($a) {
	if (!$a.is('.sel')) {
		$('#slide-control a').removeClass('sel');
		$a.addClass('sel');

		var showSlideIndex = $a.attr('href').replace('#slide', '') - 1;
		var $showSlide = $($('#home-slides .slide')[showSlideIndex]);
		$showSlide.show();

		$('#home-slides .current').fadeOut('slow', function() {
			$a.removeClass('current');
			$showSlide.addClass('current');
		});

	}
}

