/**
* LI-Menu-Functions
* (c)2005 TIMETOACT Software & Consulting, Cologne, Germany
* author: Michael Gollmick (MGO)
* requires a DOM compatible browser!
*/

var menuwidth=200; // describes the width of the pulldowns
var generalHoffset=-226; // a general horizontal offset for all pulldowns
var generalVoffset=-87; // a general vertical offset for all pulldowns
var inactiveCSSText=""; // no entry please... css does it all
var activeCSSText=""; // no entry please... css does it all

var timetohide=100;		 		 // the time for hiding a menu in mili seconds - should only be adjusted when experiencing problems
var loopmax=1000;		 		 // safety var for breaking loops if they run wild for whatever reason, only modify if you think loops are really running all this loops and your app is too slow

var openmainmenu=null;		 // objectstore for function dPD()
var openparent=null;		 		 // objectstore for submenus in function dPD()

var timeoutstore=null;		 		 // the object that can be killed by clearTimeout()
var timeoutobject=null;		 		 // the object that shall be worked with by setTimeout()

var STARTOPACITY=15;		 		 // the minimum opacity the menu starts when faded in in %; use 100 to show the menu completely
var ENDOPACITY=92;		 		 // the maximum opacity the menu reaches when faded in in %; use 100 to show the menu completely
var FADESPEED=1;		 		 // the speed of fading
var FADESTEPPING=5;		 		 // change this, if FADESPEED is not fast enough

function setActiveStyle(style,active){
		 if(style){
//		 		 alert("Setting style");
		 		 (active)?style.cssText=activeCSSText:style.cssText=inactiveCSSText;
		 }
}

function getULandStyleFromHere(o){
		 // returns a style subobject form a links parent LI if available
		 if(!o)return null;
		 if(!o.parentNode)return null;
		 var navroot=o.parentNode.getElementsByTagName("div");
		 if(!navroot.length)return null;
		 if(navroot[0].style)return(navroot[0].style);
		 return null;
}

function getLeftPos(o){
		 if(!o) return null;
		 var leftpos=(getRealOffsetH(o.parentNode)+generalHoffset);
		 var minleft=getLeftMostPosition()+generalHoffset;
		 var minright=(getRightMostPosition()-menuwidth-generalHoffset);
		 if(leftpos<minleft){
		 		 leftpos=minleft;
		 }else if(leftpos>minright){
		 		 leftpos=minright;
		 }
//alert("minleft: "+minleft+ "\nminright: "+minright+ "\nrealOffset: "+(getRealOffsetH(o.parentNode)+generalHoffset)+ "\ngetLeftMostPosition(): "+getLeftMostPosition()+"\ngetRightMostPosition(): "+getRightMostPosition()+"\nleftpos: "+leftpos);
		 return leftpos;
}

function dPD(o){
		 // shows the first sublevel and registers the menu in the object storage, so it is called only from the uppermost entries in the whole UL/LI bunch
		 // ->ususally var o is only a HTML A, so we have to work with its parentNode if possible, which should be the surrounding LI

		 // if there has been a mouseoutevent before, a call to hideSub is in the pipe, we should kill it right now!
		 if(timeoutstore){
		 		 window.clearTimeout(timeoutstore);
		 		 timeoutstore=null;
		 }

		 // if there's another main menu open, we should kill it right now - looks very strange if this two possible menus are open
		 // we need to get the current li and have to compare it with the one stored in the objectstore (openmainmenu)
		 var thismenu=(o.parentNode); // get the LI to our A tag
		 if(thismenu!=openmainmenu){
		 		 // so now we are sure, the current object is another than the one opened
		 		 // we should now hide the other menu without the time lap
		 		 if(openmainmenu)hideSub(openmainmenu.firstChild);
		 		 // now we look for the first UL below ou LI - since getULandStyleFromHere(o) searches for parentNode itself, we need our o var
		 		 var pd=getULandStyleFromHere(o);
		 		 if(pd){
		 		 		 pd.left=getLeftPos(o);
		 		 		 pd.top=getRealOffsetV(o.parentNode);
		 		 		 pd.width=menuwidth;
		 		 		 pd.display="block";
		 		 		 //fadeIn(pd);
		 		 		 setActiveStyle(o.style, true);
		 }
		 		 // now set this menu as the active one
		 		 openmainmenu=thismenu;
		 }
}

var fadeStyle=null;
var fadeOpacity=0;
function fadeIn(style,currentOpacity){
		 // fades in an object, style must be an html style object
		 if(style){
		 		 if(currentOpacity){
		 		 		 currentOpacity=(currentOpacity>ENDOPACITY)?ENDOPACITY:currentOpacity;
		 		 		 // added for browser compatibility
		 		 		style.MozOpacity = (currentOpacity/100) - 0.001; // Mozilla Flash Hack
						style.opacity = currentOpacity/100; // CSS 3 compliant
						style.filter = "alpha(opacity=" + currentOpacity + ")"; // IE
						style.KhtmlOpacity = currentOpacity/100; // Konquerer, Safari
		 		 }else{
		 		 		 currentOpacity=STARTOPACITY;
		 		 		 fadeIn(style,(currentOpacity+FADESTEPPING));
		 		 }
		 		 if(currentOpacity<ENDOPACITY){
		 		 		 fadeStyle=style;
		 		 		 fadeOpacity=currentOpacity+FADESTEPPING;
		 		 		 window.setTimeout("fadeIn(fadeStyle,(fadeOpacity));",FADESPEED);
		 		 } else {
					window.setTimeout("fadeIn(fadeStyle,(fadeOpacity));",FADESPEED);
				}
		 }
}

function fadeOut(style,currentOpacity){
		 // fades in an object, style must be an html style object
		 if(style){
		 		 if(currentOpacity){
		 		 		currentOpacity=(currentOpacity<STARTOPACITY)?STARTOPACITY:currentOpacity;
						// added for browser compatibility (MOZ/IE/FF/K)
		 		 		style.MozOpacity = (currentOpacity/100) - 0.001;
						style.opacity = currentOpacity/100;
						style.filter = "alpha(currentOpacity=" + opacity + ")";
						style.KhtmlOpacity = currentOpacity/100;
		 		 }else{
		 		 		 currentOpacity=ENDOPACITY;
		 		 		 fadeOut(style,(currentOpacity-FADESTEPPING));
		 		 }
		 		 if(currentOpacity>STARTOPACITY){
		 		 		 fadeStyle=style;
		 		 		 fadeOpacity=currentOpacity-FADESTEPPING;
		 		 		 window.setTimeout("fadeOut(fadeStyle,(fadeOpacity));",FADESPEED);
		 		 }else{hideSub(timeoutobject);}
		 }
}

function dPDs(o){
		 // like in dPD(o) we get just a HTML A
		 window.clearTimeout(timeoutstore);
var thismenu=(o.parentNode); // get the parent object
openparent=thismenu;
if(!isChildOf(thismenu,openmainmenu)){
		 		 hideSub(openmainmenu);
		 }
}


function hPD(o){
		 // starts to hide the menu as a timed process (which can be killed by other mouseovers)
timeoutobject=o;
		 timeoutstore=window.setTimeout("hideSub(timeoutobject);",timetohide);
return;
// das folgende macht noch Probleme
if(!getULandStyleFromHere(o)){
		 		 // we are possibly in a submenu now
		 		 // pd is an A in an LI in an UL in an LI -> we need the UL
		 		 timeoutobject=o.parentNode.parentNode.parentNode.parentNode.firstChild;
}else{
		 		 timeoutobject=o;
		 }
timeoutstore=window.setTimeout("fadeOut(getULandStyleFromHere(timeoutobject));",timetohide);
}

function isChildOf(child, parent){
//sucht, ob ein Object zum gleichen Baum geh&ouml;rt

var cob=child;
var a=0;
		 while (cob&&a<loopmax){
		 		 if(cob==parent) return(true);
		 		 cob=cob.parentNode;
		 }
		 return false;
}

function hideSub(o){
		 // this is the real point of hiding a menu
		 var pd=null; // the pulldown(=pd)
		 var pdp=o; // the pulldowns parent(=pdp)
		 if (o){
		 		 pd=getULandStyleFromHere(o);
		 }
		 if(!pd){
		 		 // we are possibly in a submenu now
		 		 // pd is an A in an LI in an UL in an LI -> we need the UL
		 		 if(o){
		 		 		 pd=getULandStyleFromHere(o.parentNode.parentNode.parentNode);
		 		 		 pdp=o.parentNode.parentNode.parentNode.parentNode.firstChild; //  the a we are redefining is in the first child of the parental LI element
		 		 }
		 }
		 if(pd){
		 		 pd.display="none";
		 		 openmainmenu=null;
		 		 openparent=null;
		 		 setActiveStyle(pdp.style,false);
		 }else return;
}

function getRealOffsetH(o){
		 //tries to determine the real horizontal offset of an element, currently works only for the first sublevel!
		 var offset=0;
		 var hob=o;
		 var a=0;
		 while (hob.offsetParent&&(a++)<loopmax){
		 		 offset+=hob.offsetLeft;
		 		 hob=hob.offsetParent;
		 }
		 return offset;
}

function getRealOffsetV(o){
		 //tries to determine the real vertical offset of an element, Konqueror needs this!
		 var offset=((o.offsetHeight)+(generalVoffset));
		 var vob=o;
		 var a=0;
		 while (vob.offsetParent&&(a++)<loopmax){
//alert( "getRealOffsetV(o): Name: " + vob.tagName + ", top: " + vob.offsetTop + " offset:" + offset );
		 		 offset+=vob.offsetTop;
		 		 vob=vob.offsetParent;
		 }
		 return offset;
}

		 var rightMostPosition=null;
function getRightMostPosition(){
		 var obj=document.getElementById("PDNavTDLast");
		 if(obj){
		 		 rightMostPosition=getRealOffsetH(obj)+obj.offsetWidth;
		 }
		 if(leftMostPosition==null)rightMostPosition=0;
 return rightMostPosition;
}

		 var leftMostPosition=null;
function getLeftMostPosition(){
		 var obj=document.getElementById("PDNavTDFirst");
		 if(obj){
		 		 leftMostPosition=getRealOffsetH(obj);
		 }
		 if(leftMostPosition==null)leftMostPosition=0;
 return leftMostPosition;
}

getLeftMostPosition();

/* end LI-Menu-Functions */

function cpmodPD(o,mo){

// set mouse over image
o.firstChild.src = mo;

		 // shows the first sublevel and registers the menu in the object storage, so it is called only from the uppermost entries in the whole UL/LI bunch
		 // ->ususally var o is only a HTML A, so we have to work with its parentNode if possible, which should be the surrounding LI

		 // if there has been a mouseoutevent before, a call to hideSub is in the pipe, we should kill it right now!
		 if(timeoutstore){
		 		 window.clearTimeout(timeoutstore);
		 		 timeoutstore=null;
		 }

		 // if there's another main menu open, we should kill it right now - looks very strange if this two possible menus are open
		 // we need to get the current li and have to compare it with the one stored in the objectstore (openmainmenu)
		 var thismenu=(o.parentNode); // get the LI to our A tag
		 if(thismenu!=openmainmenu){
		 		 // so now we are sure, the current object is another than the one opened
		 		 // we should now hide the other menu without the time lap
		 		 if(openmainmenu)hideSub(openmainmenu.firstChild);
		 		 // now we look for the first UL below ou LI - since getULandStyleFromHere(o) searches for parentNode itself, we need our o var
		 		 var pd=getULandStyleFromHere(o);
		 		 if(pd){
		 		 		 pd.left=getLeftPos(o);
		 		 		 pd.top=getRealOffsetV(o.parentNode);
		 		 		 pd.width=menuwidth;
		 		 		 pd.display="block";
		 		 		 //fadeIn(pd);
		 		 		 setActiveStyle(o.style, true);
		 }
		 		 // now set this menu as the active one
		 		 openmainmenu=thismenu;
		 }
}

function cpmohPD(o,st){

// set standard image
o.firstChild.src = st;


		 // starts to hide the menu as a timed process (which can be killed by other mouseovers)
timeoutobject=o;
		 timeoutstore=window.setTimeout("hideSub(timeoutobject);",timetohide);
return;
// das folgende macht noch Probleme
if(!getULandStyleFromHere(o)){
		 		 // we are possibly in a submenu now
		 		 // pd is an A in an LI in an UL in an LI -> we need the UL
		 		 timeoutobject=o.parentNode.parentNode.parentNode.parentNode.firstChild;
}else{
		 		 timeoutobject=o;
		 }
timeoutstore=window.setTimeout("fadeOut(getULandStyleFromHere(timeoutobject));",timetohide);
}
