//MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2008 Valerio Proietti, <http://mad4milk.net>, MIT Style License.

/*
Script: Tips.js
	Class for creating nice tips that follow the mouse cursor when hovering an element.

License:
	MIT-style license.
*/

var Tips = new Class({
    options: {
        onShow: function(tip){
            tip.setStyle('visibility', 'visible');
        },
        onHide: function(tip){
            tip.setStyle('visibility', 'hidden');
        },
        maxTitleChars: 30,
        showDelay: 100,
        hideDelay: 100,
        className: 'tool',
        offsets: {
            'x': 16,
            'y': 16
        },
        fixed: false,
		titleOverride: false,
		useTitles: false
    },
    initialize: function(elements, options){
        this.setOptions(options);
        if($$("." + this.options.className + '-tip').length ==0){
	        this.toolTip = new Element('div', {
	            'class': this.options.className + '-tip',
	            'styles': {
	                'position': 'absolute',
	                'top': '0',
	                'left': '0',
	                'visibility': 'hidden'
	            }
	        }).inject(document.body);
	        this.wrapper = new Element('div').inject(this.toolTip);
	        $$(elements).each(this.build, this);
	        	if (this.options.initialize){ 
	            this.options.initialize.call(this);}
        } else {
        	this.toolTip = $$("." + this.options.className + '-tip')[0];
        }
    },
    build: function(el){
        if(el.$tmp){
            el.$tmp.myTitle = (el.href && el.getTag() == 'a') ? el.href.replace('http://', '') : (el.rel || false);
            if (el.title) {
                var dual = el.title.split('::');
                if (dual.length > 1) {
                    el.$tmp.myTitle = dual[0].trim();
                    el.$tmp.myText = dual[1].trim();
                }
                else {
                    el.$tmp.myText = el.title;
                }
                el.removeAttribute('title');
            }
            else {
                el.$tmp.myText = false;
            }
            if (el.$tmp.myTitle && el.$tmp.myTitle.length > this.options.maxTitleChars) {
                el.$tmp.myTitle = el.$tmp.myTitle.substr(0, this.options.maxTitleChars - 1) + "&hellip;";
            }
        }
        el.addEvent('mouseenter', function(event){
            this.start(el);
            if (!this.options.fixed) 
                this.locate(event);
            else 
                this.position(el);
        }
.bind(this));
        if (!this.options.fixed) 
            el.addEvent('mousemove', this.locate.bindWithEvent(this));
        var end = this.end.bind(this);
        el.addEvent('mouseleave', end);
        el.addEvent('trash', end);
    },
    start: function(el){
        this.wrapper.empty();
        if(this.options.titleOverride){
	        if (el.$tmp.myText) {
	            this.text = new Element('span').inject(new Element('div', {
	                'class': this.options.className + '-text'
	            }).inject(this.wrapper)).adopt($(el.$tmp.myText).clone().setProperty('class',''));
	        }					
		}
		else{
			if(this.options.useTitles){
				if (el.$tmp.myTitle) {
		            this.title = new Element('span').inject(new Element('div', {
		                'class': this.options.className + '-title'
		            }).inject(this.wrapper)).setHTML(el.$tmp.myTitle);
		        }
			}
	        if (el.$tmp.myText) {
	            this.text = new Element('span').inject(new Element('div', {
	                'class': this.options.className + '-text'
	            }).inject(this.wrapper)).setHTML(el.$tmp.myText);
	        }				
		}

        $clear(this.timer);
        this.timer = this.show.delay(this.options.showDelay, this);
    },
    end: function(event){
        $clear(this.timer);
        this.timer = this.hide.delay(this.options.hideDelay, this);
    },
    position: function(element){
        var pos = element.getPosition();
        this.toolTip.setStyles({
            'left': pos.x + this.options.offsets.x,
            'top': pos.y + this.options.offsets.y
        });
    },
    locate: function(event){
        var win = {
            'x': window.getWidth(),
            'y': window.getHeight()
        };
        var scroll = {
            'x': window.getScrollLeft(),
            'y': window.getScrollTop()
        };
        var tip = {
            'x': this.toolTip.offsetWidth,
            'y': this.toolTip.offsetHeight
        };
        var prop = {
            'x': 'left',
            'y': 'top'
        };
        for (var z in prop) {
            var pos = event.page[z] + this.options.offsets[z];
            if ((pos + tip[z] - scroll[z]) > win[z]) 
                pos = event.page[z] - this.options.offsets[z] - tip[z];
            this.toolTip.setStyle(prop[z], pos);
        };
            },
    show: function(){
        if (this.options.timeout) 
            this.timer = this.hide.delay(this.options.timeout, this);
        this.fireEvent('onShow', [this.toolTip]);
    },
    hide: function(){
        this.fireEvent('onHide', [this.toolTip]);
    }
});
Tips.implement(new Events, new Options);
