﻿function queryVar(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function sendAjax(type, urlMethod, jsonData, successCallBack, errorCallBack) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: urlMethod,
        data: jsonData,
        dataType: type,
        success: successCallBack,
        error: function (xhr, status, error) {
            if (errorCallBack != null) {
                var err = eval("(" + xhr.responseText + ")");
                errorCallBack(err.Message);
            }
        }
    });
}

function sendAjaxText(urlMethod, jsonData, successCallBack, errorCallBack) {
    sendAjax("text", urlMethod, jsonData, successCallBack, errorCallBack);
}

function sendAjaxJSON(urlMethod, jsonData, successCallBack, errorCallBack) {
    sendAjax("json", urlMethod, jsonData, successCallBack, errorCallBack);
}

String.format = function () {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }

    return s;
}

function UpdateTableHeaders() {
    $("div.divTableWithFloatingHeader").each(function () {
        var originalHeaderRow = $(".tableFloatingHeaderOriginal", this);
        var floatingHeaderRow = $(".tableFloatingHeader", this);
        var offset = $(this).offset();
        var scrollTop = $(window).scrollTop();
        if ((scrollTop > offset.top) && (scrollTop < offset.top + $(this).height())) {
            floatingHeaderRow.css("visibility", "visible");
            floatingHeaderRow.css("top", Math.min(scrollTop - offset.top, $(this).height() - floatingHeaderRow.height()) + "px");

            // Copy cell widths from original header
            $("th", floatingHeaderRow).each(function (index) {
                var cellWidth = $("th", originalHeaderRow).eq(index).css('width');
                $(this).css('width', cellWidth);
            });

            // Copy row width from whole table
            floatingHeaderRow.css("width", $(this).css("width"));
        }
        else {
            floatingHeaderRow.css("visibility", "hidden");
            floatingHeaderRow.css("top", "0px");
        }
    });
}

(function () {
    this.stickyHeader = function (method) {
        $("table.tableWithFloatingHeader").each(function () {
            $(this).wrap("<div class=\"divTableWithFloatingHeader\" style=\"position:relative\"></div>");

            var originalHeaderRow = $("tr:first", this)
            originalHeaderRow.before(originalHeaderRow.clone());
            var clonedHeaderRow = $("tr:first", this)

            clonedHeaderRow.addClass("tableFloatingHeader");
            clonedHeaderRow.css("position", "absolute");
            clonedHeaderRow.css("top", "0px");
            clonedHeaderRow.css("left", $(this).css("margin-left"));
            clonedHeaderRow.css("visibility", "hidden");

            originalHeaderRow.addClass("tableFloatingHeaderOriginal");
        });
        UpdateTableHeaders();
        $(window).scroll(UpdateTableHeaders);
        $(window).resize(UpdateTableHeaders);
    }
})()
