$( function () {
	$("div.price_min, div.price_max").show();
	
	$values = [ $("div#filterPriceSlider").attr('data-selected-min'), $("div#filterPriceSlider").attr('data-selected-max') ];
	$min = parseInt($("div#filterPriceSlider").attr('data-min'));
	$max = parseInt($("div#filterPriceSlider").attr('data-max'));
	$("div#filterPriceSlider").slider({
		range: true,
		min: $min,
		max: $max,
		values: $values,
		slide: function( event, ui ) {
			updateSliderPrices(ui);
		},
		stop: function ( event, ui ) {
			updateSliderPrices(ui);
			resetPageAndList();
			$(this).parents('form:eq(0)').submit();
		}
	});
	
	$("ul.filter_list li").click( function (event) {
		var target = event.target || event.srcElement || event.originalTarget;
		if(!$(target).is('input')) {
			if($(this).find('input').is(':checked')) {
				$(this).find('input').attr('checked', '');
			} else {
				$(this).find('input').attr('checked', 'checked');
			}
		}
		resetPageAndList();
		$(this).parents('form:eq(0)').submit();
	}).css('cursor', 'pointer');
	
	$("form[name=filter_options]").submit( function () {
		$categories_description_bottom = '';
		$breadcrumb = $("div#breadcrums").clone();
		$breadcrums_bg = $("div#breadcrums_bg").clone();
		$categories_description = $("div.categories_info:first").clone();
		if($("div.categories_info").length == 2) $categories_description_bottom = $("div.categories_info:last").clone();
		
		$data = $(this).serialize();
		$.ajax({
			url: 'filter_file.php',
			type: 'GET',
			data: $data,
			success: function (data) {
				$("div#content").html(data);
				$("div#loading_splash").remove();
				
				$categories_description.prependTo("div#content");
				$breadcrumb.prependTo("div#content");
				$breadcrums_bg.prependTo("div#content");
				if($categories_description_bottom) $categories_description_bottom.appendTo("div#content");
			},
			beforeSend: function () {
				$('<div id="loading_splash"></div>').appendTo('body');
				$("div#loading_splash").css({
					height: $("div#content").outerHeight(),
					width: $("div#content").outerWidth(),
					top: $("div#content").offset().top,
					left: $("div#content").offset().left,
					opacity: 0.5
				});
			}
		});
		
		return false;
	});
	
	$visible_icon = '&#x25BC;';
	$hidden_icon = '&#x25C4;';
	$(".filter_level_1:visible").prev().find('span.filter_heading_toggle').html($visible_icon);
	$(".filter_level_1:hidden").prev().find('span.filter_heading_toggle').html($hidden_icon);
	$("div.filter_heading").css('cursor', 'pointer').click( function () {
		if($(this).next().is(':visible')) {
			$(this).find("span.filter_heading_toggle").html($hidden_icon);
			$(this).next().stop(true, true).slideUp('fast');
		} else {
			$(this).find("span.filter_heading_toggle").html($visible_icon);
			$(this).next().stop(true, true).slideDown('fast');
		}
	});
	
	$("a.pageResults, table.sortby a").live('click', function (event) {
		$url = $(this).attr('href');
		
		$url = $url.split('?');
		
		$params = $url[1].split('&');
		$.each($params, function($index, $param) {
			$param = $param.split('=');
			if($param[0] == 'page') $("form[name=filter_options] input[name=page]").val($param[1]);
			if($param[0] == 'list') $("form[name=filter_options] input[name=list]").val($param[1]);
		});
		
		$("form[name=filter_options]").trigger('submit');
		
		event.preventDefault();
		return false;
	});
});

function resetPageAndList() {
	$("form[name=filter_options] input[name=page], form[name=filter_options] input[name=list]").val('');
}

function updateSliderPrices(data) {
	$(".price_min").html('&euro;' + data.values[0])
	$(".price_max").html('&euro;' + data.values[1]);
	$("input[name=price\[min\]]").val( data.values[0] );
	$("input[name=price\[max\]]").val( data.values[1] );
}
