/****** Main Menu + Footerhover + Last li + Kontakthover + Glossar ******/
$(document).ready(function() {
	// Cufon
		// Cufon.replace('.rightcontent:not(p.bodytext)', { fontFamily: 'Akkurat-Light' });
		Cufon('#teaser', { fontFamily: 'Akkurat-Light' });
		Cufon('h1', { fontFamily: 'Akkurat-Light' });
		// H1 mit Link ausschließen:
		// Cufon(".csc-header h1:not(:has(a))");
		
		// Cufon('.split_rechts:not(div, div h1)', { fontFamily: 'Akkurat-Light' });
		
		Cufon('h2', { fontFamily: 'Akkurat-Light' });
		Cufon('h3', { fontFamily: 'Akkurat-Light' });
		Cufon.replace('.footer h5', { fontFamily: 'Akkurat' });
		Cufon('span.h7 a', { fontFamily: 'Akkurat', hover: { color: '#BD2716' }, hoverables: { a: true } });
		// remove_cufon('.csc-header h1 a');
		
	// Main Menu
	if(jQuery.support.opacity) {
		$(".dropdown").fadeTo(1, 0).hide();
		$(".mainmenu > .buttons > a[id$=hover]").fadeTo(1, 0).hide();
	} else {
		$(".dropdown").hide();
		$(".mainmenu > .buttons > a[id$=hover]").hide();
	}
	$(".dropdown > div").hide();
	var mainmenuprev;

	$(".mainmenu > .buttons > 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]", $(".buttons")).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 showshowroom() { // Open Showroom
		$("#showroombg").fadeTo(1, 0).show().fadeTo(500, 0.75);
		$("#showroom").fadeTo(1, 0).show().fadeTo(500, 1);
			$('html,body').animate({scrollTop: $(".wrapper").offset().top},'slow');
	}
	
	
	function closeshowroom() { // Close Showroom
		$("#showroombg").fadeTo(500, 0, function() { $(this).hide(); });
		$("#showroom").fadeTo(500, 0, function() { $(this).hide(); });
	}
	
	$(".showroom").bind("click", function() { // Showroom Link
		showshowroom();
		return false;
	});
	
	$("#closure-sr").bind("click", function() {
		$("#showroombg").fadeTo(500, 0, function() { $(this).hide(); });
		$("#showroom").fadeTo(500, 0, function() { $(this).hide(); });
		return false;
	});
	
	// Videoroom
	$("#videoroom_content").load("/fileadmin/templates/videoroom.html");
		
	function showvideoroom() { // Open Videoroom
		$("#showroombg").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();
			});
			$(".buttons [id$=hover]").stop().fadeTo(250, 0, function() { $(this).hide(); });
		} else {
			$(".dropdown").stop().hide();
			$("[id$=dropdown]", $(".dropdown")).hide().addClass("hidden");
		}
	}
}


/****** 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;
	});
}

/****** 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(); });
		}
	});
}
