//	==	sapeSlideStrip.js	==
;(function($) {

	$.fn.extend({
		sapeSlideStrip: function(options) {
			return this.each(function() {
				new $.SapeSlideStrip(this, options);
			});
		}
	});

	$.SapeSlideStrip = function(element, options) {
		var defaults = {
			itemWidth: $('li', element).outerWidth(),
			//itemWidth: 315,
			increment:  2,
			speed:      750
		};
		this.options = $.extend({}, defaults, options || {});
		this.element = $(element);
		this.itemWidth = $('li', element).outerWidth();
		this.totalItems = this.element.find('li').length;
		this.currentIndex = 0;
		this.setup();
	};

	$.extend($.SapeSlideStrip.prototype, {

		setup: function() {
			$strip = this.element;
			$strip.addClass('enabled');
			$strip.wrap('<div class="scroll-wrapper"></div>');
			this.container = $strip.parent();
			var itemWrapperWidth = $('li', $strip).outerWidth() * this.totalItems;
			$strip.css('width', itemWrapperWidth + 'px');
			var self = this;
			$('.carousel_button.prev').bind('click', function() {
				self.previous();
			});
			$('.carousel_button.next').bind('click', function() {
				self.next();
			});
			$(".carousel").each(function() {
				var elem = $(this);
				if (elem.children("li").length <= 3) {
					$("#innerScroll").hide();
				}
			});
		},

		next: function() {
			this.currentIndex = (scrollFlag)? calcIndex : this.currentIndex;
			var items = this.totalItems - this.options.increment;
			if(this.currentIndex < items ) {
				if(this.currentIndex + this.options.increment < items) {
					this.currentIndex = this.currentIndex + this.options.increment;
				} else {
					this.currentIndex = items;
				}
			}
			var scrollPosition = this.currentIndex * this.options.itemWidth;
			this.container.stop().animate({
				scrollLeft: scrollPosition +'px'},
				this.options.speed,
				'swing'
			);
			$("#innerScroll").slider('option', 'value', Math.round(scrollPosition/viewportScroll*100));
			scrollFlag = false;
		},

		previous: function() {
			this.currentIndex = (scrollFlag)? calcIndex : this.currentIndex;
			var items = this.totalItems - this.options.increment;
			if(this.currentIndex > 0 ) {
				if(this.currentIndex - this.options.increment > 0) {
					this.currentIndex = this.currentIndex - this.options.increment;
				} else {
					this.currentIndex = 0;
				}
			}
			var scrollPosition = this.currentIndex * this.options.itemWidth;
			this.container.stop().animate({
				scrollLeft: scrollPosition +'px'},
				this.options.speed,
				'swing'
			);
			$("#innerScroll").slider('option', 'value', Math.round(scrollPosition/viewportScroll*100));
			scrollFlag = false;
		}

	});

})(jQuery);

$(document).ready(function(){	
    $('.carousel').sapeSlideStrip({
        increment: 3,
		itemWidth: 315,
        speed: 1000
    });
});
//----------------------------------------


//==	Custom scrolling	--------------
var viewportScroll = 0;
var calcIndex = 0;
var scrollFlag = false;

initScrollBar = function() {
	viewportScroll = parseInt($(".carousel").outerWidth()) - parseInt($(".carousel").parent().outerWidth());
	$("#innerScroll").slider({
		min: -1,
		max: 101,
		value: -1,
		slide: setScroll
	});
}
function setScroll() {
	//var sliderVal = parseInt($(this).slider('option', 'value'));
	//var scrollVal = Math.round(viewportScroll*sliderVal/100);
	var sliderVal = $(this).slider('option', 'value');
	var scrollVal = viewportScroll*sliderVal/100;
	calcIndex = Math.ceil(scrollVal/330);
	scrollFlag = true;
	$(".carousel").parent().scrollLeft(scrollVal);
}

$(document).ready(function(){
	initScrollBar();	// Initialize scrollbar functionality
});
//----------------------------------------

