$(document).ready(function()
{
	// Tipafriend
	$("a.popup-dialog").click(function(evt)
	{
		var popupDialog = window.open(this.href,"_popup-window","width=700,height=300,scrollbars=no,status=no,resizable=no");
		return !popupDialog;
	});
	
	// Ugly color thing
	var selectsLeft = $("select.parameter_LEFT");
	var selectsRight = $("select.parameter_RIGHT");
	var updateImage = function(side)
	{
		var root = imgurl + "/";
		var idx = this.selectedIndex;
		var option = this.options[idx];
		var url = option.attributes["img"];
		if(url)
		{
			var target = $("#displayinfo_" + side);
			$("#displayinfo_pre").html("");
			
			if(url.value) target.html("<img src='" + root + url.value + "/MINITHUMB.JPG' style='width:100px;'/>");
			else target.html("");
		}
	}
	selectsLeft.change(function() { updateImage.call(this, "LEFT"); });
	selectsRight.change(function() { updateImage.call(this, "RIGHT"); });
	
	
	// Price calculation 
	var prices = $("#article-pricelist")[0].value.split("|");
	var displays = $("select[name~=COUNT_], select[name=COUNT]");
	var displayTotal = $("#showSum")[0];
	
	var priceRefresh = function()
	{
		var count = 0, priceCount = 0, pricePerArticle;
		displays.each(function(i, em) {count+=parseInt(em.value||0);});
		priceCount = Math.min(prices.length-1, Math.max(0, count));
		pricePerArticle = prices[priceCount];
		
		displayTotal.value = (pricePerArticle*count) + " kr";
	};
	displays.change(priceRefresh);
	priceRefresh();
});