﻿/*
The HTML tools offer a set of User Interface related utility methods used in different part of the application.

Tooltip binding: For all images with the class helpMessage, alertMessage or infoMessage the binding with the 
respective tooltip is managed in the 'UpdateTooltipBinding' method. The content displayed in the tooltip is 
read from the image's title attribute.

Example: <img class='helpMessage' src='....' alt='help' title='Content displayed in the tooltip.'/>

After the function 'UpdateTooltipBinding' is called, a tooltip with the 'help info' look and feel will
be displayed when the user hoovers over the image. The different tooltip templates (info, alert and help) 
can be found in the file HtmlTools.spark. By changing the template for a specific type of tooltip you change the 
look and feel for all tooltips of this type.

In case of partial updates, call the function 'UpdateTooltipBinding' on completion of the document rendering. This
way, tooltip binding will be executed for images added as result of the partial update.

*/
String.prototype.startsWith = function(str) { return (this.indexOf(str) === 0); }

berryAlloc.HtmlTools = function(element) {
};

berryAlloc.HtmlTools.prototype = {

    UpdateTooltipBinding: function() {

        var startProfile = DelawareCommons.startProfile();

        this.BindTooltipEvents(".helpMessage", "#helpTemplate");
        this.BindTooltipEvents(".alertMessage", "#alertTemplate");
        this.BindTooltipEvents(".infoMessage", "#infoTemplate");

        DelawareCommons.endProfile(startProfile, "UpdateTooltipBinding");
    },

    BindTooltipEvents: function(iconIdentifier, templateIdentifier) {

        var icons = j(iconIdentifier);
        if (icons.length == 0) {
            return;
        }

        var startProfile = DelawareCommons.startProfile();

        icons.tooltip({

            tip: "#tooltipContainer",
            predelay: 250,

            onBeforeShow: function() {
                var triggerElement = this.getTrigger();
                this.getTip().html(j(templateIdentifier).html().replace('#content#', triggerElement.data('title')));
            }
        });

        DelawareCommons.endProfile(startProfile, "BindTooltipEvents(" + iconIdentifier + ") x" + icons.length);

    }
};
