$('.productFilterHead').click(function(){
	var id = $(this).attr('id').replace(/^toggle/, '');
	
	$(this).toggleClass('productFilterHeadActive');
	if (navigator.userAgent.indexOf('Safari') != -1) {
		$('#filter' + id).toggle('fast', makeBorders);
	}
	else {
		$('#filter' + id).slideToggle('fast', makeBorders);
	}
	//window.setTimeout("makeBorders()", 500);
	//makeBorders();
	//return false;
});

$.viewportWidth = function() {
		 return self.innerWidth ||
			jQuery.boxModel && document.documentElement.clientWidth ||
			document.body.clientWidth;
};

$(window).load(function(){
	$('#ddboxcontainer').remove();
	var pos1 = $('#pcontent').offset();
	var pos2 = $('#catalogFilterOptions').offset();
	
	var diff = pos1.top - pos2.top;
	if (diff > 0) {
		$('#filterPosHelper').css("width", "100px");
		$('#filterPosHelper').animate({
			height: diff + "px"
		}, 500);
		$('#filterPosHelper').css("height", diff + "px");
	}
	else {
		
	}
	if (navigator.userAgent.indexOf('Firefox') != -1 || navigator.userAgent.indexOf('MSIE 8') != -1 || navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('Safari') != -1) {
		$('.filterLink').each(function(){
			var html = $(this).html();
			    html = html.replace(/-/g, '-&#8203;');
			    html = html.replace(/([^<]{1}\/)/g, '$1&#8203;');
			    $(this).html(html);
		});
	}
	else if (navigator.userAgent.indexOf('MSIE 6') != -1) {
		$('.filterLink').each(function(){
			var html = $(this).html();
			    html = html.replace(/-/g, '-&<wbr>');
			    html = html.replace(/([^<]{1}\/)/g, '$1<wbr>');
			    $(this).html(html);
		});
	}
	filterSorting = new Object();
	prev = false;
	$('.productFilterHead').each(function(i){
		if (prev) {
			filterSorting[prev]['next'] = $(this).attr("id");
		}
		filterSorting[ $(this).attr("id") ] = new Object();
		filterSorting[ $(this).attr("id") ]['next'] = false;
		filterSorting[ $(this).attr("id") ]['prev'] = prev;
		prev = $(this).attr("id");
	});
	
	makeBorders();
	
	if (navigator.userAgent.indexOf('MSIE 6') != -1 || navigator.userAgent.indexOf('MSIE 7') != -1) {
//		$('#pcontent').css("margin-right", "205px");
//		ieWidth();
//		window.onresize = ieWidth;
	}	
	
	setTimeout("activateHighlight()",100);
	
});


function ieWidth() {
	var w1 = $.viewportWidth();
	var w2 = parseInt($('#mainstage').css("padding-left").replace("/px/", ""));
	var w1 = w1 - w2;
	
	if ($('#centercontent').length) {
		$('#centercontent').attr('id', "centercontent1");
	}
	
	$('#centercontent1').css("width", w1 + "px");
	
	var h1 = $('#pcontent').height();
	$('#catalogFilterOptions').css("height", h1 + "px");
	
	var w3 = $.viewportWidth() - w2;
	$('#mainstage').css("width", w3 + "px");
}


var catFilters = new Array();
function filterCat(field) {
	if ($('#filterLink' + field).hasClass('filterDisabled')) {
		return;
	}
	
	if ($('#filterLink' + field).hasClass('checked')) {
		catFilters[ field ] = 0;
		$('#filterLink' + field).removeClass('checked');
	}
	else {
		catFilters[ field ] = 1;
		$('#filterLink' + field).addClass('checked');
	}
	
	var url = "";
	
	for (i in catFilters) {
		if (catFilters[i] != 0) {
			url += "&" + i + "=1";
		}
		
	}	
	
	url += "&format=" + fFormat;
	scrollToTop();
	doFilterQuery(url);
}


var fFormat = "";
function filterFormat(format, id) {
	if ($('#' + id).hasClass('filterDisabled')) {
		return;
	}
	
	if ($('#' + id).hasClass('checked')) {
		$('#' + id).removeClass('checked');
		fFormat = "";
	}
	else {
		$('.formatLink').removeClass('checked');
		$('#' + id).addClass('checked');
		fFormat = format;
	}
	
	var url = "";
	for (i in catFilters) {
		if (catFilters[i] != 0) {
			url += "&" + i + "=1";
		}
	}
	url += "&format=" + fFormat;
	doFilterQuery(url);
}

function goToPage(page) {
	url = "";
	$('.filterLink').each(function(){
		if ($(this).hasClass('checked')) {
			url += "&" + $(this).attr("id").replace(/\D/g, "") + "=1";
		}
	});
	url += "&format=" + fFormat;
	url += "&page=" + page;
	doFilterQuery(url);
}

function doFilterQuery(params) {

	if (typeof(request_type) == "undefined") {
		request_type = "";
	}
	
	$('*').css('cursor', 'wait');
	var loc = String(window.location);
	loc = loc.replace(/^[a-zA-Z]{3,5}\:\/\/[a-zA-Z0-9.-]+?\//, '/');
	params += "&type=" + request_type;
	documentReady = false;
	$.ajax({
		type: "POST",
		url: loc,
		data: "filter=true&outputtyp=ajax_content&no_cache=1" + params, 
		success: parseFilterResponse
	});
}

function parseFilterResponse(response) {
	$('*').css('cursor', '');
	
	if (navigator.userAgent.indexOf('MSIE 6') != -1) {
		document.getElementById('pcontent').innerHTML = response;
	}
	else {
		$('#pcontent').html(response);
	}
	
	$('.pagination').each(function() {
		$(this).click(function(){
			goToPage($(this).attr("rel"));
			//return false;
		});
	});
	
	$('.filterCount').each(function() {
		var id = $(this).attr('id').replace(/\D/g, '');
		//$('#filterLink' + id).hide();
		$('#filterLink' + id).addClass('filterDisabled');
		$(this).html('(0)');
	});
	
	var fieldCounter = 	$('#fieldCounter').val().replace(/;$/, '');
	    fieldCounter = 	fieldCounter.split(';');
	for (var i = 0; i < fieldCounter.length; i++) {
		var fieldData = fieldCounter[i].split("=");
		//$('#filterLink' + fieldData[0]).show();
		$('#filterLink' + fieldData[0]).removeClass('filterDisabled');
		$('#filterCount' + fieldData[0]).html("(" + fieldData[1] + ")");
	}
	
	$('.formatCounter').each(function() {
		var id = $(this).attr('id').replace(/^fc/, '');
		$('#fl' + id).addClass('filterDisabled');
		$(this).html('(0)');
	});
	
	
	var formatCounter = $('#formatCounter').val().replace(/;$/, '');
	    formatCounter = formatCounter.split(';');
	for (var i = 0; i < formatCounter.length; i++) {
		formatData = formatCounter[i].split("=");
		formatData[0] = formatData[0].replace(/(&#[0-9]{1,5};)|([^0-9x])/g, '');
		$('#fc' + formatData[0]).html("(" + formatData[1] + ")");
		$('#fl' + formatData[0]).removeClass('filterDisabled');
	}
	
	window.setTimeout("tb_reInit()", 1000);
}

function tb_reInit() {
	if (!documentReady) {
		tb_init('a.thickbox, area.thickbox, input.thickbox');
	}
}

function resetFilter() {
	$('.filterLink').removeClass('checked');
	$('.formatLink').removeClass('checked');
	$('.productFilterHead').each(function(i){
		if (i > 0 && $(this).hasClass('productFilterHeadActive')) {
			var id = $(this).attr('id').replace(/^toggle/, '');
			$(this).removeClass('productFilterHeadActive');
			$('#filter' + id).slideUp('fast', makeBorders);
		}
	});
	fFormat = "";
	catFilters = new Array();
	doFilterQuery();
	
	scrollToTop();
	
}

function scrollToTop() {
	if (typeof(document.documentElement.scrollTop) != "undefined" && document.documentElement.scrollTop > 0) {
		var y = Math.max(0, document.documentElement.scrollTop - 30);
		window.scrollTo(0, y);
		if (y > 0) {
			window.setTimeout("scrollToTop()", 15);
		}
	}
}

function makeBorders() {
	if (typeof(request_type) == "undefined") {
		request_type = "";
	}
	$('.productFilterHead').each(function(i){
		var id = $(this).attr('id').replace(/^toggle/, '');
		if (typeof(filterSorting["toggle" + id]) != "undefined") {
			if ($(this).hasClass('productFilterHeadActive')) {
				$('#toggle' + id).css('border-bottom', '');
				if (request_type == 2) {
					$('#filter' + id).css('border-bottom', '1px solid #D7E3F3');
				}
				if (filterSorting['toggle' + id]['prev'] && !$('#' + filterSorting['toggle' + id]['prev']).hasClass('productFilterHeadActive')) {
					$(this).animate({marginTop: '15px'}, 'fast');
					$('#' + filterSorting['toggle' + id]['prev']).css('border-bottom', '1px solid #D7E3F3');
				}
				if (filterSorting['toggle' + id]['next'] && !$('#' + filterSorting['toggle' + id]['next']).hasClass('productFilterHeadActive')) {
					$('#filter' + id).animate({marginBottom: '15px'}, 'fast');
				}
			}
			else {
				if (request_type == 2) {
					$('#filter' + id).css('border-bottom', '');
				}
				$(this).animate({marginTop: '0px'}, 'fast');
				$('#filter' + id).animate({marginBottom: '0px'}, 'fast');
				$('#' + filterSorting['toggle' + id]['prev']).css('border-bottom', '');
			}
		}
	});	
}		


/* hightlight preselected items */
/* katalog: ../cd.html?highlight=toggleFeature,fl11600,filterLink65 (eigenschaften zuerst aufklappen, dann checkboxen) */
function activateHighlight() {
	var get = window.location.href.split("?highlight=");
	try{
		if (get[1]!='') {
			var att = get[1].split(",");
			for (j=0; j<att.length; j++) {
				if (att[j].length<3) { eval("filterCat("+att[j]+");"); } 
				else { $('#'+att[j]).click(); }
			}
		}
	}catch(e){
	}
}


