jQuery.noConflict();

var j = jQuery;

var SB = {
	Init: function () {
		this.SlideshowInit();
	},
	SlideshowInit: function () {
		if (j('#feature_products').size() > 0) {
		
			var productCount = j('#feature_products .featured_product').size();
		
			j('#feature_products .featured_product').each(function (i, product) {
				j('#controls .dots').append('<div class="dot"></div>');
			});
			
			// Set our first dot to active state.
			j('#controls .dot').eq(0).addClass('active-dot');
			
			// Bind controls click events
			j('#controls .left_control').click(function () {
				var currentProduct = j('#feature_products .active').index('#feature_products .featured_product');
				
				if (currentProduct > 0) {
					SB.SetSlide(currentProduct - 1);
				} else {
					SB.SetSlide(productCount - 1);
				}
			});
			
			j('#controls .right_control').click(function () {
				var currentProduct = j('#feature_products .active').index('#feature_products .featured_product');
				
				if (currentProduct < (productCount - 1)) {
					SB.SetSlide(currentProduct + 1);
				} else {
					SB.SetSlide(0);
				}
			});
			
			j('#controls .dot:not(.active-dot)').live('click', function () {
				SB.SetSlide(j(this).index('.dot'));
			});
			
			// Set control div padding.
			var controlsWidth = j('#controls .controls_container').width();
			var padding = (940 - controlsWidth)/2;
			
			j('#controls').css({ paddingLeft : padding, paddingRight: padding})
		}
	},
	SetSlide: function (slideIndex) {
		j('#feature_products .active').stop(true, true).fadeOut(800, function () {
			j(this).removeClass('active');
		});
		
		j('#feature_products .featured_product').eq(slideIndex).stop(true, true).fadeIn(800, function () {
			j(this).addClass('active');
		});
		
		// Update Dots
		j('#controls .active-dot').removeClass('active-dot');
		j('#controls .dot').eq(slideIndex).addClass('active-dot');
	}
}

j(document).ready(function () {
	SB.Init();
})
