window.addEvent("domready",function(e){
	if($$(".newsEntry").length==2) $$(".newsEntry")[1].addClass("rightn");
	$$('.nav')[0].getChildren("ul").each(function( elem,a,b ){slideNav(elem.getChildren("li"));});
	
	try{if(iReferences) smallElements(iReferences.els,iReferences.config);}catch(e){};
	//iReferences
	if($$(".element").length==3) BlenderWithNavImpl(true);
	new imageResizer().init();
	new imageAutoResizer().init();
	try{if(DATES) (new showDates(DATES)).show();}catch(e){}
	
	if($$("ul.catiReference").length!=0) 
	{
		$$("strong.catiReference").each(function(ite,ind){
			var myFx = new Fx.Slide($$("ul.catiReference")[ind]).hide();
			ite.setStyles({cursor:"pointer",fontWeight: "normal"});
			ite.addEvents({'click' : function(){myFx.cancel();myFx.toggle();}});			
		});
	}
	
	$$("a").each(function(ite){
		var l=ite.href;
		if(l.length>4) if(l.substr(l.length-4).toLowerCase()===".pdf") ite.target="_blank";
	});
	
	if($$(".kdTable").length!=0) $$(".kdTable").each(function(ite,ind){formatTable(ite);});
	
	if($$(".event").length!=0) 
	{
		var pgr=new paginator($$(".event"),10);
		var top=pgr.start();
		top.inject($$(".content h1")[0],"after");
		cloneEventsRecursive(top,top.clone(true)).inject($$(".content")[0],"bottom");
		pgr.select(null,0);
	}
	try{mapsload(mapsLoader[0],mapsLoader[1])}catch(e){};
});

paginator = function(els,elementsPerPage)
{
	this.els=els;
	this.elementsPerPage=elementsPerPage;
	this.pages=new Array();
	
	this.start=function()
	{
		if(this.els.length==0) return;
		for(var x=0;x<this.els.length;x++)
		{
			var p=Math.floor(x/(this.elementsPerPage));
			if($type(this.pages[p])!=="array") this.pages[p]=new Array();
			this.pages[p][this.pages[p].length]=this.els[x];
		}
		var div=new Element("div",{text:"Seite: ",'class': "paginatorLine",styles: {clear: "both","text-align":"right"}});
		for(var x=0;x<this.pages.length;x++)
		{
			var a=new Element("a",{href: "#",styles: {borderRight: "1px solid #C5A6CC", padding: "0 4px"},'class': "paginateNavLinkA "+x,text: x+1,events:{click: this.select.bindWithEvent(this,x)}});
			if(x==this.pages.length-1) a.setStyle("borderRight","none");
			div.adopt(a);
		}
		return div;		
	}
	
	this.select=function(e,cP)
	{
		if(e!=null) e.stop();
		for(var x=0;x<this.pages.length;x++)
		{
			for(var y=0;y<this.pages[x].length;y++)
			{
				if(x!=cP) this.pages[x][y].setStyle("display","none");
				else this.pages[x][y].setStyle("display","block");
			}
			$$(".paginateNavLinkA."+x).each(function(ite){if(x!=cP) ite.removeClass("active"); else ite.addClass("active");});
			//else $$(".paginateNavLinkA."+x).each(function(ite){ite.removeClass("active");});
		}
	}
}

/*function over()
{
	$$("img.geyscale").each(function(ite,ind){
		var src=ite.src;
		ite.src="greyimg.php?src="+src;
		
		ite.addEvent("mouseenter",function(e){ite.src=src});		
		ite.addEvent("mouseleave",function(e){ite.src="greyimg.php?src="+src});
	});
}*/

var showDates=function(DATES)
{
	this.dates=DATES;
	
	this.show=function()
	{
		if($$(".chunk.c82 div.chunkText").length!==1) return;
		loadCalendar($$(".chunk.c82 div.chunkText")[0],this.cellModFunc.bind(this),null,new Date(),"evcDates");
	}
	
	this.cellModFunc=function(date,td,aktDay)
	{
		var time=date.getTime().toString();
		time=time.substr(0,time.length-3);
		
		if(this.dates[time]!=undefined)
		{
			var events=this.dates[time];
			var b=new Element("div",{'class':"infoDate"});
			for(var e=0;e<events.length;e++) 
			{
				if(events[e].pid==28) b.adopt(new Element("strong",{text: "Seminar:"}));
				else b.adopt(new Element("strong",{text: "Veranstaltung:"}));
				b.adopt(new Element("br"));
				b.appendText(events[e].title);
				b.adopt(new Element("br"));
			}
			if(events.length==1) {
				var a=new Element("a",{href:events[0].url,text:td.get("text")});
				td.set("text","");
				mouseFollowerInfoBox(a,b);
				td.adopt(a);
			}
			else mouseFollowerInfoBox(td,b);
			td.addClass("isDated");
		}
	}
}

function mouseFollowerInfoBox(el,content)
{
	el.addEvent("mouseenter",function(e){
		if($("mousefollower")!=null) $("mousefollower").dispose();
		var div=new Element("div",{
			id: "mousefollower",
			styles:
			{
				position: "absolute",
				backgroundColor: "#fff",
				//padding: 10,
				border: "1px solid #000"
			}});
		$(document.body).adopt(div);
		div.adopt(content);
	});
	
	el.addEvent("mousemove",function(e){
		if($("mousefollower")!=null) $("mousefollower").setStyles(
		{
			left: e.page.x+10,
			top: e.page.y+10
		});
	});
	el.addEvent("mouseout",function(e){
		if($("mousefollower")!=null) $("mousefollower").dispose();
	});
}
/*
function resizeonMouseover(img,imgurl)
{
	img.addEvent("mouseenter",function(e){
		if($("mousefollower")!=null) $("mousefollower").dispose();
		var div=new Element("div",{
			id: "mousefollower",
			styles:
			{
				position: "absolute",
				backgroundColor: "#fff",
				//padding: 10,
				border: "1px solid #000",
			}});
		$(document.body).adopt(div);
		div.adopt(new loadImage().init(400,300,imgurl));
	});
	
	img.addEvent("mousemove",function(e){
		if($("mousefollower")!=null) $("mousefollower").setStyles(
		{
			left: e.page.x+10,
			top: e.page.y+10
		});
	});
	img.addEvent("mouseout",function(e){
		if($("mousefollower")!=null) $("mousefollower").dispose();
	});
}*/

function smallElements(els,config)
{
	var maxElements=24;
	if($$(".element").length==3) var zz=2;
	else var zz=0;
	$$(".element")[zz].addClass("specialElement");
	$$(".element")[zz].adopt(new Element("<div",{'class':"smallElementText"}));
	
	//els=shuffle(els);
	var elsX=els;
	if(els.length<maxElements) for(var i=els.length-1;i<=22;i++) elsX[elsX.length]="";
	
	elsX.each(function(ite,ind){
		var img;
		if(ind>=maxElements) return;
		var div=new Element("div",{'class':"smallElement"});
		if(ind==1 || /*ind==10 ||*/ ind==19 || ind==15)
		{
			$$(".element")[zz].adopt(div);
			div.set("title","Das könnte Ihr Platz sein");
			div=new Element("div",{'class':"smallElement"});
		}
		
		if(ite!="") 
		{
			img=new Element("img",{'class': "out",src: "greyimg.php?src="+ite.imgurl1.substr(1),events:{
				mouseover: function(e){this.src=ite.imgurl1;},
				mouseout: function(e){this.src="greyimg.php?src="+ite.imgurl1.substr(1);}
			}});
			
			mouseFollowerInfoBox(img,new loadImage().init(400,300,ite.imgurl));
			var a=new Element("a",{href: ite.link.replace("&amp;","&")});
			a.adopt(img);
			div.adopt(a);
		}
		/*else
		{
			div.addEvent("mouseover",function(e){console.log("juri");$$(".smallElementText")[0].set("text","Das könnte ihr Platz sein ;-)")});
			div.addEvent("mouseout",function(e){$$(".smallElementText")[0].set("text","Wann dürfen wir Ihre Internet-Präsenz gestalten?")});
		}
		
		if(ite!="") 
		{
			
		}*/
		$$(".element")[zz].adopt(div);
		$(document.body).adopt(new Element("img",{src: ite.imgurl1,styles:{display: "none"}}));
	});
	return;
}

window.addEvent("load",function(e)
{
	if($$(".bigPicContent").length!=0 && $$(".subNav").length!=0 && $$(".competencesWrapper").length!=0) 
	{
		setDivsSameMinHight($$(".bigPicContent")[0],$$(".subNav")[0]);
		$$(".content")[0].setStyle("min-height",115);
		setDivsSameMinHight($$(".content")[0],$$(".rigthButtons")[0]);
	}
	/*if($$(".content").length!=0 && $$(".rigthButtons").length!=0) 
	{
		if($$(".registerCardContent").length==0) 
		{
			var height=$$(".chunkContent")[0].getCoordinates().height.toInt()+$$(".subNav")[0].getCoordinates().height.toInt();
			var dif=$$(".contentWrapper")[0].getCoordinates().height.toInt();
			if(dif-height>120) $$(".rigthButtons")[0].setStyle("min-height",dif-height);
		}
		else 
		{
			var pos=$$(".rigthButtons")[0].getCoordinates();
			var delta=$$(".registerCardNavigation")[0].getCoordinates().bottom-pos.bottom;
			console.log(delta);
			if(delta>0) $$(".rigthButtons")[0].setStyle("height",delta+pos.height);
			else $$(".content")[0].setStyle("min-height",delta*-1);
			new showCardContent().show(0);
			//$$(".registerCardWrapper")[0].setStyles({position:"absolute", bottom: 0});
		}
	}*/
	if($$(".registerCardContent").length!=0) new showCardContent().show(0);
});

function formatTable(table)
{
	var trs=table.getElements("tr");
	for(var i=0;i<trs.length;i++)
	{
		trs[i].className=(i % 2)?"even":"odd";
	}
}


showCardContent=function ()
{
	this.cardNavigation=$$(".cardNavigation");
	this.cardContents=$$(".cardContent");
	
	this.init=function()
	{
		for(var i=0;i<this.cardNavigation.length;i++) this.cardNavigation[i].addEvent("mouseover",this.show.bind(this,i));
	}
	
	this.show=function(act)
	{
		var cN;
		for(var i=0;i<this.cardNavigation.length;i++) 
		{
			cN=this.cardNavigation[i].className;
			if(cN.indexOf("active")!=-1) cN=cN.substr(0,cN.length-7);
			if(act==i) cN+=" active";
			this.cardNavigation[i].className=cN;
			
			cN=this.cardContents[i].className;
			if(cN.indexOf("active")!=-1) cN=cN.substr(0,cN.length-7);
			if(act==i) cN+=" active";
			this.cardContents[i].className=cN;
			
		}
	}
	
	this.init();
}

function slideNav(lis,x)
{
	lis.each( function( elem,a,b ){	
		var list = elem.getChildren('ul');
		
		if(!list || list.length!=1) return;
		list[0].setStyles({"display":"block",position: "absolute",width: 123});
		
		var myFx = new Fx.Slide(list[0],{mode:(x===true)?'horizontal':'vertical'}).hide();
		
		elem.addEvents({
			'mouseenter' : function(){
				myFx.cancel();
				myFx.slideIn();
			},'mouseleave' : function(){
				myFx.cancel();
				myFx.slideOut();
			}
		});
	});
}

function setDivsSameMinHight(el1,el2)
{
	if(el1==null || el2==null) return;
	var padMar=el2.getStyle("padding-bottom").toInt()+el2.getStyle("padding-top").toInt()+el2.getStyle("margin-top").toInt();
	el2.setStyle("min-height",el1.getSize().y-padMar);
}
		
function mapsload(locations,locationsAll) 
{
  if (GBrowserIsCompatible())
  {
	var div=new Element("div",{styles: {width: 733,height: 342}});
	
	$$(".smallPic")[0].adopt(div);
	//div.inject($$(".smallPic")[0],"top");
	var map = new GMap2(div);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var point = new GLatLng(locationsAll[0],locationsAll[1]);
    map.setCenter(point, locationsAll[2],G_NORMAL_MAP);
    click1=new Array(locations.length);
    for(i=0;i<locations.length;i++) map.addOverlay(createMarker(i,locations[i],locationsAll[2],map));
  }
}

function createMarker(nr,locArray,zoomDec,map)
{
	var point = new GLatLng(locArray[0],locArray[1]);
	var marker = new GMarker(point,{title: locArray[3]});
	click1[nr]=0;
	GEvent.addListener(marker, "click",function(a) {
		zoom=(click1[nr]==0)?locArray[2]:zoomDec;
		map.setCenter(point,zoom,G_NORMAL_MAP);
		click1[nr]++;
		click1[nr]=(click1[nr]==2)?0:click1[nr];
	});	
	return marker;
}

imageAutoResizer=function()
{
	this.init=function()
	{
		this.iR=new imageResizer();
		var imgs=$$(".main img");
		for(var i=0;i<imgs.length;i++)
		{
			if(imgs[i].parentNode.className=="resizeOnClick" || imgs[i].alt=="captcha" || imgs[i].parentNode.nodeName.toLowerCase()=="a") continue;
			var img=new Element("img");
			img.addEvent("load",this.parseImg.bind(this,new Array(img,imgs[i])));
			img.src=imgs[i].src;
		}
	}
	
	this.parseImg=function(imgNew,imgOld)
	{
		if(imgOld.width==imgNew.width) return true;
		else this.slimIt(imgOld);
	}
	
	this.slimIt=function(el)
	{
		var a=new Element("a",{href:el.src});
		a.wraps(el);
		this.iR.addImage(a);
	}
}

imageResizer=function()
{
	this.duration=1000;
	
	this.init=function()
	{
		var links=$$(".resizeOnClick");
		if(links.length==0) return;
		for(var i=0;i<links.length;i++) this.addImage(links[i]);
	}
	
	this.addImage=function(link)
	{
		var img=new Image();
		img=$(img);
		img.setStyles({display:"none"});
		$(document.body).adopt(img);
		img.onload=function(){this.setAttribute("width",this.width);this.setAttribute("height",this.height);};
		img.src=link.href;
		var sI=link.getElements("img");
		if(sI.length!=1 || sI[0].alt=="captcha") return false;
		link.addEvent("click",function(e){e.stop();this.show(img,sI[0])}.bind(this));
		link.target="_blank";
		return true;
	}
	
	this.show=function(img,sI)
	{
		var cI=img.clone(true);
		var fullSize={x: img.getAttribute("width"),y: img.getAttribute("height")};
		if(fullSize.x>800) 
		{
			fullSize.y=800/fullSize.x*fullSize.y;
			fullSize.x=800;			
		}
		
		if(fullSize.y>800) 
		{
			fullSize.x=800/fullSize.y*fullSize.x;
			fullSize.y=800;			
		}
		
		
		
		if(sI.getSize().y*1.5>=fullSize.y) return;		
		cI.setStyles(
		{
			width: sI.getSize().x,
			height: sI.getSize().y,
			top: sI.getCoordinates().top,
			left: sI.getCoordinates().left,
			position: "absolute",
			display: "block",
			zIndex: 3,
			border: "0px solid #000"
		});
		
		var e=new Fx.Morph(cI, {duration: this.duration});
		var borderAndPaddingOffset=3;
		e.start(
			{
				width: fullSize.x,
				height: fullSize.y,
				borderWidth: 3,
				top: sI.getCoordinates().top-Math.round((fullSize.y-sI.getSize().y)/2)-borderAndPaddingOffset,
				left: sI.getCoordinates().left-Math.round((fullSize.x-sI.getSize().x)/2)-borderAndPaddingOffset
			}
		);
		cI.addEvent("click",function(x){
			e.cancel();
			e.addEvent("complete",cI.dispose.bind(cI));
			e.start(
			{
				width: sI.getSize().x,
				height: sI.getSize().y,
				top: sI.getCoordinates().top,
				left: sI.getCoordinates().left,
				backgroundColor: "#fff",
				borderWidth: 0
			});
		});
		$(document.body).adopt(cI);
	}
}

/*function resizeImage(link)
{
	var img=link.getElements("img")[0];
	new Element("div",
	{
		styles:	
		{
			width: img.getSize().x,
			height: img.getSize().y,
			top: img.getCoordinates().top,
			left: img.getCoordinates().left,
			position: "absolute"
		}
	});
	div.adopt(bigPic);
	$(document.body).adopt(div);
}*/

function BlenderWithNavImpl(autostart)
{
	BlenderWithNav=new BlenderWithNav();
	
	if($$(".element").length==0) return;
	
	var nav=BlenderWithNav.init($$(".element"),function(i){
		var div=new Element("div",{text: i+1,'class':"blenderNavPoint"});
		return div;
	});
	
	BlenderWithNav.getBlenderDIV().className="elementBlender";
	if(autostart===true) BlenderWithNav.startFirst();
	BlenderWithNav.getBlenderDIV().inject($$(".bigPicContent")[0]);
	nav.inject($$(".bigPicContent")[0],"after");
	nav.className="blenderNav";
}

BlenderWithNav = function()
{
	this.b=new Blender();
	this.blenderDIV;
	this.navs=new Array();
	this.nav=new Element("div",{});
	this.startingFN=false;
	
	this.init=function(els,createNavPointFN)
	{
		this.blenderDIV=this.b.init(els);
		this.b.setStartingFN(this.effectStart.bind(this));
		if(els.length>1)
		{
			for(var i=0;i<els.length;i++)
			{
				this.navs[i]=createNavPointFN(i);
				if(i==0) this.navs[i].addClass("active");
				this.navs[i].addEvent("mouseenter",this.show.bind(this,i));
				this.nav.adopt(this.navs[i]);
			}
		}
		this.nav.addEvent("mouseenter",this.setStopAfterEffekt.bind(this,true));
		this.nav.addEvent("mouseleave",this.setStopAfterEffekt.bind(this,false));
		return this.nav;
	}
	
	this.setStopAfterEffekt=function(b)
	{
		this.b.setStopAfterEffekt(b);
	}
	
	this.setCompleteCurrentFN=function(fn)
	{
		this.b.setCompleteCurrentFN(fn);
	}
	
	this.effectStart=function(c)
	{
		for(var i=0;i<this.navs.length;i++)
		{
			if(this.navs[i].hasClass("active")) this.navs[i].removeClass("active");
			if(i==c) this.navs[i].addClass("active");			
		}
		if(this.startingFN!==false) this.startingFN(c);
	}	
	
	this.setStartingFN=function(fn)
	{
		this.startingFN=fn;		
	}
		
	this.getBlenderDIV=function()
	{
		return this.blenderDIV;
	}
	
	this.show=function(c)
	{
		this.b.show(c);
	}
	
	this.startFirst=function()
	{
		this.b.startFirst();
	}	
}

Blender = function()
{
	this.duration=1000;
	this.pause=5000;
	this.els=new Array();
	this.parent=new Element("div",{
	styles: {position: "relative"}
	});
	this.current=0;
	this.e=false;
	this.stopAfterEffekt=false;
	this.stop=0;
	this.startingFN=false;
	
	this.completeCurrentFN=false;
	
	this.setCompleteCurrentFN=function(fn)
	{
		this.completeCurrentFN=fn;
	}
	
	this.setStartingFN=function(fn)
	{
		this.startingFN=fn;
	}
	
	this.init=function(els)
	{
		this.max=els.length-1;
		for(var i=0;i<els.length;i++)
		{
			this.els[i]=els[i].dispose();
		}
		this.insertNew(0);
		this.parent.childNodes[0].setStyle("opacity",1);
		
		this.parent.addEvent("mouseenter",function(e){this.setStopAfterEffekt(true);}.bind(this));
		this.parent.addEvent("mouseleave",function(e){this.setStopAfterEffekt(false);}.bind(this));
		
		return this.parent;
	}
	
	this.setStopAfterEffekt=function(b)
	{
		this.stopAfterEffekt=b;
	}
	
	this.startFirst=function()
	{
		this.start.delay(this.pause,this);
	}
	
	this.show=function(i)
	{
		this.stop++;
		this.current=i;
		this.insertNew(this.current);
		this.startEffekt(i);
	}
	
	this.startEffekt=function(c)
	{
		if(this.startingFN!==false) this.startingFN(c);
		this.e.start({opacity: 1});
	}
	
	this.start=function()
	{
		if(this.stop>0) 
		{
			this.stop--;
			return;
		}
		if(this.stopAfterEffekt===true) 
		{
			this.start.delay(this.pause,this);
			return;
		}
		this.current=this.getNext();
		this.insertNew(this.current);
		this.startEffekt(this.current);
	}
	
	this.getNext=function()
	{
		if(this.current+1>this.max) return 0;
		else return this.current+1;
	}
	
	this.insertNew=function(c)
	{
		var el=cloneEventsRecursive(this.els[c],this.els[c].clone(true));
		
		el.setStyles({position: "absolute",display: "block",top: "0px",opacity: 0});
		//for IE8 bug?
		el.className=this.els[c].className;
		
		this.parent.adopt(el);
		this.e=new Fx.Morph(el, {duration: this.duration,onComplete: this.completeParticial.bind(this,c)});
	}
	
	this.completeParticial=function(c)
	{
		this.clear();
		this.start.delay(this.pause,this);
		if(this.completeCurrentFN!==false) this.completeCurrentFN(c);
	}
	
	this.clear=function()
	{
		var cN=this.parent.childNodes;
		if(cN.length>2) for(var i=0;i<cN.length-2;i++) //this.garbage[this.garbage.length]=cN[i].dispose();
		cN[i].setStyle("display","none");
	}
	this.garbage=new Array();
}

function mapsload(locations,locationsAll) 
{
  if (GBrowserIsCompatible())
  {
	var div=new Element("div",{styles: {width: 248,height: 248}});
	
	div.inject($$(".chunk.c132")[0]);
	//div.inject($$(".smallPic")[0],"top");
	var map = new GMap2(div);
    map.addControl(new GSmallMapControl());
    //map.addControl(new GMapTypeControl());
    var point = new GLatLng(locationsAll[0],locationsAll[1]);
    map.setCenter(point, locationsAll[2],G_NORMAL_MAP);
    click1=new Array(locations.length);
    for(i=0;i<locations.length;i++) map.addOverlay(createMarker(i,locations[i],locationsAll[2],map));
  }
}

function createMarker(nr,locArray,zoomDec,map)
{
	var point = new GLatLng(locArray[0],locArray[1]);
	var marker = new GMarker(point,{title: locArray[3]});
	click1[nr]=0;
	GEvent.addListener(marker, "click",function(a) {
		zoom=(click1[nr]==0)?locArray[2]:zoomDec;
		map.setCenter(point,zoom,G_NORMAL_MAP);
		click1[nr]++;
		click1[nr]=(click1[nr]==2)?0:click1[nr];
		/*if(/*locArray[4]==1 && * /click1[nr]!=0)
		{
			var myHtml=locArray[3];
			map.openInfoWindowHtml(point, myHtml);
		}*/
	});	
	return marker;
}
