var version = "@(#)popup.js	1.16 02/06/04 10:06:28";

var pop 	   	= null;
var popupImages = null;
var timeOutVal 	= null;
var interval   	= null;
var cookie 	   	= Math.floor(Math.random() * 1000);

// generic positioning methods

// examines two objects to see if they
// intersect on the page
function objectsIntersect(obj1, obj2)
{
	var horizontalIntersection = false;
	var verticalIntersection = false;

	left1 	= getOffsetLeft(obj1);
	top1 	= getOffsetTop(obj1);
	width1 	= obj1.offsetWidth;
	height1 = obj1.offsetHeight;

	left2 	= getOffsetLeft(obj2);
	top2 	= getOffsetTop(obj2);
	width2 	= obj2.offsetWidth;
	height2 = obj2.offsetHeight;

	//alert("obj1: left=" + left1 + ", top=" + top1 + ", width=" + width1 + ", height=" + height1);
	//alert("obj2: left=" + left2 + ", top=" + top2 + ", width=" + width2 + ", height=" + height2);

	if (((left1 <= left2) && (left1 + width1 > left2)) ||   // 1
		((left2 <= left1) && (left2 + width2 > left1)))     // 2
		horizontalIntersection = true;

	if (((top1 <= top2) && (top1 + height1 > top2)) ||      // 3
		((top2 <= top1) && (top2 + height2 > top1)))        // 4
		verticalIntersection = true;

	return (horizontalIntersection & verticalIntersection);
}

function getOffsetLeft(obj)
{
	var rv = obj.offsetLeft;
	while (null != (obj = obj.offsetParent)) {
		rv += obj.offsetLeft;
	}
	return rv;
}

function getOffsetTop(obj)
{
	var rv = obj.offsetTop;
	while (null != (obj = obj.offsetParent)) {
		rv += obj.offsetTop;
	}
	return rv;
}


// ie size and position methods
function ieGetWidth()
{
	return this.style.pixelWidth;
}

function ieGetHeight()
{
	return this.style.pixelHeight;
}

function ieGetTop()
{
	return this.style.pixelTop;
}

function ieGetLeft()
{
	return this.style.pixelLeft;
}

function ieSetPos(x, y)
{
	this.style.pixelLeft = x;
	this.style.pixelTop = y;
}

function ieSetSize(w, h)
{
	this.style.pixelWidth = w;
	this.style.pixelHeight = h;
}

function ieShiftY(y)
{
	var t = this.getTop() - y;
	var b = document.body.scrollTop + 5;
	if (t < b)
		t = b;
	this.style.pixelTop = t;
}

function ieShiftX(x)
{
	var l = this.getLeft() - x;
	var b = document.body.scrollLeft + 5;
	if (l < b)
		l = b;
	this.style.pixelLeft = l;
}

function ieSetImgSize(x, y)
{
	this.maxw = x + 4;
	this.maxh = y + 4;
	this.deltaw = parseInt(this.maxw/this.steps);
	this.deltah = parseInt(this.maxh/this.steps);
}

function ieWindowWidth()
{
	return document.body.clientWidth;
}

function ieWindowHeight()
{
	return document.body.clientHeight;
}

function ieShift()
{
	var docWidth = document.body.clientWidth + document.body.scrollLeft;
	var docHeight = document.body.clientHeight + document.body.scrollTop;

	var s = this.getLeft() + this.getWidth() + 5 - docWidth;
	if (s > 0)
		this.shiftX(s);
	s = this.getTop() + this.getHeight() + 5 - docHeight;
	if (s > 0)
		this.shiftY(s);
	this.hideObscuringSelects();
}

function ieGrow()
{
	var x = this.getWidth() + this.deltaw;
	var y = this.getHeight() + this.deltah;
	if (x < this.maxw && y < this.maxh) {
		this.style.pixelWidth = x;
		this.style.pixelHeight = y;
		this.shift();
		return true;
	} else {
		return false;
	}
}

function ieSetImage(timage)
{
	// Boo, IE! For shame!
	var nodestr = "<img";
	nodestr += " id=\"rollover_image\"";
	nodestr += " src=\"" + timage.src + "\"";
	nodestr += " border=\"0\"";

	if (timage.complete) {
		var size = { w:timage.width, h:timage.height };
		size = this.rescale(size);

		nodestr += " height=\"" + size.h + "\"";
		nodestr += " width=\"" + size.w + "\"";
	}
	nodestr += ">";
	this.imgnode = document.createElement(nodestr);
	this.appendChild(this.imgnode);
}

function ieHide()
{
	this.style.visibility = "hidden";
	this.showObscuringSelects();
}

function ieShow()
{
	this.style.visibility = "visible";
	this.hideObscuringSelects();
}

// moz size and position methods
function mozGetWidth()
{
	return this.offsetWidth;
}

function mozGetHeight()
{
	return this.offsetHeight;
}

function mozGetTop()
{
	return this.offsetTop;
}

function mozGetLeft()
{
	return this.offsetLeft;
}

function mozSetPos(x, y)
{
	this.style.top = y;
	this.style.left = x;
}

function mozSetSize(w, h)
{
	this.style.width = w;
	this.style.height = h;
}

function mozShiftY(y)
{
	var t = this.getTop() - y;
	var b = window.scrollY + 5 + 4;
	if (t < b)
		t = b;
	this.style.top = t;
}

function mozShiftX(x)
{
	var l = this.getLeft() - x;
	var b = window.scrollX + 5 + 4;
	if (l < b)
		l = b;
	this.style.left = l;
}

function mozSetImgSize(x, y)
{
	this.maxw = x;
	this.maxh = y;
	this.deltaw = parseInt(this.maxw/this.steps);
	this.deltah = parseInt(this.maxh/this.steps);
}

function mozWindowWidth()
{
	// innerWidth and innerHeight include scrollbars and other decorations
	// some versions of mozilla provide clientWidth and clientHeight
	return (document.body.clientWidth) ? document.body.clientWidth
		: window.innerWidth;
}

function mozWindowHeight()
{
	// innerWidth and innerHeight include scrollbars and other decorations
	// some versions of mozilla provide clientWidth and clientHeight
	return (document.body.clientHeight) ? document.body.clientHeight
		: window.innerHeight;
}

function mozShift()
{
	var docWidth = this.windowWidth() + window.scrollX;
	var docHeight = this.windowHeight() + window.scrollY;

	var s = this.getLeft() + this.getWidth() + 5 - docWidth;
	if (s > 0)
		this.shiftX(s);
	s = this.getTop() + this.getHeight() + 5 - docHeight;
	if (s > 0)
		this.shiftY(s);
}

function mozGrow()
{
	var x = this.getWidth() + this.deltaw + 4;
	var y = this.getHeight() + this.deltah + 4;
	if (x < this.maxw && y < this.maxh) {
		this.style.width = x;
		this.style.height = y;
		this.shift();
		return true;
	} else {
		return false;
	}
}

function mozSetImage(timage)
{
	this.imgnode = document.createElement("img");
	this.imgnode.setAttribute("border", "0");
	this.imgnode.setAttribute("id", "rollover_image");
	this.imgnode.setAttribute("src", timage.src);
	if (timage.complete) {
		var size = { w:timage.width, h:timage.height };
		size = this.rescale(size);
		this.imgnode.setAttribute("height", size.h);
		this.imgnode.setAttribute("width", size.w);
	}

	this.appendChild(this.imgnode);
}

function mozShow()
{
	this.style.visibility = "visible";
}

function mozHide()
{
	this.style.visibility = "hidden";
}


// generic popup functions

function hideObscuringSelects()
{
	var aSelects = document.getElementsByTagName("select");
	var j = 0;
	for (var i = 0; i < aSelects.length; i++) {
		if (objectsIntersect(aSelects[i], this)) {
			if (aSelects[i].style.visibility = "visible") {
				this.hiddenSelects[j++] = aSelects[i];
				aSelects[i].style.visibility = "hidden";
			}
		}
	}
}

function showObscuringSelects(obj)
{
	var i = 0;
	while (null != (sel = this.hiddenSelects[i])) {
		if ((this.style.visibility == "hidden")
			|| !objectsIntersect(sel, this)) {
			sel.style.visibility = "visible";
			this.hiddenSelects[i] = null;
		}
		i++;
	}
}

function setMaxSize()
{
	this.setSize(this.maxw, this.maxh);
}

function rescale(size)
{
	var ratio = size.w / size.h;
	var mh 	  = this.windowHeight() - 10;
	var mw 	  = this.windowWidth() - 10;

	if ((size.h > mh) || (size.w > mw)) {
		if ((size.h - mh) > (size.w - mw)) {
			mw = Math.round(mh * ratio);
		} else {
			mh = Math.round(mw / ratio);
		}
		size.h = mh;
		size.w = mw;
	}

	return size;
}

function unsetImage()
{
	if (this.imgnode) {
		this.removeChild(this.imgnode);
		this.imgnode = null;
	}
}

// enhance the popup node
function setup(pop, listing)
{
	pop.animate   = false;
	pop.steps     = 15;
	pop.delay     = 40;
	pop.maxw      = 0;
	pop.maxh      = 0;
	pop.deltaw    = 0;
	pop.deltah    = 0;
	pop.listing   = listing;
	pop.imgnode   = null;
	pop.hideDelay = 500;
	pop.hiddenSelects = new Array(50);

	if (document.all)
	{
		pop.getHeight  	 = ieGetHeight;
		pop.getLeft    	 = ieGetLeft;
		pop.getTop 	   	 = ieGetTop;
		pop.getWidth   	 = ieGetWidth;
		pop.grow 	   	 = ieGrow;
		pop.setImage  	 = ieSetImage;
		pop.setImgSize 	 = ieSetImgSize;
		pop.setPos 	   	 = ieSetPos;
		pop.setSize    	 = ieSetSize;
		pop.shift 	   	 = ieShift;
		pop.shiftX 	   	 = ieShiftX;
		pop.shiftY 	   	 = ieShiftY;
		pop.windowHeight = ieWindowHeight;
		pop.windowWidth  = ieWindowWidth;
		pop.hide 		 = ieHide;
		pop.show 		 = ieShow;
	} else {
		pop.getHeight  	 = mozGetHeight;
		pop.getLeft    	 = mozGetLeft;
		pop.getTop 	   	 = mozGetTop;
		pop.getWidth   	 = mozGetWidth;
		pop.grow 	   	 = mozGrow;
		pop.setImage  	 = mozSetImage;
		pop.setImgSize 	 = mozSetImgSize;
		pop.setPos 	   	 = mozSetPos;
		pop.setSize    	 = mozSetSize;
		pop.shift 	   	 = mozShift;
		pop.shiftX 	   	 = mozShiftX;
		pop.shiftY 	   	 = mozShiftY;
		pop.windowHeight = mozWindowHeight;
		pop.windowWidth  = mozWindowWidth;
		pop.hide 		 = mozHide;
		pop.show 		 = mozShow;
	}
	pop.setMaxSize = setMaxSize;
	pop.rescale = rescale;
	pop.unsetImage = unsetImage;
	pop.hideObscuringSelects = hideObscuringSelects;
	pop.showObscuringSelects = showObscuringSelects;
}


// image registration

// create a new popup ad object
function ad(listing, url)
{
	this.listing   = listing;
	this.image 	   = new Image();
	this.image.src = url;
}

// add a popup image to the image array
function register_popup(listing, location)
{
 	if (!popupImages) {
 		popupImages = new Array();
 	}
	popupImages.push(new ad(listing, location));
}


// popup display methods

// cancel specified timeout
function CTO(timeIn)
{
  clearTimeout(timeIn);
  return null;
}

// cancel specified interval
function CINT(intIn)
{
	clearInterval(intIn);
	return null;
}

// set up the popup and start the countdown to display,
// if it isn't already being displayed
function set_popup(listing, logUrl, button) {
	if (pop && (pop.listing == listing)) {
		timeOutVal = CTO(timeOutVal);
	} else {
		if (pop)
			hide_popup();

		pop 	   = document.getElementById("rollover_block");
		var bug    = document.getElementById("log_image");

		if (!pop || !button || !bug || !logUrl) {
			pop = null;
			return;
		}

		setup(pop, listing);

		var pos = { x:0, y:0 };
		pos.x += parseInt(button.offsetWidth / 2);
		pos.y += button.offsetHeight + 2;
		do {
			pos.x += button.offsetLeft;
			pos.y += button.offsetTop;
			button = button.offsetParent;
		}  while( button );

		// use a fake url parameter to force a browser request
		logUrl = logUrl.replace(/(a=)/, "ck=" + ++cookie + "&" + "$1");
		// send the logging redirector request
		bug.setAttribute("src", logUrl);

		// insert the ad image
		for (i=0; i < popupImages.length; i++) {
			if (popupImages[i].listing == listing) {
				pop.setImage(popupImages[i].image);
				break;
			}
		}
		pop.setPos(pos.x, pos.y);
		pop.setSize(104,20);
		pop.show();

		interval = setInterval('display_popup()', 10);
	}
}

// turn on the popup and start the grow light, if applicable
function display_popup()
{
	var img = document.getElementById("rollover_image");
	if (pop && img) {
		if (img.complete) {
			interval = CINT(interval);
			interval = null;
			pop.hide();
			pop.setImgSize(img.width, img.height);
			if (pop.animate) {
				pop.setSize(0, 0);
				interval = setInterval('grow_popup()', pop.delay);
			} else {
				pop.setMaxSize();
				pop.shift();
				pop.show();
			}
		}
	}
}

// it's alive!
function grow_popup()
{
	if (pop) {
		pop.show();
		if (!pop.grow()) {
			interval = CINT(interval);
			pop.setMaxSize();
			pop.shift();
		}
	}
}

// cancel any hide timer if the popup is displayed
function keep_popup() {
	if (pop)
		timeOutVal = CTO(timeOutVal);
}

// start a timer to hide the popup
function unset_popup() {
	if (pop)
		timeOutVal = setTimeout("hide_popup()", pop.hideDelay);
}

// kill the popup
function hide_popup() {
	if (pop) {
		timeOutVal = CTO(timeOutVal);
		interval = CINT(interval);
		if (pop.hide)
			pop.hide();
		pop.unsetImage();
		pop.setSize(0,0);
		pop = null;
	}
}

// all hail the W3C event model!
if (!document.all) {
	window.addEventListener("click", hide_popup, false);
}
