
function addFeatureListeners() {
	// Mouseover functions for selected feature elements.
	$(".feat_inactive").click(function(e) {
		var rowid = $(this).attr("r");
		var featid= $(this).attr("f");
		$("#feat_active").attr("src","/media/featureimg/tn/tn_"+rowid+".png");
		$("#selected_feat_value"+featid).val(rowid);
		var priceElem = $(this).parents(".shop_item").find(".price");
		var elemid = $(this).parents(".shop_item").find(".elemid").val();
		changePrice($(this).parents(".shop_item"),priceElem, elemid);
	});

	// If something changes in product feature selections, get the new price.
	$(".changeablefeature").change(function() {
		var priceElem = $(this).parents(".shop_item").find(".price");
		var elemid = $(this).parents(".shop_item").find(".elemid").val();
		changePrice($(this).parents(".shop_item"),priceElem, elemid);
	});
	
	// If something is clicked (in checkbox), get the new price
	$(".changeablefeature:checkbox").click(function() {
	
		var priceElem = $(this).siblings("#price");
		var elemid = $(this).siblings("#elemid").val();
		changePrice($(this).parents(".shop_item"),priceElem, elemid);
	});
	
	var changePrice = function(parent, priceElem, elemid) {
		priceElem.text("Loading...");
		var features = $(".changeablefeature",parent);
		var strs = new Array();
		strs.push(elemid);
		for (var i=0; i < features.length; i++) {
			var feat = features[i];
			if (feat.type == "checkbox" && !feat.checked) {
				// If the current feature is checkbox and if it is NOT checked, skip.
				// This is because only checked checkbox applies.
				continue;
			}
			if (feat != undefined) {
				var valObj = jQuery(feat).val(); 
				if (valObj != undefined) {
					strs.push(valObj);
					}
			}
		}
		var str = strs.join("_");
		var url = "/a/feat/getPrice/"+str;
		url += "?location="+defaultLocation;
		$.get(url, function(data) {
			var priceObj = null;
			try {
				priceObj = eval("("+data+")");
			} catch (err) {
				// Do nothing.
			}	
			if (priceObj != undefined) {
				$(priceElem).empty().append(priceObj.price+""+priceObj.currency);
				$(".warehouseSaldo",parent).empty();
				//.append(priceObj.warehouse_saldo);
				// Do the image stuff...
				var text = "";
				if (priceObj.warehouse_saldo.length > 5 && priceObj.warehouse_saldo != "Ei tietoa") {
						text = "Tulossa " + priceObj.warehouse_saldo + "kpl";
				} else {
						if (priceObj.warehouse_saldo > 0) {
								text = '<img class="warehousesaldo" alt="'+priceObj.warehouse_saldo+'" src="/media/img/green.png" />';
						} else {
								text = '<img class="warehousesaldo" alt="'+priceObj.warehouse_saldo+'" src="/media/img/red.png" />';
						}
				}
				if (priceObj.aware != undefined) {
						text += priceObj.aware;
				}
				$(".warehouseSaldo",parent).append(text);
			} else {
			   priceElem.text(data);
			}
		});
	}

}

function addBlogFilterListeners() {
		$("#wp_articleType").change(function() {
						// Change author and reload blog list accordingly
						var author_id = $("#wp_author").val();
						var sport = $("#wp_author").attr("sport");
						var articleType = $(this).val();
						var url = "/a/blog/_bloglist/"+author_id+"/"+sport+"/"+articleType+"/";
						$("#bloglist").load(url,{});

				});
		$("#wp_author").change(function() {
						// Change author and reload blog list accordingly
						var author_id = $(this).val();
						var sport = $(this).attr("sport");
						var url = "/a/blog/_bloglist/"+author_id+"/"+sport+"/";
						$("#bloglist").load(url,{});

		});
}

$(document).ready(function() {
			  $(".basket_field").autobox();
				//				addFeatureListeners();
				addBlogFilterListeners();


});


