/* PolyTooltip, version 1.1.0.0
 * Copyright (c) 2008-2009 Mario Smeritschnig and Polymorph OG (http://www.polymorph.at)
 * 
 * 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.
 * 
 * For details, see the Polymorph web site: http://www.polymorph.at/  
 */



document.observe("dom:loaded", function() {
	poly_tooltip.init();
});

var poly_tooltip = {
	
	settings : {
		delay : 500,
		container : "<table cellpadding='0' cellspacing='0' border='0'><tr><td class='p_tooltip_nw'></td><td class='p_tooltip_n'></td><td class='p_tooltip_ne'></td></tr><tr><td class='p_tooltip_w'></td><td class='p_tooltip' id='p_tooltip_content'></td><td class='p_tooltip_e'></td></tr><tr><td class='p_tooltip_sw'></td><td class='p_tooltip_s'></td><td class='p_tooltip_se'></td></tr></table>",
		effect : Effect.Appear,
		effect_duration : 0.5,
		x_offset : 20,
		y_offset : 20,
		move_with_mouse : true,
		center : 'none' // 'none' || 'horizontal' || 'vertical'
	},

	init : function() {
		var t=this;
		t.container = new Element('div', { id: 'p_tooltip', style: 'display: none; position:absolute; z-index: 2000;' });
		t.container.innerHTML = t.settings.container;
		$(document.body).insert( { bottom: t.container } );
		t.active=false;
		t.hasEffectLibrary = (typeof Effect != 'undefined');
		
		$$(".poly_tooltip").each(function(el) {
			var html = el.title;
			el.title="";
			el.observe("mouseover", t.show.bindAsEventListener(t, el, html));
			el.observe("mouseout", t.hide.bindAsEventListener(t));
		});
	},
	
	setup : function(s) {
		var t=this;
		if(typeof(s.delay) != 'undefined') t.settings.delay = s.delay;
		if(typeof(s.container) != 'undefined') t.settings.container = s.container;
		if(typeof(s.effect) != 'undefined') t.settings.effect = s.effect;
		if(typeof(s.effect_duration) != 'undefined') t.settings.effect_duration = s.effect_duration;
		if(typeof(s.x_offset) != 'undefined') t.settings.x_offset = s.x_offset;
		if(typeof(s.y_offset) != 'undefined') t.settings.y_offset = s.y_offset;
		if(typeof(s.move_with_mouse) != 'undefined') t.settings.move_with_mouse = s.move_with_mouse;
		if(typeof(s.center) != 'undefined') t.settings.center = s.center;
	},
	
	show : function(e, element, html) {
		var t = this;

		if(html == "") return;
		if(t.sto) window.clearTimeout(t.sto);
		if(t.hto) window.clearTimeout(t.hto);
		t.active = true;

		t.element = $(element);
		
		t.registerEvents();

		$('p_tooltip_content').innerHTML = html;
		var dimensions = Element.getDimensions( t.container );
		t.w = dimensions.width;
		t.h = dimensions.height;
		t.vp =document.viewport.getDimensions();
		t.so = document.viewport.getScrollOffsets();


		t.mouseMove(e);

		t.sto = window.setTimeout(t.show_delayed.bindAsEventListener(t), t.settings.delay);
	},
	
	show_delayed : function() {
		var t=this;
		if(t.active) {
			if(t.hasEffectLibrary) {
				if(t.current_fx) t.current_fx.cancel();
				t.current_fx = new t.settings.effect(t.container,{duration: t.settings.effect_duration});
			}
			else t.container.show();
		}
	},
	
	hide : function() {
		var t = this;
		if(!t.active) return;
		t.active = false;
		t.removeEvents();
		t.hto = window.setTimeout(t.hide_delayed.bindAsEventListener(t), 10);
	},
	
	hide_delayed : function() {
		if(!this.active) this.container.hide();
	},
	
	registerEvents: function() {
		if(this.settings.move_with_mouse) Event.observe(this.element, "mousemove", this.mouseMove.bindAsEventListener(poly_tooltip));
	},	

	removeEvents: function() {
		if(this.settings.move_with_mouse) Event.stopObserving(this.element, "mousemove", this.mouseMove.bindAsEventListener(poly_tooltip));
	},
	
	mouseMove : function(e) {
		var t=this, mouse_x = Event.pointerX(e), mouse_y = Event.pointerY(e), x, y, dx, dy;
		
		if(t.settings.center == 'horizontal') {
			x = mouse_x + t.settings.x_offset - Math.round(t.w / 2);
			y = mouse_y + t.settings.y_offset;
		} else if(t.settings.center == 'vertical') {
			x = mouse_x + t.settings.x_offset;
			y = mouse_y + t.settings.y_offset - Math.round(t.h / 2);
		} else {
			x = mouse_x + t.settings.x_offset;
			y = mouse_y + t.settings.y_offset;
		}
		
		// keep tooltip inside viewport, if possible
		dx = t.vp.width + t.so.left - x - t.w;
		dy = t.vp.height + t.so.top - y - t.h;
		if(dx < 0) x += dx;
		if(dy < 0) y += dy;
		if(x < 0) x = 0;
		if(y < 0) y = 0;



		t.container.style.left = x + "px";
		t.container.style.top = y + "px";
	}

}
