/*
 *  Common scripts for the JN Zijtaart website
 */


/* Hack for getting the viewport filled when there is not enough content 
   For the hack I use a DIV called "minheight" and that div gets resized after
   the page is loaded to the viewportheight - height_of_header */

/*
Original code from: http://www.pixeline.be/blog/index.php/2005/10/04/36-javascriptcss-calculate-a-height-value-on-resize
*/

function setMinHeight()
{

  myObject = document.getElementById('minheight');
//  myObject = document.getElementById('right');
  mySize = 94;  // 94 is size of header

// THIS BIT DETECTS THE AVAILABLE SCREEN SPACE 
// IT ORIGINATES FROM http://www.quirksmode.org/viewport/compatibility.html
	var x,y;
	
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	// THIS IS THE ACTUAL RESIZING OF THE VIEWPORT
	  viewportHeight = y - mySize;

         if (viewportHeight > 10)
         {
         	 myObject.style.height = viewportHeight + "px";
         }
//	 return 'resized';
}

window.onresize = setMinHeight;
   



/* The stuff below is for the menu*/

/*
Script by RoBorg
RoBorg@geniusbug.com
http://javascript.geniusbug.com
Please do not remove or edit this message
*/

/*
Adaptions by Michel van de Wetering
- Changed a few lines to get rid of some JavaScript warnings
- Button returns to "unpressed" state when the menu has collapesed again
- Added cookie remember code from same site as original script
- Major overhaul of the "default rolled down" code.
   - Now it will not hide and roll out the menu, but the menu stays rolled down.
   - Use inputvars in initMenus i.s.o. cookie for remembering menu state. Now the
     menu works is fed pageid's from the CMS.
*/

var slideSpeed = 30;

var	slider = new Array();
var	buttons = new Array();

function initMenus(parent, pageid)
{
	if(!document.getElementsByTagName) return;

  var bSkip = false;
  var menuid = parent;
	var divs = document.getElementsByTagName('div');
	var x = 0;

  // If parent=0 (==root) use pageid instead. Used for initial rollout later
  if (menuid == 0)
  {
  	menuid = pageid;
  }

  // General setup of the buttons and menus
	for(x=0; x<divs.length; x++)
	{
		divs[x].originalHeight = divs[x].offsetHeight;
		if(divs[x].className == 'menu') divs[x].speed = -1;
		if(divs[x].className == 'button') divs[x].onclick = function() { toggle(this); }
		if(divs[x].className == 'pressedButton') divs[x].onclick = function() { toggle(this); }
	}


  // Check if a menu should be "down" initially
	for(x=0; x<divs.length; x++)
	{
		if (divs[x].className == 'button' || divs[x].className == 'pressedButton')
    {
    	// Check if this section should be hidden
    	if (divs[x].id == 'm_'+menuid)
  		{
      	divs[x].className = 'pressedButton';  // Makes the button look like it is rolled down
  			bSkip = true;
  		}
  		else
  		{
  			bSkip = false
  		}
  	}

 		if ((divs[x].className == 'menu'))
 		{
  		if (bSkip)
  		{
  			// Make sure the menu goes up first time you press the button
  		  divs[x].speed = 1;
  		}
      else
      {
    		divs[x].style.height = '1px';
    		divs[x].style.display = 'none';
    	}
    }
	}
}


function toggle(obj)
{
  var but = obj;
	while(obj.nextSibling && (obj.className != 'menu')) obj = obj.nextSibling;

	obj.speed = -1 * obj.speed;
	if(obj.slideTimer) return;	//Already moving

	var x = slider.length;

	but.className = 'pressedButton';
	buttons[x] = but;

	slider[x] = obj;
	slide(x);
}


function slide(x)
{
	var obj = slider[x];
	if(obj.style.display != 'block') obj.style.display = 'block';
	var height = obj.offsetHeight + obj.speed * slideSpeed;
	var targetHeight = getChildrensHeights(obj);

	if(height > targetHeight)
	{
		// Completely rolled down
		obj.style.height = targetHeight + 'px';
		obj.slideTimer = false;
		resizeParents(obj, 0);
		return;
	}

	if(height <= 1)
	{
		// Completely rolled up
		obj.style.height = '1px';
		obj.style.display = 'none';
		obj.slideTimer = false;
		resizeParents(obj, 0);
		buttons[x].className = 'button';
		return;
	}

  // Rolling
	obj.style.height = height + 'px';
	obj.slideTimer = setTimeout('slide(' + x + ');', 50);
	resizeParents(obj, targetHeight - height);
}


function getChildrensHeights(obj)
{
	if(!obj.firstChild) return 0;
	if(!obj.tagName.match(/div/i)) return 0;

	var height = 0;
	var tmp = obj;
	obj = obj.firstChild;
	do height += getChildrensHeights(obj);
	while(obj = obj.nextSibling);

	if(height == 0) height = tmp.offsetHeight;

	return height;
}


function resizeParents(obj, diff)
{
	if(obj.className == 'menuContainer') return;
	obj = obj.parentNode;
	var height = getChildrensHeights(obj) - diff;
	obj.style.height = height + 'px';
	resizeParents(obj, diff);
}

