$(document).ready(function() {
	
	// show number of found products
	$('#pc-product_filter select, #pc-product_filter input[type="radio"]').bind('change', function(){
		getNumberOfProducts($(this));
	});
	
	// add highlight for results
	/*
	$('#pc-result_list tr td:not(.select, .document)').toggle(
		function(){
			$(this).parent().addClass("active");
			$(this).parent().find('input').attr('checked', true);
		},
		function(){
			$(this).parent().removeClass("active");
			$(this).parent().find('input').attr('checked', false);
		}
	);
	*/
	
	$('#pc-result_list tr td:not(.select):not(.document)').toggle(
		function(){
			$(this).parent().addClass("active");
			$(this).parent().find('input').attr('checked', true);
		},
		function(){
			$(this).parent().removeClass("active");
			$(this).parent().find('input').attr('checked', false);
		}
	);
	
	$('#pc-result_list .select input').click(function(){
		if($(this).attr('checked'))
			$(this).parent().parent().addClass("active");
		else
			$(this).parent().parent().removeClass("active");;
	});
	
	$("#pc-result_slider").slider({
   		animate: true,
    		change: handleSliderChange,
    		slide: handleSliderSlide
  	});
	
});

function  handleSliderChange(e, ui)
{
  var maxScroll = $("#pc-result_list").attr("scrollWidth") -
                  $("#pc-result_list").width();
  $("#pc-result_list").animate({scrollLeft: ui.value *
     (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $("#pc-result_list").attr("scrollWidth") -
                  $("#pc-result_list").width();
  $("#pc-result_list").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function getNumberOfProducts( object )
{
	var data = '';
	data += '&groupuid=' + $('#groupuid').val();
	
	// get selects
	$('.tx-productcatalog-pi1 select option:selected').each(function(){
		data += '&' + $(this).parent().attr('name') + '=' + $(this).val();
	});
	
	// get radiobuttons
	$('.tx-productcatalog-pi1 input[type="radio"]:checked').each(function(){
		data += '&' + $(this).attr('name') + '=' + $(this).val();
	});
	
	$.ajax({
		url: "",
		data: "eID=productcatalog" + data,
		success: function(html){
		//	$('#result').html(html);
		}
	});
	
	$.ajax({
		url: '',
		dataType: 'json',
		data: 'eID=productcatalog' + data,
		success: function(data){
			$.each(data, function(i, value){
				
				$('select[name="tx_productcatalog_pi1[' + i + ']"] option').each(function(){
					if( $(this).val() != '' ) {
						var field = $(this);
						var fieldValue = $(this).val();
						var found = false;
						$.each(value, function( j, val ){
							if (val == fieldValue) {
								field.removeAttr("disabled");
								field.removeClass("disabled");
								found = true;
							}
							else {
								if(!found) {
									field.attr("disabled", "disabled");
									field.addClass("disabled");
								}
							}
						});
					}
				}); 
				
			});
			if( $('#result').length )
				$('#result').html(data.result);
			else
				$('.pc-buttons').prepend('<div id="result">' + data.result + '</div>');
		}
	});
}
