var hintIndex = 0;
function hintsHP(){
	var speed1 = "middle";
	var speed2 = "slow";
	
	if (hintIndex<8){
		// hpnavi ein- und ausblenden
		hintIndex++;
		if (hintIndex%2==1){
			$("#hpnav li span").eq((hintIndex-1)/2).fadeIn(speed1, hintsHP);								
		} else {
			$("#hpnav li span").eq(hintIndex/2-1).fadeOut(speed1, hintsHP);								
		}
	} else if (hintIndex < 10){
		// funktionsnavi einblenden
		hintIndex++;
		
		var fnavItemIndex = hintIndex-9;
		var fnavItem = $("#fnav a").eq(fnavItemIndex);
		
		if (fnavItemIndex>0){
			var prevFnavItem = $("#fnav a").eq(fnavItemIndex-1);
			$("img", prevFnavItem).attr("src", $("img", prevFnavItem).attr("src").replace("_h.png", ".png"));
		}
		$("img", fnavItem).attr("src", $("img", fnavItem).attr("src").replace(".png", "_h.png"));
		$("#fnav .sub-info").hide();
		$("#fnav .sub-info").text(fnavItem.attr("title"));
		$("#fnav .sub-info").fadeIn(speed2, hintsHP);
	} else if (hintIndex == 10){
		// letztess funktionsnavi-element wieder reseten
		$("#fnav .sub-info").text("");
		var prevFnavItem = $("#fnav a:last");
		$("img", prevFnavItem).attr("src", $("img", prevFnavItem).attr("src").replace("_h.png", ".png"));
	}
}

function bindHPNav(){
	$("#hpnav li").bind("mouseover",function(){
		$("span", this).show();
	});
	$("#hpnav li").bind("mouseout",function(){
		$("span", this).hide();
	});
}


function bindNavBlocks(){
	$(".fn-navblock").each(function(index, elm){
		$(elm).data("defaultText", $(".sub-info", elm).text());
		$("a", elm).each(function(linkindex, linkelm){
			var tmp = $("img", linkelm).attr("src").match(/_h\.png/);
			$(linkelm).data('defaultHighlight', ($.isArray(tmp) && tmp.length>0) ? 'y' : 'n');
		});
		$("a", elm).bind("mouseover", function(){
			if ($(this).data('defaultHighlight')!='y') $("img", this).attr("src", $("img", this).attr("src").replace(".png", "_h.png"));
			if ($(this).closest(".fn-navblock").attr("id") == "mnav") {
				/*
				var keyword = $(this).attr("title").replace(/^.* (\S+)$/g, "$1").toLowerCase();
				$(this).closest(".fn-navblock").find(".sub-info").html('<img src="img/mnav/'+keyword+'_txt.png" />');
				*/ 
			} else {
				$(this).closest(".fn-navblock").find(".sub-info").text($(this).attr("title"));
			}
		});
		$("a", elm).bind("mouseout", function(){
			if ($(this).data('defaultHighlight')!='y') $("img", this).attr("src", $("img", this).attr("src").replace("_h.png", ".png"));
			if ($(this).closest(".fn-navblock").attr("id") == "mnav") {
				//$(this).closest(".fn-navblock").find(".sub-info").html("");
			} else {
				$(this).closest(".fn-navblock").find(".sub-info").text($(this).closest(".fn-navblock").data("defaultText"));
			}
		});
	});
}

function buildBreadcrumbNav(){
	$("#navbreadcrumbs>span>a").each(function(index, elm){
		// aktuell ausgewaehltes element ermitteln
		var current = $(elm).closest("span").find(".hovernav strong");
		// vorgaenger in der liste ermitteln
		var prevCount = $(current).closest("li").prevAll("li").length;
		// vertikale positionierung anhand der vorgaenger setzen
		$(current).closest(".hovernav").css("top", (-8-prevCount*11-prevCount*(17-11))+"px");
		// link-breite auf container-breite setzen
		$(current).closest(".hovernav").find("a").width($(current).closest(".hovernav").width());										
	});
	// jeder link aktiviert seinen zugehoerigen layer
	$("#navbreadcrumbs>span>a").bind("mouseover", function(){
		$(this).closest("span").find(".hovernav").fadeIn("fast");
	});
	// jeder layer deaktiviert sich selbst
	$(".hovernav").bind("mouseleave", function(){
		$(this).fadeOut("fast");
	});
}

function showMagnifier(){
	var ajaxImages = $("a.nyroModal[href$='.png']>img,a.nyroModal[href$='.jpg']>img,a.nyroModal[href$='.gif']>img");
	ajaxImages.closest("a").css('position', 'relative').css('z-index', '10').append('<img src="img/magnifier.png" style="position: absolute; bottom: 8px; right: 8px; width: 18px; height: 20px; z-index: 10;" />')
}


$(document).ready(function () {
	if ($("#page.hp").length>0){
		bindHPNav();
		hintsHP();
	}
	buildBreadcrumbNav();
	bindNavBlocks();
	
	showMagnifier();
	
	$('#fnav a,a.mark-as-js-call').each(function(index, elm){
		$(elm).attr("href", $(elm).attr("href")+"?js=y");
	});
});
