$(function() {
	var n = $('.item').size();
	$('#toleft').toggle();
	$('#toright').toggle();
	$('#desc').toggle(); $('#softer').toggle();
	$('#showcase').css('border', '2px solid #b8b8b8').css('height', '162px');
	$('#juosta li:not(.item)').appendTo('#descriptions');
	$('#portlist li:first').addClass('selected');
	$('#juosta').css('width', n*450 + 'px');
	var time = 800;
	var type = "easeInQuad";
	var x = 2;
	$('#juosta .item:last').clone(true).insertBefore('#juosta .item:first');
	$('#juosta .item:first').css('margin-left', -450 + 'px');
	$('#juosta .item:nth-child(2)').clone(true).insertAfter('#juosta .item:last');
	$('#juosta .item:last').css('margin-right', -450 + 'px');
	$('#descriptions li:last').clone(true).insertBefore('#descriptions li:first');
	$('#descriptions li:first').css('margin-left', -495 + 'px');
	$('#descriptions li:nth-child(2)').clone(true).insertAfter('#descriptions li:last');
	$('#descriptions li:last').css('margin-right', -495 + 'px');
	while(x<=2+n) {
		$('#juosta .item:nth-child(' + x + ')').css('left', (x-2)*450 + 'px');
		x++;
	}
	$('#descriptions').css('width', n*495 + 'px');
	var z = 1;
	while(z<=n) {
		$('#descriptions div:nth-child(' + z + ')').css('left', (z-1)*495 + 'px');
		z++;
	}
	var i = 0;
	$('#portlist li a').click(function() {
		i = $(this).attr('title') - 1;
		$('#juosta').stop().animate({"left" : i*-450 + 'px'}, time, type);
		$('#descriptions').stop().animate({"left" : i*-495 + 'px'}, time, type);
		$('#portlist li').removeClass('selected');
		$('#portlist li:nth-child(' + (1+i) + ')').addClass('selected');
		return false;
	});
	$('#toleft').click(function() {
		if(i==0){ 
			i = n;
			i--;
			$('#juosta').stop().animate({"left": i*-450 + 'px'}, 2*time, "easeInBack");
			$('#descriptions').stop().animate({"left" : i*-495 + 'px'}, 2*time, "easeInBack");
		} else {
			i--;
			$('#juosta').stop().animate({"left" : i*-450 + 'px'}, time, type);
			$('#descriptions').stop().animate({"left" : i*-495 + 'px'}, time, type);
		}
		$('#portlist li').removeClass('selected');
		$('#portlist li:nth-child(' + (1+i) + ')').addClass('selected');
		return false;
	});
	$('#toright').click(function() {
		i++;
		if(i==n){
			i = 0;
			$('#juosta').stop().animate({"left" : i*-450 + 'px'}, 2*time, "easeInBack");
			$('#descriptions').stop().animate({"left" : i*-495 + 'px'}, 2*time, "easeInBack");
		} else {
			$('#juosta').stop().animate({"left" : i*-450 + 'px'}, time, type);
			$('#descriptions').stop().animate({"left" : i*-495 + 'px'}, time, type);
		}
		$('#portlist li').removeClass('selected');
		$('#portlist li:nth-child(' + (1+i) + ')').addClass('selected');
		return false;
	});
	$('#showcase #juosta li div img').fadeTo("fast", 0.85);
	$('#showcase #juosta li div img').hover(function() {
		$(this).stop().fadeTo("fast", 1);
	}, function() {
		$(this).stop().fadeTo("fast", 0.85);
	});
});
