Function.prototype.method = function (name, func) {
	this.prototype[name] = func;
	return this;
};

function SimplePopupClass(element, content, width) {
	function SimplePopupClass() {
		this.x = null;
		this.y = null;
		this.width = width;
		this.content = content;
		this.popup = null;
		this.element = element;
		this.element.onmouseover = this.elementOnMouseOver;
	 	this.element.onmouseout = this.elementOnMouseOut;
		element.control = this;
	}

	with(SimplePopupClass){

		method("elementOnMouseOver", function(e){
			if(window.event!=null){ e = window.event; }
			this.control.getXY(false);
			if(this.popup==null){
				this.popup = top.document.createElement("div");
				this.popup.className = "SimplePopup";
				this.popup.innerHTML = this.control.content;
				this.popup.style.position = "absolute";
				this.popup.style.width = this.control.width + "px";
				top.document.body.appendChild(this.popup);
			}
			//---- does not allow popup to stretch off the page ----//
			this.popup.style.left = (this.control.windowWidth() < this.control.x + this.control.width ? this.control.windowWidth() - this.control.width - 10 : this.control.x) + "px";
	 		this.popup.style.top = (this.control.y + 16) + "px";
			this.popup.style.display = "block";
		});

		method("elementOnMouseOut", function(e){
			if(window.event!=null){ e = window.event; }
			if(this.popup!==null){
				this.popup.style.display = "none";
			}
		});

		method("getXY", function(subtractScroll){
			var obj = this.element;
			var x = 0;
			var y = 0;
			var somethingFoundX = false;
			var somethingFoundY = false;

			//---- Calculate xy position --------------------------------------------
			while (obj.offsetParent) {
				x += obj.offsetLeft;
				if(subtractScroll && obj.scrollLeft){ x -= obj.scrollLeft; }
				somethingFoundX = true;
				y += obj.offsetTop;
				if(subtractScroll && obj.scrollTop){ y -= obj.scrollTop; }
				somethingFoundY = true;
				obj = obj.offsetParent;
			}
			if(obj.offsetLeft){
				x += obj.offsetLeft;
				if(subtractScroll && obj.scrollLeft){ x -= obj.scrollLeft; }
				somethingFoundX = true;
			}
			if(obj.offsetTop){
				y += obj.offsetTop;
				if(subtractScroll && obj.scrollTop){ y -= obj.scrollTop; }
				somethingFoundY = true;
			}
			if(!somethingFoundX){ x = null; }
			if(!somethingFoundY){ y = null; }
			this.x = x;
			this.y = y;
		});
		
		method("windowWidth", function(){
			var win = window;
			if(win.document.body && win.document.body.offsetWidth){
				return win.document.body.offsetWidth;
			}else if(win.document.documentElement && win.document.documentElement.clientWidth){
				return win.document.documentElement.clientWidth;
			}else{
				return win.innerwidth;
			}
		});

	}

	return new SimplePopupClass();
}
