$(document).ready(function() { 
    $(".helpbubble").tooltip();
	$(".detail").overlay({effect: 'apple',	expose: {
			color: '#111',
			loadSpeed: 300,
			opacity: 0.7
		}
	});
	if ($("#product-attr") && $("#product-attr").text().length > 15) {
		$("#mehr-details").click(function() {
			$("#product-attr").show('slow');
		});
	}
	else $("#mehr-details").hide();
	$(".chain-selectors select").change(function(e) {
		$('.selectattributes').empty();
		var html = '<ul>';
		$(".chain-selectors select").each(function(i) {
			var label = this.id.replace(/tx-feattr-commerce-/, '');
			var value = $(this).val();
			var num = $(this).find('[value=' + value + ']').text();
			var evenodd = 'even';
			if (i % 2 == 0) evenodd = 'odd';
			html += '<li  class="select-attr ' + evenodd + '"><strong>' + label + ': </strong>' + num + '</li>';
		});
		html += '</ul>';
		$('.selectattributes').html(html);
	});
	if ($(window).height() > 250) {
		var bottomHeight = $(window).height() - 250;
		$("#content-right").css('min-height', bottomHeight + 'px');
		$("#container-bottom").css('min-height', bottomHeight + 'px');
		$("#right-bottom").css('min-height', bottomHeight + 'px');
	}
	if ($('#privatradio').attr('checked')) {$('#fields-privat').show(); $('#fields-gewerbe').hide(); $('#fields-italien').hide(); $('#fields-eu').hide(); $('#vatrequest').hide();}
	if ($('#gewerberadio').attr('checked')) {$('#fields-privat').hide(); $('#fields-gewerbe').show(); countryOnChange();}
	$('#gewerbe-country').change(function() {countryOnChange();});
	if (typeof(articleMinMax) == 'object') setMinMax();
	$("#scroller ul").draggable({ axis: 'y' });
});

function setMinMax() {
	for (var inputId in articleMinMax) {
		$("#" + inputId).SpinButton({min: articleMinMax[inputId][0], max: 1000, step: articleMinMax[inputId][1]});
		$("#" + inputId).attr("readonly","readonly");
	}
}

function layeredClick(sender) {
	// make other in same fieldset disappear
	if (sender.checked) {
		$(sender.parentNode.parentNode).find('input').each(function(i) {
			if (this != sender) {
				$(this).removeAttr('checked');
				$(this).attr('disabled', 'disabled');
				$(this).parent().addClass('disabled');
			}
			else {
				$(this).parent().addClass('selected');
			}
		});
		var spanId = sender.id.replace('V', '#C');
		tx_commercesearch_pi1updateLayers(xajax.getFormValues('layeredsearchform'));
	}
	else {
		$(sender.parentNode.parentNode).find('input').each(function(i) {
			if (this != sender) {
				$(this).removeAttr('disabled');
				$(this).parent().removeClass('disabled');
			}
			else {
				$(this).parent().removeClass('selected');
			}
		});
		tx_commercesearch_pi1updateLayers(xajax.getFormValues('layeredsearchform'));
	}
}

function applyOptions() {
	var json = $('#ajax-container').html();
	var anzahlen = new Object();
	eval('anzahlen = ' + json);
	var total = anzahlen['total'];
	$('.ergebniscount').text(total);
	$('.filter').empty();
	$('#layeredsearchform span').each(function(i) {
		if (anzahlen[this.id]) {
			total -= anzahlen[this.id];
			if ($(this).text() != anzahlen[this.id]) {
				$(this).text(anzahlen[this.id]);
			}
		}
		else if (this.id.indexOf('CO') == 0) {
			$(this).text(total);
			if (total == 0) {
				$(this).parent().parent().addClass('disabled');
				$(this).parent().prev().attr('disabled', 'disabled');
			}
			total = anzahlen['total'];
		}
		else {
			$(this).text('0');
			$(this).parent().parent().addClass('disabled');
			$(this).parent().prev().attr('disabled', 'disabled');
		}
	});
	$('#layeredsearchform ul').each(function(i) {
		if ($(this).find('.disabled').length == $(this).find('li').length - 1) {
			$(this).find('li').each(function(j) {
				if (!$(this).hasClass('disabled')) {
					$(this).addClass('selected');
					$(this).find('input').attr('checked', 'checked');
				}
			});
		}
	});
	var filters = '';
	$('.selected').each(function(i) {
		filters += $(this).parent().parent().find('legend').text();
		filters += ': ';
		var v = $(this).find('label').text();
		var w = '(' + $(this).find('span').text() + ')';
		filters += v.replace(w, ' | ');
	});
	$('.filter').html(filters);
}

function showProducts() {
	$('#ajax-container').empty();
	$('.close').click();
	tx_commercesearch_pi1showProducts(xajax.getFormValues('layeredsearchform'));
}

function submitToBasket(sender) {
	$("#basketalert").overlay({
		effect: 'apple',	
		expose: {
			color: '#111',
			loadSpeed: 300,
			opacity: 0.7
		},
		api: true
	}).load();
		
	sender.form.submit();
}

function privatClick(sender) {
	if (sender.checked) {
		$('#fields-privat').show(); 
		$('#fields-gewerbe').hide(); 
		$('#fields-italien').hide(); 
		$('#fields-eu').hide(); 
		$('#vatrequest').hide();
	}
}

function gewerbeClick(sender) {
	if (sender.checked) {
		$('#fields-privat').hide(); 
		$('#fields-gewerbe').show(); 
		countryOnChange();
	}
}

function countryOnChange() {
	if ($('#gewerbe-country').val() == 'ITA' || $('#gewerbe-country').val() == 'SMR' || $('#gewerbe-country').val() == 'VAT') {
		$('#fields-italien').show();
		$('#fields-eu').hide();
		$('#vatrequest').hide();
	}
	else if ($('#gewerbe-country').val() == 'ALA' || $('#gewerbe-country').val() == 'AUT' || $('#gewerbe-country').val() == 'BEL' || $('#gewerbe-country').val() == 'CYP' || $('#gewerbe-country').val() == 'CZE' || $('#gewerbe-country').val() == 'DEU' || $('#gewerbe-country').val() == 'DNK' || $('#gewerbe-country').val() == 'ESP' || $('#gewerbe-country').val() == 'EST' || $('#gewerbe-country').val() == 'FIN' || $('#gewerbe-country').val() == 'FRA' || $('#gewerbe-country').val() == 'GBR' || $('#gewerbe-country').val() == 'GRC' || $('#gewerbe-country').val() == 'HUN' || $('#gewerbe-country').val() == 'IRL' || $('#gewerbe-country').val() == 'LTU' || $('#gewerbe-country').val() == 'LUX' || $('#gewerbe-country').val() == 'LVA' || $('#gewerbe-country').val() == 'MLT' || $('#gewerbe-country').val() == 'NLD' || $('#gewerbe-country').val() == 'POL' || $('#gewerbe-country').val() == 'PRT' || $('#gewerbe-country').val() == 'SVK' || $('#gewerbe-country').val() == 'SVN' || $('#gewerbe-country').val() == 'SWE') {
		$('#fields-eu').show();
		$('#vatrequest').show();
		$('#fields-italien').hide();
	}
	else {
		$('#fields-italien').hide();
		$('#fields-eu').hide();
		$('#vatrequest').hide();
	}
}
