/**
 * Replaces MSIE6's missing :hover pseudoclass support
 *
 * @see prototype.js
 **/
kHover = Class.create();
kHover.prototype = {
	initialize: function(s) {
		if (document.all && !window.XMLHttpRequest) {                      // If MSIE v<7
			this.settings = s;
			this.events = [];
			this.records = [];
			this.s = {
				applyTo: [],                                                   // Selectors to pay attention to, everything else is ignored.
				hoverClass: 'hover',                                            // The class to attach to an element during the time it is being mouseovered
				delay: 0
			}
			for (var key in s) this.s[key] = s[key];

			for (var x=0; x<this.s.applyTo.length; x++) {
				var elements = document.getElementsBySelector(this.s.applyTo[x]);
				for (var y=0; y<elements.length; y++) {
					this.events.push(Event.observe(elements[y], 'mouseover', this.hover.bind(this, elements[y])));
					this.events.push(Event.observe(elements[y], 'mouseout',  this.unHover.bind(this, elements[y])));

					var curEl = elements[y];
					var body = document.getElementsByTagName('body')[0];
					var ancestors = [];
					while (curEl != body) {
						curEl = curEl.parentNode;
						ancestors.push(curEl);
					}

					this.records.push({
						node: elements[y],
						unHoverTimer: null,
						hovered: false,
						ancestors: ancestors
					});
				}
			}
		} else {
			this.destruct();
			return false;
		}
	},

	hover: function(el) {
		var rec = this.getRec(el);
		rec.hovered = true;
		if (this.s.delay != 0) {
			clearTimeout(rec.unHoverTimer);
			rec.unHoverTimer = null;
		}
		Element['addClassName'](el, this.s.hoverClass);
		for (var x=0; x<rec.ancestors.lenth; x++) {
			Element['addClassName'](rec.ancestors[x], this.s.hoverClass);
		}
	},

	unHover: function(el) {
		var rec = this.getRec(el);
		if (this.s.delay != 0) {
			if (rec.unHoverTimer == null) {
				rec.unHoverTimer = setTimeout(function(rec, el) {
					Element['removeClassName'](el, this.s.hoverClass);
					for (var x=0; x<rec.ancestors.lenth; x++) {
						Element['removeClassName'](rec.ancestors[x], this.s.hoverClass);
					}
				}.bind(this, rec, el), this.s.delay);
			}
		} else {
			Element['removeClassName'](el, this.s.hoverClass);
			for (var x=0; x<rec.ancestors.lenth; x++) {
				Element['removeClassName'](rec.ancestors[x], this.s.hoverClass);
			}
		}
		rec.hovered = false;
	},

	getRec: function(el) {
		for (var x=0; x<this.records.length; x++) {
			if (this.records[x].node == el) return this.records[x];
		}
		return false;
	},

	destruct: function() {
		// Once we learn how to destruct, we'll do it here.
	}
}