//----------------------------
// basisfunktionen
//----------------------------

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//----------------------------
// eigene funktionen
//----------------------------

function showLayer(name){
	MM_showHideLayers(name,'','show');
}

function hideLayer(name){
	MM_showHideLayers(name,'','hide');
}



// begrenzung von zeichen in einer textarea auf maximal-laenge
function checkMax(field, max) {
	var fieldLength = field.value.length;
	if (fieldLength>max) {
		field.value = field.value.substr(0,max);
		alert('Es sind max. '+max+' Zeichen erlaubt.');
	}
}


// neue text textaerea maximallaenge
// maxlength for textareas => START
function setMaxLength()
{		
	var x = document.getElementsByTagName('textarea');	
	var counter = document.createElement('div');
	counter.className = 'counter';
	
	for (var i=0;i<x.length;i++)
	{
		if (x[i].getAttribute('maxlength'))
		{
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();						
		}

	}
}

function checkMaxLength()
{
	
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	
	if (currentLength > maxLength)
	{	
		//this.relatedElement.className = 'toomuch';
		this.value=this.value.slice(0, maxLength);		
	}
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}
// maxlength for textareas => END



/* sticky footer */

/**
 * Prueft, ob der verwendete Browser Internet Explorer 6 oder aelter ist.
 * @return boolean true, wenn User Agent IE6 oder aelter ist, sonst false
 */
function isIE6OrLess(){
	var agt=navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
	var is_minor = parseFloat(navigator.appVersion);
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	return (is_ie && (is_major == 4) && (agt.search(/msie [56]\./)!=-1) );
}

$(document).observe("dom:loaded", function() {
	
	// wenn wir mit nem normalen Browser hier sind, initialisiere
	// die dynamischen Funktionalitaeten der Seite
	if (!isIE6OrLess()) {
		
		// Füge dem body Klasse hinzu, damit im CSS zwischen JS und
		// Non-JS unterschieden werden kann
		$(document.body).addClassName('dynd');
		
		// initialisiere globales Overlay
		if ($('overlay')) 
			window.overlay = new Overlay($('overlay'));

		// initialisiere das Layer am Footer bei Klick auf Fingerstern
		if ($('layertrigger') && $('sternlayer')) {
			$('layertrigger').observe('click', function(event) {
				event.stop();
				if (overlay.show($('sternlayer'))) {
					$('sternlayer').down('h2.akm').writeAttribute('tabindex', "-1");
				}
			});
			
			$('sfooterlogo').observe('mouseover', function(event) {
				Effect.Queues.get('sfooter').each(function(effect) { effect.cancel(); });

				new Effect.Parallel([
						new Effect.Morph($('layertrigger').down("img"), {
							style: "width:142px;",
							sync: true
						}),
						new Effect.Morph($('sfooterlogo'), {
							style: "top:-20px;",
							sync: true
						})
					], {
						duration:0.5,
						delay:0.1,
						queue: {
							scope:'sfooter'
						}
					}
				);
			});
			
			$('sfooterlogo').observe('mouseout', function(event) {
				Effect.Queues.get('sfooter').each(function(effect) { effect.cancel(); });
				
				new Effect.Parallel([
						new Effect.Morph($('layertrigger').down("img"), {
							style: "width:75px;",
							sync: true
						}),
						new Effect.Morph($('sfooterlogo'), {
							style: "top:0px;",
							sync: true
						})
					], {
						duration:0.5,
						delay:0.1,
						queue: {
							scope:'sfooter'
						}
					}
				);
			});
		}
	}
});




var Overlay = Class.create({
	initialize: function(elm) {
		this.element = $(elm).hide();
		this.open = false;
		this.content = null;
		this.lastContent = null;
		
		this.element.observe('click', function(event) {
			event.stop();
			this.hide();
		}.bind(this));
	},
	show: function(content) {
		this.content = content;
		if (!this.open && this.content) {
			
			this.element.setStyle({
				height: ( Math.max($(document.viewport).getHeight(), $(document.body).getHeight()) )+"px"
			});
			
		
			this.element.appear({
				duration: 0.4,
				to:0.8,
				afterFinish: function() {
					this.open = true;
					document.fire("overlay:opened");
					
					if (this.content) {
						this.content.appear({duration:0.4});
					}
					
				}.bind(this)
			});
			
			this.lastContent = this.content;
			return true;
		}
		else if(this.content && this.content != this.lastContent) {
			this.lastContent.fade({duration:0.4});
			this.content.appear({duration:0.4});
			this.lastContent = this.content;
			return true;
		}
		else {
			return false;
		}
	},
	hide: function() {
		if (isIE6OrLess()) {
			return true;
		}
		
		if (this.open) {
			if (this.content) {
				this.content.fade({duration:0.4});
			}
			
			this.element.fade({
				duration: 0.4,
				afterFinish: function() {
					this.open = false;
					document.fire("overlay:closed");
				}.bind(this)
			});
			return true;
		}
		else {
			return false;
		}		
	}
});