$(document).ready( function () {
	
	
	//init images
	var marginParam = 0.86;
	//var sizeParam = 0.9;
	
	
	
	var countImgs = $("#carousel img").length;
	var sizeParam = Math.pow(0.94, Math.ceil(countImgs/3));
	var imgMedian = (countImgs+1)/2;
	var contWidth = $("#carousel").parent().css("width");
	
	
	var i=1;
	var originalWidth = Array();
	var originalHeight = Array();
	var currentWidth = Array();
	var currentHeight = Array();
	var imgWidthSum = 0;
	var positions = Array();
	var leftParams = Array();
	
	
	
		$("#carousel img").each( function() {
			originalWidth[i] = $(this).attr("width");
			originalHeight[i] = $(this).attr("height");
			$(this).attr("name", "car"+i);
			
			//zaokraglenie do pelnego piksela, 0.8^n daje przeskalowanie, gdzie n to odleglosc od srodkowego elementu
			currentHeight[i] = Math.round(Math.pow(sizeParam, (Math.floor(Math.abs(imgMedian-i)))) * originalHeight[i]);
			currentWidth[i] = Math.round(Math.pow(sizeParam, (Math.floor(Math.abs(imgMedian-i)))) * originalWidth[i]);
			imgWidthSum += currentWidth[i];
			
			positions[i] = i;
			
			i++;
		});
	
	
	var itemSpace = Math.round((parseInt(contWidth) - imgWidthSum)/(countImgs-1));
	
	
		var suma=0;
	
	if(countImgs==3)
	{
		 itemSpace = imgWidthSum - (2*originalWidth[1]);
		 suma = 430 - (2*originalWidth[1]);;
	}
		if(countImgs==2)
	{
		 itemSpace = imgWidthSum - originalWidth[1];
		 suma = 430 - originalWidth[1];
	}
	if(countImgs==1)
	{
		 itemSpace = imgWidthSum - originalWidth[1];
		 suma = 430 - (originalWidth[1]/2);
	}
	
	var numToId = Array();
	
	for(var k = 1; k<=countImgs; k++)
	{
		leftParams[k] = suma;
		$("img[name=car" +  k+"]").animate({"height": currentHeight[k], "width": currentWidth[k], "left": suma, "opacity": 1/(Math.floor(Math.abs(imgMedian-k)+1)), "marginTop" : Math.pow(marginParam, (Math.abs(imgMedian-k)+2)) * (originalHeight[k]-currentHeight[k])}, 50);
		suma += (currentWidth[k]+itemSpace);
			
	}
	
	updateMouseOvers();
	
	function MoveImgsLeft (durationFactor) {
	
		var effectTime = 1000/durationFactor;
	
		var nCurrentHeight = Array();
		var nCurrentWidth = Array()	
		var newPositions = Array();
		var ctrl = "";
		newPositions[countImgs] = positions[1];
		//$("img[name=car" +   positions[1] +"]").fadeOut("fast");
		
		nCurrentHeight[positions[1]] = currentHeight[countImgs];
		nCurrentWidth[positions[1]] = currentWidth[countImgs];
		
		for(var j = 2; j<=countImgs; j++)
		{
			var pos = positions[j];
			var npos = positions[j-1];
			
			ctrl += pos+" - cuur:" + currentHeight[pos] + " - prev:" + currentHeight[npos] + " ";
			
			$("img[name=car" +   pos +"]").animate({"height": currentHeight[npos], "width": currentWidth[npos],"left": leftParams[j-1], "opacity": 1/(Math.floor(Math.abs(imgMedian-j+1)+1)), "marginTop" : Math.pow(marginParam, (Math.abs(imgMedian-(j-1))+	2	)) * (originalHeight[npos]-currentHeight[npos])}, effectTime);
			nCurrentHeight[pos] = currentHeight[npos];
			nCurrentWidth[pos] = currentWidth[npos];
			newPositions[j-1] = pos;
		}
	
		//alert(ctrl);
		pos = newPositions[countImgs];
		nCurrentHeight[pos] = currentHeight[positions[countImgs]];
		nCurrentWidth[pos] = currentWidth[positions[countImgs]];
		
		//$("img[name=car" +  pos +"]").css("left", "1050");
		$("img[name=car" +  pos +"]").animate({"height": nCurrentHeight[pos], "left": leftParams[countImgs], "opacity": 1/(Math.floor(Math.abs(imgMedian-countImgs)+1)), "marginTop" : Math.pow(marginParam, (Math.abs(imgMedian-j+1)+2)) * (originalHeight[pos]-currentHeight[pos])}, effectTime);
		//$("img[name=car" +  pos +"]").fadeTo("slow", 1/(Math.abs(imgMedian-countImgs+1)+1));
		positions = newPositions;
		currentHeight = nCurrentHeight;
		currentWidth = nCurrentWidth;
		
		updateMouseOvers();
		
	}
	
	function MoveImgsRight (durationFactor) {
	
		var effectTime = 1000/durationFactor;
	
		var nCurrentHeight = Array();
		var nCurrentWidth = Array()	
		var newPositions = Array();
		var ctrl = "";
		newPositions[1] = positions[countImgs];
		//$("img[name=car" +   positions[1] +"]").fadeOut("fast");
		
		nCurrentHeight[positions[countImgs]] = currentHeight[1];
		nCurrentWidth[positions[countImgs]] = currentWidth[1];
		
		for(var j = 1; j<countImgs; j++)
		{
			var pos = positions[j];
			var npos = positions[j+1];
			
			ctrl += pos+" - cuur:" + currentHeight[pos] + " - prev:" + currentHeight[npos] + " ";
			
			$("img[name=car" +   pos +"]").animate({"height": currentHeight[npos], "width": currentWidth[npos],"left": leftParams[j+1], "opacity": 1/(Math.floor(Math.abs(imgMedian-j-1)+1)), "marginTop" : Math.pow(marginParam, (Math.abs(imgMedian-(j+1))+	1	)) * (originalHeight[npos]-currentHeight[npos])}, effectTime);
			nCurrentHeight[pos] = currentHeight[npos];
			nCurrentWidth[pos] = currentWidth[npos];
			newPositions[j+1] = pos;
		}
	
		//alert(ctrl);
		pos = newPositions[1];
		nCurrentHeight[pos] = currentHeight[positions[1]];
		nCurrentWidth[pos] = currentWidth[positions[1]];
		
		//$("img[name=car" +  pos +"]").css("left", "1050");
		$("img[name=car" +  pos +"]").animate({"height": nCurrentHeight[pos], "left": leftParams[1], "opacity": 1/(Math.floor(Math.abs(imgMedian)+1)), "marginTop" : Math.pow(marginParam, (Math.abs(imgMedian-j+1))) * (originalHeight[pos]-currentHeight[pos])}, effectTime);
		//$("img[name=car" +  pos +"]").fadeTo("slow", 1/(Math.abs(imgMedian-countImgs+1)+1));
		positions = newPositions;
		currentHeight = nCurrentHeight;
		currentWidth = nCurrentWidth
		
		updateMouseOvers();
	}
	
	function updateDescription (id) 
	{
		$.get("/products/get_desc/" + id + "/cat", function(data){
 			 $("#product_desc").html(data);
		});

	}
	
	$("#carousel img").click ( function (){
		
		var elNumber = $(this).attr("name").replace("car", "");
		for(i in positions)
		{
			if(positions[i] == elNumber) var clicked = i;
			
		}
		var numberMovePlaces = Math.abs(imgMedian - clicked);
		if(clicked < imgMedian) {
			for(var k = 1; k<=numberMovePlaces; k++) MoveImgsRight(numberMovePlaces);	
			updateDescription($("img[name=car" +  positions[Math.floor(imgMedian)] +"]").attr("id"));
		}
		if(clicked > imgMedian) {
			for(var k = 1; k<=numberMovePlaces; k++) MoveImgsLeft(numberMovePlaces);
			updateDescription($("img[name=car" +  positions[Math.floor(imgMedian)] +"]").attr("id"));	
		}
		if(clicked == Math.floor(imgMedian) || clicked == Math.floor(imgMedian)+1)
		{
			if(!((clicked == Math.floor(imgMedian)+1) && (Math.floor(imgMedian)==imgMedian))) window.location = "/produkt/"+ $(this).attr("id") + "/l";
		}

	 });

	$("#car_prev").click (function (){
		MoveImgsRight(1);
		
	})
	
	$("#car_next").click (function (){
		MoveImgsLeft(1);
		updateDescription($("img[name=car" +  positions[Math.floor(imgMedian)] +"]").attr("id"));
	})
	
	
	function updateMouseOvers()
	{
		
		$("img").each (function () { $(this).unbind("mouseover"); });
		
		$("img[name=car" +  positions[Math.floor(imgMedian)] +"]").mouseover( function () {
			updateDescription($(this).attr("id"));
			$("#product_desc").show();
		});
		$("img[name=car" +  positions[Math.floor(imgMedian)] +"]").mouseout( function () {
			
			$("#product_desc").hide();
			$("#product_desc").html("");
			
		});
		
		$("img[name=car" +  positions[Math.floor(imgMedian)+1] +"]").mouseover( function () {
			if(Math.floor(imgMedian) != imgMedian) 
			{
				updateDescription($(this).attr("id"));
				$("#product_desc").show();
				
			}
		});
		
		$("img[name=car" +  positions[Math.floor(imgMedian)+1] +"]").mouseout( function () {
			
			$("#product_desc").hide();
			$("#product_desc").html("");
			
		});
	}
});






