/* 
	Tripple Nav Menu
	(c) Dominik Hochreiter
	dominik@soft.uni-linz.ac.at
	Fri Jul 28 2006
	
	changed Mi Aug 29 2007
	changed Th Jul 23 2009
*/



/* Define own Objects */
function objEntry ( _yTop, _yBottom, _id,  _stat)
{
	this.top = _yTop;
	this.bottom = _yBottom;
	this.current = _yBottom;
	this.id = _id;
	this.stat = _stat; 		/* open or close */
	this.body = 'hidden'; 		/* Menu Entry Content BG is by default hidden */
	this.bodyContent = 'hidden';	/* Content is hidden */
}


/* Define Global Vars */
var glbBody;
var glbBrowserWidth = 0;



var glbMenuSubEntries = new Array(); //3
var glbMenuSubObjs = new Array();
var glbPressedEntry = -1;
var glbMenuMoveStep = 10; //4
var glbMenuMoveStepSleep = 1;


/* Define Global Styles*/
var styMenuSubH = 460; //480, 390
var styMenuSubW = 800;
var styMenuSubEntryH = 20; //25
var styMenuSubLeftBg = new Array( 204, 153, 0 );
var styMenuSubCenterBg = new Array( 30, 21, 114 ); // new Array( 0, 255, 0 );
var styMenuSubRightBg = new Array( 193, 200, 13 ); // new Array( 0, 0, 255 );
var styMenuSubEntryHeadAlpha = 0.6;//0.9//0.6
var styMenuSubEntryBodyAlpha = 0.5;//0.7//0.4
var styMenuSubEntryBodyImg = "/img/menubg.gif";


var debugTimeStr = "";
var DebugZeit = new Date();


function initMenu ()
{
	glbBody = document.getElementsByTagName("body")[0];
	initMenuGlobal();
	initMenuSubEntries();
	initMenuSubPositions();

}


function initMenuGlobal ()
{
	if( env.browser == defBrowserIE ) {
		glbBrowserWidth = document.body.clientWidth;
	} else {
		glbBrowserWidth = window.innerWidth; 
	}

	/* Additionals 
	 *
	 * MSIE 8.0 STD Mode slow JS CSS dynamics
	 *
	 * */


	/*
	 * we use now <meta http-equiv="X-UA-Compatible" content="IE=7" />
	 *

	if( env.browser == defBrowserIE ) {
		if( env.browserVersion ) {
			glbMenuMoveStep = 10;
			glbMenuMoveStepSleep = 8;
		}
	}
	*/	

}


function initMenuSubEntries ()
{

	/* Auto detect sub menu entries */
	for( var m = 0; m < 1; m++ ) {
		glbMenuSubEntries[m] = new Array();
		for( var n = 0; n < 15; n++ ) {
			if( document.getElementById( "menuSub_"+m+"_"+n ) ) {
				glbMenuSubEntries[m].push( "menuSub_"+m+"_"+n );
			}
		}
	}


	for( var a = 0; a < glbMenuSubEntries.length; a++ ) {


		if(  glbMenuSubEntries[a].length < 1)
			continue;

		var bottoPos = ( styMenuSubEntryH * glbMenuSubEntries[a].length ) + styMenuSubEntryH;

		for( var b = glbMenuSubEntries[a].length - 1; b >= 0; b-- ) {
			
			glbMenuSubObjs.push( 
				new objEntry(
					styMenuSubEntryH * ( b + 1 ),
					styMenuSubH - ( bottoPos - ( styMenuSubEntryH * ( b + 1 ) ) ),
					glbMenuSubEntries[a][b],
					0
				)
			);

		}
	}
}


function initMenuSubPositions ()
{
        for( var a = 0; a < glbMenuSubObjs.length; a++ ) {
		document.getElementById( glbMenuSubObjs[ a ].id ).style.top =  glbMenuSubObjs[ a ].current+"px"; 

		document.getElementById( glbMenuSubObjs[ a ].id+"_b" ).style.height = "0px"; 
		document.getElementById( glbMenuSubObjs[ a ].id+"_b" ).style.top = styMenuSubEntryH+"px";
                document.getElementById( glbMenuSubObjs[ a ].id+"_b" ).style.visibility = glbMenuSubObjs[ a ].bodyContent;	

		document.getElementById( glbMenuSubObjs[ a ].id+"_b_B" ).style.height = "0px";
                document.getElementById( glbMenuSubObjs[ a ].id+"_b_B" ).style.top = styMenuSubEntryH+"px";
                document.getElementById( glbMenuSubObjs[ a ].id+"_b_B" ).style.visibility = glbMenuSubObjs[ a ].body;

        }

	for(var a = 0; a < glbMenuSubEntries.length; a++ ) {

		var color = new Array( 0, 0, 0 );
		var colorMover = new Array( 0, 0, 0);
		var colorMoverSize = new Array( 0, 0, 0);

		if( a == 0 ) {
			color = styMenuSubLeftBg;
		}
		else if( a == 1) {
			color = styMenuSubCenterBg;
		}
		else if( a == 2) {
			color = styMenuSubRightBg;
		} 

		colorMoverSize[0] = Math.round( ( color[0] / 5) / glbMenuSubEntries[a].length ); 
		colorMoverSize[1] = Math.round( ( color[1] / 5) / glbMenuSubEntries[a].length );
		colorMoverSize[2] = Math.round( ( color[2] / 5) / glbMenuSubEntries[a].length );



		for(var b = 0; b < glbMenuSubEntries[a].length; b++ ) {
			var byte2 = color;
			byte2[0] = color[0] - colorMover[0];
			byte2[1] = color[1] - colorMover[1];
			byte2[2] = color[2] - colorMover[2];

			if( b % 3 == 0 )
				colorMover[0] += colorMoverSize[0];
			if( (b+1) % 3 == 0 )
				colorMover[1] += colorMoverSize[1];
			if( (b+2) % 3 == 0 )
				colorMover[2] += colorMoverSize[2];
			
			var head = document.getElementById( glbMenuSubEntries[a][b]+"_h_B" );
			var body = document.getElementById( glbMenuSubEntries[a][b]+"_b_B" );
			
			body.style.background = "rgb("+byte2[0]+","+byte2[1]+","+byte2[2]+")";

			menuAlphaElement( head, styMenuSubEntryHeadAlpha ); 

		}
	}
}


function removePxAttr ( _styleAttr )
{
	var search = _styleAttr.indexOf("px");
	if( search > 0) {
		return _styleAttr.substr( 0, search );
	} else {
		return _styleAttr;
	}
}


function getMenuSubEntrieObjNr ( _id ) 
{
	if(! glbMenuSubObjs.length > 0 )
                alert("JavaScript not compatible with this browser! In the moment! Update is in progress");

	for( var a = 0; a < glbMenuSubObjs.length; a++ ) {
		if( glbMenuSubObjs[a].id == _id ) {
			return a;
		}
	}
}




function getMenuSubEntrieObjOrder( _id, _beforAfter )
{
	/* _beforAfter:  befor = 1, after = 0 */


	var foundObjNr = new Array();
	for( var a = 0; a < glbMenuSubObjs.length; a++ ) {
		if( glbMenuSubObjs[a].id.substr(0,9) == _id.substr(0,9) ) {  /* menuSub_1(_1) */
			var nrObjs = parseInt( glbMenuSubObjs[a].id.substr(10,2) );
			var nr = parseInt( _id.substr(10,2) );			

			if( _beforAfter == 1) {
				if( nrObjs < nr ) {
					foundObjNr.push( a );
				}
			} else {
				if( nrObjs > nr ) {
                                        foundObjNr.push( a );
                                }
			}
		}
	}

	return foundObjNr;
}

function menuSubEntryHeadAlpha( _id, _stat )
{
	/* 1 = alpha, 0 = off */
	var alpha = 1.0;
	if( _stat == 1) 
		alpha = styMenuSubEntryHeadAlpha;


	for( var a = 0; a < glbMenuSubObjs.length; a++ ) {
                if( glbMenuSubObjs[a].id.substr(0,9) == _id.substr(0,9) ) {
			
			//document.getElementById( glbMenuSubObjs[ a ].id+"_h_B" ).style.MozOpacity = alpha;
			menuAlphaElement( document.getElementById( glbMenuSubObjs[ a ].id+"_h_B" ), alpha); 
		}
	}

}


function menuSubEntryDoit ( _id )
{

//	debugTimeStr += "menuSubEntryDoit(start)="+DebugZeit.getMilliseconds()+", ";

	backgoundImgLoopInterrupt(1);

	var menuSubEntrieObjNr = getMenuSubEntrieObjNr( _id );

	glbPressedEntry = menuSubEntrieObjNr;
	menuSubEntryHeadAlpha( _id, 0 );

	if( glbMenuSubObjs[menuSubEntrieObjNr].stat == 0 ) {



		//glbPressedEntry = menuSubEntrieObjNr;
		menuSubEntryScaleOpen( menuSubEntrieObjNr );

		glbMenuSubObjs[ menuSubEntrieObjNr ].body = "visible";
		document.getElementById( glbMenuSubObjs[ menuSubEntrieObjNr ].id+"_b_B" ).style.visibility = glbMenuSubObjs[ menuSubEntrieObjNr ].body;

		var resultObjNr = getMenuSubEntrieObjOrder( _id, 1 );
		if( resultObjNr.length > 0 ) {
			for( var ob = 0; ob < resultObjNr.length; ob++ ) {
				
				menuSubEntryScaleOpen( resultObjNr[ ob ] );
			}
		}

	} else {


		//menuAlphaElement( document.getElementById( glbMenuSubObjs[ menuSubEntrieObjNr ].id+"_b_B" ), 1.0 ); 

		/* Blend menu above in TODO: sort */
		var resultObjNr = getMenuSubEntrieObjOrder( _id, 1 ); 
		resultObjNr = 0;

		glbMenuSubObjs[ menuSubEntrieObjNr ].body = "visible";
		glbMenuSubObjs[ menuSubEntrieObjNr ].bodyContent = "visible";
		document.getElementById( glbMenuSubObjs[ menuSubEntrieObjNr ].id+"_b_B" ).style.visibility = glbMenuSubObjs[ menuSubEntrieObjNr ].body;
		document.getElementById( glbMenuSubObjs[ menuSubEntrieObjNr ].id+"_b" ).style.visibility = glbMenuSubObjs[ menuSubEntrieObjNr ].bodyContent;		

		resultObjNr = getMenuSubEntrieObjOrder( _id, 0 );
		if( resultObjNr.length > 0 ) {
                        for( var ob = 0; ob < resultObjNr.length; ob++ ) { // ob = 0


				glbMenuSubObjs[ resultObjNr[ ob ] ].bodyContent = "hidden";
				document.getElementById( glbMenuSubObjs[ resultObjNr[ ob ] ].id+"_b" ).style.visibility = glbMenuSubObjs[ resultObjNr[ ob ] ].bodyContent;

				menuAlphaElement( document.getElementById( glbMenuSubObjs[  resultObjNr[ ob ] ].id+"_b_B" ), 1.0 ); 

                                menuSubEntryScaleClose( resultObjNr[ ob ] );
                        }
                }

	}

//	debugTimeStr += "menuSubEntryDoit(end)="+DebugZeit.getMilliseconds()+", ";
}


/* NEW Menu Background Image [start] */
var styMenuSubEntryBodyImgAlpha = 0.0;
var styMenuSubEntryBodyImgElm = document.createElement("img");
styMenuSubEntryBodyImgElm.border = "0";
styMenuSubEntryBodyImgElm.src = styMenuSubEntryBodyImg;

function menuSubEntryAddImg ( _objNr )
{
	//alert(_objNr);
	var divEntryBody = document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B" );
	//divEntryBody.appendChild(styMenuSubEntryBodyImgElm);
	divEntryBody.style.backgroundImage = "url('"+styMenuSubEntryBodyImg+"')";
	styMenuSubEntryBodyImgAlpha = 1.0;
}

function menuSubEntryRemoveImg ( _objNr )
{
	var divEntryBody = document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B" );
	//divEntryBody.removeChild(styMenuSubEntryBodyImgElm);
	divEntryBody.style.backgroundImage = "";
}
/* NEW Menu Background Image [end] */



function menuSubEntryScaleOpen ( _objNr )
{

       	var divEntry = document.getElementById( glbMenuSubObjs[ _objNr ].id );
	var divEntryBody = document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B" );	


	if( defBrowserNetscape == env.browser) {
		document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B").style.zIndex = 200 + parseInt( glbMenuSubObjs[ _objNr ].id.substr(10,2) );
		document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b").style.zIndex = 201 + parseInt( glbMenuSubObjs[ _objNr ].id.substr(10,2) );
	}


        var divEntryH = parseInt(divEntryBody.style.height); //removePxAttr( divEntryBody.style.height );
        if( ! isFinite( divEntryH ) ) {
                divEntryH = 0;
	}

        if ( glbMenuSubObjs[ _objNr ].current > glbMenuSubObjs[ _objNr ].top ) {

                glbMenuSubObjs[ _objNr ].current -= glbMenuMoveStep;
		divEntryH += glbMenuMoveStep;

                divEntry.style.top = glbMenuSubObjs[ _objNr ].current+"px";
		if( ( glbMenuSubObjs[ _objNr ].bottom  ) > glbMenuSubObjs[ _objNr ].current )
                	divEntryBody.style.height = divEntryH+"px";

		//NEW! add background image 
		//divEntryBody.style.backgroundImage = "url('"+styMenuSubEntryBodyImg+"')";
		menuSubEntryAddImg( _objNr );
		//menuSubEntryAddImg( glbPressedEntry );

                window.setTimeout("menuSubEntryScaleOpen("+_objNr+")", glbMenuMoveStepSleep);//1
        } else {


		//NEW! remove background image 
		//divEntryBody.style.backgroundImage = "";
		menuSubEntryRemoveImg( _objNr );
		//menuSubEntryRemoveImg( glbPressedEntry );

		glbMenuSubObjs[ _objNr ].stat = 1;

		var resultObjNr = getMenuSubEntrieObjOrder( glbMenuSubObjs[ _objNr ].id , 1 );
                if( resultObjNr.length > 0 ) {
                        for( var ob = 0; ob < resultObjNr.length; ob++ ) {

                                glbMenuSubObjs[ resultObjNr[ ob ] ].body = "hidden";
				glbMenuSubObjs[ resultObjNr[ ob ] ].bodyContent = glbMenuSubObjs[ resultObjNr[ ob ] ].body;
                                document.getElementById( glbMenuSubObjs[  resultObjNr[ ob ] ].id+"_b_B" ).style.visibility = glbMenuSubObjs[ resultObjNr[ ob ] ].body;
				document.getElementById( glbMenuSubObjs[  resultObjNr[ ob ] ].id+"_b" ).style.visibility = glbMenuSubObjs[ resultObjNr[ ob ] ].bodyContent;

                        }
                }

		//document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B" ).style.MozOpacity = styMenuSubEntryBodyAlpha;
		menuAlphaElement( document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B" ), styMenuSubEntryBodyAlpha ); 
		
		if( glbPressedEntry == _objNr ) {

			menuSubEntryHeadAlpha(   glbMenuSubObjs[ _objNr ].id , 1 );
	
			glbMenuSubObjs[ _objNr ].bodyContent = "visible";
			document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b" ).style.visibility = glbMenuSubObjs[  _objNr  ].bodyContent;

			glbPressedEntry = -1;
		}
	}
}


function menuSubEntryScaleClose ( _objNr )
{
	var divEntry = document.getElementById( glbMenuSubObjs[ _objNr ].id );
	var divEntryBody = document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B" );


        var divEntryH = parseInt(divEntryBody.style.height); //removePxAttr( divEntryBody.style.height );


	if( ! isFinite( divEntryH ) ) {
                divEntryH = 0;
        }



	if ( glbMenuSubObjs[ _objNr ].current < glbMenuSubObjs[ _objNr ].bottom ) {

                glbMenuSubObjs[ _objNr ].current += glbMenuMoveStep;
                divEntryH -= glbMenuMoveStep;

                divEntry.style.top = glbMenuSubObjs[ _objNr ].current+"px";

		if(divEntryH > 0)
		        divEntryBody.style.height = divEntryH+"px";
		else
			divEntryBody.style.height = "0px";

		//NEW! add background image 
		//divEntryBody.style.backgroundImage = "url('"+styMenuSubEntryBodyImg+"')";
		menuSubEntryAddImg( _objNr );
		//menuSubEntryAddImg( glbPressedEntry );	

                window.setTimeout("menuSubEntryScaleClose("+_objNr+")", glbMenuMoveStepSleep);
        } else {

		//NEW! remove background image 
		//divEntryBody.style.backgroundImage = "";
		menuSubEntryRemoveImg( _objNr );
		//menuSubEntryRemoveImg( glbPressedEntry );

		if( defBrowserNetscape == env.browser) {
                	document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b_B").style.zIndex = 2;
                	document.getElementById( glbMenuSubObjs[ _objNr ].id+"_b").style.zIndex = 1;
        	}


		glbMenuSubObjs[ _objNr ].stat = 0;


		divEntryBody.style.visibility="hidden";


		if( glbPressedEntry == _objNr ) {
                        menuSubEntryHeadAlpha(  glbMenuSubObjs[ _objNr ].id , 1 );
                        glbPressedEntry = -1; 
                }
	}

}


/* Menu is by default opened! */
function menuSubEntryOpen ( _id )
{
	var menuSubEntrieObjNr = getMenuSubEntrieObjNr( _id );
	var divEntry = document.getElementById( glbMenuSubObjs[ menuSubEntrieObjNr ].id );
        var divEntryBody = document.getElementById( glbMenuSubObjs[ menuSubEntrieObjNr ].id+"_b_B" );



	glbMenuSubObjs[ menuSubEntrieObjNr ].current = glbMenuSubObjs[ menuSubEntrieObjNr ].top;

	
        divEntry.style.top = glbMenuSubObjs[ menuSubEntrieObjNr ].current+"px";
	var divEntryH = glbMenuSubObjs[ menuSubEntrieObjNr ].bottom - glbMenuSubObjs[ menuSubEntrieObjNr ].top ; //-6
	divEntryBody.style.height = divEntryH+"px";


	var resultObjNr = getMenuSubEntrieObjOrder( _id, 1 );
        if( resultObjNr.length > 0 ) {
	        for( var ob = 0; ob < resultObjNr.length; ob++ ) {

			var divEntry = document.getElementById( glbMenuSubObjs[ resultObjNr[ob] ].id );
		        var divEntryBody = document.getElementById( glbMenuSubObjs[ resultObjNr[ob] ].id+"_b_B" ); 

		        glbMenuSubObjs[ resultObjNr[ob] ].current = glbMenuSubObjs[ resultObjNr[ob] ].top;


		        divEntry.style.top = glbMenuSubObjs[ resultObjNr[ob] ].current+"px";
		        var divEntryH = glbMenuSubObjs[ resultObjNr[ob] ].bottom - glbMenuSubObjs[ resultObjNr[ob] ].top ; //-6
		        divEntryBody.style.height = divEntryH+"px";

               	}
        }


	menuSubEntryDoit( _id );

}


/* Alpha func */
function menuAlphaElement ( _elem, _value )
{
	if( env.browser == defBrowserIE ) {
		var tmp = _value * 100;
		_elem.style.filter = 'Alpha(opacity='+tmp+', finishopacity='+tmp+', style=1)';
	}
	else if( env.browser == defBrowserSafari ) {
		_elem.style.Opacity = _value;
	} 
	else {
		_elem.style.MozOpacity = _value;
	}
}




/* Background Image Show */


var glbBackgroundImgs = new Array();
var glbBackgroundImgsLen = 0;
var glbBgImgPos = 1;
var glbBgImgStepTime = 100;   /* blending 100 */
var glbBgImgSleepTime = 4000; /* sleep between */
var glbBgImgInterTime = 6000; /* from menu */
var glbBgStartSleep = 2000; /* sleep at start */
var glbBgImgPrevTime = 0;
var glbBgImgPath = "";
var glbBgImgCurrent = "";
var glbBgImgNext = "";
var glbBgImgCurrentAlpha = 1.0; //1.0
var glbBgImgNextAlpha = 0.0; //0.0,-0.1
var glbBgImhAlphaStepSize = 0.05; //0.05
var glbBgImgSleep = glbBgStartSleep; //glbBgImgSleepTime;
var glbBgImgInterCounter = 0;
var glbBgStart = 1;

function backgroundImgPath ( _path ) 
{
	glbBgImgPath = _path;
}

function backgroundImgLoad ( _img ) 
{
	if( glbBackgroundImgs.length == 0 ) {
		glbBgImgCurrent = glbBgImgPath+"/"+_img;

		//if( env.browser != defBrowserIE ) {
		//	document.getElementById( "contentImgSrc3" ).style.visibility = 'hidden';
		//}
	}

	if( glbBackgroundImgs.length == 1 ) {
                glbBgImgNext = glbBgImgPath+"/"+_img;
        } 

	/* Preload ? */
	document.getElementById( "contentImgSrcHidden" ).src = glbBgImgPath+"/"+_img;
	glbBackgroundImgs.push( _img );
	glbBackgroundImgsLen = 1;
}


function backgoundImgLoop ()
{
	//return 1;

        if( glbBgImgInterCounter > 0 )
                return 1;
	
	if(glbBackgroundImgsLen == 0)
		return 1;

        if( glbBgImgCurrentAlpha == 1.0 ) {


                document.getElementById( "contentImgSrc3" ).src = glbBgImgCurrent;

		menuAlphaElement( document.getElementById( "contentImgSrc2" ), 0.0 );
		document.getElementById( "contentImgSrc2" ).src = "";
                document.getElementById( "contentImgSrc2" ).src = glbBgImgNext; 

                menuAlphaElement( document.getElementById( "contentImgSrc1" ), 1.0 );
                document.getElementById( "contentImgSrc1" ).src = glbBgImgCurrent;



                glbBgImgSleep = glbBgImgStepTime;
        }


	if( glbBgImgCurrentAlpha > 0 ) {

		glbBgImgCurrentAlpha -= glbBgImhAlphaStepSize;
                glbBgImgNextAlpha += glbBgImhAlphaStepSize;
		menuAlphaElement( document.getElementById( "contentImgSrc2" ), glbBgImgNextAlpha );

		if( glbBgStart == 1 ) {
			//if( glbBgImgCurrentAlpha < 0.95 ) { // 80 && env.browser == defBrowserIE
			if( glbBgImgCurrentAlpha == 0.95 ) { //0.95
				document.getElementById( "contentImgSrc3" ).style.visibility = 'hidden';
				glbBgStart = 0;
			} 
		} 

        } else {	


		glbBgImgSleep = glbBgImgSleepTime;
                glbBgImgCurrentAlpha = 1.0; //1.0
                glbBgImgNextAlpha = 0.0;

                glbBgImgCurrent = glbBgImgNext;

                if( glbBgImgPos < (glbBackgroundImgs.length - 1) )
                        glbBgImgPos++;
                else
                        glbBgImgPos = 0;

		glbBgImgNext = glbBgImgPath+"/"+glbBackgroundImgs[ glbBgImgPos ];

	

		//switcher
                glbBgStart = 1;
		document.getElementById( "contentImgSrc3" ).src = "";
		document.getElementById( "contentImgSrc3" ).src = glbBgImgCurrent;
                document.getElementById( "contentImgSrc3" ).style.visibility = 'visible'


        }

        window.setTimeout("backgoundImgLoop()", glbBgImgSleep);
}

function  backgoundImgLoopInterrupt ( _do )
{
        if( _do == 1 ) {
                glbBgImgInterCounter++;
                window.setTimeout("backgoundImgLoopInterrupt(2)", glbBgImgInterTime);
        } else {
                glbBgImgInterCounter--;

                if(  glbBgImgInterCounter == 0 ) {
                        backgoundImgLoop();
                        return 1;
                }
        }

}







