  var addEvent = function(elem, type, eventHandle) {
      if (elem == null || elem == undefined) return;
      if ( elem.addEventListener ) {
          elem.addEventListener( type, eventHandle, false );
      } else if ( elem.attachEvent ) {
          elem.attachEvent( "on" + type, eventHandle );
      }
  };
  
  function use_class(element_id, class_name, include)
  {
    var element = document.getElementById(element_id);
    var has_class = (element.className.indexOf(class_name) >= 0);

    if (include && !has_class)
      element.className += ' ' + class_name;
    else if (!include && has_class)
      element.className = element.className.replace(' ' + class_name, '');
  }

  function update_class_for_width(element_id, class_name, min_width)
  {
     var viewportwidth;
     
     // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
     
     if (typeof window.innerWidth != 'undefined')
     {
          viewportwidth = window.innerWidth;
     }
     
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    
     else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0)
     {
           viewportwidth = document.documentElement.clientWidth;
     }
     
     // older versions of IE
     
     else
     {
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
     }

     var is_min_width = ((min_width > 0) ? (viewportwidth > min_width) : (viewportwidth <= -min_width)); 
     use_class(element_id, class_name, is_min_width);
  }
  
  function update_css_on_resize()
  {
     var element_id = 'htmlbody_inner';
  
     update_class_for_width(element_id, 'small_screen', -900);
     update_class_for_width(element_id, 'middle_screen', 900);
     update_class_for_width(element_id, 'middle2_screen', 1050);
     update_class_for_width(element_id, 'big_screen', 1200);
  }
  
  addEvent(window, "resize", update_css_on_resize);
  addEvent(window, "load", update_css_on_resize);
  
  
// perldoc.js
//
// JavaScript functions for perldoc.perl.org


//-------------------------------------------------------------------------
// perldoc - site-level functions
//-------------------------------------------------------------------------

var mcSite = {

  // startup - page initialisation functions ------------------------------

  startup: function() {
    toolbar.setup();
  },
}

//-------------------------------------------------------------------------
// toolbar - functions to control the floating toolbar
//-------------------------------------------------------------------------

var toolbar = {
  
  // state - holds the current CSS positioning state (fixed or static)
  
  state: null,
  
  toolbar_tag_id: 'fixedHeader',
  next_tag_id: 'menu',
  
  originalPosition: null,
  originalOffsetTop: null,
  originalOffsetHeight: null,
  originalNextTagRelativeTop: null,
  
  
  
  // setup - initialises the window.onscroll handler
  
  setup: function() {
    var toolbarType = 'standard1';

    var toolbarElement = document.getElementById(toolbar.toolbar_tag_id);
    var nextElement = document.getElementById(toolbar.next_tag_id);
    toolbar.originalposition = toolbarElement.style.position;
    toolbar.originalOffsetTop = toolbarElement.offsetTop;
    toolbar.originalOffsetHeight = toolbarElement.offsetHeight;
    toolbar.originalNextTagRelativeTop = nextElement.offsetTop - toolbarElement.offsetTop;
    toolbar.state = toolbar.originalPosition;

    if (toolbarType != 'standard') {
      toolbar.checkPosition();
   
      if (toolbar.state == 'fixed') {
        // If an internal link was called (x.html#y) the link position will be
        // behind the toolbar so the page needs to be scrolled 90px down
        anchor = location.hash.substr(1);
        if (anchor) {
          var allLinks = document.body.getElements('a');
          allLinks.each(function(link) {
            if (link.get('name') == anchor) {
              window.scrollTo(0,link.offsetTop + (toolbar.originalOffsetHeight + 5));
            }
          });
        }
      }

      toolbar.rewriteLinks();
      addEvent(window, "scroll", toolbar.checkPosition);
    }
  },
  
  
  // checkPosition - checks the scroll position and updates toolbar
  
  checkPosition: function() {
    if ((toolbar.state == toolbar.originalPosition) && (toolbar.getCurrentYPos() > toolbar.originalOffsetTop - 5)) {
      use_class(toolbar.toolbar_tag_id, 'fixed', true);
      document.getElementById(toolbar.next_tag_id).style.marginTop = toolbar.originalNextTagRelativeTop + "px"; 
      
      toolbar.state = 'fixed';
    }
    
    if ((toolbar.state == 'fixed') && (toolbar.getCurrentYPos() <= toolbar.originalOffsetTop - 5)) {
      use_class(toolbar.toolbar_tag_id, 'fixed', false);
      document.getElementById(toolbar.next_tag_id).style.marginTop = "0px"; 
      toolbar.state = toolbar.originalPosition;
    }    
  },
  
  
  // getCurrentYPos - returns the vertical scroll offset
  
  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },
  
  
  // goToTop - scroll to the top of the page
  
  goToTop: function() {
    use_class(toolbar.toolbar_tag_id, 'fixed', false);
    toolbar.state = toolbar.originalPosition;
    window.scrollTo(0,0);
  },
  
  // rewriteLinks - stop internal links appearing behind the toolbar
  // Based on code written by Stuart Langridge - http://www.kryogenix.org
  
  rewriteLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.body.getElementsByTagName('a');
    
    // Walk through the list
    for(var link in allLinks) {
      if ((link.href && link.href.indexOf('#') != -1) && 
          ( (link.pathname == location.pathname) ||
	    ('/'+link.pathname == location.pathname) ) && 
          (link.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        link.addEvent('click',toolbar.linkScroll);
      }
    } 
  },
  
  
  // linkScroll - follow internal link and scroll the page
  // Based on code written by Stuart Langridge - http://www.kryogenix.org
  
  linkScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.body.getElements('a');
    var destinationLink = null;
    for(var link in allLinks) {
      if (link.name && (link.name == anchor)) {
        destinationLink = link;
      }
    }
    if (!destinationLink) destinationLink = document.getElementsByName()[0];

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var desty    = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      desty += thisNode.offsetTop;
    }

    // Follow the link    
    location.hash = anchor;
    
    // Scroll if necessary to avoid the top nav bar
    if ((window.pageYOffset > 120) && ((desty + window.innerHeight - 120) < toolbar.getDocHeight())) {
      window.scrollBy(0,-90);
    }
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },
  
  // getDocHeight - return the height of the document
  
  getDocHeight: function() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
  }
          
};

  addEvent(window, "load", mcSite.startup);

