﻿// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function () {
    log.history = log.history || [];   // store logs to an array for reference
    log.history.push(arguments);
    if (this.console) {
        console.log(Array.prototype.slice.call(arguments));
    }
};
//// catch all document.write() calls
//(function (doc) {
//    var write = doc.write;
//    doc.write = function (q) {
//        log('document.write(): ', arguments);
//        if (/docwriteregexwhitelist/.test(q)) write.apply(doc, arguments);
//    };
//})(document);



String.prototype.trimEnd = function (c) {
    if (c)
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function (c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}

String.prototype.escapeRegExp = function () {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};

String.prototype.format = function () {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};

var globalFunctions = {
    fixHeight: function (context, el, isHeight) {
        ///<sammary>Set Same height to  columns</sammary>
        var maxH = 0;
        $(context).find(el).each(function () {
            maxH = Math.max($(this).height(), maxH);
        });
        //$(context).find(el).height(maxH);
        if (isHeight)
            $(context).find(el).css("height", maxH);
        else
            $(context).find(el).css("min-height", maxH);
    },

    RequestQueryString: function (key, _default) {
        /// <summary>Get HTTP Query String Values</summary>
        /// <param name="key" type="string">Query String name</param>
        /// <param name="_default">Set Default value if no QueryString was found (default is null)</param>

        if (!_default) _default = null;
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return _default;
        else
            return qs[1];
    },
    setUrlKey: function (key, value) {
        /// <summary>Update QueryString params</summary>
        /// <param name="key" type="string">Query String name</param>
        /// <param name="value">Query String Value</param>
        /// <return>New QueryString</return>

        query = window.location.search;
        var q = query + "&";
        var re = new RegExp("[?|&]" + key + "=.*?&");
        if (!re.test(q))
            q += key + "=" + value;
        else
            q = q.replace(re, "&" + key + "=" + value + "&");
        q = q.trimStart("&").trimEnd("&");
        return q[0] == "?" ? q : q = "?" + q;
    },
    disableAnchorNavigation: function (container) {
        $(container).find("a[href='#']").click(function () {
            return false;
        });
    }
}
