﻿

(function ($) {
    $.widget("nmk.confirmdata", {
        options: {
            session: "",
            id: 0,
            data: "",
            chk: "",    
            type: "",
            t: "",
            user: function (user) { }
        },
        url: 'json/confirmdata.ashx',
        currentStatus: 0,
        refresh: function (options2) {
            if (options2)
                this.options = $.extend({}, options2);
            this._load();
        },
        _create: function () {
            this.randomnr = randomFromInterval(0, 99999);
            this._load();
        }, 
        _ajax: function (detta, item, callback) {
            if (item.title == undefined) {
                item.title = "";
            }
            item.session = detta.options.session;
            item.identifier = detta.options.identifier;
            console.log(item);
            if (item.title.length > 0)
                loadingBar(true, 1, "Vänta...", item.title + "...");
            $.ajax({
                url: detta.url,
                error: function (xhr, textStatus, errorThrown) {
                    if (item.title.length > 0)
                        loadingBar(false, 1);
                },
                cache: false,
                data: item,
                type: "POST"
            }).done(function (data) {
                var obj = $.parseJSON(data);
                if (item.title.length > 0)
                    loadingBar(false, 1);
                callback(obj);
                if (obj.checkitem) {
                    detta.options.user(obj.checkitem.medlem);
                }

            });
        },
        item: null,
        _load: function () {
            this.element.empty();
            var detta = this;

            var div = $("<div/>").appendTo(detta.element);
            var item = new Object();
            item.command = "set";
            item.type = detta.options.type,
            item.id = detta.options.id;
            item.chk = detta.options.chk;
            item.t = detta.options.t;
            item.data = detta.options.data;
            item.title = "Hämtar";

            detta._ajax(detta, item, function (obj) {
                     $("<div/>").appendTo(detta.element).mypanel({
                        header: { text: obj.title}, content: { text: obj.text }
                    });
                   
            });

            
           

        },
        destroy: function () {
            this.element.empty();
            // Call the base destroy function.
            $.Widget.prototype.destroy.call(this);
        }
    });
})(jQuery);