﻿// -------------- tooltip helper class -------------- //
Ext.ux.TooltipHelper = Ext.extend(Object, {

    // internal fields
    
    
    // standard constructor
    constructor: function(hiddenTooltipIds) {   
        this.m_iHiddenTooltipIds = hiddenTooltipIds;  
           
        Ext.ux.ActionsMenuHelper.superclass.constructor.call(this);
    },
    
    // standard initializer
    initComponent: function() {
        Ext.ux.TooltipHelper.superclass.initComponent.apply(this, arguments);

        // extra args have to go here as base class initComponent doesn't get called for some reason
        var config = {};

        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
    },

    
    // initialize the tooltips with the specified array of tooltip items
    initialize: function(tooltips) {

        for (var i = 0; i < tooltips.length; i++) {
            $("#" + tooltips[i].linkId + "").bind("click", tooltips[i], delegate(this, this.handleButtonClick));
            $.preloadImages(tooltips[i].hoverImagePath, tooltips[i].inactiveImagePath);

            // bind hover images
            $.bindHoverImages(
                tooltips[i].imageId,
                tooltips[i].hoverImagePath,
                tooltips[i].inactiveImagePath,
                0,
                null, null, null, null, null, null);
        }
    },

    // hide a tooltip and don't show it again
    handleButtonClick: function(event) {
        var tooltipId = event.data.intData;

        // hide the tooltip
        $("#" + event.data.stringData).tooltip().hide();
        
        // cache the fact that the specified tooltip is hidden
        this.m_iHiddenTooltipIds |= tooltipId;
              
        // persist this info
        // create json data to pass back to server
        // fields must match the mappings in IdInfo class
        var tooltipInfo = { id: tooltipId };
        var tooltipInfoJson = $.toJSON(tooltipInfo);

        // post ajax request to server
        $.ajax({
            url: g_setTooltipHiddenAsyncUrl, // note: global url
            type: g_method_post,
            dataType: g_dataType_json,
            data: tooltipInfoJson,
            contentType: g_contentType_json,
            error: jQuery_ajaxRequestFailed
        });
    },

    // determine whether the tooltip is allowed to be shown
    allowShow: function(tooltipId) {
        var bAllowShow = true;

        // tooltip is hidden
        if ((tooltipId & this.m_iHiddenTooltipIds) == tooltipId) {
            bAllowShow = false;
        }

        return bAllowShow;
    },
    
    // convenience method for hooking up tooltips with some standard options
    hookUpTooltip: function(triggerHtmlElementId, tipHtmlElementId, positionY, positionX, offsetY, offsetX, tipId) {

        var me = this; // can't use 'this'

        $("#" + triggerHtmlElementId).tooltip({
            tip: "#" + tipHtmlElementId,
            effect: 'fade',
            position: [positionY, positionX],
            offset: [offsetY, offsetX],
            onBeforeShow: delegateWithArguments(me, me.allowShow, tipId)
        });
    }
});