$(document).ready(function() {
	// Layer Legend StockIcons
	$(".stock-ok").hover(
      function () {
        $(this).parents("td").append("<span class='legendlayer stok'>Ab Lager</span>");
      }, 
      function () {
        $(this).parents("td").find("span").remove();
      }
    );
	$(".stock-nok").hover(
      function () {
        $(this).parents("td").append("<span class='legendlayer stnok'>Wird f&uuml;r Sie bestellt.</span>");
      }, 
      function () {
        $(this).parents("td").find("span").remove();
      }
    );
    
    //Sitechange Deutschland-Schweiz
	$("#country option:first").attr("selected", "selected");
	$("#country").bind("change", function(){
    		if ($(this).val() != "Deutschland")
         	window.location.href = "http://www.hy-line.de";
         	//alert("test");
	});
	
	
	// 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
  	});
	
	// add layer for single info
    	$('.attribute-part_number').qtip({
         	content: 'Loading Data ...', // Give it a loading message while request is being sent
	      	position: {
         		corner: {
            			target: 'topMiddle',
            			tooltip: 'bottomMiddle'
         		}
      		},
      		style: { 
      			padding: '0px 0px', // Give it some extra padding
      			width: { min: 350 } 
       		},
      		hide: {
            		fixed: true // Make it fixed so it can be hovered over
         	},
		api: {
         		// Retrieve the content when tooltip is first rendered
         		onRender: function()
         		{
            			var self = this;
            			
            			var data = '';
        			data += '&groupuid=' + $('#groupuid').val();
        			data += '&productuid=' + self.elements.target.parent().find('td.select input').val();
        			data += '&action=loadsingledata';
        
        			$.ajax({
            				url: '',
            				data: 'eID=productcatalog' + data,
            				success: function(data) {
                				self.updateContent('<div id="singledata">' + data + '</div>');
                			}
        			});
         		}
      		}
      	});
});

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: '',
		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() != '' ) 
					{
						if( $.inArray( $(this).val(), value ) >= 0 )
							$(this).removeAttr("disabled").removeClass("disabled");
						else
							$(this).attr("disabled", "disabled").addClass("disabled");
					}
				}); 
				
			});
			if( $('#result').length )
				$('#result').html(data.result);
			else
				$('.pc-buttons').prepend('<div id="result">' + data.result + '</div>');
		}
	});

}

