// Pop!
// The Stage6 tooltip.
// Written with a little bit of prototyping and a whole lot o' heart.

function Pop() {
	this.delay = 650;
	this.tooltip_width = 200;
	this.from = false;
	this.to = false;
	this.legit = false;
	this.new_from = false;
	this.new_to = false;
	this.new_legit = false;
	this.now_showing = false;
}

Pop.prototype.start = function (from, to, side) {
	this.new_from = from;
	this.new_to = document.getElementById(to);
	this.new_legit = true;
	this.side = side;
	
	this.new_from.onmouseout = this.new_to_quit
	setTimeout('pop.show()', this.delay);
}
Pop.prototype.show = function () {
	if (this.new_legit && !this.now_showing) {
		// Now showing...
		this.now_showing = true;
		
		// Kill any old mouseover.
		this.legit = false;
		this.hide();
		
		// Make the switch.
		this.from = this.new_from;
		this.to = this.new_to;
		
		// Left or right?
		if (this.side == null) {
			this.side = this.get_x() < this.half_window_width() ? 'left' : 'right';
		}
		
		// First, if "from" is text, make it simple.
		if (typeof(this.from.width) == 'undefined' || typeof(this.from.height) == 'undefined') {
			this.to.style.top = (this.get_y() + 10) + 'px';
			this.to.style.left = this.get_x() + 'px';
		}
		else {
			this.to.style.top = (this.get_y() + this.from.height - 10) + 'px';
			
			// Left or right.
			if (this.side == 'left') {
				this.to.style.left = (this.get_x() + 10) + 'px';
			}
			else {
				this.to.style.left = (this.get_x() + this.from.width - this.tooltip_width) + 'px';
				for (var i = 0; i < this.to.childNodes.length; i++) {
					if (this.to.childNodes[i].className == 'tooltip-zoom-left') {
						this.to.childNodes[i].className = 'tooltip-zoom-right';
						break;
					}
				}
			}
		}
		//this.to.style.display = 'block';
		new Effect.Appear(this.to.id, { duration: 0.2 });
		
		// Set all the mouse events.
		this.old_onmouseover = this.from.onmouseover;
		this.to.onmouseover = this.too_legit;
		this.from.onmouseover = this.too_legit;
		this.to.onmouseout = this.to_quit;
		this.from.onmouseout = this.to_quit;
	}
}
Pop.prototype.hide = function () {
	if (!this.legit && this.to != false) {
		//this.to.style.display = 'none';
		new Effect.Fade(this.to.id, { duration: 0.2 });
		this.from.onmouseover = this.old_onmouseover;
		this.from = false;
		this.to = false;
		clearInterval(this.interval);
		this.now_showing = false;
	}
}

Pop.prototype.too_legit = function () {
	pop.legit = true;
}
Pop.prototype.to_quit = function () {
	pop.legit = false;
	pop.countdown();
}
Pop.prototype.countdown = function () {
	if (this.interval) {
		clearInterval(this.interval);
	}
	this.interval = setInterval('pop.hide()', this.delay);
}
Pop.prototype.new_to_quit = function () {
	pop.new_legit = false;
}

// Retrieve x and y coordinates of the 'from' elements.
// It appears the Netscape way (element.x) is not smart.
Pop.prototype.get_x = function () {
	var x = this.from.offsetLeft;
	var p = this.from.offsetParent;
	while (p != null) {
		x += p.offsetLeft;
		p = p.offsetParent;
	}
	return x;
}
Pop.prototype.get_y = function () {
	var y = this.from.offsetTop;
	var p = this.from.offsetParent;
	while (p != null) {
		y += p.offsetTop;
		p = p.offsetParent;
	}
	return y;
}
Pop.prototype.half_window_width = function () {
	if (typeof(window.innerWidth) == 'undefined') {
		// IE.
		// We should have is_ie / is_netscape / whatever available everywhere.
		return document.body.offsetWidth / 2;
	}
	return window.innerWidth / 2;
}

pop = new Pop();
