//$().ready(function(){
//	
//	var itemsHome = [
//			0,
//		 -520,
//		-1040,
//		-1560,
//		-2080,
//		-2600,
//		-3120,
//		-3640,
//		-4160,
//		-4680
//	];
//	
//	var custom = {
//		state: 0,
//		position: 0,
//		list: $('.rotation-small > li')
//	};
//	
//	rand = Math.floor(Math.random()*10);
//	console.log(rand);
//	$('.rotation-small').css('left', itemsHome[rand]);
//	
//	console.log($('.rotation-small').position());
//	var scrollOptions = {
//		target: $('.rotation-small-container'),
//		items: custom.list,
//		next: '.rotation-small a.next',
//		prev: '.rotation-small a.previous',
//		axis: 'x',
//		duration: 800,
//		easing: 'easeOutSine',
//		onAfter: function(div, item) {
//			position = $('.rotation-small').position();
//			console.log(position.left);
//			if(position.left == -5580 || position.left == -7200 ) {
//				$('.rotation-small a.next').fadeOut(100);
//			} else if (position.left == 0) {
//				$('.rotation-small a.previous').fadeOut(100);
//			} else {
//				$('.rotation-small a.next').fadeIn();
//				$('.rotation-small a.previous').fadeIn();
//			}
//			$(this).removeClass('fin');
//			$(this).css('opacity', 1.0);
//			
//			//console.log(item);
//			if (custom.position > custom.state) {
//				// take last element move to the end
//				//console.log('RIGHT');
//				item = ((custom.position - 1) == 0) ? custom.list[0] : custom.list[custom.position - 1];
//				$('ul.rotation-small').append($('ul.rotation-small').children(':first-child').clone());
//				$('ul.rotation-small').children(':first-child').remove();
//				$('ul.rotation-small').css('left', position.left - (($(item).width() + $(item).css('margin-right')) * 2));
//				console.log(item);
//				custom.state = custom.position;
//			} else {
//				// take previous element move to start
//				console.log('LEFT');
//			}
//		},
//		onBefore: function(e, element, panel, items, pos) {
//			custom.position = pos;
//		}
//	};
//	
//	
//	
//	$('.rotation-small').position({left: 0});
//
//	$('.rotation-small-container').serialScroll(scrollOptions);
//	$.localScroll(scrollOptions);
//	$.localScroll.hash(scrollOptions);
//});

jQuery.fn.carousel = function(previous, next, options){
	var sliderList = jQuery(this).children()[0];
	
	if (sliderList) {
		var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		
		for (i = 0; i < shownInViewport; i++) {
			jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
			jQuery(sliderList).append(jQuery(elmnts[i]).clone());
		}
		
		jQuery(previous).click(function(event){
			if (!isAnimating) {
				if (firstElementOnViewPort == 1) {
					jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
					firstElementOnViewPort = numElmts;
				}
				else {
					firstElementOnViewPort--;
				}
				
				jQuery(sliderList).animate({left: "+=" + increment},{
					duration: 700,
					easing: 'swing',
					left: "-=" + increment,
					y: 0,
					complete:function(){isAnimating = false; $('.rotation-small').css('opacity', 1);$('.rotation-small').removeClass('fin');},
					step: function() {
						if (!$(this).hasClass('fin')) {
							$(this).css('opacity', (eval($(this).css('opacity')) - .2));
							if ($(this).css('opacity') <= -3.2) {
								$(this).addClass('fin');
							}
						} else {
							$(this).css('opacity', (eval($(this).css('opacity')) + .2));
						}
					}
				});
				isAnimating = true;
			}
			return false;
		});
		
		jQuery(next).click(function(event){
			if (!isAnimating) {
				if (firstElementOnViewPort > numElmts) {
					firstElementOnViewPort = 2;
					jQuery(sliderList).css('left', "0px");
				}
				else {
					firstElementOnViewPort++;
				}

				//jQuery(sliderList).animate({
				//	left: "-=" + increment,
				//	y: 0,
				//	queue: true
				//}, "swing", function(){isAnimating = false;});
				//isAnimating = true;
				jQuery(sliderList).animate({left: "-=" + increment},{
					duration: 700,
					easing: 'swing',
					left: "-=" + increment,
					y: 0,
					complete:function(){isAnimating = false; $('.rotation-small').css('opacity', 1);$('.rotation-small').removeClass('fin');},
					step: function() {
						if (!$(this).hasClass('fin')) {
							$(this).css('opacity', (eval($(this).css('opacity')) - .2));
							if ($(this).css('opacity') <= -3.2) {
								$(this).addClass('fin');
							}
						} else {
							$(this).css('opacity', (eval($(this).css('opacity')) + .2));
						}
					}
				});
				isAnimating = true;
			}
			
			return false;
		});
	}
};

$().ready(function(){
	
	//var itemsHome = [
	//		'0px',
	//	'-490px',
	//	'-980px',
	//	'-1470px',
	//	'-1961.22px',
	//	'-2450px',
	//	'-2940px',
	//	'-3430px',
	//	'-3920px',
	//	'-4410px',
	//];
	//
	//var custom = {
	//	state: 0,
	//	position: 0,
	//	list: $('.rotation-small > li')
	//};
	
	//rand = Math.floor(Math.random()*10);
	//console.log(rand);
	
	items = $('ul.rotation-small').children();
	
	$('ul.rotation-small').html();
	
	// randomly sort
	items.sort(function() {return 0.5 - Math.random()});
	
	for (i=0;i!=items.length;i++) {
		$('ul.rotation-small').append(items[i]);
	}
	
	//$('.rotation-small').css('left', itemsHome[rand]);

	$('.rotation-small-container').carousel('.rotation-small li a.previous','.rotation-small li a.next');
});
