Listen(window,'load',Load);

//loadOnce switches on after the site has loaded, preventing resizes from exploding the pollbars
var loadOnce = true;

Listen(window,'resize',Load);

function LoadBuffer()
{
   setTimeout('Load()',500);
}

// last-minute layout tweaks en fixes
function Load()
{
   //toggle scrollbar based on width
   if (measure('window').x < Thing('container').offsetWidth)
      Things('html')[0].style.overflowX = 'scroll';
   else
      Things('html')[0].style.overflowX = 'hidden';

   //size the menudiv
   var leftChildren = _childNodes(Thing('leftmenu'));
   menuHeight = 0;
   //itemCount = 0

   var out = '';
   for (var i=0; i < leftChildren.length; i++)
   {
      if (leftChildren[i].className == 'm0l0oout' || leftChildren[i].className == 'm0l0oover')
         menuHeight += leftChildren[i].offsetHeight;
   }
   Style('leftmenu').height = menuHeight + 'px';

   //get the total height of elements in mainmenu; this is the minimal site height.
   var menuChilds = _childNodes(Thing('mainmenu'));
   var value = '';
   var siteHeight = 0;
   for (var i=0;i<menuChilds.length ;i++ )
   {
      var child = menuChilds[i];
      value += child.nodeName +'.'+ child.className +'#'+ child.id +' - '+ child.offsetHeight + '<br />\n';
      siteHeight += child.offsetHeight;
   }
   
   //set the other two height-definers
   var viewPortHeight = measure('window').y;
   var ctHeight = Thing('topbar').offsetHeight + Thing('content').offsetHeight;

   //get largest height-definer. The 60 consists of the spaces between all elements in #mainmenu.
   var minHeight = largestValue((siteHeight+60),viewPortHeight);

   //pull down divs to bottom of content or viewport. The sequence is important! You can't pull down the content before you pull down its parent.
   Style('container').height = minHeight + 'px';
   Style('content').height = minHeight - 105 + 'px';
   
   if (Thing('sidebar')) Style('sidebar').height = minHeight - 124 + 'px'; //if there's a sidebar, stretch it high
   else Style('content').width = Thing('rightcol').offsetWidth - 72 + 'px'; //if there's NO sidebar, stretch the content wide

   var ctScroller = Thing('ctScroller');
   if (ctScroller) Style('ctScroller').height = getStyleValue(ctScroller.parentNode,'height',true) - (Thing('subscribe')? 140:40) + 'px';

   //position banner divs
   Style('banners').bottom = 70 + 'px';
   Style('staticBanners').bottom = 10 + 'px';
 
   //scale poll result bars, but ONLY ONCE!
   if (!loadOnce)
   {
      for (var i=0; Thing('bar'+i); i++)
         Style('bar'+i).width = Math.round(Thing('bar'+i).offsetWidth*1.4) + 'px';
   }

/*
   /////////Ruler and bugbox
   var Ruler1 = spawnBox('Ruler1','','1');
   Style('Ruler1').height = menuHeight + 'px';
   Style('Ruler1').top = Thing('leftmenu').offsetTop + 'px';


   //var Ruler2 = spawnBox('Ruler2','','2');
   //Style('Ruler2').height = ctHeight + 'px';

   var bugBox = spawnBox('bugBox');

   bugBox.innerHTML += (measure('window').x < Thing('container').offsetWidth);
   /////////////
*/
}
