/*
 * scroll gallery jQuery Plugin
 *
 * To init:
 * Call $('#element').scroller();
 *
 * To update:
 * Call $('#element').scroller('<option>', <value>);
 *
 */
(function($) {
	$.widget('ui.scroller', {
		leftArrow: null,
		rightArrow: null,
		scroller: null,
		active: $(),
		_init : function() {
			if (!this.element.is('ul'))	{
				this.scroller = this.element.empty();
				this.element = $('<ul></ul>');
			} else {
				this.scroller = $('<div class="scroller" />');
				this.element.removeClass('scroller').addClass('scroller-ul');
				this.scroller.insertBefore(this.element);
			}

			for (var i = this.element.children('li').length; i<this.options.total; i++)
				this.element.append('<li></li>')

			this.element.children('li').addClass('scroller-li');

			var widget = this;
			if (this.options.arrows) {
				this.leftArrow = $('<div class="arrow left hidden"/>').click(function(){ widget.prev(); });
				this.rightArrow = $('<div class="arrow right hidden"></div>').click(function(){	widget.next(); });
				this.scroller.append(this.leftArrow);
				this.scroller.append(this.rightArrow);

				this.rightArrow.toggleClass('hidden', this.element.children('li').length < 2);
			}

			var container = $('<div class="container" />');
			this.scroller.append(container);

			if (this.options.mousewheel) {
				container.mousewheel(function(event, delta) {
					if(delta>0)
						widget.prev();
					else if (delta<0)
						widget.next();
					event.preventDefault();
				});
			}

			this.element.appendTo(container).show();

			// @todo: check if counting width is right
			var w = this.element.children('li:first');
			if (w.width())
				w = w.outerWidth(true);
			else
				w = Number(w.css('width').replace('px',''))
					+ w.css('marginLeft').replace('px','')
					+ w.css('marginRight').replace('px','');
			this.element.width(this.element.children('li').length * w);

			this.select(this.options.active);
		},
		destroy : function() {
			$.widget.prototype.destroy.apply(this, arguments);
			return this;
		},
		_scrollTo : function(element) {
			if (!element)
				return;
			var marginLeft = -element.outerWidth(true) * this.element.children('li').index(element);
			var deltaN = 1;
//			var deltaN = Math.round(Math.abs(marginLeft - this.element.css('marginLeft').replace('px','')) / element.outerWidth(true));
			if (this.options.animateScroll) {
				if (this.element.is(':animated'))
					this.element.stop();
				this.element.animate({marginLeft:marginLeft}, { queue:false, duration:this.options.animateScroll?this.options.animateScroll*deltaN:0 });
			} else {
				this.element.css('marginLeft', marginLeft);
			}
		},
		show : function() {
			if (this.scroller && this.scroller.is(':hidden'))
				this.scroller.show();
		},
		hide : function() {
			if (this.scroller && this.scroller.is(':visible'))
				this.scroller.hide();
		},
		select : function(num) {
			if (num < 0 || num >= this.element.children('li').length)
				return;
			
			var activeNum = this.element.children('li').index(this.active);
			if (activeNum == num)
				return;

			//switching arrows
			if (this.options.arrows) {
				this.leftArrow.toggleClass('hidden', num==0);
				this.rightArrow.toggleClass('hidden', num==this.element.children('li').length-1);
			}

			// triggering event beforeselect
			this._trigger('beforeselect', 0, num);

			// deactivating current active
			this.active.removeClass('active');

			// finding new active
			this.active = this.element.children('li').eq(num);

			//scrolling to new active
			this._scrollTo(this.active);
			
			// triggering event select
			this._trigger('select', 0, num);

			// activating new active
			this.active.addClass('active');
		},
		prev : function() {
			var num = this.element.children('li').index(this.active.prev());
			if (num >= 0)
				this.select(num);
		},
		next : function() {
			var num = this.element.children('li').index(this.active.next());
			if (num >= 0)
				this.select(num);
		},
		getPageContent : function(num, content) {
			return this.element.children('li').eq(num).html();
		},
		setPageContent : function(num, content) {
			this.element.children('li').eq(num).html(content);
		},
		getPageData : function(num, name) {
			return this.element.children('li').eq(num).data(name);
		},
		setPageData : function(num, name, value) {
			this.element.children('li').eq(num).data(name, value);
		},
		getActive : function() {
			return this.active;
		}
	});

	$.extend($.ui.scroller, {
		version : '0.1.0',
		getter: "getPageContent getPageData getActive",
		defaults : {
			active: 0,
			animateScroll: 0,
			arrows: true,
			mousewheel: true,
			onSelect: null,
			addClass: null
		}
	});

})(jQuery);