// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/

/* SOURCE FILE: AnchorPosition.js */
function getAnchorPosition(anchorname){var useWindow=false;var coordinates=new Object();var x=0,y=0;var use_gebi=false, use_css=false, use_layers=false;if(document.getElementById){use_gebi=true;}else if(document.all){use_css=true;}else if(document.layers){use_layers=true;}if(use_gebi && document.all){x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);}else if(use_gebi){var o=document.getElementById(anchorname);x=AnchorPosition_getPageOffsetLeft(o);y=AnchorPosition_getPageOffsetTop(o);}else if(use_css){x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);}else if(use_layers){var found=0;for(var i=0;i<document.anchors.length;i++){if(document.anchors[i].name==anchorname){found=1;break;}}if(found==0){coordinates.x=0;coordinates.y=0;return coordinates;}x=document.anchors[i].x;y=document.anchors[i].y;}else{coordinates.x=0;coordinates.y=0;return coordinates;}coordinates.x=x;coordinates.y=y;return coordinates;}
function getAnchorWindowPosition(anchorname){var coordinates=getAnchorPosition(anchorname);var x=0;var y=0;if(document.getElementById){if(isNaN(window.screenX)){x=coordinates.x-document.body.scrollLeft+window.screenLeft;y=coordinates.y-document.body.scrollTop+window.screenTop;}else{x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;}}else if(document.all){x=coordinates.x-document.body.scrollLeft+window.screenLeft;y=coordinates.y-document.body.scrollTop+window.screenTop;}else if(document.layers){x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;}coordinates.x=x;coordinates.y=y;return coordinates;}
function AnchorPosition_getPageOffsetLeft(el){var ol=el.offsetLeft;while((el=el.offsetParent) != null){ol += el.offsetLeft;}return ol;}
function AnchorPosition_getWindowOffsetLeft(el){return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;}
function AnchorPosition_getPageOffsetTop(el){var ot=el.offsetTop;while((el=el.offsetParent) != null){ot += el.offsetTop;}return ot;}
function AnchorPosition_getWindowOffsetTop(el){return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;}


/* ADAPTED FROM SOURCE FILE: PopupWindow.js */
// Set the position of the popup window based on the anchor
function PopupWindow_getXYPosition(anchorname) {
	var coordinates= getAnchorWindowPosition(anchorname);
	this.x = coordinates.x;
	this.y = coordinates.y;
}
// Set width/height of popup window
function PopupWindow_setSize(width,height) {
	this.width = width;
	this.height = height;
}
// Set the URL to go to
function PopupWindow_setUrl(url) {
	this.url = url;
}
// Set the window popup properties
function PopupWindow_setWindowProperties(props) {
	this.windowProperties = props;
}
// Position and show the popup, relative to an anchor object
function PopupWindow_showPopup(anchorname) {
	if (!this.popupWindow) {
		this.getXYPosition(anchorname);
		this.x += this.offsetX;
		this.y += this.offsetY;
		if (this.x<0) { this.x=0; }
		if (this.y<0) { this.y=0; }
		if (screen && screen.availHeight) {
			if ((this.y + this.height) > screen.availHeight) {
				this.y = screen.availHeight - this.height;
			}
		}
		if (screen && screen.availWidth) {
			if ((this.x + this.width) > screen.availWidth) {
				this.x = screen.availWidth - this.width;
			}
		}
		this.popupWindow = window.open(this.url,"window_"+anchorname,this.windowProperties+",width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+"");
		this.popupWindow.focus();
	}
}
// Hide the popup
function PopupWindow_hidePopup() {
	if (this.popupWindow) {
		this.popupWindow.close();
		this.popupWindow = null;
	}
}
// Call this to make the popup close automatically when mouse is clicked outside it
function PopupWindow_autoHide() {
	this.autoHideEnabled = true;
}
// This global function checks all PopupWindow objects onmouseup to see if they should be hidden
function PopupWindow_newListener() {
	// call old listener if exists
	if (popupWindowOldListener != null) {
		popupWindowOldListener();
	}
	// scan open popups
	for (var i=0; i<popupWindowObjects.length; i++) {
		if (popupWindowObjects[i] != null) {
			var p = popupWindowObjects[i];
			if (p.autoHideEnabled) p.hidePopup();
			popupWindowObjects[i] = null;
		}
	}
}
// Attach the event listener
function PopupWindow_attachListener() {
	if (!listenerAttached) {
		if (document.layers) {
			document.captureEvents(Event.MOUSEUP);
		}
		popupWindowOldListener = document.onmouseup;
		document.onmouseup = PopupWindow_newListener;
		listenerAttached = true;
	}
}

// Global variables
var popupWindowOldListener = null;
var listenerAttached = false;
var popupWindowIndex = 0;
var popupWindowObjects = new Array();

// CONSTRUCTOR for the PopupWindow object
function PopupWindow() {
	// initializations
	PopupWindow_attachListener();
	popupWindowObjects[popupWindowIndex++] = this;
	// properties
	this.popupWindow = null;
	this.width=0;
	this.height=0;
	this.url="";
	this.windowProperties="toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no";
	this.offsetX = 0;
	this.offsetY = 0;
	this.x = 0;
	this.y = 0;
	this.autoHideEnabled = false;
	// methods
	this.getXYPosition = PopupWindow_getXYPosition;
	this.setUrl = PopupWindow_setUrl;
	this.setWindowProperties = PopupWindow_setWindowProperties;
	this.showPopup = PopupWindow_showPopup;
	this.hidePopup = PopupWindow_hidePopup;
	this.setSize = PopupWindow_setSize;
	this.autoHide = PopupWindow_autoHide;
}

/* Main function */
function web_popup(url, pos, w, h) {

	var popup = new PopupWindow();

	var w_max = Math.round(screen.width * 0.90);
	var h_max = Math.round(screen.height * 0.90);
	if (w <= 0 || w > w_max) w = w_max;
	if (h <= 0 || h > h_max) h = h_max;

	popup.setSize(w, h);
	popup.setUrl(url);
	popup.autoHide();
	popup.showPopup(pos);
}

