﻿$(document).ready(function() {
    $("div#loading").data("count", 0);
});

function test() {

    $.webService("/ajax/card.asmx/Test", { param: "  lalala " }, function(res) {
        alert(res);
    }, null);

}

(function($) {

    $.extend({
            namespace: function(spaces) {
                var parent_space = window;
                var namespaces = spaces.split(".");

                for (var i = 0; i < namespaces.length; i++) {
                    if (typeof parent_space[namespaces[i]] == "undefined") {
                        parent_space[namespaces[i]] = new Object();
                    }

                    parent_space = parent_space[namespaces[i]];
                }

                return parent_space;
            }
        });

})(jQuery);

(function($) {


    $.webService = function(method, params, success, failure) {

        if (!params) params = { };

        if (!failure)
            failure = function(xhr, status, error) {

                $("div#loading").html("  AJAX error  ");
                $("div#loading").addClass("error");
                setTimeout(function() {
                    $("div#loading").removeClass("error");
                    $("div#loading").html("Loading...");
                    var count = $("div#loading").data("count");
                    count--;
                    if (count < 0) count = 0;
                    $("div#loading").data("count", count);
                    if (count == 0)
                        $("div#loading").hide();
                }, 2000);


                $.cards.popups.messageBox("Ajax error", xhr.responseText);
                //alert("AJAX error: " + xhr.statusText + "\n" + xhr.responseText);
            };
        var count = $("div#loading").data("count");
        count++;
        $("div#loading").data("count", count);
        $("div#loading").show();

        $.ajax({
                type: "POST",
                url: method,
                data: JSON.stringify(params),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(res) {
                    success.apply(this, arguments);
                    var count = $("div#loading").data("count");
                    count--;
                    if (count < 0) count = 0;
                    $("div#loading").data("count", count);
                    if (count == 0)
                        $("div#loading").hide();

                },
                error: failure,
                dataFilter: function(data) {
                    var response;

                    if (typeof(JSON) !== "undefined" && typeof(JSON.parse) === "function")
                        response = JSON.parse(data);
                    else
                        response = val("(" + data + ")");

                    if (response.hasOwnProperty("d"))
                        return JSON.stringify(response.d);
                    else
                        return response;
                }
            });

    };
})(jQuery);

(function($) {
    $.webService_sync = function(method, params, failure) {

        if (!params) params = { };

        if (!failure)
            failure = function(xhr, status, error) {
                alert("AJAX error: " + xhr.statusText);
            };
        var data = $.ajax({
                async: false,
                type: "POST",
                url: method,
                data: JSON.stringify(params),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: failure
            }).responseText;

        var response;

        if (typeof(JSON) !== "undefined" && typeof(JSON.parse) === "function")
            response = JSON.parse(data);
        else
            response = val("(" + data + ")");

        if (response.hasOwnProperty("d"))
            return response.d;
        else
            return response;

    };
})(jQuery);
