var ie55 = /MSIE ((5\.[56789])|([6789]))/.test( navigator.userAgent ) &&
			navigator.platform == "Win32";
if ( !ie55 ) {
	window.onerror = function (message, file, line) {
		alert(message + "\n" + file + "\n Line " + line);
		return true;
	};
}
function writeNotSupported() {
	if ( !ie55 ) {
		document.write( "<p class=\"warning\">" +
			"This script only works in Internet Explorer 5.5" +
			" or greater for Windows</p>" );
	}
}

var posLib = {

	getIeBox:		function (el) {
		return this.ie && el.document.compatMode != "CSS1Compat";
	},
	
	// relative client viewport (outer borders of viewport)
	getClientLeft:	function (el) {
		var r = el.getBoundingClientRect();
		return r.left - this.getBorderLeftWidth(this.getCanvasElement(el));
	},

	getClientTop:	function (el) {
		var r = el.getBoundingClientRect();
		return r.top - this.getBorderTopWidth(this.getCanvasElement(el));
	},

	// relative canvas/document (outer borders of canvas/document,
	// outside borders of element)
	getLeft:	function (el) {
		return this.getClientLeft(el) + this.getCanvasElement(el).scrollLeft;
	},

	getTop:	function (el) {
		return this.getClientTop(el) + this.getCanvasElement(el).scrollTop;
	},

	// relative canvas/document (outer borders of canvas/document,
	// inside borders of element)
	getInnerLeft:	function (el) {
		return this.getLeft(el) + this.getBorderLeftWidth(el);
	},

	getInnerTop:	function (el) {
		return this.getTop(el) + this.getBorderTopWidth(el);
	},

	// width and height (outer, border-box)
	getWidth:	function (el) {
		return el.offsetWidth;
	},

	getHeight:	function (el) {
		return el.offsetHeight;
	},

	getCanvasElement:	function (el) {
		var doc = el.ownerDocument || el.document;	// IE55 bug
		if (doc.compatMode == "CSS1Compat")
			return doc.documentElement;
		else
			return doc.body;
	},

	getBorderLeftWidth:	function (el) {
		return el.clientLeft;
	},

	getBorderTopWidth:	function (el) {
		return el.clientTop;
	},

	getScreenLeft:	function (el) {
		var doc = el.ownerDocument || el.document;	// IE55 bug
		var w = doc.parentWindow;
		return w.screenLeft + this.getBorderLeftWidth(this.getCanvasElement(el)) +
			this.getClientLeft(el);
	},

	getScreenTop:	function (el) {
		var doc = el.ownerDocument || el.document;	// IE55 bug
		var w = doc.parentWindow;
		return w.screenTop + this.getBorderTopWidth(this.getCanvasElement(el)) +
			this.getClientTop(el);
	}
};

posLib.ua =		navigator.userAgent;
posLib.safari =	/safari/i.test(posLib.ua);posLib.opera =	/opera [56789]|opera\/[56789]/i.test(posLib.ua);posLib.ie =		(!posLib.opera) && /MSIE/.test(posLib.ua);
posLib.ie5 =	posLib.ie && /MSIE 5/.test(posLib.ua);
posLib.ie55 = 	ie55;
posLib.ie50 =	posLib.ie5 && !posLib.ie55;
posLib.ie6 =	posLib.ie && /MSIE 6/.test(posLib.ua);
posLib.ie7 =	posLib.ie && /MSIE [789]/.test(posLib.ua);
posLib.moz =	!posLib.opera && /gecko/i.test(posLib.ua);


function getPageMode() { // Private method
  if (document.compatMode) {
    switch (document.compatMode) {
      case "BackCompat":
        return 0;
      case "CSS1Compat":
        return 1;
      case "QuirksMode":
        return 0;
    }
  } else {
    if (posLib.ie5) {
      return 0;
    }
    if (posLib.safari) {
      return 1;
    }
  }
  return 0;
}
posLib.pageMode = getPageMode();

//
function emulateEventHandlers(eventNames) {
   for (var i = 0; i < eventNames.length; i++) {	
      document.addEventListener(eventNames[i], function (e) { window.event = e; }, true); // using capture
   }
}

//
function emulateAllModel() {
	var allGetter = function () {
	   var a = this.getElementsByTagName("*");
	   var node = this;
	   a.tags = function (sTagName) {
	      return node.getElementsByTagName(sTagName);
	   };
	   return a;
	};
	HTMLDocument.prototype.__defineGetter__("all", allGetter);
	HTMLElement.prototype.__defineGetter__("all", allGetter);
}

//
if (posLib.moz) {
	emulateEventHandlers(["mousedown", "click", "contextmenu", "selectstart", "dragstart", "keydown", "mousemove", "mouseover", "drop"]);
	emulateAllModel();
}
