$(document).ready(function() {

	

	/* HOMEPAGE PRODUCTS SCROLLER */
	var counter = 0;
	
	// onload, place items in a row
	$('div.slider-item').each(function() {
		$(this).css("left", (210*counter)+"px");
		counter ++;
	});
	
	$('img.product-right').parent().click(function() {
		if (counter > 4) {
			$('#slider-items > div.slider-item').stop(true, true); // kill any animation
			$('div.slider-item').each(function() { if (parseInt($(this).css("left"), 10) < 0) { $(this).css("left", "840px"); } }); // make sure the offscreen items are in place.
	
			$('#slider-items > div.slider-item').animate({left: '-=210'}, 650, function() {
				if (parseInt($(this).css("left"), 10) < 0) { $(this).css("left", "840px"); }
			});
		}
		return false;
	});

	$('img.product-left').parent().click(function() {
		if (counter > 4) {
			$('#slider-items > div.slider-item').stop(true, true); // kill any animation
			$('div.slider-item').each(function() { if (parseInt($(this).css("left"), 10) >= 840) { $(this).css("left", "-210px"); } }); // make sure the offscreen items are in place.

			$('#slider-items > div.slider-item').animate({left: '+=210'}, 650, function() {
				if (parseInt($(this).css("left"), 10) > 840) { $(this).css("left", "-210px"); }
			});
		}
		return false;
	});
	
	


	/* WHOLE PAGE SCROLLER */
	if ($.browser.msie && $.browser.version == '6.0') return;
	
	var $btn = $('<div id="scroll-more" class="down"></div>');
	var isAuto = false;

	$btn.click(function() {
		
		var bottom = $(document).height() - $(window).height();
		var scrollPos = 0;
	
		if (window.pageYOffset > 0) { // ff, webkit
			
			if ((window.pageYOffset + 500) < bottom) {
				scrollPos = window.pageYOffset + 500;
			} else {
				scrollPos = bottom;
			}
		} else { // IE
		
			if ((document.documentElement.scrollTop + 500) < bottom) {
				scrollPos = document.documentElement.scrollTop + 500;
			} else {
				scrollPos = bottom;
			}
		}
	
	
	
		var timerID = setInterval(function() {
	
			if ( $('#scroll-more').hasClass('down') ) {
				window.scrollBy(0, 25);  // been asked to auto-scroll down
				isAuto = true;
				if( window.pageYOffset >= scrollPos || window.pageYOffset >= bottom ) { isAuto = false; clearInterval(timerID); } // ff, webkit: hit either 500px or bottom of page.
				else if ( document.documentElement.scrollTop >= scrollPos || document.documentElement.scrollTop >= bottom ) { isAuto = false; clearInterval(timerID); } // ie: hit hit either 500px or bottom of page.
			} else {
				window.scrollBy(0, -25);  // been asked to auto-scroll up
				isAuto = true;
				if( window.pageYOffset == 0 ) { isAuto = false; clearInterval(timerID); } // ff, webkit: hit top of page.
				else if ( document.documentElement.scrollTop == 0 && $.browser.msie ) { isAuto = false; clearInterval(timerID); } // ie: hit top of page.
			}

		}, 13);
	});

	$(window).scroll(function() {
		var bottom = $(document).height() - $(window).height();
	
		if( window.pageYOffset >= bottom ) { // ff, webkit
			// scrolled to bottom, flip button
			$('#scroll-more').removeClass('down');
			$('#scroll-more').addClass('up');
		} else if ( document.documentElement.scrollTop >= bottom && $.browser.msie ) { // IE
			// scrolled to bottom, flip button
			$('#scroll-more').removeClass('down');
			$('#scroll-more').addClass('up');
		} else if ( window.pageYOffset == 0 ) { // ff, webkit
			// scrolled to top, reset button
			$('#scroll-more').removeClass('up');
			$('#scroll-more').addClass('down');
		} else if ( document.documentElement.scrollTop == 0 && $.browser.msie ) { // IE
			// scrolled to top, reset button
			$('#scroll-more').removeClass('up');
			$('#scroll-more').addClass('down');
		} else if (!isAuto) {
			// was at bottom of page, user has manually scrolled up some, so reset button
			$('#scroll-more').removeClass('up');
			$('#scroll-more').addClass('down');
		}
	
	}).scroll();

	$('body').append($btn);
});

