var aPrices = new Array();

$(document).ready(function() {

	$("#exm_ps_minprice").html(exm_ps_current_minvalue);
	$("#exm_ps_maxprice").html(exm_ps_current_maxvalue);
	$("#exm_ps_form_minprice").val(exm_ps_current_minvalue);
	$("#exm_ps_form_maxprice").val(exm_ps_current_maxvalue);
	
	$.getJSON("exm_priceslider/ajax_funcs.php?method=get_product_count&catid="+catid+
		"&minprice="+exm_ps_min+"&maxprice="+exm_ps_max, 
		function(json) {
			$.each(json.products, function(i,product)
			{
				aPrices.push(product.price);
			});
			
			exm_ps_countproducts(exm_ps_current_minvalue, exm_ps_current_maxvalue);
			$("#exm_ps_loading_panel").hide();
			$("#exm_ps_active_panel").show();
	});

});

$(function() {
	$("#exm_priceslider_range").slider({
		range: true,
		min: exm_ps_min,
		max: exm_ps_max,
		step: exm_ps_step,
		values: [exm_ps_current_minvalue, exm_ps_current_maxvalue],
		slide: function(event, ui) {
			$("#exm_ps_minprice").html(ui.values[0]);
			$("#exm_ps_maxprice").html(ui.values[1]);
			
			$("#exm_ps_form_minprice").val(ui.values[0]);
			$("#exm_ps_form_maxprice").val(ui.values[1]);
			
			exm_ps_countproducts(ui.values[0], ui.values[1]);
		}
	});
});

function exm_ps_countproducts(pricemin, pricemax)
{
	var productcount = 0;
	
	for (var i = 0; i < aPrices.length; i++)
	{
		if ((aPrices[i] >= pricemin) && (aPrices[i] <= pricemax))
			productcount++;
	}
	
	$("#exm_ps_productcount_label").html(productcount);
	if (productcount > 0)
	{
		$("#exm_ps_products_available").show();
		$("#exm_ps_no_products_available").hide();
	}
	else
	{
		$("#exm_ps_products_available").hide();
		$("#exm_ps_no_products_available").show();
	}
}

