/**
 * Base constructor for scroller widget
 */  
kff.widgets.Scroller = function(element, options)
{
	this.options = $.extend({
		carousel: 'ul',
		items: 'li',
		prev: '.btn-prev',
		next: '.btn-next',
		scrollWidth: 100,
		carouselWidth: null,
		axis: 'x',
		speed: 1000,
		name: null
	}, options);
	this.$element = $(element);
	this.scrollPosition = 0;
	this.realScrollPosition = 0;
	this.instanceNumber = ++kff.widgets.Scroller.instanceCount;
	this.animating = false;
};

kff.widgets.Scroller.instanceCount = 0;


kff.widgets.Scroller.prototype.activate = function()
{
	var that = this;
	this.$carousel = this.$element;
	var options = this.options;
	var isXAxis = this.options.axis != 'y';

	if(this.$carousel.size())
	{
		this.scrollWidth = this.options.scrollWidth;
		this.ulWidth = 0;
		this.carouselWidth = isXAxis ? this.$carousel.width() : this.$carousel.height();
		
		if(this.options.carouselWidth) isXAxis ? this.$carousel.width(this.options.carouselWidth) : this.$carousel.height(this.options.carouselWidth);
		
		this.carouselWidth = isXAxis ? this.$carousel.width() : this.$carousel.height();
		
		if(isXAxis)	this.$carousel.find(this.options.items).each(function(){
			that.ulWidth += $(this).outerWidth(true);
		});
		else this.$carousel.find(options.items).each(function(){
			that.ulWidth += $(this).outerHeight(true);
		});

		if(this.ulWidth > this.carouselWidth)
		{
			this.$carousel.addClass('kff-scroller-active');
			this.$carousel.find(this.options.carousel).css(isXAxis ? 'width' : 'height', this.ulWidth);

			$(options.next).bind('click', function(){
			    that.scrollNext();
			    return false;

			});
			$(options.prev).bind('click', function(){
			    that.scrollPrev();
			    return false;
			});

			this.bindScroll();

			this.realScrollPosition = this.loadPosition();
			if(this.realScrollPosition != 0)
			{
				this.scrollPosition = Math.floor(this.realScrollPosition / this.options.scrollWidth);
				if(isXAxis)	this.$carousel.scrollLeft(this.realScrollPosition);
				else this.$carousel.scrollTop(this.realScrollPosition);
			}
			this.updateButtonsState();
		}
		else
		{
			$(options.next).hide();
			$(options.prev).hide();
		}
	}
}

kff.widgets.Scroller.prototype.animate = function(position, callback)
{
	var isXAxis = this.options.axis != 'y';
	this.animating = true;
    this.scrollPosition = position;
    this.realScrollPosition = this.scrollPosition * this.scrollWidth;
    if(this.realScrollPosition > this.ulWidth - this.carouselWidth)
    {
		this.realScrollPosition = this.ulWidth - this.carouselWidth;
		this.scrollPosition = Math.floor(this.realScrollPosition / this.scrollWidth);
	}
    else if(this.realScrollPosition < 0)
	{
		this.scrollPosition = this.realScrollPosition = 0;
	}
	
	var animateOption = isXAxis ? { scrollLeft: this.realScrollPosition } : { scrollTop: this.realScrollPosition };
	
	var that = this;
	this.$carousel.stop().animate(animateOption, this.options.speed, "easeOutCubic", function(){
		that.savePosition();
		that.animating = false;
	});
	this.updateButtonsState();
}
			
kff.widgets.Scroller.prototype.scrollNext = function()
{
	this.animate(this.scrollPosition + 1);
}

kff.widgets.Scroller.prototype.scrollPrev = function()
{
	this.animate(this.scrollPosition - 1);
}

kff.widgets.Scroller.prototype.scrollFirst = function()
{
	this.animate(0);
}

kff.widgets.Scroller.prototype.updateButtonsState = function()
{
	if(this.realScrollPosition >= this.ulWidth - this.carouselWidth)
	{
		$(this.options.next).addClass('btn-disabled');
		$(this.options.prev).removeClass('btn-disabled');
	}
	else if(this.realScrollPosition <= 0)
	{
		$(this.options.next).removeClass('btn-disabled');
		$(this.options.prev).addClass('btn-disabled');
	}
	else
	{
		$(this.options.next).removeClass('btn-disabled');
		$(this.options.prev).removeClass('btn-disabled');	
	}
}

kff.widgets.Scroller.prototype.deactivate = function()
{
	this.unbindScroll();
	this.$element.stop().removeClass('kff-scroller-active');	
	if(this.options.axis != 'y')
	{
		this.$element.scrollLeft(0).css({width: this.carouselWidth}).find(this.options.carousel).css('width', 'auto');
	}
	else
	{ 
		this.$element.scrollTop(0).css({height: this.carouselWidth}).find(this.options.carousel).css('height', 'auto');
	}
	$(this.options.next).unbind('click');
	$(this.options.prev).unbind('click');
	this.animating = false;
}

kff.widgets.Scroller.prototype.getInstanceName = function()
{
	return this.options.name || ('Scroller' + this.instanceNumber);
}

kff.widgets.Scroller.prototype.savePosition = function()
{
	if($.cookie) $.cookie(this.getInstanceName(), this.realScrollPosition, { expires: 7, path: '/' });
}

kff.widgets.Scroller.prototype.loadPosition = function()
{
	return $.cookie ? $.cookie(this.getInstanceName()) || 0 : 0;
}

kff.widgets.Scroller.prototype.bindScroll = function()
{
	var that = this;
	var timeout;
	var isXAxis = this.options.axis != 'y';
	that.$element.bind('scroll', function(){
		if(!that.animating)
		{
			var oldScrollPosition = that.realScrollPosition;
			that.realScrollPosition = isXAxis ? that.$element.scrollLeft() : that.$element.scrollTop();
			that.scrollPosition = Math.floor(that.realScrollPosition / that.options.scrollWidth);
			clearTimeout(timeout);
			timeout = setTimeout(function(){ that.savePosition(); }, 500);
		}
	});
}

kff.widgets.Scroller.prototype.unbindScroll = function()
{
	this.$element.unbind('scroll');
}

