var loading_div = "<div class=\"loader\"><span>Loading&hellip;</span></div>";
function express_filter_clicked(current, other, input)
{
	if (input.attr("checked")){
		other.find("input").attr("disabled", true);
		other.find("label").toggleClass("disabled", true);
	}else {
		if (current.find("input[checked]").length == 0)
		{
			other.find("input").attr("disabled", false);
			other.find("label").toggleClass("disabled", false);
		}
	}
}
(function($) {
    $(function() {
	$(".carousel").scrollable().each(function (){
		if ($(this).find(".carousel-view").length > 1)
		{
			$(this).parent().find("a.browse.right").toggleClass("disabled", false);
		}
	});
    	$('div.option input').attr("autocomplete", "off");
    	var expressdeliveryform = $("form[name='filter'][action='sofas/express-delivery/']");
    	if (expressdeliveryform.length > 0)
    	{
    		var sofa_types = expressdeliveryform.find("fieldset.filter-grp:nth-child(2) div.option");
    		var chair_types = expressdeliveryform.find("fieldset.filter-grp:nth-child(3) div.option");
    		sofa_types.each(function (){
    			var input = $(this).find("input");
    			input.click(function (){ express_filter_clicked(sofa_types, chair_types, input) });
    			express_filter_clicked(sofa_types, chair_types, input);
    		});
    		chair_types.each(function (){
    			var input = $(this).find("input");
    			input.click(function (){ express_filter_clicked(chair_types, sofa_types, input) });
    			express_filter_clicked(sofa_types, chair_types, input);
    		});

		$('a.remove-filter').click(function (){
			$('div.option input').attr("disabled", false);
			$('div.option label').toggleClass("disabled", false);
    		});
    	}


    	// set the ajax field to 'true', so we can differentiate an AJAX request from a non-JS POST request
    	$("form[name='filter']").append("<input type=\"hidden\" name=\"ajax_req\" id=\"ajax_req\" value=\"1\" />");

		$("form[name='filter'] input").each(function(input, elem) {
			if ($(elem).attr("readonly")) {
				$(elem).attr("disabled", true);
			}
		});

		// remove filter button
		$("#product-filter .head a.remove-filter").live("click", function() {

			// set all inputs to be unchecked
			$("form[name='filter'] input").each(function() {
				if (!($(this).attr("disabled"))) {
					$(this).attr("checked", false);
				}
			});

			// back to standard pagination
			if ($(".page-navigation").hasClass("has-ajax")) {
				$(".page-navigation").removeClass("has-ajax");
			}

			// update results
			updateResults($("form[name='filter']"), true);

			// remove 'remove' link
			$(this).addClass("hidden");
			return false;
		});

		$("#product-list .no-results a.remove-filter").live("click", function() {
			// set all inputs to be unchecked
			$("form[name='filter'] input").each(function() {
				if (!($(this).attr("disabled"))) {
					$(this).attr("checked", false);
				}
			});

			// back to standard pagination
			if ($(".page-navigation").hasClass("has-ajax")) {
				$(".page-navigation").removeClass("has-ajax");
			}

			// update results
			updateResults($("form[name='filter']"), true);

			return false;
		});

		// update results on any form changes
		$("form[name='filter']").change(function() {
			updateResults(this);
		});
 	});

	// live pagination
 	$(".page-navigation ul li a").live("click", function(event) {
		// create the input element (if not already existing) so it can be posted
		var new_page_number = $(this).attr("rel");
		if ($("form[name='filter'] input[name='page_number']").length > 0) {
			$("form[name='filter'] input[name='page_number']").attr("value", new_page_number);
		} else {
			$("form[name='filter']").append("<input type=\"hidden\" name=\"page_number\" id=\"page_number\" value=\""+ new_page_number +"\" />");
		}

		// if we have clicked bottom pagination, move up to head of page
		var pagination_block = $(this).parent().parent().parent();
		if (pagination_block.hasClass("bottom") || pagination_block.parent().hasClass("bottom")) {
			$("html, body").animate({scrollTop:0}, "slow");
		}

		// update results
		updateResults($("form[name='filter']"), null, new_page_number);

		return false;
	});


	function updateResults(form, revert_pagination, page) {
		if (!page) {
			page = 1;
		}

		var is_ie6 = ($('html').hasClass('ie6'));
		var product_list = $((is_ie6?'.ie6 ':'') + '#product-list');

		// show loader
		if (product_list.find('.loader').length == 0) {
			product_list.append(loading_div);

			// fix height for IE6
			if (is_ie6) {
				product_list.find('.loader').height(product_list.height() + 'px');
				product_list.find('.loader span').height(product_list.height() + 'px');
			}
		}

		// hide pagination temporarily
		//$(".page-navigation").html("");

		// format POST data
		var data = $(form).serialize();

		// add remove filter link (only if not there already!)
		if ($("#product-filter a.remove-filter").hasClass("hidden")) {
			$("#product-filter a.remove-filter").removeClass("hidden");
		}

		$.post(relative_url, data, function(response) {

			if (response.filter_removed) {
				$("#product-filter .head a.remove-filter").addClass("hidden");
			}

			// update pagination
			if (response.pagination) {
				// do we need to create the pagination element?
				if ($(".page-navigation.top").length == 0) {
					$(response.pagination).insertBefore("#product-filter");
				} else {
					// no, lets just update whats there already
					$(".page-navigation.top").replaceWith(response.pagination);
				}
				// do we need to create the pagination element?
				if ($(".page-navigation.bottom").length == 0) {
					$(response.lower_pagination).insertAfter("#product-list");
				} else {
					// no, lets just update whats there already
					$(".page-navigation.bottom").replaceWith(response.lower_pagination);
				}

				// reset page number field
				if ($("form[name='filter'] input[name='page_number']").length > 0) {
					$("form[name='filter'] input[name='page_number']").attr("value", 1);
				}
			} else {
				// remove pagination
				$(".page-navigation").html("");
			}

			// update product list
			product_list.html(response.html + loading_div);

      if (response.alternateList)
      {
        $("#product-list").append(response.alternateList);
      }

			if (response.carousels)
			{
				if (response.range_count <=9)
				{
					$("#smaller-carousel").html(response.carousels);
					$("#larger-carousel").empty();
				}else {
					$("#larger-carousel").html(response.carousels);
					$("#smaller-carousel").empty();
				}

			}else {
				$("#larger-carousel").empty();
				$("#smaller-carousel").empty();
			}

			$(".carousel").scrollable().each(function (){
				if ($(this).find(".carousel-view").length > 1)
				{
					$(this).parent().find("a.browse.right").toggleClass("disabled", false);
				}
			});

			// update adInsight
			$("#product-list .adinsightNumber491").html($("#order-direct-contact-telephone").html());

			product_list.ajaxStop(function() {
				// hide loader after all requests completed
				$("#product-list .loader").remove();

				// ie6 png fix
				if (is_ie6 && typeof ie6_pngbg_fix == 'function') {
					ie6_pngbg_fix();
				}
			});
		});
	}

})(jQuery);

