var currLang="e";
var curSelId; var AnzHVs=0; var HVTopVis=-1; var FATopVis=0; var AnzFAs=0; var firmenListe; var HVListe; var curSelHv=0; var curSelComp=0; var AnzListPP=25;
	function hideMenu(idM) {
		if (curSelId!=idM) {
			$("#"+idM).children(".menuSubUL").hide("blind", 100);
		}
	}
	function hideAllOtherMenus(idM) {
		$(".menuTopLI").not("#"+idM).children(".menuSubUL:visible").hide("blind", 100);
	}

	function showMenu(idM) {
		$("#"+idM).children(".menuSubUL").not(":visible").show("blind", 100);
	}

$(document).ready(function() {
	//-----Initiierung der Site
	$.history.init(pageload);
	$(".menuSubUL").hide();
	$(".e, .d").hide();
	
function getFirmenListe(fill, show) {
	$.getJSON('ajax.php', {'task' : '2'})
	.success(function(data) {
		firmenListe=data;
		if (fill) {
			fillFirmenListe(firmenListe, "");
		}
		if (show) {
			showContentLayers("Firmen");
		}
	})
	.error(function() { alert('Fehler in der InternetverbindungFirmenListe!') });
}

function getHVListe(fill, show) {
	$.getJSON('ajax.php', {'task' : '1'})
	.success(function(data) {
		HVListe=data;
		if (fill) {
			fillHVListe(HVListe);
		}
		if (show) {
			showContentLayers("HV");
		}
	})
	.error(function() { alert('Fehler in der InternetverbindungFirmenListe!') });
}

	//-----Hilfsfunktionen
	function pageload(hash) {
		if(hash) {			
			var hs=hash.split("~");
			task=hs[0];
			currLang=hs[1];
			if (currLang=="d") {
				currLang="e";
			} else {
				currLang="d";
			}
			changeLng();
			switch (task) {
				case "hvf":
					curSelComp=hs[2];
					curSelHv=hs[3];
					showHvFacts();
					break;
				case "hvs":
					if (hs[2]=="-1") {HVTopVis=-1} else {HVTopVis=parseInt(hs[2])};
					if (HVListe==undefined) {
						getHVListe(true, true);
					} else {
						if (HVTopVis==-1) {HVTopVis=0};
						showHVs();
						showContentLayers("HV");
					}
					break;
				case "fas":
					FATopVis=parseInt(hs[2]);
					if (firmenListe==undefined) {
						getFirmenListe(true, true);
					} else {
						fillFirmenListe(firmenListe, "")
						//showFA();
						showContentLayers("Firmen");
					}
					break;
				case "imp":
					showContentLayers("Impressum");
					break;
			}
		} else {
			if (navigator.appName=="Microsoft Internet Explorer") {
				
				if (navigator.userLanguage.indexOf('de')>-1) {
					//alert(navigator.userLanguage);
					currLang="e";
				} else {
					currLang="d";
				}
			} else {
				if (navigator.language.indexOf('de')>-1) {
					currLang="e";
				} else {
					currLang="d";
				}
			}
			changeLng();
			getFirmenListe(true, false);
			location.hash="#hvs~"+currLang+"~-1";
		}
	}
	
	function changeLng() {
		$(".v"+currLang).hide();

		if (currLang=="d") {
			currLang="e";
		} else {
			currLang="d";
		}
		$(".v"+currLang).show();
	}
	
	//-----Menüfunktionen
	$(".menuTopLI").hover( 
		function() {
			$(this).children(".spn").css("color", "rgb(220, 0, 0)");
			if (curSelId!=$(this).attr("id")) {
				curSelId=$(this).attr("id");
				setTimeout("showMenu('"+$(this).attr("id")+"')",100);
				setTimeout("hideAllOtherMenus('"+$(this).attr("id")+"')", 100);
			}
			curSelId=$(this).attr("id");
		},
		function() {
			$(this).children(".spn").css("color", "#D4EEFB");
			setTimeout("hideMenu('"+$(this).attr("id")+"')", 100);
			curSelId="q";
		}
	)
	$(".menuSubLI").hover( 
		function() {
			$(this).children(".spn").css("color", "rgb(220, 0, 0)");
			curSelId=$(this).parent().parent().attr("id");
		},
		function() {
			$(this).children(".spn").css("color", "#D4EEFB");
			curSelId="q";
		}
	)
	
	//--Contentsteuerungsfunktionen	
	function showContentLayers(idL) {
			$(".h.ve").removeClass("ve").addClass("e");
			$(".h.vd").removeClass("vd").addClass("d");
			$(".h:visible").hide();
			$("."+idL+".e").removeClass("e").addClass("ve");
			$("."+idL+".d").removeClass("d").addClass("vd");
			$("."+idL+".v"+currLang).show();
			if (idL=="Firmen") {
				$("#txtSrchFa").focus();
			}
	}
	
	function fillFirmenListe(dta, fltrStrg) {
		FADiv=$(".Firmen");
		$(".faListEintr").remove();
		$.each(dta, function(idx, data) {
			if (fltrStrg!="") {
				if (data.strName.toLowerCase().indexOf(fltrStrg.toLowerCase())>-1) {
					FADiv.append("<div class='faListEintr' id='FAL"+data.IDCompany+";"+data.iHV+"'><b>"+data.strName+"</b></div>");
				}
			} else {
				FADiv.append("<div class='faListEintr' id='FAL"+data.IDCompany+";"+data.iHV+"'><b>"+data.strName+"</b></div>");
			}
		})
		AnzFAs=$(".faListEintr").length;
		showFA();
	}
	
	function showFA() {
		$(".faListEintr").slice(0, FATopVis).hide();
		$(".faListEintr").slice(FATopVis, FATopVis+AnzListPP).show();
		$(".faListEintr").slice(FATopVis+AnzListPP).hide();
		if (FATopVis==0) {
			$("#arrowLeftFA").hide();
			$("#txtSrchFa").css("margin-left","34%");
		} else {
			$("#arrowLeftFA").show();
			$("#txtSrchFa").css("margin-left","14%");
		};
		if (AnzFAs>FATopVis+AnzListPP+1) {
			$("#arrowRightFA").show();
			$("#txtSrchFa").css("margin-right","1%");
		} else {
			$("#arrowRightFA").hide();
			$("#txtSrchFa").css("margin-right","21%");
		};
	}
	
	$("#arrowRightFA").on("click", function() {
		FATopVis+=25;
		location.hash="#fas~"+currLang+"~"+FATopVis;
	})
	
	$("#arrowLeftFA").on("click", function() {
		FATopVis-=25;
		location.hash="#fas~"+currLang+"~"+FATopVis;
	})
	
	$("#txtSrchFa").keyup(function() {
		fillFirmenListe(firmenListe, $(this).val())
	})
	
	function fillHVListe() {
		var HVDiv=$(".HV"); var HVBezE;
		$(".hvListEintr").remove();

		$.each(HVListe, function(idx, data) {
			if (data.strBezchngOrigE=="" || data.strBezchngOrigE==null) {
				HVBezE=data.strBezchngOrig;
			} else {
				HVBezE=data.strBezchngOrigE;
			}
			if (parseInt(data.datD)<1 && HVTopVis==-1) {
				if (parseInt(idx)<25) {
					HVTopVis=0;
				} else {
					HVTopVis=(Math.floor((parseInt(idx)+1)/25)*25)-1;
				}
			}
			HVDiv.append("<div class='hvListEintr' id='HVL"+data.intIDCompany+";"+data.IDHV+"'><b>"+data.datH+" <i>&nbsp;"+data.strName+"</i></b>, <span style='display: none;' class='vd'>"+data.strBezchngOrig+"</span><span style='display: none;' class='ve'>"+HVBezE+"</span></div>");
		})
		if (HVTopVis==-1) {HVTopVis=0};
		$(".hvListEintr").children(".v"+currLang).css('display', 'inline');
		AnzHVs=$(".hvListEintr").length;
		showHVs();
	}
	
	$(".Firmen").on("click", ".faListEintr", function() {
		var id=$(this).attr("id").split(";");
		curSelHv=id[1];
		curSelComp=id[0].substr(3);
		location.hash="#hvf~"+currLang+"~"+curSelComp+"~"+curSelHv;
	})
	
	$(".HV").on("click", ".hvListEintr", function () {
		var id=$(this).attr("id").split(";");
		curSelHv=id[1];
		curSelComp=id[0].substr(3);
		location.hash="#hvf~"+currLang+"~"+curSelComp+"~"+curSelHv;
	})
	
	$(".hvFacts").on("click", ".faHvListEintr", function () {
		var id=$(this).attr("id").split(";");
		curSelHv=id[1];
		curSelComp=id[0].substr(3);
		location.hash="#hvf~"+currLang+"~"+curSelComp+"~"+curSelHv;
	})
	
	function showHvFacts() {
		$.get("hvdata.php", { comp: curSelComp, hv: curSelHv, lang: currLang })
			.success(function(data) {
				$(".hvFacts").html(data);
				$("#hvFactTabs").accordion({active: false, collapsible: true});
				
				showContentLayers('hvFacts');
				$("#hvFactTabs").accordion("resize");
			})
			.error(function() {alert('Fehler in der InternetverbindungHVFaListe!')});
	}
	
	function showHVs() {
		if (HVTopVis==0) {
			$(".hvListEintr").slice(AnzListPP).hide();
			$(".hvListEintr").slice(0, AnzListPP).show();
		} else {
			$(".hvListEintr").slice(0, HVTopVis).hide();
			$(".hvListEintr").slice(HVTopVis, HVTopVis+AnzListPP).show();
			$(".hvListEintr").slice(HVTopVis+AnzListPP).hide();
		}
		if (HVTopVis<AnzListPP) {$("#arrowLeft").hide();} else {$("#arrowLeft").show()}
		if (AnzHVs-HVTopVis<AnzListPP) {$("#arrowRight").hide();} else {$("#arrowRight").show()}
	}
	$("#arrowRight").on("click", function() {
		HVTopVis+=AnzListPP;
		location.hash="#hvs~"+currLang+"~"+HVTopVis;
	})
	
	$("#arrowLeft").on("click", function() {
		HVTopVis-=AnzListPP;
		location.hash="#hvs~"+currLang+"~"+HVTopVis;
	})
	
	$(".spn").on("click", function() {
		var id=$(this).parent().attr("id");
		if (id) {
			switch(id) {
				case "HV":
					HVTopVis=-1
					location.hash="#hvs~"+currLang+"~-1";
					break;
				case "Firmen":
					$("#txtSrchFa").val('');
					location.hash="#fas~"+currLang+"~0";
					break;
				case "Links":
					
					break;
				case "Locations":
					
					break;
				case "Impressum":
					location.hash="#imp~"+currLang;
					break;
				case "English":
					changeLng();
					break;
			}
		}
	})
})
