
/**
 * Tooltip class
 * Shows tooltips on top of
 * some other elements
 */
function Tooltip() {

    this._clazz = null;
    this._tooltip = null;
    this._lang = 'de';

    /**
     * Constructor. Takes a styleSheet
     * class and adds hover effects
     *
     * @param clazz the class as string
     */
    this.construct = function(clazz, lang) {
        this._clazz = clazz;
        if(this._lang && this._lang=='de'){
            this._init();
        }
        
    }
    
    /**
     * Initializes the tooltips by
     * adding event listeners
     */
    this._init = function() {
        //fixed width for gallerytooltip
        if(this._clazz=='galleryTooltip'){
            //build the tooltip
            this._tooltip = $('<div id="tooltip_gallery"><div><h3 class="headline font12b DarkGray"></h3><span class="subline font11 DarkGray"></span></div></div>');
        
        }
        else if(this._clazz=='tvmodulTooltip'){
            //no tooltip for big video
            $('.bigVideo .tvmodulTooltip').removeClass("tvmodulTooltip");
            //build the tooltip
            this._tooltip = $('<div id="tooltip_tvmodul"><div><span class="type_img_small"></span><span class="White font11 Upper small vidDetails"></span><span class="headline font12b White title"></span></div></div>');
        }
        else{
            //build the tooltip
            this._tooltip = $('<div id="tooltip"><div><h3 class="headline font12b DarkGray"></h3><span class="subline font11 DarkGray"></span></div></div>');
        }
        $('body').append(this._tooltip);
        this._tooltip.hide();
        //event listeners
        $('.' + this._clazz).mouseenter(jQuery.proxy(this._showTooltip, this)); 
        $('.' + this._clazz).mouseleave(jQuery.proxy(this._removeTooltip, this)); 
    }
    
    /**
     * Displays the tooltip on top
     * of the given element
     *
     * @param event with target
     */
    this._showTooltip = function(ev) {
        //get event target
        var target = $(ev.target).parent();
        
        //fill the tooltip
        this._tooltip.show();
        if(this._clazz=='tvmodulTooltip'){
            this._tooltip.find('.vidDetails').text(target.attr('date')+' - '+target.attr('cat'));
            this._tooltip.find('.headline').text(target.attr('title'));
            this._tooltip.find('.type_img_small').addClass('detailsTypeSmall_'+target.attr('type'));
        }
        else{
            this._tooltip.find('.headline').text(target.attr('title'));
            this._tooltip.find('.subline').text(target.attr('rel'));
        }
        
        
        //remove title
        target.attr('title', "");
        //position it
        var pos = target.offset();
        this._tooltip.css({
            top  : (pos.top - this._tooltip.height()) + "px",
            left : (pos.left-5) + "px"
        });
    }
    
    this._removeTooltip = function(ev) {
        //get event target
        var target = $(ev.target).parent();
        //add title
        target.attr('title', this._tooltip.find('.headline').text());
        //hide the tooltip
        this._tooltip.hide();
        if(this._clazz=='tvmodulTooltip'){
            this._tooltip.find('.type_img_small').removeClass('detailsTypeSmall_'+target.attr('type'));
        }
    }
}
