﻿var AmigoReader;
if (!AmigoReader) {

    AmigoReader = {}

    AmigoReader.CheckBoxSelector = function(containerId, inputFieldId, className) {
        if (containerId && $("#" + containerId)[0]) {
            className = (className) ? "." + className : "";
            this.CbList = $("#" + containerId).find("input" + className + "[type=checkbox]");
        }
        if (inputFieldId && $("#" + inputFieldId)[0]) this.InputField = $("#" + inputFieldId)[0];

        this.InitializeCheckBoxes();
    }

    AmigoReader.CheckBoxSelector.prototype.InitializeCheckBoxes = function() {
        if (this.CbList) {
            var selectionStr = (this.InputField ? "," + this.InputField.value + "," : "");
            var me = this;
            this.CbList.each(function(index) {
                this.onclick = function() { me.OnSelectionUpdated() };
                this.checked = (selectionStr.indexOf("," + this.value + ",") != -1);
            });
        }
    }

    AmigoReader.CheckBoxSelector.prototype.SelectOne = function(cBox, isChecked) {
        cBox.checked = isChecked;
        this.OnSelectionUpdated();
    }

    AmigoReader.CheckBoxSelector.prototype.SetValue = function(value) {
        if (this.InputField)
            this.InputField.value = value;
    }

    AmigoReader.CheckBoxSelector.prototype.SelectAll = function() {
        this.CheckItems(function(item) { return true });
    }

    AmigoReader.CheckBoxSelector.prototype.SelectNone = function() {
        this.CheckItems(function(item) { return false });
    }

    AmigoReader.CheckBoxSelector.prototype.SelectWithClass = function(className) {
        this.CheckItems(function(item) { return (" " + item.className + " ").indexOf(" " + className + " ") != -1 });
    }

    AmigoReader.CheckBoxSelector.prototype.OnSelectionUpdated = function() {
        if (this.InputField && this.CbList) {
            var resultStr = "";
            this.CbList.each(function(index) {
                if (this.checked) {
                    if (resultStr) resultStr = resultStr + ",";
                    resultStr = resultStr + this.value + "";
                }
            });
            this.InputField.value = resultStr;
        }
    }

    AmigoReader.CheckBoxSelector.prototype.CheckItems = function(func) {
        if (this.CbList) {
            this.CbList.each(function() {
                if (func(this)) {
                    this.checked = true;
                } else
                    this.checked = false;
            });
            this.OnSelectionUpdated();
        }
    }


    AmigoReader.AjaxWidjet = function(options) {
        this.Options = {}
        if (options.ContainerId && $("#" + options.ContainerId)[0]) {
            this.Options.ContainerId = options.ContainerId;
            this.Container = $("#" + options.ContainerId)[0];
            AmigoReader.AjaxWidjet.prototype.AddToInstancesMap(this);
        }
        if (options.Url) this.Options.Url = options.Url;
        if (options.Parameters) this.Options.Parameters = options.Parameters;
        if (options.SuccessCallback) {
            this.SuccessCallback = options.SuccessCallback;
        } else {
            this.SuccessCallback = this.SuccessCallbackDefault;
        }

    }

    AmigoReader.AjaxWidjet.prototype.CreateInstance = function(options, registerOnLoad) {
        if (registerOnLoad == true) {
            $(window).load(function() { new AmigoReader.AjaxWidjet(options); });
        } else {
            new AmigoReader.AjaxWidjet(options);
        }
    }


    AmigoReader.AjaxWidjet.prototype.AddToInstancesMap = function(instance) {
        if (!AmigoReader.AjaxWidjet.InstanceMap) AmigoReader.AjaxWidjet.InstanceMap = {};
        AmigoReader.AjaxWidjet.InstanceMap[instance.Options.ContainerId] = instance;
    }

    AmigoReader.AjaxWidjet.prototype.GetInstance = function(containerId) {
        return AmigoReader.AjaxWidjet.InstanceMap ? AmigoReader.AjaxWidjet.InstanceMap[containerId] : null;
    }

    AmigoReader.AjaxWidjet.prototype.GetParentInstance = function(element) {
        var tElement = element;
        while (tElement && !AmigoReader.AjaxWidjet.prototype.GetInstance(tElement.id)) {
            tElement = tElement.parentNode;
        }
        return tElement ? AmigoReader.AjaxWidjet.prototype.GetInstance(tElement.id) : null;
    }

    AmigoReader.AjaxWidjet.prototype.SuccessCallbackDefault = function(data, textStatus, xmlHttpRequest) {

        if ($.trim(data) == "") return;
        $(this.Container).html(data);
    }

    AmigoReader.AjaxWidjet.prototype.Submit = function(params) {
        var parameters = this.MergeParameters(this.Options.Parameters, params);
        var me = this;
        if (me.OnSubmitStarted) me.OnSubmitStarted(me);
        $.ajax({ type: 'POST', url: this.Options.Url, data: parameters, success: function(data, textStatus, httpObj) {
            if (me.SuccessCallback) me.SuccessCallback(data, textStatus, httpObj);
            if (me.OnSubmitFinished) me.OnSubmitFinished(me);
        },
         error: function() {if (me.OnSubmitFinished) me.OnSubmitFinished(me); } 
        });
    }

    AmigoReader.AjaxWidjet.prototype.MergeParameters = function(params1, params2) {
        if (!params1) return params2;
        if (!params2) return params1;
        var mergedParams = {};
        for (var name in params1) {
            if (!(params1[name] instanceof Function)) mergedParams[name] = params1[name];
        }
        for (var name in params2) {
            if (!(params2[name] instanceof Function)) mergedParams[name] = params2[name];
        }
        return mergedParams;
    }
}

SetPersistWidjetParameters = function(element, params, widjetId) {
    var elemWidget = GetAjaxWidjet(element, widjetId);
    if (elemWidget) elemWidget.Options.Parameters = elemWidget.MergeParameters(elemWidget.Options.Parameters, params);
}

RemovePersistWidjetParameter = function(element, param, widjetId) {
    var elemWidget = GetAjaxWidjet(element, widjetId);
    if (elemWidget && elemWidget.Options.Parameters[param]) delete elemWidget.Options.Parameters[param];
}

SubmitAjaxWidjet = function(element, params, widjetId) {
    var elemWidget = GetAjaxWidjet(element, widjetId);
    if (elemWidget) elemWidget.Submit(params);
}

GetAjaxWidjet = function(element, widjetId) {
    if (widjetId) {
        return AmigoReader.AjaxWidjet.prototype.GetInstance(widjetId);
    } else {
     return AmigoReader.AjaxWidjet.prototype.GetParentInstance(element);
    }
}

