

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var logTooltip = Class.create({
    initialize: function(options){
        //logHistory.instances.push(this);
	//if (history.navigationMode) history.navigationMode = 'compatible';

        this.events = [];
        this.events_tooltips = [];
        this.tooltips = [];
        this.effects = [];
        this.executor = [];

        this.delay = 0.6;			// IN SECS
        this.eff_speed = 0.4;	// IN SECS
        this.tolerance = 15;	// IN PX
        //this.container = $(container);
        //this.container.update('');
        this.options = {};

        Object.extend(this.options, options || {});
	
        if (!this.options.delay) this.options.delay = this.delay;
        if (!this.options.tolerance) this.options.tolerance = this.tolerance;
    },
// ASSIGN DELEGATE TIMER TO HIDE THE TOOLTIP
    start: function(el_tooltip) {
	//alert('Start Timer - ' + el_tooltip + ' Delay ' + this.options.delay);
	this.stop(el_tooltip);
	this.executor[el_tooltip] = new PeriodicalExecuter(function() {
            this.delegate(el_tooltip); //start slidehow
        }.bind(this), this.options.delay);
    },
// STOP DELEGATE TIMER
    stop: function(el_tooltip) {
	//alert('Stop Timer - ' + el_tooltip);
	try {this.executor[el_tooltip].stop();} catch(e) {}
    },

// TOOLTIP IS NOW MADE INVISIBLE
    delegate: function(el_tooltip) {
	// HIDE ELEMENT
	this.stop(el_tooltip);

	try { this.effects[el_tooltip].cancel(); } catch (e) {}
	try { this.effects[el_tooltip] = new Effect.Fade(this.tooltips[el_tooltip], {duration: this.eff_speed});} catch (e) {}

	this.tooltips[el_tooltip].onmouseover = Prototype.emptyFunction;
	this.tooltips[el_tooltip].onmouseout = Prototype.emptyFunction;

	this.active_event = ""; this.active_tooltip = "";
    },

// BIND EVENTS AND TOOLTIPS
    add: function (el_event, el_tooltip) {
	this.events[el_event] = $(el_event);
	this.tooltips[el_tooltip] = $(el_tooltip);
	this.events_tooltips[el_event] = el_tooltip;

	this.events[el_event].onmouseover = this.mouseOver.bind(this, el_event);
	this.events[el_event].onmouseout = this.mouseOut.bind(this, el_event);
		
    },

    mouseOver: function (el_event) {
	// CHECK IF ELEMENTS ARE NOT EQUAL
    // STOP HIDING TIMER
		this.stop(this.events_tooltips[el_event]);
    // IF
		if (this.active_event != el_event) {
		// STOP TIMER IF ACTIVE
			this.stop(this.events_tooltips[this.active_event]);

		// TOOLTIP NAME - OLD & NEW
			var tt_old = this.events_tooltips[this.active_event];
			var tt_new = this.events_tooltips[el_event];

		// DISABLE OLD TOOLTIPS
			if (tt_old != tt_new) {

				if (tt_old) {
				// HIDE OLD TOOLTIP - EFFECT FADE
					try { this.effects[tt_old].cancel(); } catch (e) {}
					try { this.effects[tt_old] = new Effect.Fade(this.tooltips[tt_old], {duration: this.eff_speed});} catch (e) {}

					this.tooltips[tt_old].onmouseover = Prototype.emptyFunction;
					this.tooltips[tt_old].onmouseout = Prototype.emptyFunction;
				}
			
			} else {
			// MOVE NEW TOOLTIP - NO ANIMATION
				this.setPosition(el_event, tt_new);
			// SAME TOOLTIP ANIMATE MOVEMENT
				this.tooltips[tt_new].show();
			}

		// MOVE NEW TOOLTIP - NO ANIMATION
			this.setPosition(el_event, tt_new);

		// SHOW NEW TOOLTIP - EFFECT APPEAR
			try { this.effects[tt_new].cancel(); } catch (e) {}
			try { this.effects[tt_new] = new Effect.Appear(this.tooltips[tt_new], {duration: this.eff_speed});} catch (e) {}

		// SET ACTIVE EVENT AND TOOLTIP ELEMENT
			this.active_event = el_event;
			this.active_tooltip = tt_new;

		// BIND EVENTS
			this.tooltips[tt_new].onmouseover = this.mouseOverTooltip.bind(this, tt_new);
			this.tooltips[tt_new].onmouseout = this.mouseOutTooltip.bind(this, tt_new);
		} else {
			this.tooltips[this.events_tooltips[el_event]].show();
			this.active_event = el_event;
			this.active_tooltip = this.events_tooltips[el_event];
		}
    },

    mouseOut: function (el_event) {
	// START CHECKING POSITION ALLOW SOME TIME OUT
	this.start(this.events_tooltips[el_event]);
    },

    mouseOverTooltip: function (el_tooltip) {
	this.stop(el_tooltip);
	this.tooltips[el_tooltip].show();
    },

    mouseOutTooltip: function (el_tooltip) {
	this.start(el_tooltip);
    },

    setPosition: function(el_event, el_tooltip) {
	var o = this.events[el_event];
	oTop = o.offsetTop;
	while (o.offsetParent != null) {
	    oParent = o.offsetParent;
	    oTop += oParent.offsetTop;
	    o = oParent;
	}

	o = this.events[el_event];
	oLeft = o.offsetLeft;
	while (o.offsetParent != null) {
	    oParent = o.offsetParent;
	    oLeft += oParent.offsetLeft;
	    o = oParent;
	}

	this.tooltips[el_tooltip].absolutize();
	this.tooltips[el_tooltip].setStyle({
	    top: oTop + 'px',
	    left: (oLeft - this.tooltips[el_tooltip].getWidth()) + 'px'
	});

    }
});

