var letter = "";
var origHeight = 0;
var eventHandler = new Array();

eventHandler['load'] 	= new Array();
eventHandler['resize'] 	= new Array();

if (navigator.appName.indexOf("tscape") != -1) document.captureEvents(Event.KEYPRESS);

document.onkeypress = function (keyed) {
	input = (navigator.appName.indexOf("tscape") !=-1) ? keyed.which : event.keyCode;
	letter+=String.fromCharCode(input);
	if(letter=="login" || letter=="admin" ) {
		if ( typeof pid != 'undefined' ) location.href = "p-"+pid+".php?othertempl=login.php";
		else if ( typeof mid != 'undefined' ) location.href = "m-"+mid+".php?othertempl=login.php";
		else if ( typeof ch  != 'undefined' ) location.href = "index.php?ch="+ch+"&othertempl=login.php";
		else location.href = "index.php?othertempl=gt_login.php";
	}
}

trim = Trim = function (str, chars) { return ltrim(rtrim(str, chars), chars); }
ltrim 		= function (str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); }
rtrim 		= function (str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); }
mail_to 		= function (subject,domainpart,localpart) { location.href = 'mailto:' + localpart + "@" + domainpart + (( subject != '' ) ? ( '?subject=' + subject) : '' ); }

var ajaxworking=false;
$(function() {			  
	$.getScript("javascript/win.js");	// qalidator
	ie6fix();
	
	$("#main").css({marginTop:-$("#main").height()/2});
		
	$("#logo a").click(function(ev){
		$("#menublock1 a.active").removeClass("active");
		$("#menublock2 a.active").removeClass("active");
	});
		
	$("#menublock1 li a").click(function(ev){
		ajaxget(this,ev);
		$("#menublock1 a.active").removeClass("active");
		$("#menublock2 a.active").removeClass("active");
		$(this).addClass("active");
	});
	$("#menublock2 li a").click(function(ev){
		ajaxget(this,ev);
		$("#menublock1 a.active").removeClass("active");
		$("#menublock2 a.active").removeClass("active");
		$(this).addClass("active");
	});
	$("a.logo").click(function(ev){
		ajaxget(this,ev);
	});
	animateChair();
	
	hooks(); // after load
	hookreflinks(); // after load
	
});

function animateChair() {
	if ($("#sesselrot").length != 0) {
		window.setTimeout(function(){ 
			$("#sesselrot").rotateAnimation(-25);
			window.setTimeout(function(){ 
				$("#sesselrot").rotateAnimation(15);
				window.setTimeout(function(){ 
					$("#sesselrot").rotateAnimation(0);
				}, 150);
			}, 150);
		}, 150);
	}
}
	
var actrefimg=1;
function hooks(){
	jQuery.each($("a:contains('[at]')"),function(){$(this).html($(this).html().replace('[at]','@'));}); // [at] -> @
	$("a#refprev").click(function(ev){
		prev();
		ev.preventDefault();	
	});
	$("a#refnext").click(function(ev){ 
		next();
		ev.preventDefault();			
	});
	$("#imgul a.fancy").fancybox({ 	'zoomSpeedIn': 500, 
												'zoomSpeedOut': 500, 
												'zoomOpacity': true, 
												'overlayShow':true,
												'hideOnContentClick':false,
												'callbackOnShow':function(){
														if(ie(6)) runPngFix();
													}
												});
	if(_go=="n2_team_galerie.php"){
		startTeamAni();
	}

	if(ie(6)) runPngFix();
	//if(ie(6)){ DD_belatedPNG.fix('img, div'); }
}
function hookreflinks(){
	$("#reflinks a").click(function(ev){
		ajaxget(this,ev,"getImgs=true","refdiv");
		$("#reflinks a.active").removeClass("active");
		$(this).addClass("active");
	});
}

function next(){
	actrefimg=$("#li_"+(actrefimg+1)).length?actrefimg+1:1;		
	$("#imgdiv").scrollTo($("#li_"+actrefimg),200);
}
function prev(){
	actrefimg=$("#li_"+(actrefimg-1)).length?actrefimg-1:$("#imgul").children().length;
	$("#imgdiv").scrollTo($("#li_"+actrefimg),200);
}

function ajaxget(lnk,ev,addparam,targetid){
	var l;
	var q = lnk.href.indexOf("?")==-1?"?":"&";
	if($(lnk)[0].rel){ l = "/m-" + $(lnk)[0].rel.split("l_")[1] + ".php?aj=true"; }
	else{	l = lnk.href+q+"aj=true"+(addparam?"&"+addparam:""); }
	
	var cMen;
	if (lnk.href.indexOf("m-") > -1) {
		cMen = "mid=" + lnk.href.split("m-")[1].replace(".php","");
	} else if (lnk.href.indexOf("p-") > -1) {
		cMen = "pid=" + lnk.href.split("p-")[1].replace(".php","");
	}
	$.ajax({ url: "/includes/getStartMen.php?"+cMen,
			 success: function(msg) {
				 if (msg == "y") $("#inhalt").addClass("start");
				 else $("#inhalt").removeClass("start");
			 }
	});

	var i = $("#"+(targetid || "inhalt"));
	var c = $("#content");
	i.animate({opacity:0},200, function(){
		progress("show");												
		$.ajax({ url: l,
			success: function(msg){
				if(!targetid){
					var ch=c.height(); 				// contenthöhe speichern
					c.css({height:ch});				// contenthöhe fixieren
					var oldiheight = i.height();	// inhalthöhe speichern
					i.html(msg.split('<script type="text/javascript" language="javascript">')[0]); // neuen content einsetzen
					var newiheight = i.height();	// neue inhalthöhe speichern
					i.css({height:oldiheight});	// inhalthöhe auf gespeicherte inhalthöhe setzen
					c.css({height:""});				// fixierte contenthöhe löschen
															
					// inhalthöhe auf neue animieren und höhe löschen
					if($(lnk).is("#menublock1 a")){
						i.addClass("bg");
					}else{
						i.removeClass("bg");	
					}
					
					i.animate({height:newiheight,opacity:1},500,function(){ i.css({height:""});}); 
					$("#main").animate({marginTop:-($("#main").height()+(newiheight-oldiheight))/2},500);
					hookreflinks(); // nicht in hooks weil sonst die reflinks mehrere clickhandler bekommen
				}else{ // ref
					i.html(msg.split('<script type="text/javascript" language="javascript">')[0]); // neuen content einsetzen
					i.css({opacity:""}).hide().fadeIn(500);
				}
				actrefimg=1;
				progress("hide");
				hooks();
			}
		});
	});

	ev.preventDefault();
}

function startTeamAni(){	
	var imgdiv = $("#imgdiv"); //imgdiv
	
	if (imgdiv.length > 0) {
		var imgs = $("#imgdiv").children();
			
		$("#infodiv").css({"top":$("#reflinks a.active").offset().top - $("#inhalt").offset().top + 7});
		
		/*$(imgs[0]).fadeIn(0, function(){
			$(imgs[1]).fadeIn(0, function(){
				$(imgs[2]).fadeIn(0);
				$(imgs[1]).fadeOut(0);
			});
			$(imgs[0]).fadeOut(0);
		});*/
		
		window.setTimeout(function(){ 
			$("#infodiv").fadeIn(600,"easeInQuint");	
			imgdiv.animate({marginLeft:0},1000,"easeOutQuint");
			$(imgs[0]).show(0); 	
			window.setTimeout(function(){ 
				$(imgs[1]).show(0); 
				$(imgs[0]).hide(0); 
				window.setTimeout(function(){ 
					$(imgs[2]).show(0); 
					$(imgs[1]).hide(0); 
				}, 150);
			}, 150); 
		}, 300);
	} else {
		$("#imgdivteam_office a.fancy").fancybox({ 	'zoomSpeedIn': 500, 
												'zoomSpeedOut': 500, 
												'zoomOpacity': true, 
												'overlayShow':true,
												'hideOnContentClick':false,
												'callbackOnShow':function(){
														if(ie(6)) runPngFix();
													}
												});
	}
}

var loadingTimer,loadingFrame=1;
function progress(what){
	switch(what){
		case "show":
			html = '<div id="loader"><div></div></div>';
			$(html).appendTo("#content");
			$("#loader").fadeIn(100);
			loadingTimer = setInterval(animateLoad, 66);			
		break;
		case "hide":
			$("#loader").fadeOut(100);
			clearInterval(loadingTimer);
		break;				
	}
}
function animateLoad(){
	$("#loader > div").css('top', (loadingFrame * -40) + 'px');
	loadingFrame = (loadingFrame + 1) % 12;
}

$(window).resize(function(){
	ie6fix();
	setDims();
});
$(window).scroll(function () { 
	setDims();
});

function setDims(){
}

function ie(version){
	return $.browser.msie && $.browser.version==version;
}


function ie6fix(){
	if(ie(6)){
	}
}
