

(function($){

 $.fn.PriceSlider = function(options) {

 

	var defaults = {

    min : 0,

    max : 0,

	minAbs: 0,

	maxAbs: 0,

    displayZeroPrice : true,

    baseUrl : '',

    minPrice : 0,

    maxPrice : 0,

	

    templateWithoutItems  : '',

    templateWith1Item : '',

    templateWithItems : '',

    priceSteps : 1,

	prices: {} 

};

	var options = $.extend(defaults, options);

	

	var wprotpl = new Template(options.templateWithoutItems); 

	var pro1tpl = new Template(options.templateWith1Item ); 

	var prostpl = new Template(options.templateWithItems ); 



	

	var timeout = null;



	function countproducts(min, max) {

		if (options.prices) {			

			var counted = 0;

			jQuery.each(options.prices, function(i, item) {

				if (i > min - 1 && i < max + 1) {

					counted += item;

				}

			});

			if (counted == 1) {

				jQuery("#priceslide").show();

				jQuery("#priceslider-no-products").hide();

				jQuery("#priceslide").html(pro1tpl.evaluate({num:counted}));

			} else if(counted == 0) {

				jQuery("#priceslide").hide();

				jQuery("#priceslider-no-products").show();			

			} else {

				jQuery("#priceslide").show();

				jQuery("#priceslider-no-products").hide();

				jQuery("#priceslide").html(prostpl.evaluate({num:counted}));

			}

		}

	}

	

	function formatPrice(price)

	{

		return formatCurrency(price, options.priceFormatJs);

	}

 

	jQuery(document).ready(function() {

			jQuery("#priceslider-no-products").hide();

 

			if (options.prices)

			{

				countproducts(options.min, options.max);
				if (options.min == options.max)
				{
					var data  = {min: formatPrice(options.min - options.priceSteps), max: formatPrice(options.max) };

					jQuery("#amount").html(wprotpl.evaluate(data));
					jQuery("#priceslide").hide();
				}
				else {
				if (options.min == -1 & options.max == 0)
				{
					jQuery("#priceslide").hide();
				}
				else
				{

					jQuery("#priceslider").slider({

						range: true,

						min: options.minAbs, 

						step: options.priceSteps, 

						max: options.maxAbs, 

						values: [options.min, options.max],

				

						stop: function(event, ui) {

							countproducts(jQuery("#priceslider").slider("values", 0),jQuery("#priceslider").slider("values", 1) );	

						},

						slide: function(event, ui) {

							var data  = {min: formatPrice(Math.floor(ui.values[0])), max: formatPrice(Math.ceil(ui.values[1])) };

							jQuery("#amount").html(wprotpl.evaluate(data));

							countproducts(ui.values[0], ui.values[1]);

						}

					});

			

					var data  = {min: formatPrice(jQuery("#priceslider").slider("values", 0) ), max: formatPrice(jQuery("#priceslider").slider("values", 1) ) };

					jQuery("#amount").html(wprotpl.evaluate(data));

		 

					jQuery("#priceslide").click(function(){

						window.location = jQuery.jqURL.set({ 'min':jQuery("#priceslider").slider("values", 0),'max':jQuery("#priceslider").slider("values", 1)});

						return false;

					});
				}
			  }
 
			}

		});

	return this;

 };



})(jQuery); 

 
