(function($) {
	// main thread
	$.fn.gallery = function (options) {
		return this.each(
			function (i) {
				$list = $(this);
				$.fn.gallery.options = $.extend({}, $.fn.gallery.defaults, options);
				$.fn.gallery.options.slideCount = $('li', $list).length;
				$list.css({'width' : ($.fn.gallery.options.slideCount * $.fn.gallery.options.slideWidth) + 'px'});
				$.fn.gallery.inTransition = false;
				$.fn.gallery.slide($list, -$.fn.gallery.options.slideWidth, $.fn.gallery.options.direction);
				$.fn.gallery.createBindings($list);
			}
		);
	};
	// bind arrow links
	$.fn.gallery.createBindings = function ($list) {
		$('#homepageScroller > div.arrow').bind('click', function (e) {
			$.fn.gallery.options.direction = $(e.currentTarget).attr('data-direction');
		});
	};
	// slide
	$.fn.gallery.slide = function ($list) {
		offset = -$.fn.gallery.options.slideWidth;
		newOffset = 0;
		if ($.fn.gallery.options.direction == 'right') {
			offset = 0;
			newOffset = -$.fn.gallery.options.slideWidth + 'px';
			$list.prepend($('li:eq(' + ($.fn.gallery.options.slideCount - 1) + ')', $list));
		}
		$list
			.css({left: newOffset})
			.animate(
				 {left: offset + 'px'}
				,{
					 duration	: $.fn.gallery.options.animationSpeed
					,easing		: 'linear'
					,complete	: function () {
						$.fn.gallery.inTransition = false;
						switch ($.fn.gallery.options.direction) {
							case 'left': $list.append($('li:eq(0)', $list));
							case 'right': $list.css({left: -$.fn.gallery.options.slideWidth + 'px'});
						}
						$.fn.gallery.slide($list);
					 }
				 }
			);
	};
	// defaults
	$.fn.gallery.defaults = {
		 animationSpeed: 4500
		,direction: 'left'
		,minOpacity: 0.05
		,slideWidth: 310
		,timeOut: 0
	};
})(jQuery);
