// Not necessary to test for menu_supported. site.php (that includes this) is only used for supported browsers

window.onload = init_site;
window.onresize = init_site;

function init_site() {
  // IE can't work with "right" and "bottom" specification for iframes. Fix it here.
  var contentClass = get_style_class('mainFrame');
  var left = contentClass.left;
  var right = contentClass.right;
  var top = contentClass.top;
  var bottom = contentClass.bottom;
  var width = contentClass.width;
  var height = contentClass.height;
  var screenheight = get_client_height();
  if(width=='' && left!='' && right!='') {
    var screenwidth = get_client_width();
    // Add 16 for the scroll bars
    var iframe_width = parseInt(screenwidth)-parseInt(left)-parseInt(right)-16;
    set_width('mainFrame',iframe_width);
  }
  if(height=='' && top!='' && bottom!='') {
    var screenheight = get_client_height();
    set_height('mainFrame',parseInt(screenheight)-parseInt(top)-parseInt(bottom));
  }
  set_visible('mainFrame',1);
}

// Return a stylesheet rule with the mentioned name
function get_rule(name) {
  // W3C
  if(document.getElementById && document.styleSheets && document.styleSheets[0].cssRules) {
    for ( sheet = 0; sheet < document.styleSheets.length; sheet++ )
      for(rule=0; rule<document.styleSheets[sheet].cssRules.length; rule++)
        if(document.styleSheets[sheet].cssRules[rule].selectorText.toLowerCase() == name.toLowerCase())
          return(document.styleSheets[sheet].cssRules[rule]);
  }
  // IE 
  else if(document.all && document.styleSheets) {
    for ( sheet = 0; sheet < document.styleSheets.length; sheet++ )
      for(rule=0; rule<document.styleSheets(sheet).rules.length; rule++)
        if(document.styleSheets(sheet).rules(rule).selectorText == name)
          return(document.styleSheets(sheet).rules(rule));
  }
  return(0);
}

function get_style_class(s) {
  var theclass = null;
  if(document.layers)
    theclass = eval('document.classes.' + s + '.all');
  else{
    myclass = get_rule('.' + s);
    if(!myclass)
      myclass = get_rule(s);
    if(!myclass)
      myclass = get_rule('*.' + s);
    if(myclass)
      return(myclass.style);
  }
  return(theclass);
}

function get_client_width(){
  if(window.innerWidth)
    return(window.innerWidth);
  if(document.body && document.body.clientWidth)
    return(document.body.clientWidth);
  if(document.body && document.body.scrollWidth)
    return(document.body.scrollWidth);
  return(0);
}

function get_client_height(){
  if(window.innerHeight) {
    if(document.layers)
      return(window.innerHeight+3); // NS4
    else
      return(window.innerHeight);
  }
  if(document.documentElement && document.documentElement.clientHeight)
    return (document.documentElement.clientHeight); // IE6
  if(document.body.clientHeight)
    return(document.body.clientHeight);  // IE5+
  if(document.body.scrollHeight)
    return(document.body.scrollHeight);  // IE5 Mac
}

function set_width(i,w) {
  if (!i || isNaN(w)) return;
  if (document.all && document.all[i])
    document.all[i].style.pixelWidth=w;
  if (document.layers && document.layers[i])
    document.layers[i].width=w;
  if (document.getElementById && document.getElementById(i))
    document.getElementById(i).style.width=w + "px";
}

function set_height(i,h) {
  if (!i || isNaN(h)) return;
  if (document.all && document.all[i])
    document.all[i].style.pixelHeight=h;
  if (document.layers && document.layers[i])
    document.layers[i].height=h;
  if (document.getElementById && document.getElementById(i))
    document.getElementById(i).style.height=h + "px";
}

function set_visible(i,v) {
  if (document.all && document.all[i])
	  document.all[i].style.visibility = v? "visible" : "hidden";
  if (document.layers && document.layers[i])
    document.layers[i].visibility = v? "show" : "hide";
  if (document.getElementById && document.getElementById(i))
    document.getElementById(i).style.visibility = v? "visible" : "hidden";
}


