// Maak een schone versie van het menu script.
// Het doel is global variables te hanteren in een efficient script

var shrink = "on";
var endpos = 140;
var delayupmenu = 50;
var movMelater = 0;

// give start & end colors for do color in array's as RGB values.
strcolor = new Array(0,0,0);
endcolor = new Array(245,245,245);
finishcolor = new Array(200,200,200);



function startmenu()	{

// Declareer global variabelen.
navPic = document.getElementById("navpic");
navMenu = document.getElementById("navmenu");
navUl = document.getElementById("navmenuul");

// Hide menu items here, as they are visible in CSS for non javascript browsers.
hideit(navMenu);
// Makes mouse a pointer when over the element.
topointer(navPic);

navPic.onmouseover=function(){
	checkstate(navMenu);
	}
	
navPic.onmouseout = function(){
	checkstate(navMenu);
	exeptioncheck();
	}


navUl.onmouseover=function(){
	holdtimer();
	}

navUl.onmouseout = function()	{
	exeptioncheck();
	}	


var oq = document.getElementById('navmenuul').getElementsByTagName('a');
//get all the links in an array from the unordered list with id navmenuul

for(i=0; i<oq.length; i++)	{

oq[i].onmouseout=function(){checkstate(navMenu);};

oq[i].onmouseover=function(){
holdtimer();

docolor(this,strcolor,endcolor,finishcolor);

};
						}
	
}



function hideit(obj)	{

//if this element exists, hide it.
if(obj)	{
 obj.style.display = "none";
 	}
}

function topointer(obj)	{
//if this element exists, make mouse a pointer.
if(obj)	{
obj.style.cursor = "pointer";
	}
}

function holdtimer() {

if( navMenu.style.top.replace(/px/,"") > endpos)	{

	window.clearInterval(navMenu.movMeHere);
	clearTimeout ( movMelater );
		}
//alert("gestopt");
//docolor(obj,strcolor,endcolor,finishcolor);
	}

	
function exeptioncheck()	{

movMelater = setTimeout(
function ()	{

moveit(navMenu,"up",endpos,10,4,20);;
	}
, 2000);

}

function checkstate(obj)	{

if(obj)	{



	var state = obj.style.display;

	//alert(state);

if(state == "none")	{

	// alert("checkstate contstateerd display none");

	moveit(obj,"down",10,endpos,4,50);
			}
			
if(state == "block")	{

	// Check if the menu is fully rolled out!
		
	if( obj.style.top.replace(/px/,"") > endpos)	{
	
	moveit(obj,"up",endpos,10,4,20);
	clearTimeout ( movMelater );		
			}
		}	
	}
}



// Function moveit takes parameter direction as well!! 
// For sideway movement extra code is still needed.

function moveit(obj,direction,start,end,step,speed)	{

if(obj.movMeHere) window.clearInterval(obj.movMeHere);
	
var pos = start;
var sp = speed;
var st =step;
var x = end - start;
var y = start - end;
var str = 1;
var delayup = 0;


var expnd = document.getElementById('navmenuul').getElementsByTagName('li');
var sz = 5;
var delay = 0;



// Begin recurring loop here:

obj.movMeHere = window.setInterval(
function()	{
switch (direction)	{
case "down": {

if(sz <= 25)	{
for(i=0; i<expnd.length; i++)	{
if(sz == 5)	{
expnd[i].style.overflow="hidden";
}
if(sz > 24)	{
expnd[i].style.overflow="visible";
}

expnd[i].style.height=sz+"px";
	}
sz++;
}

if( start == pos) {
obj.style.display="block";
	}
if(start < (pos + (x/4)))	{
step +=1;
speed -=1;
	}
if(start > (end - (x/3)))	{
step -= 1;
speed += 10;
	} 
if(step < 1) step = 1;
start += step;
obj.style.top=start+"px";

if(start > end)	{
	window.clearInterval(obj.movMeHere);
	
		}
break;
	}
case "up":	{


if(delayup > delayupmenu )	{

if(start > (pos - (y/6)) )	{
speed = 500;
if(str > step) str = step;
}
if(start < (pos - (y/7)))	{
str +=2;
speed = sp;
}
start -= str;
obj.style.top = start+"px";

if(start < end)	{

	window.clearInterval(obj.movMeHere);
	obj.style.display = "none";
	}
			
	}else	{
		delayup++;
		}	
	
break;
		}
		
default: alert("Het menu heeft een script fout. Herlaad de pagina");
	}
}
, speed)
}
// End of function moveit;





function docolor(o,arst,arend,arfin)	{
if(o)	{
var obj = o.parentNode;
if(obj.style)	{
var r=arst[0];
var g=arst[1];
var b=arst[2];
if(o.frfloop) window.clearInterval(o.frfloop);
o.frfloop = window.setInterval(
function()	{
obj.style.borderBottomColor = "rgb("+r+","+g+","+b+")";
r += 10;
g += 10;
b += 10;
if(b > arend[0])	{
window.clearInterval(o.frfloop);
obj.style.borderBottomColor = "rgb("+arfin[0]+","+arfin[1]+","+arfin[2]+")";
		}
		}
, 30);	
	}
	}
}

