﻿String.prototype.format = function (params) {
    var formatted = this;
    for (arg in params) {
        formatted = formatted.replace("{" + arg + "}", params[arg]);
    }
    return formatted;
};

Function.prototype.partial = function () {
    var fn = this, args = Array.prototype.slice.call(arguments);
    return function () {
        var arg = 0;
        for (var i = 0; i < args.length && arg < arguments.length; i++) {
            if (args[i] === undefined)
                args[i] = arguments[arg++];
        }

        if (arg < arguments.length)
            args = args.concat(Array.prototype.slice.call(arguments, arg));

        return fn.apply(this, args);
    };
};

/* jQuery Extension to serialize a form to the correct format to post it to the server */
if (jQuery && typeof jQuery.fn.serializeObject === 'undefined') {
    jQuery.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (typeof (o[this.name]) === 'undefined' && this.name !== 'format') {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
}

if (typeof (TextKing) == 'undefined') {
    var TextKing = (function ($) {
        var module = {};

        module.EMPTY_GUID = '00000000-0000-0000-0000-000000000000';

        module.IsEmptyGuid = function (val) {
            return (typeof val === 'string' && (val === module.EMPTY_GUID || $.trim(val) === ''));
        };

        module.testFunc = function () {
            alert("This is TextKingCore js!");
        };

        module.centerButton = function (buttonId, constrainingParentId) {
            var buttonJQ = $('#' + buttonId);
            var constrainingParentJQ = null;

            if (constrainingParentId == null) {
                constrainingParentJQ = buttonJQ.parents('div').first();
            }

            var parentW = constrainingParentJQ.width();
            var buttonW = buttonJQ.width();
            var buttonLeft = parentW / 2 - buttonW / 2;
            buttonJQ.css('margin-left', buttonLeft);
        };

        module.initTipTip = function () {
            $(function () {
                if ($("a").tipTip) {
                    // $("a").tipTip({ delay: 300, maxWidth: "400px", activation: "hover" });
                    //  $("div.task-row").tipTip({ delay: 300, maxWidth: "400px", activation: "hover",defaultPosition:"left" });

                    //$(".onclicktooltip").tipTip({ delay: 200, maxWidth: "400px" });
                }

            });
        };

        module.hideAllQTips = function () {
            $('.qtip').each(function () {
                $(this).qtip('hide')
            });
        };

        module.initqTip = function () {
            var qtipClasses = 'ui-tooltip-dark ui-tooltip-shadow ui-tooltip-rounded';

            $(".onclicktooltip").qtip({
                content: {
                    attr: 'title'
                },
                show: {
                    event: 'click'
                },
                hide: {
                    event: 'click'
                },
                position: {
                    my: 'right center',
                    at: 'left center'
                },
                style: {
                    classes: qtipClasses
                }
            });

            $("a[title], input[title], span[title]").qtip({
                position: {
                    my: 'top center',
                    at: 'bottom center'
                },
                style: {
                    classes: qtipClasses
                }
            });

            $('div.q-tip-anchor').each(function () {
                $currentLink = $(this);

                $currentLink.qtip({
                    content: {
                        text: 'Loading...', // The text to use whilst the AJAX request is loading
                        ajax: {
                            url: $currentLink.attr('data-url'), // URL to the local file
                            type: 'GET', // POST or GET
                            data: {} // Data to pass along with your request
                        }
                    },
                    show: {
                        event: 'click'
                    },
                    hide: {
                        event: 'click'
                    },
                    position: {
                        my: 'right center',
                        at: 'left center'
                    },
                    style: {
                        classes: qtipClasses
                    }
                });
            });
        };

        module.log = function () {
            if (console && console.log) {
                console.log(arguments.length > 1 ? Array.prototype.slice.call(arguments) : arguments[0]);
            }
        }

        return module;
    })(jQuery);
}
