﻿function toggleInlineHelp(helpContext, helpSubContext, controlId, hlpBtn) {
    //Close popup for this control if already open.
    if ($('#' + controlId).is(':visible')) {
        $('.info').hide();
    }
    else {
        //GA event tracking.
        csEvent('Inline Help', helpContext, helpSubContext);

        $('.info').hide();

        //Show the loading image
        var help_text = '<img src="/images/progress.gif" style="width:auto;height:auto;">';
        $('#' + controlId).html(help_text);
        $('#' + controlId).removeClass('info');
        $('#' + controlId).addClass('infoLoading');
        setPopupPosition(controlId, true, $('#' + controlId).hasClass('updatePanel'), hlpBtn);
        $('#' + controlId).show();

        var url = "/inlinehelp.ashx?helpcontext=" + helpContext + "&helpsubcontext=" + helpSubContext + "&lang=en"

        //Should we force a reload?
        if (location.href.indexOf("ReloadPage") > 0) {
            url += "&ReloadPage=true";
        }

        $.ajax({
            timeout: 60000,
            type: "GET",
            url: url,
            data: "",
            success: function (html) {
                //Populate the help html by replacing the helpContainer and removing the infoLoading style.
                $('#' + controlId).removeClass('infoLoading');
                $('#' + controlId).addClass('info');
                $('#' + controlId).html(html);
                setPopupPosition(controlId, false, $('#' + controlId).hasClass('updatePanel'), hlpBtn);
            }
        });
    }
}

function insertHelpControls() {
    $('img.helpControl.*').each(function (index) {
        var helpContext = $(this).attr('id').split(".");
        var isUpdatePanel = $(this).hasClass("updatePanel");
        insertHelpControl(this, 'helpCtl' + index, helpContext[0], helpContext[1], isUpdatePanel);
    });
}

function insertHelpControl(control, controlId, helpContext, helpSubContext, isUpdatePanel) {
    $(control)
        .attr('alt', '?')
        .attr('src', '/images/ico/question.png')
        .click(function () {
            toggleInlineHelp(helpContext, helpSubContext, controlId, this);
            return false;
        })
        .after("<div id=\"" + controlId + "\" class=\"info" + (isUpdatePanel ? " updatePanel" : "") + "\" style=\"display:none;\" onclick=\"$(this).hide();\"><span class=\"helpContainer\"></span></div>")
}

function setPopupPosition(controlId, isLoading, isUpdatePanel, hlpBtn) {
    var left = $(hlpBtn).position().left;
    var top = $(hlpBtn).position().top;

    if (!isLoading) {
        if ($('#' + controlId).offset().left > ($(window).width() / 2)) {
            var width = $('#' + controlId).css('width').replace("px", "");
            //Fix for IE.
            if (width == "auto") { width = $('#' + controlId).width(); }
            if (!isUpdatePanel) {
                left = left - width;
            }
            else {
                left = 0;
            }
        }
        $('#' + controlId).css("left", left + "px");
    }
    else {
        $('#' + controlId).css("left", left + "px");
    }
}

$(document).ready(function () {
    try{var prm = Sys.WebForms.PageRequestManager.getInstance();}catch(e){}
    if (prm) {
        prm.add_endRequest(function () {
            insertHelpControls();
        });
    }
    insertHelpControls();
});
