// helper to leverage the browser-differences of the behaviour
// (concerning the fired events) of an iframe
function setOnLoadWrapper(frame, func) {
  this.ieIframeLoadWrapper = function(frame,func){
    if(frame.readyState == "complete"){
      func();
    }
  }
  if (isIe()==true){
    // ie fires onreadystatechange of the iframe
    // if it's source is changed programtically
    // the wrapper is necessary to correctly handle
    // the iframe's readyState attribute during loading
    frame.onreadystatechange=new ContextFixer(
      this.ieIframeLoadWrapper,this,frame,func).execute;
  }else{
    // moz saimply fires onload...
    frame.onload=func;
  }
}

// removed the stuff set by setOnLoadWrapper()
function removeOnLoadWrapper(frame, ie){
  if (isIe()){
    frame.onreadystatechange=null;
  }else{
    frame.onload=null;
  }
}

// helper to determine whether the browser is ie
function isIe( ){
  return (document.getElementById && document.all && document.styleSheets) ? 1:0;
}

// helper to determine whether the browser is nn
function isNn() {
  return (document.getElementById && !document.all) ? 1:0;
}


// helper to calculate the dimensions (in px) of a given element.
// the calculation reflects that child elements of an element may
// be larger than it's parent. actually the function calcualtes the
// minimal rectangle containg all elements. the parameter ignoreTopLevel
// ignores the dimensions of the given element and only consideres it's
// sub-element-tree.
Dimension = function(element, ignoreTopLevel){
  var positions = new Array();
    this._calculateDimension = function(element, ignoreTopLevel){
    if (ignoreTopLevel== null || ignoreTopLevel == false){
      var leftPosition = 0;
      var topPosition = 0;
      var width = element.offsetWidth;
      var height =element.offsetHeight;
      if (element.offsetParent) {
        leftPosition = element.offsetLeft;
        topPosition = element.offsetTop;
        var parent = element.offsetParent;
        if (parent != null)
          do {
            leftPosition += parent.offsetLeft;
            topPosition += parent.offsetTop;
          } while (parent = parent.offsetParent)
      }
      positions.push({left:leftPosition,top:topPosition,width:width,height:height});
    }
    var all = element.childNodes;
    for(var i = 0; i < all.length; i++){
      var node = all[i];
      if (node.nodeType == 1 && // dom-node
        node.offsetLeft >= 0 && // filter out hidden stuff
        node.id.indexOf("menu")!= 0 && // filter out menu-elements (their left is at -2000)
        node.nodeName!="SCRIPT" && // filter out scriptelements
        node.type !=  "hidden") // filter out hidden inputfields
          this._calculateDimension(node, false);
    }
    }
  this._calculateDimension(element, ignoreTopLevel);

  this.left = 9999;
  this.top = 9999;
  var topElement = null;
  var leftElement = null;
  for (var i = 0; i < positions.length; i++){
    var position = positions[i];
    if (this.left > position.left) {
      this.left = position.left;
      leftElement = position;
    }
    if (this.top > position.top) {
      this.top = position.top;
      topElement = position;
    }
  }
  this.width = 0;
  this.height = 0;
  for (var i = 0; i < positions.length; i++){
    var position = positions[i];
    var offsetLeft = position.left-this.left;
    var currentWidth = offsetLeft+position.width;
    if(currentWidth > this.width)
      this.width= currentWidth;
    var offsetTop = position.top-this.top;
    var currentHeight = offsetTop+position.height;
    if(currentHeight > this.height)
      this.height = currentHeight;
  }
}

// helper to get an XMLHttpRequest instance
getXmlHttpRequest = function(){
  if (window.XMLHttpRequest && window.DOMParser &&
        document.implementation && document.implementation.createDocument) {
            this.request = new XMLHttpRequest();
            return this.request;
    }
    try {
        this.request = new ActiveXObject("MSXML2.XmlHttp");
        return this.request;
    } catch (e) {}
    try {
        this.request = new ActiveXObject("Microsoft.XmlHttp");
        return this.request;
    } catch (e) {}
    try {
        this.request = new ActiveXObject("MSXML.XmlHttp");
        return this.request;
    } catch (e) {}
    try {
        this.request = new ActiveXObject("MSXML3.XmlHttp");
        return this.request;
    } catch (e) {}
}

// wrapper class for eventhandlers, ensures that "this" points
// to the instance the handler is a member of and not to "window
function ContextFixer(func, context) {
    /* Make sure 'this' inside a method points to its class */
    this.func = func;
    this.context = context;
    this.args = arguments;
    var self = this;
    this.execute = function() {
        /* execute the method */
        var args = new Array();
        // the first arguments will be the extra ones of the class
        for (var i=0; i < self.args.length - 2; i++) {
            args.push(self.args[i + 2]);
        };
        // the last are the ones passed on to the execute method
        for (var i=0; i < arguments.length; i++) {
            args.push(arguments[i]);
        };
        self.func.apply(self.context, args);
    };
}


