﻿//***** AJAX OBJECT *****//
var AjaxObj = function() {
    this.httpRequest = null;
    this.service = null;
    this.method = null;
    this.data = null;
    this.success = null;
    this.error = null;
    this.timeout = 20; // Default 20 second timeout.
    this.parameters = null;
}

AjaxObj.prototype.POST = function() {
    this.execute("POST");
}

AjaxObj.prototype.GET = function() {
    this.execute("GET");
}

AjaxObj.prototype.execute = function(type) {
    // Stop current request if it is executing.
    if (this.httpRequest != null) {
        try {
            this.httpRequest.abort();
        }
        catch (exception) {
        }
    }

    // Convert parameters to json.
    if (this.parameters != null)
        this.data = JSON2.stringify(this.parameters);

    this.httpRequest = jq.ajax({
        type: type,
        timeout: 1000 * this.timeout,
        url: "/sitecore modules/Web/sitecore search solution/layouts/ServiceProxy.aspx?s=" + encodeURIComponent(this.service) + "&m=" + encodeURIComponent(this.method),
        data: this.data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: this.onSuccess.bind(this),
        error: this.onError.bind(this)
    });
}

AjaxObj.prototype.onSuccess = function(data) {
    this.httpRequest = null;
    if (this.success != null) {
        if (data == null)
            this.success(null);
        else
            this.success(AjaxObj.expand(data));
    }
}

AjaxObj.prototype.onError = function(data) {
    this.httpRequest = null;
    if (this.error != null) {
        var exception = eval('(' + data.responseText + ')');
        this.error(exception.m);
    }
}

//***** STATIC AJAX FUNCTIONS *****//

// General expand function that takes the object to
// expand. A check is made on the object type and if
// an expander is present for the given type. If not, 
// the original object is returned.
AjaxObj.expand = function(obj) {
    if (obj == null) return null;

    // Recursively expand array elements.
    if (CommonHelper.isArray(obj)) {
        var arr = new Array();
        for (var i = 0; i < obj.length; i++)
            arr.push(AjaxObj.expand(obj[i]));
        return arr;
    }

    var type = obj["__type"];
    if (type == undefined) return obj;
    var expander = Expanders[type];
    if (expander == undefined) return obj;
    return expander(obj);
}

// General shrink function that takes the object to
// shrink and the type of the object. A check is made
// if a shrinker is present for the given type. If
// not, the original object is returned.
AjaxObj.shrink = function(obj, type) {
    if (obj == null) return null;

    // Recursively shrink array elements.
    if (CommonHelper.isArray(obj)) {
        var arr = new Array();
        for (var i = 0; i < obj.length; i++)
            arr.push(AjaxObj.shrink(obj[i], type));
        return arr;
    }

    var shrinker = Shrinkers[type];
    if (shrinker == undefined) return obj;
    return shrinker(obj);
}

// Enables binding objects to callbacks. 
if (!Function.prototype.bind) {
    Function.prototype.bind = function() {
        if (arguments.length < 2 && arguments[0] === undefined)
            return this;
        var toArray = function(iterable) {
            if (!iterable) return [];
            if (iterable.toArray) return iterable.toArray();
            var length = iterable.length
            var results = new Array(length);
            while (length--)
                results[length] = iterable[length];
            return results;
        }
        var __method = this;
        var args = toArray(arguments);
        var object = args.shift();
        return function() {
            return __method.apply(object, args.concat(toArray(arguments)));
        }
    }
} 

