﻿(function($) {
    $(document).ready(function(){    
	    $.fn.hint = function() {
	        return this.each(function(i) {
	            $.fn.hint.widgets.push(new Hint($(this), i));        
	        });
	    };
	
	    $.fn.hint.widgets = [];
	
	    // override-able method proving hint content;
	    $.fn.hint.getContent = function(el) {
            var title = $(el).data('title');
            if (! title) {
                title = el.attr('title');
                $(el).data('title', el.attr('title'));
                el.attr('title', ''); 
            };
	        return '<span>'+title+'</span>';
	    };
	
	    var Hint = function(el, i) {
	        this.indx = i;
	        this.element = el;
	        this.init();
	    };
	
	
	    Hint.prototype = {
	        init: function() {
	            this.setId('hint');
	            this.hide();
	        },
	
	        setId: function(prefix, i) {
	            return this.element.attr('id', prefix+'_'+this.indx);
	        },
	
	        hide: function() {
	            return this.element.css({'visibility': 'hidden', 'top': '-9999px'});
	        },
	
	        show: function(el, x, y) {
	            var content = $.fn.hint.getContent(el);
	            return this.element.find('.gutter')
	                .empty().append(content).end()
	                .css({'visibility': 'visible', 'top': y+15, 'left': x-15});
	        }
	    };
	
	
	    $("<div class='hint'><div class='gutter'>\
	        </div><div class='ft'></div>\
	    </div>").appendTo('body').hint();


        if ($.browser.msie && $.browser.version === '6.0') {
            DD_belatedPNG.fix('div.hint .gutter, div.hint .ft');
        };
	
	    
	    $('.help .trigger').live('mouseenter', function(e) {
	        var o = $.fn.hint.widgets[0];
	        var w = $(window);
	        o.show.call(o, $(e.target).parent(), parseInt(w.scrollLeft())+e.clientX, parseInt(w.scrollTop())+e.clientY); 
	    });
	
	    $('.help .trigger').live('mouseleave', function(e) {
	        var o = $.fn.hint.widgets[0];
	        o.hide.call(o); 
	    });

        $('.help .trigger').live('click', function(e) {
            var o = $.fn.hint.widgets[0];
	        var w = $(window);
	        o.show.call(o, $(e.target).parent(), parseInt(w.scrollLeft())+e.clientX, parseInt(w.scrollTop())+e.clientY); 
	        return false; 
	    });
    });
})(jQuery);
