/*
 * Persiehl Javascripts
 * Author: Tim Isenheim - freshlabs.de
 * 
 */
 
 var sctop = "260px";
 var scrolljump = 3;
 var subnavtimer;
 
window.onload = function(){
	popupWindows();
	fixMenu();
}

document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = new Array();
  
	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}
	return elements;
}

function fixMenu(){
	var uls = document.getElementsByClassName('subnav');
	for(i=0; i<uls.length; i++){
		var li;
		//alert(uls[i].className);
		uls[i].onmouseover = function(){
			li = this.parentNode.parentNode.parentNode.parentNode.parentNode;
			clearTimeout(menutimer);
			clearTimeout(subnavtimer);
			if(li.className == ''){
				//li.className = 'over';
			}
		}
		uls[i].onmouseout = function(){
			li = this.parentNode.parentNode.parentNode.parentNode.parentNode;
			subnavtimer = setTimeout("hideHoveredSubnav()", 700);
		}
		
		/* Hauptmenuepunkte */
		m = uls[i].parentNode.parentNode.parentNode.parentNode.parentNode;
		m.onmouseover = function(){
		//alert(this.className);
			// eventuelle Subnav-Aufrufe abbrechen
			clearTimeout(subnavtimer);
			/* Submenue erst zurueckhalten */
			if(this.className == ''){
				this.className = 'hidden';
			}
			if(this.className == 'active'){
				this.className = 'active hidden';
			}
			subnavtimer = setTimeout("showSubnav()", 700);
		}
		m.onmouseout = function(){
		//alert(this.className);
			clearTimeout(subnavtimer);
			if(this.className == '' || this.className == 'over'){ 
				this.className = 'active over';
			}
			if(this.className == 'active'){
				this.className = 'active current';
			}
			menutimer = setTimeout("hideHoveredSubnav()", 700);
		}	

	}


}


function showSubnav(){
	var uls = document.getElementById('nav').getElementsByTagName('li');
	for(i=0; i<uls.length; i++){
		if(uls[i].className == "hidden")
			uls[i].className = "over";
		if(uls[i].className == "active hidden")
			uls[i].className = "active";
	}
}

function hideHoveredSubnav(){
	var uls = document.getElementById('nav').getElementsByTagName('li');
	for(i=0; i<uls.length; i++){
		if(uls[i].className == "active over" || uls[i].className == "hidden")  // Pseudo-aktiven resetten
			uls[i].className = '';
		if(uls[i].className == "active current") // real aktiven sichtbar machen
			uls[i].className = "active";
	}
}

/* Scroller */

function scroll(element, direction){
	
	if(direction.indexOf("down") >= 0){
		document.getElementById(element).scrollTop += scrolljump;
	}
	else{
		document.getElementById(element).scrollTop -= scrolljump;
	}
	timer = setTimeout("scroll('"+element+"','" + direction + "', 3)", 10);
}




function stopScroll() {
	clearTimeout(timer);
}

function popupWindows() {
    if(!document.getElementsByTagName) {
         return;
    }
    var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var linkDest = anchor.getAttribute("href");
         var relIndex = anchor.getAttribute("rel");
         if(relIndex != null){
	         var relSplit = relIndex.split("|");
	         var windowAttributes = "";
	         if(relSplit[0] == "popup") {
	              if (relSplit[1] > scrW) {
	                 pW = scrW - 10;
	              }
	              else {
	                 pW = relSplit[1];
	              }
	              if (relSplit[2] > scrH) {
	                 pH = scrH - 40;
	              }
	              else {
	                 pH = relSplit[2];
	              }
	              scrX = (scrW - pW - 10) * .5;
	              scrY = (scrH - pH - 30) * .5;
	              var windowAttributes = "width=" + pW + ",height=" + pH + ",left=" + scrX + ",top=" + scrY + ",screenX=" + scrX + ",screenY=" + scrY;
	              windowAttributes += ",location=" + relSplit[4] + ",resizable=" + relSplit[4] + ",scrollbars=" + relSplit[4];
	              anchor.href = "javascript:popupWin('" + linkDest + "','" + windowAttributes + "')";
	         }
	      }
    }
}

function popupWin(link,attribs) {
    var popupWin = null;
    popupWin = window.open(link,'winPopup',attribs);  
}
