/*
NavTip 1.0.0 (c) 2009 by Osem Websystems

Requires MooTools.

EXAMPLE:

window.addEvent("domready", function() {
	myNavTip = new NavTip({ holder: $("silly_menu") });
});

<body>
  <div id="silly_menu">
	<div class="element">
	  OPTION 1
	</div>
	<div class="tip">
	  Hello there.
	</div>
	<div class="element">
	  OPTION 2
	</div>
	<div class="tip">
	  And hello again.
	</div>
  </div>
</body>
*/

var NavTip = new Class({

	Implements: [Options, Events],

	options: {
		holder: null,
		tipXOffset: null,
		tipYOffset: null
	},

	initialize: function(options) {
		this.holder = $type(options) == "element" ? options : options.holder;
		if (this.holder == null) return false;
		this.holder.o = this;
		this.els = this.holder.getElements(".element");
		this.tips = this.holder.getElements(".tip");
		this.els.setStyles({ position: "relative", cursor: "pointer" });
		this.tips.setStyles({ position: "absolute", zIndex: 1000 });
		this.tips.addClass("off");
		for (var i = 0; i < this.els.length; i++) {
			var coords = this.els[i].getCoordinates();
			var xOff = this.parseOffset(options.tipXOffset, coords.width);
			var yOff = this.parseOffset(options.tipYOffset, coords.height);
			this.tips[i].setStyles({ left: coords.left + xOff, top: coords.top + yOff });
			this.els[i].o = this;
			this.els[i].tip = this.tips[i];
			this.els[i].addEvent("mouseenter", function() {
				this.o.collapse();
				if (this.tip.innerHTML.replace(/\s/g, "") != "") {
					this.tip.removeClass("tip");
					this.tip.addClass("tip_on");
				}
			});
		}
		this.holder.addEvent("mouseleave", function() {
			this.o.collapse();
		});
	},

	collapse: function() {
		this.tips.each(function(tip) {
			tip.removeClass("tip_on");
			tip.addClass("tip");
		});
	},

	parseOffset: function(off, src) {
		if ($type(off) == "number") return off;
		if (String(off).contains("%")) off = parseInt((parseFloat(off) / 100) * src, 10);
		return isNaN(off) ? 0 : off;
	}
});

function addStyle(s) {
	var head = document.getElementsByTagName("head")[0];
	var el = document.createElement("style");
	el.type = "text/css";
	el.media = "screen";
	if (el.styleSheet) el.styleSheet.cssText = s; else el.appendChild(document.createTextNode(s));
	head.appendChild(el);
	return el;
}

addStyle(".tip { display: none; } .tip_on { display:; }");