$(function() {
	/* submenu */
	$("#smallbasket").load("/arajax/basket", {});
	
	$(".autosubmitOnEnter").keydown(function(event) {
		if (event.keyCode == 13) $("#loginform").submit();
	});
	
	$(".arloginfield").focus(function() {
		$(this).parent().hide();
		$(this).val("");
		$(".usrfield").show();
		$(".usrfield input").focus();
	});
	
	$(".arpwdfield").focus(function() {
		$(this).parent().hide();
		$(this).val("");
		$(".pwdfield").show();
		$(".pwdfield input").focus();
	});
	
	$(".menuitemcontainer").mouseover(function() {
		$(this).stopTime();
		$(this).children(".submenu").show();
	});
	$(".menuitemcontainer").mouseout(function() {
		$(this).oneTime(1000, function() {
			$(this).children(".submenu").hide();
		})
	});

	$("#paymentform").submit();
	
	$(".prodpicsmall").click(function() {
		var s = $(this);
		var l = $(".prodpiclarge");
		var sid = s.attr("id");
		var lid = l.attr("id");
		s.attr("id", lid);
		l.attr("id", sid);
		s.children("img").attr("src", "/image/show/?pid=" + lid + "&width=" + parseInt(s.width()) + "&height=" + parseInt(s.height()));
		l.children("img").attr("src", "/image/show/?pid=" + sid + "&width=" + parseInt(l.width()) + "&height=" + parseInt(l.height()));
	});
	
	$(".prodpiclarge").click(function() {
		showLb($(this).attr("id"));
	});	
	
	$(".choicemenus").load("/arajax/pdmenu", {prodid: $(".choicemenus").attr("id")}, function() {
		refreshListeners();
	});
	
	$(".sortmenu").change(function() {
		var currenturl = window.location.toString();
		var urlparts = currenturl.split("?");
		var url = urlparts[0] + "?sort=" + $(this).val();
		document.location.href = url;
	});
	
	if ($(".decorationimage").attr("id")) loadNewPic();
	$(".quantityfield, .kuljetusfield").change(function() {
		//$(".kassabutton").hide();
		$(".kassabutton").addClass("arbuttondim");
		$(".kassabutton").attr("href", "javascript:updateWarning()");
	});
});

function updateWarning()
{
	alert("Ole hyvä ja päivitä ostoskori ennen jatkamista kassalle!");
}

function loadNewPic() {
	/*
	$(".preloader").load("/arajax/newdecoration", {oldid: $(".decorationimage").attr("id")}, function() {
		$(".decoration").oneTime(30000, function() {
			$(this).html($(".preloader").html());
			loadNewPic();
		});
	});
	*/
}

function refreshListeners()
{
	$(".prodchoice select").each(function() {
		$(this).change(function() {
			var choices = new Array();
			$(".prodchoice select").each(function() {
				choices.push($(this).val());
			});
			var dataString = $.toJSON(choices);
			$(".choicemenus").load("/arajax/pdmenu", {prodid: $(".choicemenus").attr("id"), choices: dataString}, function() {
				refreshListeners();
			});
		});
	});	
}

function addToBasket() {
	var prodid = $(".prodcode").attr("id");
	var prodamount = $("#prodamount").val();
	//$("#smallbasket").load("/arajax/basket", {addprod: prodid, addamount: prodamount});
	$.post("/arajax/basket", {addprod: prodid, addamount: prodamount}, function(data) {
		if (data.length < 10) {
			$("#prodamount").val(data);
			$(".saldowarning").show();
		}
		else {
			$("#smallbasket").html(data);
			$("#prodamount").val("1");
			$(".saldowarning").hide();
		}
	});
}

function checkShopForm() {
	var oktosend = true;
	if ($("#paymentmethod").val() == "0") oktosend = false;
	if (oktosend) {
		$("#checkoutform").attr('action', '/basket/payment');
		$("#checkoutform").submit();
	}
	else alert($("#missingpmttype").val());
}
