function getAfmeting() {
	var options = {};

	var value	= $("#afmeting_option").val();
	var data	= value.split(':');

	if(data[1] == undefined) {
		data[1] = 0;
	}

	options.id		= data[0];
	options.price	= parseFloat(data[1]);

	return options;
}

function getColor() {
	var options = {};

	var value	= $("#color_option").val();
	var data	= value.split(':');

	if(data[1] == undefined) {
		data[1] = 0;
	}

	options.id		= data[0];
	options.price	= parseFloat(data[1]);

	return options;
}

function getType() {
	var options = {};

	var value	= $("#type_option").val();
	var data	= value.split(':');

	if(data[1] == undefined) {
		data[1] = 0;
	}

	options.id		= data[0];
	options.price	= parseFloat(data[1]);

	return options;
}

function animate(element)
{
	var last_margin = $(element).css("margin-left");
	if(last_margin == null) {
		last_margin = "0px";
	}
	var new_margin = parseFloat(last_margin.substr(0, last_margin.length-2)) + 5;
	
	$(element).animate({"margin-left": new_margin.toString() + "px"}, 100, function(){
		$(element).animate({"margin-left": "0px"}, 100);
	});
}

$(document).ready(function(){
	var index = 0;
	var images = $("#gallery img");
	var thumbs = $("#thumbs img");
	var imgHeight = $(thumbs).attr("height");
	var currimg = null;
	var firstscroll = 0;
	var hovering = 0;
	
	//$(thumbs).slice(0,3).clone().appendTo("#thumbs");
	//for (i=0; i<thumbs.length; i++)
	//{
	//	$(thumbs[i]).addClass("thumb-"+i);
	//	$(images[i]).addClass("image-"+i);
	//}
	
	
	show(index);
	setInterval(sift, 6000);
	
	function sift()
	{
		if (index<(thumbs.length-1)) {
			index += 1; 
		} else {
			index = 0;
		}
		show (index);
	}
	
	function show(num)
	{
		if(hovering == 0) {
			if(currimg == num) {
				return;	
			}		
			$(images).fadeOut(firstscroll);
			$(".image-"+num).fadeIn(firstscroll);
			currimg = num;
			
			//var scrollPos = (num+1)*imgHeight;
			//$("#thumbs").stop().animate({scrollTop: scrollPos}, 400);		
			//console.log(scrollPos, "img.image-"+num);
			
			firstscroll = 250;
		}
	}
	
	$(".thumb-0").mouseover(function(){
		hovering = 0;
		show(0);
		hovering = 1;
	});
	$(".thumb-1").mouseover(function(){
		hovering = 0;
		show(1);
		hovering = 1;
	});
	$(".thumb-2").mouseover(function(){
		hovering = 0;
		show(2);
		hovering = 1;
	});
	$(".thumb-3").mouseover(function(){
		hovering = 0;
		show(3);
		hovering = 1;
	});
	$(".thumb-0").mouseout(function(){
		hovering = 0;
	});
	$(".thumb-1").mouseout(function(){
		hovering = 0;
	});
	$(".thumb-2").mouseout(function(){
		hovering = 0;
	});
	$(".thumb-3").mouseout(function(){
		hovering = 0;
	});
	
	
	change();
	
	function change(){
		
		var afmeting	= getAfmeting();
		var pid			= afmeting.id;
		var price		= afmeting.price;

		if($("#type_option").length !== 0)
		{
			var type	= getType();
			pid			= pid.substr(0, pid.length - 2) + type.id + '.';
			price		+= type.price;
		}

		var color	= getColor();
		pid			+= color.id + '.';
		price		+= color.price;
		
		//alert('hi');
		
		var halfprice = price / 2;
		
		var dp = halfprice / 0.2;
		dp = Math.ceil(dp);
		
		halfprice = halfprice.toFixed(2);
		halfprice = halfprice.replace('.', ',');
        
		var designpoints = price * (document.getElementById('dp_percents').value / 100) / 0.2;
		designpoints = Math.floor(designpoints);
		
		price = price.toFixed(2);
		price = price.replace('.', ',');
		
		$.post('/test/getprice/' + pid, function(data) {
			if(parseFloat(price) > parseFloat(data)) {
				$("#price").html("&euro; " + data);
				$("#halfprice").html("&euro; " + data);
				$("#dp").html(0);
				$("#product_id").html(pid);
				$("#designpoints").html(0);
			} else {
				$("#price").html("&euro; " + price);
				$("#halfprice").html("&euro; " + halfprice);
				$("#dp").html(dp);
				$("#product_id").html(pid);
				$("#designpoints").html(designpoints);
			}
	
		});
				
		animate("#price");
		animate("#product_id");

	}
	
	$("#afmeting_option").change(change);
	$("#color_option").change(change);
	$("#type_option").change(change);

});

