// MIT License
// ===========
// Copyright (c) 2006 Edward J. Cianci (http://boygeni.us/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var Tooltip = {
	Version: '1.1.0',
	
	EffectOptions: {},

	prepare: function(links, options) {
		$A(links).each(function(a){
			var title = a.title, alt = a.alt, href = a.href;
			if(!title || !href || !a.id) return;

			Tooltip.EffectOptions = options;

			Tooltip.render(a, title, alt);
			a.removeAttribute('title');
			a.removeAttribute('alt');
			['mouseover', 'mouseout'].each(function(e){
				Event.observe(a, e, Tooltip['on' + e].bindAsEventListener(this));
			});
		});
	},

	render: function(element, head, body) {
		var element = $(element);

		var ttid = 'tt' + element.id;
		element._ttid = ttid;

		var container = Builder.node('div', { id: ttid, className: 'tt' }, [
			Builder.node('div', [
				Builder.node('h3', head),
				Builder.node('p', body)
			])
		]);
		var objBody = document.getElementsByTagName("body").item(0);
		objBody.appendChild(container);
		//element.parentNode.insertBefore(container, element.nextSibling);
		Position.absolutize(container);
		container.hide();
		
		this.eventBind = this.mouseBinder.bindAsEventListener(this, container);
		document.observe('mousemove', this.eventBind);
		
	},
	
	mouseBinder: function(ev, element) {
		element.style.top = (Event.pointerY(ev) - 10) + 'px';
		element.style.left = (Event.pointerX(ev) + 15) + 'px';
	},
	
	onmouseover: function(ev) {
		ev._over = true;
		Tooltip.display(ev);
	},

	onmouseout: function(ev) {
		ev._over = false;
		Tooltip.display(ev);
	},

	reset: function(element) {
		var effect = $(element)._effect;
		if(effect) effect.cancel();
	},

	display: function(ev) {
		var over = ev._over;
	
		var element = Event.element(ev);
		if(element.nodeType == 3) element = element.parentNode; //fix safari bug
		element = $(element._ttid);

		Tooltip.reset(element);

		var options = $H({
			duration: over ? 0.66 : 0.33,
			offset: 8
		}).merge(Tooltip.EffectOptions);

		//if(over) Tooltip.position(element, ev);
		
		element._effect = new Effect[over ? 'Appear' : 'Fade'](element, {duration: .5});
	},
	

	
	position: function(element, ev) {
		//var offset = options.offset;

		//var dimensions = element.getDimensions(),
		//	boundaries = Tooltip.boundaries();
		
		var x = Event.pointerX(ev) //Tooltip.determine(Event.pointerX(ev), dimensions.width, offset, boundaries.width),
			y = Event.pointerY(ev) //Tooltip.determine(Event.pointerY(ev), dimensions.height, offset, boundaries.height);

		element.setStyle({
			top: y - 10 + 'px',
			left: x + 15 + 'px'
		});

	},
	
	determine: function(i, current, min, max) {
		if(i + current + min >= max) {
			i = i - current - min;
		}
		else {
			i += min;
		}

		return (i < min) ? min : i;
	},
	
	boundary: function(hw) {
		var bound;

		if(self['inner' + hw]) {
			bound = self['inner' + hw];
		}
		else {
			bound = (document.documentElement || document.body)['client' + hw];
		}

		return bound;
	},
	
	boundaries: function() {
		return {
			height: Tooltip.boundary('Height'),
			width: Tooltip.boundary('Width')
		}
	}
}
