// slider
$(document).ready(function() {
	
	$("#slider-range").slider({
		range: true,
		min: 0,
		max: 80,
		//step: 10,
		values: [lowerPrice, upperPrice],
		slide: function(event, ui) {
			$("#amount").val('$' + ui.values[0] + ' - $' + ui.values[1]);
		}
	});
	$("#amount").val('$' + $("#slider-range").slider("values", 0) + ' - $' + $("#slider-range").slider("values", 1));
	
	
	$("#slider-range").bind( "slidechange", function(event, ui) {
		getProducts();
		});

	
	// type filter
	$('.gift_filter li a').click(function(event) {

		// cancel the link
		event.preventDefault();
		
		type = $(this).attr('class');
		
		getProducts();
		
		// Set active
		$('.gift_filter li').attr('class', '');
		$(this).parent().attr('class', 'active');
	});
	
	
	// retrieve products
	function getProducts() {
		
		$('.group .block').remove();
		$('#loading').remove();
		$('#content').append("<div id='loading'><p>Loading...</p><img src='/images/ajax-loader.gif'/></div>");
		
		
		var lowerPrice = $("#slider-range").slider("values", 0);
		var upperPrice = $("#slider-range").slider("values", 1);
		var cat = $('ul.category_menu li.active a').attr('id').replace('menu-', '').replace('all', '');
		var popular = $('ul.category_menu li.active a').attr('id').replace('menu-', '').replace('all', '');
		if(isNaN(popular))
		{
			popular = 'true';
			cat = '';
		}
		else
		{
			popular = '';
		}
		$.get("/products/list",
				{ p0: lowerPrice, p1: upperPrice, keywords: $('#keywords').val(), type: type, category: cat, popular: popular },
			   function(data){
						
					$('#loading').remove();
					
					// ensure blocks are removed if double-load
					$('.group .block').remove();
					
					$('#content').append(data);
			     
			     $('.product_cell').hide();
			     $('.product_cell').each(function(index) {
			    	 $(this).delay(index*200).fadeIn();
			    	  });
			   });
	}
});
