$(document).ready(function(){

	//Shorten Article description text and add "read more" link
	$('#ArticleDescription').readmore({breakOnBr:true, moreText:"<br><br>Lue lisää...", ellipses:""});
	$('.readMoreBreakOnBr').readmore({breakOnBr:true, moreText:"Lue lisää...", ellipses:""});

	// 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"); });
	
	
	// Update count in buy-button
	var displays = $("select[name^=COUNT_], select[name=COUNT]");
	var articleCount = $("#articleCount");
	var articleCountRefresh = function()
	{
		var count = 0
		displays.each(function(i, em) {count+=parseInt(em.value||0);});
		articleCount.html(count);
	};
	displays.change(articleCountRefresh);
	articleCountRefresh();
});
