// xajax loader

$(document).ready(function () {
	checkRequiredVisual();
	$(".required").blur(checkRequiredVisual);
	$(".valinta").change(calcSum);

	$(".hilight").hover(
		function(){
			$(this).css("background-position", "0 -" + $(this).css('height'));
		},
		function(){
			$(this).css("background-position", "0 0");
		}
	);

	$('.hover_product_img').hover(
	function(){
		var tmp = $(this).attr('src');
		$(this).attr('src', $(this).data('swapImg'));
		$(this).data('swapImg', tmp);
	},
	function(){
		var tmp = $(this).attr('src');
		$(this).attr('src', $(this).data('swapImg'));
		$(this).data('swapImg', tmp);
	});

});

function orderDone()
{
	$("#message_div").remove();
	$("<div />").attr("id", "message_div").addClass("ok").text("Tuote lisättiin ostoskoriin").appendTo("body").show().delay(4000).fadeOut("slow");
	$('#orderDone').show();
}

function calcSum()
{
	xajax_updateSum(xajax.getFormValues('orderForm'));
}

function checkRequiredVisual()
{
	$(".required").each(function(){
		if(this.value == undefined || this.value == ''){
			$(this).removeClass("required_ok");
			$(this).addClass("required_wrong");
		}
		else
		{
			$(this).removeClass("required_wrong");
			$(this).addClass("required_ok");
		}
	});
}

function checkRequired()
{
	var valid;
	valid = false;

	$(".required").each(function(){
		if(this.type == "checkbox")
		{
			if(!this.checked)
			{
				valid = false;
				alert("Arvo puuttuu: " + $(this).attr('rel'));
				return false;
			}
		}
		else
		{

			if(this.value == undefined || this.value == ''){
				valid = false;
				alert("Arvo puuttuu: " + $(this).attr('rel'));
				return false;
			}
		}
		valid = true;
		return true;
	});

	return valid;
}

