ToolTipsHandler = Class.create({
	initialize: function(conID) {
		this.conID 	= conID;
		this.list	= $H({});
	},
	
	add: function(id, tt) {
		this.list.set(id, tt);
	},
	
	exists: function(id) {
		if (this.list.keys().include(id)) {
			return true;
		} else {
			return false;
		}
	},
	
	remove: function(id) {
		var tt = (typeof id == 'string') ? this.list.get(id) : this.list.get(id.getAttribute('id'));
		
		tt.destroy();
		this.list.unset(id);
	},
	
	dump: function() {
		this.list.each(function(tt) {
			tt[1].destroy();
		});
		
		this.list = $H({});
	},
	
	update: function(id, text) {
		this.list.get(id).edit(text);
	}
});

GeneralTooltips = new ToolTipsHandler('General');

Tooltip = Class.create({
	initialize: function(element, text, underline, dontescape, tipclass, useCollection) {
		if ($(element)) {

			this.element 	= $(element);
			this.id 		= this.element.hasAttribute("id") ? this.element.getAttribute("id") : randomString(false);
			this.text		= text;

			if (!dontescape) {
				var tdiv 	= new Element('div').update(unescape(this.text));
				this.text 	= tdiv.textContent;
			}

			if (underline != false) {
				if (this.element.src) this.element.setStyle({border: 'none', cursor: 'pointer'});
				else this.element.addClassName('hastooltip');
			}

			this.options = {
				'width': 	'220px',
				'height':	'auto',
				'offsetX':	10,
				'offsetY':	10,
				'align':	'left',
				'class':	(typeof tipclass != undefined && tipclass != undefined) ? tipclass : 'tooltip',
				'zIndex':	10000
			};

		    this.eventMouseOver 	= this.show.bindAsEventListener(this);
		    this.eventMouseOut   	= this.hide.bindAsEventListener(this);
		    this.eventMouseMove		= this.position.bindAsEventListener(this);

			Event.observe(this.element, "mouseover", this.eventMouseOver);
			Event.observe(this.element, "mouseout", this.eventMouseOut);
			Event.observe(this.element, "mousemove", this.eventMouseMove);
			
			this.element.makeUnselectable();
		} else {
			Debug.add('TT: Element ('+element+') does not exist');
		}
	},
	
	show: function(event) {
		Event.stop(event);
		
		if (!$('tooltip_container')) {
			$('win').appendChild(new Element('div', {
				'id': 		'tooltip_container',
				'style': 	'display: none;'
			}));
		}
		
		$('tooltip_container').update(this.text).setStyle({
			'maxWidth': 	this.options.width,
			'height': 		this.options.height,
			'textAlign': 	this.options.align,
			'class':		'panel rounded shadow'
		}).addClassName(this.options.class);
		
		this.position(event);
		
		$('tooltip_container').show();
	},
	
	position: function(event) {
		var pos = Event.pointer(event);
		var wW	= this.getWindowWidth();
		var wH	= this.getWindowHeight();
		
		if (($('tooltip_container').getWidth() + pos.x) > wW) {
			pos.x -= ($('tooltip_container').getWidth());
		} else {
			pos.x += this.options.offsetX;
		}
		
		/*if (($('tooltip_container').getHeight() + (pos.y - document.documentElement.scrollTop)) > wH) {
			pos.y -= ($('tooltip_container').getHeight());
		} else {
			pos.y += this.options.offsetY;
		}*/
		
		$('tooltip_container').setStyle({
			'position':	'absolute',
			'left':		pos.x+'px',
			'top':		pos.y+'px',
			'zIndex':	this.options.zIndex
		});
	},
	
	getWindowWidth: function() {
		return (document.documentElement.clientWidth || document.body.clientWidth || 0);
	},
	
	getWindowHeight: function() {
		return (document.documentElement.clientHeight || document.body.clientHeight || 0);
	},
	
	hide: function(event) {
		$('tooltip_container').removeClassName(this.options.class);
		$('tooltip_container').hide();
	},
	
	destroy: function() {
		this.hide();
		
		Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
		Event.stopObserving(this.element, "mouseout", this.eventMouseOut);
		Event.stopObserving(this.element, "mousemove", this.eventMouseMove);
	},
	
	edit: function(newText) {
		this.text = newText;
	}
});

StickyTipsHandler = Class.create({
	initialize: function(conID) {
		this.conID 	= conID;
		this.list	= $H({});
	},
	
	add: function(id, tt) {
		this.list.set(id, tt);
	},
	
	exists: function(id) {
		if (typeof this.list.get(id) != undefined && this.list.get(id) != undefined) {
			return true;
		} else {
			return false;
		}
	},
	
	remove: function(id) {
		if (typeof id == "string") {
			var tt = this.list.get(id);
		} else {
			var tt = this.list.get(id.getAttribute("id"));
		}
		
		tt.remove();
		this.list.unset(id);
	},
	
	dump: function() {
		var me = this;
	
		this.list.each(function(tt) {
			tt[1].options.ttCollection.remove(tt[0]); //God damn hacks
		});
	},
	
	update: function(id, text) {
		this.list.get(id).options.contents = text;
		this.list.get(id).open();
	},
	
	open: function(id, event) {
		var tt 	= this.list.get(id);
		var pos = findPos(tt.element);
	}
});

GeneralStickyTips = new StickyTipsHandler('General');

StickyTip = Class.create({
	initialize: function(element, contents, handler) {
		this.element 	= $(element);
		this.contents 	= contents.elementize();
		
		if (handler !== undefined) {
			handler.add((this.element.hasAttribute('id') ? this.element.getAttribute('id') : randomString()));
		}
	}
});