//----------------------------
// 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];}
}


//----------------------------
// std 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.');
	}
}


//maxlength for textareas
function setMaxLength(max, id, textprev, textnext){		
	var counter = document.createElement('div');
	var maxLength = max;
	counter.className = 'counter';
	
	if(!maxLength){return;}
	
	if(!textprev){
		var htmltextprev='Anzahl der Zeichen, die Sie noch eingeben k&ouml;nnen:';
	} else {
		var htmltextprev = textprev;
	}
	
	if(!textnext){
		var htmltextnext='';
	} else {
		var htmltextnext = textnext;
	}
	
	if (!id) {
		var x = document.getElementsByTagName('textarea');
		for (var i=0;i<x.length;i++){
			setMaxLength_Loop(counter, maxLength, htmltextprev, htmltextnext, x[i]);
		}
	} else {
		var x = $(id);	
		setMaxLength_Loop(counter, maxLength, htmltextprev, htmltextnext, x);
	}
}
//part of setMaxLength2
function setMaxLength_Loop(counter, maxLength, htmltextprev, htmltextnext, x ){
	var counterClone = counter.cloneNode(true);
	counterClone.relatedElement = x;
	counterClone.innerHTML = htmltextprev + ' <span aria-live="polite">0</span> ' + htmltextnext;
	x.parentNode.insertBefore(counterClone,x.nextSibling);
	x.relatedElement = counterClone.getElementsByTagName('span')[0];
	x.onkeyup = x.onchange = function(){
		var currentLength = this.value.length;
		if (currentLength > maxLength){	
			this.value=this.value.slice(0, maxLength);		
		}
		if (currentLength < maxLength){
			this.relatedElement.firstChild.nodeValue = maxLength-currentLength;
		} else { 
			this.relatedElement.firstChild.nodeValue = 0;
		}
	};
	x.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
}

//----------------------------
// pixljamr funktionen
//----------------------------
function openPixljamr(href) {
    var height,width,top,left,popup;
    if (screen.availWidth && screen.availWidth) {
        height = screen.availHeight>750 ? 750 : screen.availHeight;
        width = screen.availWidth>1150 ? 1150 : screen.availWidth;
        top = screen.availHeight>750 ? 50 : 0;
        left = screen.availHeight>1150 ? 50 : 0;
    } else {      
		height = 750;
        width = 1150;
        top = 50;
        left = 50;
    }
    popup = window.open(href, "PIXLjamR", "width="+width+",height="+height+",left="+left+",top="+top+",resizable=yes");
    if (window.focus) {
        popup.focus();
    }
    return false;
}

function openPolitikjamr(href){
	openPixljamr(href);
}

function closeForRegister(sid) {
    var href = '../div/account/registrieren.php';
    if (sid)
        href += '&sid='+sid;
    opener.location.href = href;
    self.close();
}

function closeForLogin(sid) {
    var href = 'opener.php';
    if (sid)
        href += '&sid='+sid;
    opener.location.href = href;
    self.close();
}

function closeApp(sid) {
    var href = 'collagen.php?mode=meine';
    if (sid)
        href += '&sid='+sid;
    opener.location.href = href;
    self.close();
}

function closeForEcard(cid, sid) {
    var href = 'ecard_senden.php?cid='+cid;
    if (sid)
        href += '&sid='+sid;
    opener.location.href = href;
    self.close();
}

function addEvent(obj, typ, fun) {
	if (obj.addEventListener) {
		obj.addEventListener( typ, fun, false); 
		return true; 
	} else if (obj.attachEvent) {
		return obj.attachEvent("on"+typ, fun);
	}
	return false;
}

$(document).observe("dom:loaded", function() {
	$(document.body).addClassName('has_js');
});

/* 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 ($('soverlay')) 
			window.soverlay = new Overlay($('soverlay'));

		// initialisiere das Layer am Footer bei Klick auf Fingerstern
		if ($('layertrigger') && $('sternlayer')) {
			$('layertrigger').observe('click', function(event) {				
				event.stop();
				if (soverlay.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;
		}		
	}
});

