window.addEvent("domready",function(e){
	var act=$("topnav").getElement(".active");
	if(act!==null) if(act.getPrevious()!==null) act.getPrevious().id="noLining";
	
	new imageResizer().init();
	if($$(".newsPrviewPic").length>1)
	$$(".newsPrviewPic").each(function(ite,ind){if(ind%2==0) ite.setStyles({clear:"left",marginRight: "14px"});/*else ite.setStyles({clear:"left",marginRight: "14px"});*/});
	
	wetter.delay(100);
});

function wetter()
{
	if($$(".wetter")[0]===undefined) return;
	var iF=new Element("iframe",{src:"wetter.html", scrolling:"no", border:"0", frameborder:"0", width:"170px", height:"200px", scrolling:"none"});
	$$(".wetter")[0].adopt(iF);
}

function printFunc()
{
	try
	{
		var c=$("content").innerHTML;
		$(document.body).innerHTML=c;
		$(document.body).setStyles({
		backgroundColor: "#fff"
		});
	}
	catch(e){}
	return false;
}


GDBManger=function()
{
	this.init=function(url)
	{
		this.url=url;
	}
	
	this.delReq=function(id)
	{
		var Check = confirm("Wollen Sie diesen Eintrag wirklich löschen?");
		if(Check===false) return;
		this.req('action=delete&gbEntryId='+id,id,function(responseJSON,responseText,id){if(responseJSON===true) $("gdbEntry"+id).dispose();}.bind(this));
	}
	
	this.editReq=function(id,willPost)
	{
		if(willPost==null) willPost="";
		this.req(willPost+'&action=edit&gbEntryId='+id,id,function(responseJSON,responseText,id){
			
			if(responseJSON["isMSG"]===true) 
			{
				if(responseJSON["reload"]===true) 
				{
					addOverlay(new Element("div",{html:responseJSON.html,styles: {border: "3px solid #ccc",backgroundColor: "#fff",padding:"30px"} }));
					document.location.reload();
				}
				else alert(responseJSON.html);
				return;
			}
			var ddd=new Element("div",{html:responseJSON.html,styles: {border: "3px solid #ccc",backgroundColor: "#fff",padding:"30px"} });
			addOverlay(ddd);
			ddd.getElements("script").each(function(ite,ind){eval(ite.innerHTML);});
			new Element("div",{
				text: "absenden",
				"class": "flatbutton",
				events:
				{
					"click": function(e){
						e.stop();
						
						var x=new Object();
						$$('form["name=gdb]')[0].getElements("input").each(function(ite,ind){
							x[ite.name]=ite.value;
						});
						x["kategorie"]=$$("select[name=kategorie]")[0].value;
						x["memo"]=$$("textarea[name=memo]")[0].value;
						closeOverlay();
						this.editReq(id,Object.toQueryString(x));
					}.bind(this)
				}
			}).inject($$('input[name=senden]')[0],"after");
			
			new Element("div",{
					text: "schließen","class": "flatbutton",
					events: {"click": function(e){closeOverlay();}}
				}).inject($('leftSubmit'));
			
			$$('input[name=senden]')[0].type="hidden";
			
		}.bind(this));
		
	}
	
	this.changeActReq=function(id,act)
	{
		this.req('action='+(act===true?"act":"deact")+'&gbEntryId='+id,id,function(responseJSON,responseText,id){
			if(responseJSON!==true) return;
			$("gdbEntry"+id).className="gdb_block status"+(act===false?"n":"f");
			$("deactGdbEntry"+id).setStyle("display",(act===false?"none":"block"));
			$("actGdbEntry"+id).setStyle("display",(act===true?"none":"block"));
		}.bind(this));
	}
	
	this.req=function(postParms,id,syncFnc)
	{
		var myFunc=syncFnc;
		var errorFunc=function(){alert("Fehler, Aktion konnte nicht ausgeführt werden")};
		var myRequest = new Request.JSON(
		{
			url: this.url,
			onSuccess: function(responseJSON,responseText){closeOverlay(); if(responseJSON===false) errorFunc();else myFunc(responseJSON,responseText,id);}.bind(this),
			onError: errorFunc
		});
		addOverlay(new Element("div",{styles: {border: "3px solid #ccc",backgroundColor: "#fff",padding:"30px",fontSize:"18pt"},text: "bitte warten..."}));
		myRequest.post(postParms);
	}
}

/*function addOverlay(el)
{
	var div=cEm("div");
	div.id="overlay";
	div.style.position="absolute"
	div.style.top="0px";
	div.style.left="0px";
	div.style.height=$(document.body).getScrollSize().y+"px";
	div.style.width=$(document.body).getScrollSize().x+"px";
	div.style.zIndex=1000;
	$(document.body).insertBefore(div,$(document.body).firstChild);
	
	div=cEm("div");
	div.style.zIndex=1001;
	div.id="overlayInput";
	div.style.position="absolute";
	var backLink=cEm("a");
	backLink.href="#";
	backLink.title="Schließen";
	backLink.id="closeOverlayDivA";
	backLink.appendChild(document.createTextNode("x"));
	backLink.onclick=function(){closeOverlay(); return false;};
	
	div.appendChild(backLink);
	div.appendChild(el);
	
	$(document.body).insertBefore(div,$(document.body).firstChild);
	window.addEvent("resize",function()
	{
    newPosOverlay();
	});
	newPosOverlay();
}

function newPosOverlay()
{
  var boSz=$(document.body).getSize();
	var boSc=$(document.body).getScroll();
	var elSz=$("overlayInput").getSize();
	var t=Math.round((boSz.y-elSz.y)/2);
	t=(t<0)?0:t;
	$("overlayInput").style.top=t+boSc.y+"px";
	var l=Math.round((boSz.x-elSz.x)/2);
	l=(l<0)?0:l;
	$("overlayInput").style.left=l+boSc.x+"px";
}

function closeOverlay()
{
	$(document.body).removeChild($("overlay"));
	$(document.body).removeChild($("overlayInput"))
	return true;
}*/
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) 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();
		var fullSize={x: img.getAttribute("width"),y: img.getAttribute("height")};
		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.onComplete=function(){cI.dispose();};
			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);
	}
}
