/****** Main Menu *******/

var rmprev;
function initMenu(id, closeable) {
	if(closeable == 1) {
		closeable = false;
	} else {
		closeable = true;
	}

	$("#"+id+" > li > ul").hide();
	$("#"+id+" > li > ul:first").show();
	$("#"+id+" > li > a:not([id^=c])").addClass("rightmenubutton");
	$("#"+id+" > li:first > a:not([id^=c])").addClass("ac");
	rmprev = $("#"+id+" > li:first > a:not([id^=c])");

	$("#"+id+" > li > a:not([id^=c])").click(function() {
		var checkElement = $(this).next();

		if((checkElement.is("ul")) && (checkElement.is(":visible"))) {
			if(closeable) {
				$(this).removeClass("ac");
				$("#"+id+" ul:visible").slideUp("normal");
			}

			return false;
		}

		if((checkElement.is("ul")) && (!checkElement.is(":visible"))) {
			$("#"+id+" ul:visible").slideUp("normal");
			checkElement.slideDown("normal");
			rmprev.removeClass("ac");
			$(this).addClass("ac");
			rmprev = $(this);

			return false;
		}

		return false;
	});
}



/****** Slider Teaser ******/

function carousel_itemVisibleInCallback(carousel, item, i, state, evt) {
	var idx = carousel.index(i, carousel_itemList.length);
	carousel.add(i, carousel_getItemHTML(carousel_itemList[idx - 1]));
}

function carousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
	carousel.remove(i);
}

function carousel_getItemHTML(item) {
	video = "";
	if(item.video) {
		video = '<img src="../../templates/scripts/images/teaser/teaser_thumb_playermask.png" class="teaservideomask" />';
	}
	return '<div class="scrollerteaser">'+video+'<img src="../../templates/scripts/'+item.url+'" /><b>'+item.title+'</b>'+item.subtitle+'</div>';
}

var active1 = 0;
var active2 = 0;
var active3 = 0;
var stprev = 0;
function current_active(carousel, item, i, state, evt) {
	var max = carousel_itemList.length;
	if(state == "init") {
		if(active1 == 0) {
			active1 = i;
		} else {
			if(active2 == 0) {
				active2 = i;
			} else {
				if(active3 == 0) {
					active3 = i;
				}
			}
		}
	}
	if(state == "next") {
		active1 = active1 + 1;
		active2 = active2 + 1; // Mitte
		active3 = active3 + 1;
		if(active1 > max) { active1 = 1; }
		if(active2 > max) { active2 = 1; }
		if(active3 > max) { active3 = 1; }
	}
	if(state == "prev") {
		active1 = active1 - 1;
		active2 = active2 - 1; // Mitte
		active3 = active3 - 1;
		if(active1 < 1) { active1 = max; }
		if(active2 < 1) { active2 = max; }
		if(active3 < 1) { active3 = max; }
	}

	if(active1 > 0 && active2 > 0 && active3 > 0) {
		if(stprev != 0) { $("#teaserimg"+stprev).fadeTo(750, 0).css("z-index", "10"); }
		$("#teaserimg"+active2).fadeTo(500, 1, function() { $(this).css("filter", ""); }).css("z-index", "100");
		//Cufon.replace('.scrollerteaser b');
		stprev = active2;
	}
}

function carousel_initCallback(carousel) {
	jQuery('.jcarousel-list li').live('click', function() {
		carousel.scroll(jQuery(this).attr('jcarouselindex')-1);
		carousel.startAuto(0);
		return false;
	});

	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});
}

function initTeaser(carousel, teaser) {
	jQuery("#"+carousel).jcarousel({
		auto: 8,
		wrap: 'circular',
		scroll: 1,
		initCallback: carousel_initCallback,
		itemVisibleInCallback: {
			onBeforeAnimation: carousel_itemVisibleInCallback,
			onAfterAnimation: current_active
		},
		itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
	});
	// Teaser
	$("#"+teaser+" > div").each(function(i) {
		if(i == 1) { // Zweites Bild!
			$(this).attr("id", "teaserimg"+(i+1)).css("z-index", "100");
		} else {
			$(this).attr("id", "teaserimg"+(i+1)).fadeTo(1, 0).show().css("z-index", "10");
		}
	});
	$("#"+teaser+" > div > h1").each(function() {
		pos = "lines"+($("br", $(this)).size()+1);
		$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>").addClass(pos);
	});
	$("#"+teaser+" > div > p").each(function() {
		$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>");
	});
}

function initTeaser_noslide(teaser) {
	$("#"+teaser+" h1").each(function() {
		pos = "lines"+($("br", $(this)).size()+1);
//		$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>").addClass(pos);
		$(this).addClass(pos);
	});
}



/****** Main Menu + Footerhover + Last li + Kontakthover + Glossar ******/

$(document).ready(function() {
	setTimeout(function() { Cufon.replace("h1", {fontFamily:'Akkurat-Light'}); }, 500);
	// Main Menu
	if(jQuery.support.opacity) {
		$(".dropdown").fadeTo(1, 0).hide();
		$(".mainmenu > .knappar > a[id$=hover]").fadeTo(1, 0).hide();
	} else {
		$(".dropdown").hide();
		$(".mainmenu > .knappar > a[id$=hover]").hide();
	}
	$(".dropdown > div").hide();
	var mainmenuprev;

	$(".mainmenu > .knappar > a:not([id$=hover])").bind("mouseenter click", function() {
		clearTimeout(mainmenutimer);

		thisid = $(this).attr("id");

		if($(".dropdown").is(":visible")) {
			if(jQuery.support.opacity) {
				$("#"+mainmenuprev+"hover").stop().fadeTo(1, 0).hide();
				$("#"+thisid+"hover").stop().fadeTo(1, 1).show();
			} else {
				$("#"+mainmenuprev+"hover").hide();
				$("#"+thisid+"hover").show();
			}
			$("#"+mainmenuprev+"dropdown").hide().addClass("hidden");
			$("#"+thisid+"dropdown").show().removeClass("hidden");
		} else {
			if(jQuery.support.opacity) {
				$(".dropdown > div").stop().hide();
				$("[id$=hover]", $(".knappar")).stop().hide();
				$(".dropdown").stop().show().fadeTo(250, 1);
				$("#"+thisid+"hover").stop().show().fadeTo(250, 1);
			} else {
				$(".dropdown").show();
				$("#"+thisid+"hover").show();
			}
			$("#"+thisid+"dropdown").show().removeClass("hidden");
		}

		$("#"+mainmenuprev+"hover").unbind();
		$(".dropdown").unbind();

		$("#"+thisid+"hover").bind("mouseleave", function() {
			hidemainmenu(thisid);
		}).bind("mouseenter click", function() {
			clearTimeout(mainmenutimer);

			return false;
		});

		$(".dropdown").bind("mouseleave", function() {
			hidemainmenu(thisid);
		}).bind("mouseenter", function() {
			clearTimeout(mainmenutimer);
		});

		mainmenuprev = thisid;

		return false;
	});

	// Kontakt-Tabs
	initTabs("dropdownkontakttabmainbar", "dropdownkontakttabmaincontent");

	// Footer
	$(".footer2 a div.hover").show().fadeTo(1, 0);
	$(".footer2 a").bind("mouseenter", function() {
		$("div.hover", $(this)).stop().fadeTo(250, 1);
	}).bind("mouseleave", function() {
		$("div.hover", $(this)).stop().fadeTo(250, 0);
	});

	// Last li & ul
	$("ul li:last-child").addClass("lastli");
	$(":not(.accordion ul li) > ul li:first-child").addClass("firstli");
	$("ul:last-child:not(:first-child)", $(".accordion ul li:last-child")).addClass("lastul");
	$("ul:last-child:not(:last)", $(":not(.accordion) ul li:last-child")).addClass("lastul");

	// Kontakthover
	$("#kontaktimg_hover").fadeTo(1, 0);
	$("#kontakta").bind("mouseenter", function() {
		$("#kontaktimg").stop().fadeTo(500, 0);
		$("#kontaktimg_hover").stop().fadeTo(250, 1);
	}).bind("mouseleave", function() {
		$("#kontaktimg_hover").stop().fadeTo(500, 0);
		$("#kontaktimg").stop().fadeTo(250, 1);
	});

	// Glossar
	$(".tx-sgglossary-pi1 > form > ul > li > b").parent().addClass("ac");
	$(".tx-sgglossary-pi1 > dl").parent().append('<div class="accordion"><ul id="mainaccordion"></ul><div class="clear"></div></div>');
	$(".tx-sgglossary-pi1 > dl > dt").each(function() {
		titles = $(this).html();
		content = $(this).next().html();
		$("#mainaccordion").append('<li class="noblob"><a href="#">'+titles+'</a><a href="#" class="righttext">einblenden</a><a href="#" class="righttext inac">ausblenden</a><ul><li class="pushin">'+content+'</ul></li></li>');
	});
	$(".tx-sgglossary-pi1 > dl").remove();
	$(".tx-sgglossary-pi1 > form > p").remove();
	$(".tx-sgglossary-pi1 > form > br").remove();
	if($(".tx-sgglossary-pi1").length) {
		initAccord("mainaccordion", 0, true);
	}
	
	// Showroom
	function showvisningsrum() { // Open Showroom
		$("#visabilderstor").fadeTo(1, 0).show().fadeTo(500, 0.75);
		$("#visningsrum").fadeTo(1, 0).show().fadeTo(500, 1);
			$('html,body').animate({scrollTop: $(".wrapper").offset().top},'slow');
	}
	
	
	function closevisningsrum() { // Close Showroom
		$("#visabilderstor").fadeTo(500, 0, function() { $(this).hide(); });
		$("#visningsrum").fadeTo(500, 0, function() { $(this).hide(); });
	}
	
	$(".visningsrum").bind("click", function() { // Showroom Link
		showvisningsrum();
		return false;
	});
	
	$("#closure-sr").bind("click", function() {
		$("#visabilderstor").fadeTo(500, 0, function() { $(this).hide(); });
		$("#visningsrum").fadeTo(500, 0, function() { $(this).hide(); });
		return false;
	});
	
	// Videoroom
	$("#videoroom_content").load("/filerbfkonsult/templates/videoroom.html");
		
	function showvideoroom() { // Open Videoroom
		$("#visabilderstor").fadeTo(1, 0).show().fadeTo(500, 0.75);
		$("#videoroom").fadeTo(1, 0).show().fadeTo(500, 1);
		$('html,body').animate({scrollTop: $(".wrapper").offset().top},'slow');
		$("#intro").fadeTo(1, 0).show().fadeTo(500, 1);
	}

	$(".videoroom").bind("click", function() { // Videoroom Link
		showvideoroom();
		return false;
	});
	
	$(".videoroom1").bind("click", function() { // Videoroom Link - Dr. Ottmann
		showvideoroom();
		$("#intro").fadeTo(1, 0, function() { $(this).hide(); });
		$("#play-video-1").fadeTo(1, 0).show().fadeTo(500, 1);
		return false;
	});
	
	$(".videoroom2").bind("click", function() { // Videoroom Link - Immobilienverwaltung
		showvideoroom();
		$("#intro").fadeTo(1, 0, function() { $(this).hide(); });
		$("#play-video-2").fadeTo(1, 0).show().fadeTo(500, 1);
		return false;
	});
	
	$(".videoroom3").bind("click", function() { // Videoroom Link - Projektentwicklung
		showvideoroom();
		$("#intro").fadeTo(1, 0, function() { $(this).hide(); });
		$("#play-video-3").fadeTo(1, 0).show().fadeTo(500, 1);
		return false;
	});
	
	$(".videoroom4").bind("click", function() { // Videoroom Link - SHB in Berlin
		showvideoroom();
		$("#intro").fadeTo(1, 0, function() { $(this).hide(); });
		$("#play-video-4").fadeTo(1, 0).show().fadeTo(500, 1);
		return false;
	});
	
	$(".videoroom5").bind("click", function() { // Videoroom Link - Messe Film
		showvideoroom();
		$("#intro").fadeTo(1, 0, function() { $(this).hide(); });
		$("#play-video-5").fadeTo(1, 0).show().fadeTo(500, 1);
		return false;
	});

});

var mainmenutimer;
function hidemainmenu(thisid) {
	if(thisid) {
		mainmenutimer = setTimeout(function() {
			if(jQuery.support.opacity) {
				$(".dropdown").stop().fadeTo(250, 0, function() {
					$(this).hide();
					$("#"+thisid+"dropdown").stop().hide();
				});
				$("#"+thisid+"hover").stop().fadeTo(250, 0, function() { $(this).hide(); });
			} else {
				$("#"+thisid+"hover").hide();
				$("#"+thisid+"dropdown").hide().addClass("hidden");
				$(".dropdown").stop().hide();
			}
		}, 500);
	} else {
		if(jQuery.support.opacity) {
			$(".dropdown").stop().fadeTo(250, 0, function() {
				$(this).hide();
				$("[id$=dropdown]", $(this)).stop().hide();
			});
			$(".knappar [id$=hover]").stop().fadeTo(250, 0, function() { $(this).hide(); });
		} else {
			$(".dropdown").stop().hide();
			$("[id$=dropdown]", $(".dropdown")).hide().addClass("hidden");
		}
	}
}



/****** News ******/

function initNews(news) {
	$("#"+news+" > div").each(function() {
		if($(this).height() > $("#"+news).height()) {
			$("#"+news).css("height", $(this).height()+"px");
		}
	});
	var newsac = 1;
	var newsmax = $("#"+news+" > div").size();
	$("#"+news+" > div:gt(0)").fadeTo(1, 0).hide();
	$("#newsnext").bind("click", function() {
		$("#"+news+" > div:eq("+(newsac-1)+")").stop().fadeTo(250, 0, function() { $(this).hide(); });
		newsac = newsac + 1;
		if(newsac > newsmax) { newsac = 1; }
		$("#"+news+" > div:eq("+(newsac-1)+")").stop().show().fadeTo(200, 1, function() { $(this).css("filter", ""); });

		return false;
	});
}



/****** Accordion ******/

var aprev = new Array;
function initAccord(id, closeable, startopen) {
	$("#"+id+" > li:not(:has(ul))").addClass("nodropdown");

	if(closeable == 1) {
		closeable = false;
	} else {
		closeable = true;
	}

	$("#"+id+" > li:last:not(:first)").addClass("noborder");
	$("#"+id+" > li:first").addClass("firstli");
	if($("#"+id+" > li").size() == 1) {
		$("#"+id).addClass("firstul clearfix");
	}
	$("#"+id+" > li > ul").hide();
	if(startopen) {
		$("#"+id+" > li > ul:first").show();
		notinac = $(".righttext:not(.inac)", $("#"+id+" > li > ul:first").parent());
		inac = $(".righttext.inac", $("#"+id+" > li > ul:first").parent());
		inac.removeClass("inac");
		notinac.addClass("inac");
		$("#"+id+" > li > ul:first").parent().addClass("ac");
		aprev[id] = $("#"+id+" > li > ul:first");
	}

	$("#"+id+" > li > a").unbind("click").bind("click", function() {
		var checkElement = $("ul[class!=newList]", $(this).parent());

		if(typeof(map) != "undefined") {
			setTimeout(function() { map.checkResize(); }, 250);
		}

		notinac = $(".righttext:not(.inac)", $(this).parent());
		inac = $(".righttext.inac", $(this).parent());
		if(aprev[id]) {
			pnotinac = $(".righttext:not(.inac)", aprev[id].parent());
			pinac = $(".righttext.inac", aprev[id].parent());
		}

		inac.removeClass("inac");
		notinac.addClass("inac");
		if(aprev[id]) {
			pinac.removeClass("inac");
			pnotinac.addClass("inac");
		}

		if((checkElement.is("ul")) && (checkElement.is(":visible"))) {
			if(closeable) {
				$(this).parent().removeClass("ac");
				$("#"+id+" ul:visible").slideUp("normal");
				$(".biggercenter", $("#"+id+" ul:visible").parent()).show();
				aprev[id] = "";
			}

			return false;
		}

		if((checkElement.is("ul")) && (!checkElement.is(":visible"))) {
			$("#"+id+" ul:visible").slideUp("normal");
			$(".biggercenter", $("#"+id+" ul:visible").parent()).show();
			checkElement.slideDown("normal");
			$(".biggercenter", checkElement.parent()).hide();
			if(aprev[id]) {
				aprev[id].parent().removeClass("ac");
			}
			$(this).parent().addClass("ac");
			aprev[id] = $(this);

			return false;
		}

		return false;
	});
}



/****** Tabs ******/

var prev = new Array();
function initTabs(tabbar, tabcontent) {
	$("#"+tabcontent+" > div:not(.clear)").each(function(i) {
		$(this).attr("id", "tabcontent"+(i+1));
		if(i != 0) {
			$(this).addClass("inac");
		}
	});
	$("#"+tabbar+" > div").each(function(i) {
		$(this).attr("id", "tab"+(i+1));
		if(i == 0) {
			$(this).addClass("ac");
		}
	}).bind("click", function() {
		ac = $(this).attr("id").replace("tab", "");
		if(ac != prev[tabbar]) {
			if(prev[tabbar] != 0) {
				$("#"+tabcontent+" > div:not(.inac):not(.clear)").addClass("inac");
				$("#"+tabbar+" > div.ac").removeClass("ac");
			}
			$(this).addClass("ac");
			$("#"+tabcontent+" > div:eq("+(ac-1)+")").removeClass("inac");
			prev[tabbar] = ac;
		}
	});
	tab = getUrlVars()["tab"];
	if(typeof(tab) != "undefined") {
		if(tab == "e") {
			setTimeout(function() { $(".rightcontent > #tabmainbar > div:eq(0)").trigger("click"); }, 200);
		}
		if(tab == "m") {
			setTimeout(function() { $(".rightcontent > #tabmainbar > div:eq(1)").trigger("click"); }, 200);
		}
	}
}


/****** Sliders ******/

function initSliders(slider1, slider2, slider3, miete) {
	$("#"+slider1).slider({
		min: 49,
		max: 201,
		value: 201,
		slide: function(event, ui) {
			val = ui.value;
			if(val >= 201) {
				val = "beliebig";
				topval = "beliebige Größe";
			} else {
				if(val <= 49) {
					val = "< 50 m²";
				} else {
					val = val+" m²";
				}
				topval = val;
			}
			$("#"+slider1+"text").html(val);
			$("#"+slider1+"toptext").html(topval);
		}
	});
	$("#"+slider2).slider({
		min: 1,
		max: 9,
		value: 9,
		slide: function(event, ui) {
			val = ui.value;
			if(val == 1) { val = "1 Zimmer"; }
			if(val == 2) { val = "2 Zimmer"; }
			if(val == 3) { val = "2,5 Zimmer"; }
			if(val == 4) { val = "3 Zimmer"; }
			if(val == 5) { val = "3,5 Zimmer"; }
			if(val == 6) { val = "4 Zimmer"; }
			if(val == 7) { val = "4,5 Zimmer"; }
			if(val == 8) { val = "5 Zimmer"; }
			topval = val;
			if(val == 9) { val = "beliebig"; topval = "beliebig viele Zimmer"; }
			$("#"+slider2+"text").html(val);
			$("#"+slider2+"toptext").html(topval);
		}
	});
	$("#"+slider3).slider({
		min: 1,
		max: 5,
		value: 5,
		slide: function(event, ui) {
			val = ui.value;
			if(miete) {
				if(val == 1) { val = "< 300 €"; }
				if(val == 2) { val = "300 € bis 700 €"; }
				if(val == 3) { val = "700 € bis 1500 €"; }
				if(val == 4) { val = "> 1500 €"; }
			} else {
				if(val == 1) { val = "< 300.000 €"; }
				if(val == 2) { val = "300.000 € bis 350.000 €"; }
				if(val == 3) { val = "350.000 € bis 400.000 €"; }
				if(val == 4) { val = "> 400.000 €"; }
			}
			topval = val;
			if(val == 5) { val = "beliebig"; topval = "beliebiger Preis"; }
			$("#"+slider3+"text").html(val);
			$("#"+slider3+"toptext").html(topval);
		}
	});
}




/****** Login Hover ******/

var fladdraprev;
function initLoginHover() {
	if(jQuery.support.opacity) {
		$(".flaggor").fadeTo(1, 0).hide();
		$(".mainmenu .topknappar a#fladdra").fadeTo(1, 0).hide();
	} else {
		$(".flaggor").hide();
		$(".mainmenu .topknappar a#fladdra").hide();
	}
	$(".flaggor > div").hide();

	$(".mainmenu .topknappar a#login").bind("click", function() {
		clearTimeout(fladdratimer);
		hidemainmenu();

		thisid = $(this).attr("id");

		if($(".flaggor").is(":visible")) {
			if(jQuery.support.opacity) {
				$("#"+fladdraprev+"hover").fadeTo(1, 0).hide();
				$("#"+thisid+"hover").fadeTo(1, 1).show();
			} else {
				$("#"+fladdraprev+"hover").hide();
				$("#"+thisid+"hover").show();
			}
			$("#"+fladdraprev+"dropdown").hide();
			$("#"+thisid+"dropdown").show();
		} else {
			if(jQuery.support.opacity) {
				$(".flaggor").show().fadeTo(250, 1);
				$("#"+thisid+"hover").show().fadeTo(250, 1);
			} else {
				$(".flaggor").show();
				$("#"+thisid+"hover").show();
			}
			$("#"+thisid+"dropdown").show();
		}

		$("#"+fladdraprev+"hover").unbind();
		$(".flaggor").unbind();

		$("#"+thisid+"hover").bind("mouseleave", function() {
			hidefladdra(thisid);
		});
		$("#"+thisid+"hover").bind("mouseenter", function() {
			clearTimeout(fladdratimer);
		});

		$(".flaggor").bind("mouseleave", function() {
			hidefladdra(thisid);
		});
		$(".flaggor").bind("mouseenter", function() {
			clearTimeout(fladdratimer);
		});

		fladdraprev = thisid;
	});
}

var fladdratimer;
function hidefladdra(thisid) {
	fladdratimer = setTimeout(function() {
		if(jQuery.support.opacity) {
			$(".flaggor").fadeTo(250, 0, function() {
				$(this).hide();
				$("#"+thisid+"dropdown").hide();
			});
			$("#"+thisid+"hover").fadeTo(250, 0, function() { $(this).hide(); });
		} else {
			$(".flaggor").hide();
			$("#"+thisid+"dropdown").hide();
			$("#"+thisid+"hover").hide();
		}
	}, 500);
}



/****** 0 Select ******/
function initKontakt(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type) {
	bindkontakt1(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);
	bindkontakt2(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);

	$("#"+kontaktselect1).sSelect({ddMaxHeight: 200});
	$("#"+kontaktselect2).sSelect({ddMaxHeight: 200});

	tab = getUrlVars()["tab"];
	kontakt = getUrlVars()["kontakt"];
	if(typeof(tab) != "undefined" && typeof(kontakt) != "undefined") {
		if(tab == type) {
			$("#"+kontaktselect1).val(tab+kontakt);
			$("#"+kontaktselect1).resetSS();
			bindkontakt1(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);

			$.get(selectphp, { select1: $("#"+kontaktselect1).val() }, function(data) {
				var nodes = data.split(";");
				$("#"+kontaktselect2).html("");
				for(i=0;i<nodes.length;i=i+1) {
					tmpnodes = nodes[i].split(",");
					$("#"+kontaktselect2).append('<option value="'+tmpnodes[0]+'">'+tmpnodes[1]+'</option>');
					if(nodes.length == 1 && tmpnodes[1] == " ") {
						$("#"+kontaktselect2).next().hide();
					} else {
						$("#"+kontaktselect2).next().show();
					}
				}
				$("#"+kontaktselect2).resetSS();
				bindkontakt2(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);
				$.get(contentphp, { select1: $("#"+kontaktselect1).val(), select2: $("#"+kontaktselect2).val() }, function(data) {
					tmpdata = data.split("{}");
					$("#"+container).html(tmpdata[0]);
					$("#"+infocontainer).html(tmpdata[1]);
				});
			});
		}
		setTimeout(function() {
			$("#"+kontaktselect1).resetSS();
			bindkontakt1(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);
			$("#"+kontaktselect2).resetSS();
			bindkontakt2(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);
		}, 200);
	}
}

function bindkontakt1(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type) {
	$("#"+kontaktselect1).unbind("change").bind("change", function() {
		$.get(selectphp, { select1: $("#"+kontaktselect1).val() }, function(data) {
			var nodes = data.split(";");
			$("#"+kontaktselect2).html("");
			for(i=0;i<nodes.length;i=i+1) {
				tmpnodes = nodes[i].split(",");
				$("#"+kontaktselect2).append('<option value="'+tmpnodes[0]+'">'+tmpnodes[1]+'</option>');
				if(nodes.length == 1 && tmpnodes[1] == " ") {
					$("#"+kontaktselect2).next().hide();
				} else {
					$("#"+kontaktselect2).next().show();
				}
			}
			if($("#"+kontaktselect2).next().is(":visible")) {
				$("#"+kontaktselect2).resetSS();
			}
			bindkontakt2(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type);
			$.get(contentphp, { select1: $("#"+kontaktselect1).val(), select2: $("#"+kontaktselect2).val() }, function(data) {
				tmpdata = data.split("{}");
				$("#"+container).html(tmpdata[0]);
				$("#"+infocontainer).html(tmpdata[1]);
			});
		});
	});
}

function bindkontakt2(selectphp, contentphp, kontaktselect1, kontaktselect2, container, infocontainer, type) {
	$("#"+kontaktselect2).unbind("change").bind("change", function() {
		$.get(contentphp, { select1: $("#"+kontaktselect1).val(), select2: $("#"+kontaktselect2).val() }, function(data) {
			tmpdata = data.split("{}");
			$("#"+container).html(tmpdata[0]);
			$("#"+infocontainer).html(tmpdata[1]);
		});
	});
}

function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i=0;i<hashes.length;i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}



/****** Post-checkboxen ******/
function initPostcheckbox(classy, targety) {
	$("."+classy+" input[type=checkbox]").bind("change", function() {
		count = $("."+classy+" input[type=checkbox]:checked").size();
		$("#"+targety+" span").text(count);
		Cufon.refresh("h4");
	});
}



/****** Architektenheader ******/
function initTeaser_archi(teaser) {
	$("#"+teaser+" div div").each(function(i) {
		$(this).attr("id", "architekt"+(i+1));
		if($(this).attr("class") != "active") {
			$(this).fadeTo(1, 0.5);
			$("h2", $(this)).fadeTo(1, 0).hide();
		}
	}).bind("mouseenter", function() {
		if($(this).attr("class") != "active" && $(this).attr("class") != "empty") {
			$(this).stop().fadeTo(250, 1);
			$("h2", $(this)).stop().show().fadeTo(250, 1);
		}
	}).bind("mouseleave", function() {
		if($(this).attr("class") != "active") {
			$(this).stop().fadeTo(250, 0.5);
			$("h2", $(this)).stop().fadeTo(250, 0, function() { $(this).hide(); });
		}
	});
}



/****** Small Gallery ******/
function initSmallgallery() {
	$(".smallgallery").append('<div class="thumbs"></div>');
	$(".smallgallery").append('<div class="title"></div>');
	if($(".smallgallery").parent().parent().is(":hidden")) {
		$(".smallgallery").parent().parent().show();
		hideit = 1;
	} else { hideit = 0; }
	$(".smallgallery div:eq(0) img").each(function(i) {
		if(i > 0) {
			$(this).fadeTo(1, 0).hide();
			if($(this).height() > height) { height = $(this).height(); }
		} else {
			height = $(this).height();
		}
		$(this).attr("id", "smallgalleryimg"+(i+1));

		$(".smallgallery .title").append('<span id="smallgallerytitle'+(i+1)+'"><b>'+$(".smallgallery div:eq(0) b:eq("+i+")").html()+'</b><p>'+$(".smallgallery div:eq(0) p:eq("+i+")").html()+'</p><a>'+$(".smallgallery div:eq(0) a:eq("+i+")").html()+'</a></span>');

		src = $(this).attr("src");
		src = src.replace(/(\.gif|\.jpg|\.png)/g, '_thumb$1');

		$(".smallgallery .thumbs").append('<img src="../../templates/scripts/'+src+'" id="smallgallerythumb'+(i+1)+'" />');
		if(i > 0) {
			$(".smallgallery .thumbs img:eq("+i+")").fadeTo(1, 0.5);
			$(".smallgallery .title #smallgallerytitle"+(i+1)).fadeTo(1, 0).hide();
		}
	});
	if(hideit == 1) {
		$(".smallgallery").parent().parent().hide();
	}
	$(".smallgallery div:eq(0) b, .smallgallery div:eq(0) p, .smallgallery div:eq(0) a").remove();
	$(".smallgallery div:eq(0)").css("height", height+"px");
	$(".smallgallery .title").css("height", height+"px");
	$(".smallgallery .thumbs img").bind("click", function() {
		var id = $(this).attr("id").replace(/thumb/, 'img');
		var title = $(this).attr("id").replace(/thumb/, 'title');

		$(".smallgallery .thumbs img:not(#"+$(this).attr("id")+")").stop().fadeTo(250, 0.5);
		$("#"+$(this).attr("id")).stop().fadeTo(250, 1);

		$(".smallgallery div:not(.thumbs, .title) img:not(#"+id+")").stop().fadeTo(250, 0, function() { $(this).hide(); });
		$("#"+id).show().stop().fadeTo(250, 1);

		$(".smallgallery .title span").stop().fadeTo(250, 0, function() { $(this).hide(); });
		$("#"+title).show().stop().fadeTo(250, 1);
	});
}



/****** Such-Filter ******/
function initFilter() {
	$("#tx_realty_pi1_list_view > br").remove();
	$("#tx_realty_pi1_list_view > *").slideUp(1);

	initcount = 0;
	$("#tx_realty_pi1_list_view .searchitem:not(.nomargin)").each(function() {
		incity = $(".city", $(this)).text();
		if(incity == "München") {
			$(this).slideDown(1);
			$(this).next().slideDown(1);
			$(this).next().next().slideDown(1);
			initcount++;
		}
	});

	$(".tx-realty-pi1 .list-view h4").show().text("Ihre Suche erzielte "+initcount+" Treffer");
	Cufon.replace(".tx-realty-pi1 .list-view h4");

	$(".searchsubmit input[type=submit]").unbind("click").bind("click", function() {
		if($("#imraumselect").val() != "0") {
			$("#loading").fadeTo(250, 1);
			$("#error").fadeTo(250, 0);
			var thisnthat = $(this);
			searchtimer = setTimeout(function() {
				thisnthat.val("Aktualisieren");
				city = $(".newListSelected .selectedTxt").text().replace(/\s\(.*?\)$/g, '');
				space = $("#slider1text").text().replace(/\sm.*?$/, '');
				rooms = $("#slider2text").text().replace(/\sZimmer$/, '').replace(/,/, '.');
				if(rooms != "beliebig") { rooms = parseFloat(rooms); }
				cost = $("#slider3text").text();

				spacefrom = "0";
				if(/</.test(space)) {
					spaceto = space.replace(/<\s/, '');
				} else {
					spaceto = space;
				}

				costfrom = "0";
				costto = "0";
				if(/</.test(cost)) {
					costto = cost.replace(/<\s/, '');
				}
				if(/bis/.test(cost)) {
					costfrom = cost.replace(/\sbis\s.*?$/, '');
					costto = cost.replace(/^.*?\sbis\s/, '');
				}
				if(/>/.test(cost)) {
					costfrom = cost.replace(/>\s/, '');
				}
				costfrom = parseInt(costfrom.replace(/[^0-9]/, ''));
				costto = parseInt(costto.replace(/[^0-9]/, ''));

				var totalac = 0;
				$("#tx_realty_pi1_list_view .searchitem:not(.nomargin)").each(function() {
					living = $(".livingarea", $(this)).text().replace(/\s.*?$/, '').replace(/,00/, '');
					numberofrooms = parseFloat($(".numberofrooms", $(this)).text().replace(/,/, '.'));
					price = $(".price", $(this)).text().replace(/,00/, '').replace(/\s[^0-9]*?$/, '').replace(/\s/, '');
					incity = $(".city", $(this)).text();
					isac = false;

					if(city == incity) {
						if(space == "beliebig" || (living >= spacefrom && living <= spaceto)) {
							if(rooms == "beliebig" || numberofrooms <= rooms) {
								if(cost == "beliebig" || (price >= costfrom && (price <= costto || costto == 0))) {
									isac = true;
									totalac++;
								}
							}
						}
					}

					if(isac) {
						$(this).slideDown(500);
						$(this).next().slideDown(500);
						$(this).next().next().slideDown(500);
					} else {
						$(this).slideUp(500);
						$(this).next().slideUp(500);
						$(this).next().next().slideUp(500);
					}
				});

				$("#loading").fadeTo(250, 0);
				if(totalac == 0) {
					$("#error").fadeTo(250, 1);
					$(".tx-realty-pi1 .list-view h4").hide();
				} else {
					$(".tx-realty-pi1 .list-view h4").show().text("Ihre Suche erzielte "+totalac+" Treffer");
					Cufon.replace(".tx-realty-pi1 .list-view h4");
				}
			}, 500); // searchtimer
		}
	});
}



/* ScrewIE */
$(document).ready(function() {
	$(function() {
		var zIndexNumber = 10000;
		$("ul, li").each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});
});
