$(document).ready(function() {
	var current_date = new Date();
	$('div.LaddaUppSpelare div.LaddaUppSlider').slider({ min: 0, max: 220, animate: true, step: 20, orientation: 'vertical'});

	$('div.LaddaUppSpelare div.LaddaUppSlider').slider('value', (11 - current_date.getMonth()) * 20);

	$('div.LaddaUppSpelare span.MonthTag').each(function() {
		if (parseInt($(this).html()) != (current_date.getMonth() + 1)) {
			$(this).parent().hide();
		}
	});

	$('div.LaddaUppSpelare div.Manad a').click(function() {
		var $elem = $(this).parent().parent().next().next().next().children('div.Background');
		if ($elem.position().top == 0) {
			// Down

			$(this).parent().parent().next().next().next().children('div.Background').animate({top: '+=274'}, 500, 'easeOutExpo');
			$('div.LaddaUppSpelare span.MonthTag').each(function() {

				if (parseInt($(this).html()) != (12 - ($('div.LaddaUppSpelare div.LaddaUppSlider').slider('value') / 20))) {
					$(this).parent().hide();
				} else {
					$(this).parent().show();
				}
			});

			// Recalculate number of items
			var length = $(this).parent().parent().parent().find('li:visible').length;
			$(this).parent().parent().parent().children('div.Viewport').children('ul').data('items', length);
			$(this).parent().parent().parent().children('div.Viewport').children('ul').data('current', 1);
			$(this).parent().parent().parent().children('div.Viewport').children('ul').data('index', 1);

			// Scroll all the way back to the beginning
			$(this).parent().parent().parent().children('div.Viewport').children('ul').animate({left: 0}, 500, 'easeOutExpo');
		} else {
			// Up
			$(this).parent().parent().next().next().next().children('div.Background').animate({top: '-=274'}, 500, 'easeOutExpo');
		}

		return false;
	});

	$('div.LaddaUppSpelare ul').each(function(i, e) {
		$(this).data('items', $(this).find('li:visible').length);
		$(this).data('current', 1);
		// $(this).data('interval', setInterval(function() {LaddaUppSpelare_AutoSlide(i)}, 5500));
		$(this).data('index', i);
	});


	function LaddaUppSpelare_AutoSlide(index) {
		$('div.LaddaUppSpelare ul').each(function(i, e) {

			// alert('i: '+i+' index: '+index);

			if (i != index)
				return;

			var cur = $(this).data('current');
			var total = $(this).data('items');
			var left = 640;

			// Don't autoslide hidden sliders
			if (!$(this).is(":visible"))
				return;
	
			if (cur == total) {
				left = -640 * (total - 1);
				cur = 0;
			}
	
			$(this).animate({left: '-='+left+''}, 1500, 'easeOutExpo');
			$(this).data('current', cur+1);
		});
	}

	$('div.LaddaUppSpelare div.Left').click(function() {
		var cur = $(this).parent().children('div.Viewport').children('ul').first().data('current');
		var total = $(this).parent().children('div.Viewport').children('ul').first().data('items');
		var left = 640;

		if (cur == 1) {
			left = -640 * (total - 1);
			cur = total+1;
		}

		$(this).parent().children('div.Viewport').children('ul').each(function(i, e) {
			$(this).animate({left: '+='+left+''}, 1500, 'easeOutExpo');
		});
		$(this).parent().children('div.Viewport').children('ul').first().data('current', cur-1);

		return false;
	});

	$('div.LaddaUppSpelare div.Right').click(function() {
		var cur = $(this).parent().children('div.Viewport').children('ul').first().data('current');
		var total = $(this).parent().children('div.Viewport').children('ul').first().data('items');
		var left = 640;

		if (cur == total) {
			left = -640 * (total - 1);
			cur = 0;
		}

		$(this).parent().children('div.Viewport').children('ul').each(function(i, e) {
			$(this).animate({left: '-='+left+''}, 1500, 'easeOutExpo');
		});
		$(this).parent().children('div.Viewport').children('ul').first().data('current', cur+1);

		return false;
	});
});

