var num = 1;

window.onload = function() {
	
	var t1 = $("#t1").css("top").split("px")[0] * 1;
	$("#t1").animate({top: t1 - 20, opacity: 1, width: 236}, 800, function() {
		var t2 = $("#t2").css("top").split("px")[0] * 1;
		$("#t2").animate({top: t2 - 20, opacity: 1, width: 213}, 800, function() {
			var t3 = $("#t3").css("top").split("px")[0] * 1;
			$("#t3").animate({top: t3 - 20, opacity: 1, width: 220}, 800, function() {
				var t4 = $("#t4").css("top").split("px")[0] * 1;
				$("#t4").animate({top: t4 - 20, opacity: 1, width: 202}, 800, function() {
					var t5 = $("#t5").css("top").split("px")[0] * 1;
					$("#t5").animate({top: t5 - 20, opacity: 1, width: 234}, 800);
					
					setInterval("changeIcons()", 2500);			
				})
			})
		})
	});
}

$(function() {
	$("#carousel ul").jcarousel({
		wrap: "circular",
		auto: 4,
		scroll: 1, 
		speed: 6000,
		initCallback: initCallback
	});
	
	$('#select').click(function(){
		var values = $("#frm_work").serialize();
		$.ajax({
				type: "post",
				url: "/addon/load.php",
				data: values,
				dataType: "json",
				success: function(data, textStatus) {
					if ( data.result )
					{
						$('#result').html('<div>Объем: '+ data.capacity +'м<sup>3</sup></div><div>Всего часов: '+ data.total +'ч</div><div>Производительность: '+ data.efficiency +'м<sup>3</sup>/ч</div><div>Рекомендуемая модель: <a href="'+ data.url +'" target="_blank">'+ data.model + '</a></div>');
					}
					else
					{
						alert('Заполните все поля!');
					}	
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					//TODO error
					alert(XMLHttpRequest.responseText);
					alert('Error: ' + textStatus + '; ' + errorThrown);
				}
			}); 
	});
});

function initCallback(carousel) {
	$('#c-next').bind('click', function() {
		carousel.stopAuto();
	    carousel.next();
	    return false;
	});
	
    $('#c-prev').bind('click', function() {
		carousel.stopAuto();
        carousel.prev();
        return false;
    });
	
	$("#carousel").hover(
		function() {
			carousel.stopAuto();
		},
		function() {
			carousel.startAuto();
		}
	);

}

function changeIcons() {
	var n = num + 1;
	if (num == 5) {
		n = 1;
	}
	var w1 = $("#t" + num).width();
	var w2 = $("#t" + n).width();
	$("#t" + num).animate({width: w1 - 100}, 300);
	$("#t" + n).animate({width: w2 + 100}, 300);
	
	if (num == 5) {
		num = 1;
	}
	else {
		num++;
	}	
}

