﻿$(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, success, 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);


$.extend($.namespace("$.clubnet"),
    {
        LoginVK: function (expire, mid, secret, sid, sig, success_func, error_func) {
        $.webService(
                "/ajax.asmx/LoginVK",
                { 
                    expire:expire,
                    mid:mid,
                    secret:secret,
                    sid:sid,
                    sig:sig
                },
                success_func,
                error_func);
            },
            LoginFB: function (access_token, base_domain, expires, secret, session_key, uid, sig, success_func, error_func) {
                $.webService(
                "/ajax.asmx/LoginFB",
                {
                    access_token: access_token,
                    base_domain: base_domain,
                    expires: expires,
                    secret: secret,
                    session_key: session_key,
                    uid:uid,
                    sig: sig
                },
                success_func,
                error_func);
            },
        AttachUserToVK: function (expire, mid, secret, sid, sig, success_func, error_func) {
            $.webService(
            "/ajax.asmx/AttachUserToVK",
            {
                expire: expire,
                mid: mid,
                secret: secret,
                sid: sid,
                sig: sig
            },
            success_func,
            error_func);
        }, 
        AttachUserToFB: function (access_token, base_domain, expires, secret, session_key, uid, sig, success_func, error_func) {
                $.webService(
                "/ajax.asmx/AttachUserToFB",
                {
                    access_token: access_token,
                    base_domain: base_domain,
                    expires: expires,
                    secret: secret,
                    session_key: session_key,
                    uid:uid,
                    sig: sig
                },
                success_func,
                error_func);
            },
    });


