﻿/// <reference path="jquery-min.js" />
/// <reference path="jquery-ui-custom-min.js" />
/// <reference path="jquery-vsdoc.js" />

// Retuns the entire element as html string. 
jQuery.fn.outerHtml = function () {
    return $('<div>').append(this.eq(0).clone()).html();
};


jQuery.extend(
{
    // display error to client
    // Optional selector of item to highlight in red. if === true will append the 
    displayError: function (message, options) {
        var settings = {};
        var defaults = {};

        //set the config
        if (options != null)
            $.extend(settings, defaults, options);

        $('ui-state-error-text-box').removeClass('ui-state-error-text-box');

        if (settings.addEnd) {
            message = message + $.genericErrorMessageEnd();
        }
        if (settings.selector) {
            $(selector).addClass('ui-state-error-text-box');
        }

        $('body').append('<div class="info-message ui-state-error ui-corner-all">' + message + '</div>');

        $('.info-message').click(function () { $(this).hide(); });
    },

    // disply info to client.
    displayInfo: function (message) {
        $('body').append('<div class="info-message ui-state-highlight ui-corner-all">' + message + '</div>');
        $('.info-message').click(function () { $(this).hide(); });
    },

    genericErrorMessageEnd: function () {
        return ' Please reload your page and try again. If the issue persists please contact your administrator.';
    },

    soap: function (options) {
        var settings = {};
        var defaults = {
            url: siteConfiguration.defaultWebServiceUrl,
            success: function (data) { $.displayInfo('Success!'); },
            error: function (data) { $.displayError($.parseJSON(data.responseText).Message, { addEnd: true }); },
            dataType: 'json',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            async: true
        };

        //set the config
        if (options != null)
            $.extend(settings, defaults, options);

        if (settings.data && settings.url && settings.method) {
            // call ajax.
            $.progress();
            $.ajax({
                async: settings.async,
                type: settings.type,
                contentType: settings.contentType,
                dataType: settings.dataType,
                url: settings.url + settings.method,
                data: settings.data,
                success: function (data) {
                    $.progress('destroy');
                    return settings.success(data);
                },
                error: function (data) {
                    $.progress('destroy');
                    return settings.error(data);
                }
            });
        } else {
            // display error message.
            $.displayError('Asyncrhonous call could not be completed the data or url or method are missing.', { addEnd: true });
        }
    },
    progress: function (command) {
        if (!window.progressCount)
            window.progressCount = 0;
        if (!command) {
            if (window.progressCount == 0) {
                window.progressCount = window.progressCount + 1;
                // show the popup
                $('<div />').addClass('modalBackground').appendTo('body').show();
                $('<div />').addClass('modal ui-progressbar-value').appendTo('body').progressbar({
                    value: 100
                }).fadeIn();
                document.body.style.cursor = 'wait';
            }
            else {
                window.progressCount = window.progressCount + 1;
            }
        }
        else if (command == 'destroy') {
            if (window.progressCount == 1) {
                window.progressCount = window.progressCount - 1;
                $('.modal').fadeOut('normal', function () {
                    $('.modal').remove();
                    $('.modalBackground').remove();
                });
                document.body.style.cursor = 'auto';
            }
            else {
                window.progressCount = window.progressCount - 1;
            }
        }
    }
}
);