﻿// Globale Variablen
var id = 0; // Navigation
var sid = 0; // Subnavigation
var status = 0;
var firstloading = true;
var orderBrochureState = false;

// Initialisierung
function init(){
	logo = $(".logo");
	glanz = $(".glanz");
	claim = $(".claim");
	button_brochuere = $(".button_brochuere");
	header = $(".header");
	headerImages = $(".header img");
	content = $(".content");
	loadNavigation();
	setTimeout(function(){
		//claim.find("h1").text("Kompetent. Unabhängig. Flexibel.");
		//claim.css("top","-30px").animate({"top":"60px"},1000);
		logo.fadeIn(1000, function(){
			animateClaim();
			timeoutGlanz();
		});
	},500);
	logo.click(function(){
		id=0;
		bilder.eq(id).trigger("click");
	});
	button_brochuere.click(function(){
		orderBrochure();
	});
}

// Loop Glanzanimation
var clearGlanz = 0;
function timeoutGlanz(){
	animateGlow();
	clearGlanz = setTimeout("timeoutGlanz()",15000);
}

// Glanz Animation
var firstloadingGlow = true;
function animateGlow(){
	if(!firstloadingGlow){
		glanz.hide().stop(true, true).css({"top":"-15px", "left":"0px"}).show().animate({"top":"28px", "left":"90px"},900, function(){
			$(this).hide();
		});
	}else{
		glanz.hide();
		firstloadingGlow = false;
	}
}

// Claim Animation
var counter = 1;
var clear = 0;
function animateClaim(){
	text = "Kompetent. Unabhängig. Flexibel.";
	txtLength = text.length;
	if(counter<=txtLength){
		string = text.substring(0,counter);
		claim.find("h1").text(string);
		counter++;
		clear = setTimeout("animateClaim()",70);
	}else{
		if(clear!=0){clearTimeout("clear"); clear=0;}
	}
}

// Bestellbutton Broschüre
function orderBrochure(){
	orderBrochureState = true;
	id=3;
	sid=2;
	bilder.each(
		function(){
			$(this).attr("src", $(this).data("normal")).data("status", 0).css({cursor:"pointer"});
		}
	);
	bilder.eq(id).attr("src",bilder.eq(id).data("over")).data("status", 1).css({cursor:"default"});
	loadContent(id);
}

// Navigation
function loadNavigation(){
	// Bilder mit Daten versehen
	bilder = $(".navi img");
	newNavImgObj = [];
	newNavImgObj2 = []; 
	bilder.each(
		function(i){
			var dateiname = $(this).attr('src');
			var name = dateiname.slice(0, dateiname.length-4);
			var typ = dateiname.slice(dateiname.length-4, dateiname.length);
			var dateiname1 = name+typ;
			var dateiname2 = name+"2"+typ;
			newNavImgObj[i] = new Image();
			newNavImgObj[i].src = dateiname1;
			newNavImgObj2[i] = new Image();
			newNavImgObj2[i].src = dateiname2;
			$(this).data("normal", newNavImgObj[i].src).data("over", newNavImgObj2[i].src).data("status", 0).data("menu", i);
		}
	);
	// Startzustand setzen
	bilder.eq(id).attr("src", bilder.eq(id).data("over")).data("status", 1).css({cursor:"default"});
	setMenu();
	loadContent(id);
}

function setMenu(){
	// Rollover
	if(navigator.userAgent.indexOf("iPad")==-1 && navigator.userAgent.indexOf("iPhone")==-1){
		bilder.hover(
			function(){
				if($(this).data("status")==0){
					$(this).attr("src", $(this).data("over"));
				}
			},
			function(){
				if($(this).data("status")==0){
					$(this).attr("src", $(this).data("normal"));
				}
			}
		);
	}
	// Menüpunkt auswählen
	bilder.click(
		function(){
			if($(this).data("status")==0){
				bilder.each(
					function(){
						$(this).attr("src", $(this).data("normal")).data("status", 0).css({cursor:"pointer"});
					}
				);
				$(this).attr("src",$(this).data("over")).data("status", 1).css({cursor:"default"});
				// Subnavigation
				sid = 0;
				// Content laden
				loadContent($(this).data("menu"));
			}
		}
	);
}

function loadContent(pId){
	// Inhalte laden
	id = pId;
	if(clearGlanz!=0){
		clearTimeout(clearGlanz);
		clearGlanz=0;
		timeoutGlanz();
	}
	animateGlow();
	switch(id){
		case 0:
			content.load("php/unternehmen.php", function(){
				loadSubnavigation();
			});
		break;
		case 1:
			content.load("php/bausteine.php", function(){
				loadSubnavigation();
			});
		break;
		case 2:
			content.load("php/news.php", function(){
				loadSubnavigation();
			});
		break;
		case 3:
			content.load("php/kontakt.php", function(){
				loadSubnavigation();
			});
		break;
		case 4:
			content.load("php/impressum.php", function(){
				loadSubnavigation();
			});
			header.load("php/header_impressum.php");
			header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
		break;
	}
}

// Subnavigation
function loadSubnavigation(){
	subnavi = $(".subnavi ul li");
	subnaviBox = $(".subnavi");
	subnaviBox.hide();
	slideSubnavi();
	//subnavi.hide();
	//animateNavi();
	subnavi.each(
		function(i){
			$(this).data("status", 0).data("menu", i);
		}
	);
	// Startzustand setzen
	subnavi.eq(sid).addClass("active").data("status", 1).css({cursor:"default"}).find("span").addClass("active");
	if(subnavi.eq(sid).hasClass("newsstyle")){
		subnavi.eq(sid).css("color","#999");
	}
	setSubnaviMenu();
	loadSubnaviContent(sid);
}

// Subnavigation animieren
function slideSubnavi(){
	subnaviBox.slideDown(700);
}

// Menüpunkte animieren Subnavigation
var j=0;
function animateNavi(){
	if(j<subnavi.length){
		subnavi.eq(j).show();
		j++;
		setTimeout("animateNavi()",200);
	}else{
		j=0;
	}
}

function setSubnaviMenu(){
	// Rollover
	if(navigator.userAgent.indexOf("iPad")==-1 && navigator.userAgent.indexOf("iPhone")==-1){
		subnavi.hover(
			function(){
				if($(this).data("status")==0){
					$(this).addClass("active").find("span").addClass("active");
					if($(this).hasClass("newsstyle")){
						$(this).css("color","#999");
					}
				}
			},
			function(){
				if($(this).data("status")==0){
					$(this).removeClass("active").find("span").removeClass("active");
					if($(this).hasClass("newsstyle")){
						$(this).css("color","#FFF");
					}
				}
			}
		);
	}
	// Menüpunkt auswählen
	subnavi.click(
		function(){
			if($(this).data("status")==0){
				subnavi.each(
					function(){
						$(this).removeClass("active").data("status", 0).css({cursor:"pointer"}).find("span").removeClass("active");
						if($(this).hasClass("newsstyle")){
							$(this).css("color","#FFF");
						}
					}
				);
				$(this).addClass("active").data("status", 1).css({cursor:"default"}).find("span").addClass("active");
				if($(this).hasClass("newsstyle")){
					$(this).css("color","#999");
				}
				loadSubnaviContent($(this).data("menu"));
			}
		}
	);
}

function loadSubnaviContent(pSid){
	// Inhalte laden
	sid = pSid;
	category = $(".category");
	category.hide();
	newsbox = $(".newsbox");
	if(id==0){
		category.eq(pSid).show();
		newsbox.load("php/newsentries.php", function(){
			news = $(".news li");
			news.mouseover(function(){
				$(this).css("color","#999");
			}).mouseout(function(){
				$(this).css("color","#FFF");
			}).click(function(){
				bilder.eq(2).trigger("click");
				sid=news.index($(this));
			});
		});
		switch(pSid){
			case 0:
				if(!firstloading){
					header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
					header.load("php/header_philosophie.php");
				}else{
					header.load("php/header_philosophie.php");
					firstloading = false;
				}
			break;
			case 1:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_netzwerk.php");
				
			break;
			case 2:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_agb.php");
				
			break;
		}
	}else if(id==1){
		category.eq(pSid).show();
		newsbox.load("php/newsentries.php", function(){
			news = $(".news li");
			news.mouseover(function(){
				$(this).css("color","#999");
			}).mouseout(function(){
				$(this).css("color","#FFF");
			}).click(function(){
				bilder.eq(2).trigger("click");
				sid=news.index($(this));
			});
		});
		switch(pSid){
			case 0:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_bausteine.php");
			break;
			case 1:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_kredit.php");
			break;
			case 2:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_kredit.php");
			break;
			case 3:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_factoring.php");
			break;
			case 4:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_factoring.php");
			break;
			case 5:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_factoring.php");
			break;
			case 6:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_factoring.php");
			break;
			case 7:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_factoring.php");
			break;
			case 8:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_factoring.php");
			break;
			case 9:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_projekt.php");
			break;
			case 10:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_kaution.php");
			break;
			case 11:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_vertrauen.php");
			break;
			case 12:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_financial.php");
			break;
		}
	}else if(id==2){
		category.eq(pSid).show();
		newsbox.load("php/newsentries.php", function(){
			$(".news").addClass("subnavi").css("margin",0);
			news = $(".news li");
			news.addClass("newsstyle");
			subnavi = $(".subnavi ul li");
			subnavi.each(
				function(i){
					$(this).data("status", 0).data("menu", i);
				}
			);
			subnavi.eq(sid).addClass("active").data("status", 1).css({cursor:"default"}).find("span").addClass("active");
			if(subnavi.eq(sid).hasClass("newsstyle")){
				subnavi.eq(sid).css("color","#999");
			}
			setSubnaviMenu();
		});
		header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
		header.load("php/header_news.php");
	}else if(id==3){
		category.eq(pSid).show();
		newsbox.load("php/newsentries.php", function(){
			news = $(".news li");
			news.mouseover(function(){
				$(this).css("color","#999");
			}).mouseout(function(){
				$(this).css("color","#FFF");
			}).click(function(){
				bilder.eq(2).trigger("click");
				sid=news.index($(this));
			});
		});
		$(".vita").css("display","none");
		switch(pSid){
			case 0:
				$(".kontakt img").click(function(){
					if(status==0){
						status=1;
						$(this).next().slideToggle(700, function(){
							status=0;
						});
					}
				});
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_kontakt.php");
			break;
			case 1:
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_anfahrt.php");
			break;
			case 2:
				if(!orderBrochureState){
					newBrochure = $(".broschuere").clone().css({"float":"left", "display":"block", "margin-right":0});
					$(".form_broschuere .submit").css({"clear":"both", "display":"block"}).before(newBrochure);
					$(".form_broschuere .broschuere").first().remove();
				}else{
					newBrochure = $(".broschuere").clone().css({"float":"right", "margin-right":"59px"});
					$(".form_broschuere").prepend(newBrochure);
					$(".form_broschuere .broschuere").last().remove();
					orderBrochureState = false;
				}
				download = $(".download");
				if(navigator.userAgent.indexOf("iPad")==-1 && navigator.userAgent.indexOf("iPhone")==-1){
					download.html("Onlineversion Unternehmensbroschüre: <a href='broschuere/index.html' target='_blank'>Deutsch</a> | <a href='broschuere_eng/index.html' target='_blank'>Englisch</a>");
				}else{
					download.html("Download Unternehmensbroschüre: <a href='download/broschuere_cfg.pdf' target='_blank'>Deutsch</a> | <a href='download/broschuere_cfg_eng.pdf' target='_blank'>Englisch</a>");
				}
				header.css("background", "url("+headerImages.eq(0).attr("src")+") no-repeat");
				header.load("php/header_kontakt.php");
			break;
		}
	}else if(id==4){
		category.eq(pSid).show();
		newsbox.load("php/newsentries.php", function(){
			news = $(".news li");
			news.mouseover(function(){
				$(this).css("color","#999");
			}).mouseout(function(){
				$(this).css("color","#FFF");
			}).click(function(){
				bilder.eq(2).trigger("click");
				sid=news.index($(this));
			});
		});
	}
}

// Initialisierung
$(document).ready(function(){
	init();
});
