﻿/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('Argip');

Argip.ArgipTreePreview = function () {
    //Call Base Method
    Argip.ArgipTreePreview.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._settings = {};
    //this._updateButton = null;

    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        _updateSuccessDelegate: Function.createDelegate(this, this._updateSuccess),
        _updateFailDelegate: Function.createDelegate(this, this._updateFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad),
        componentPropChangedDelegate: Function.createDelegate(this, this._onPropChanged)
    };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

Argip.ArgipTreePreview.prototype =
{
    //Properties
    get_ns: function () { return this.get_id() + '_'; },
    get_msgs: function () { return this._msgs; },
    set_msgs: function (value) { this._msgs = value; },
    get_settings: function () { return this._settings; },
    set_settings: function (value) { this._settings = value; },
    get_name: function () { return "Tree"; },


    //Events
    initialize: function () {
        //Call Base Method    
        Argip.ArgipTreePreview.callBaseMethod(this, 'initialize');

        //hookup event handlers
        //this._updateButton = $get(this.get_ns() + 'cmdUpdate');
        //$addHandlers(this._updateButton, {"click": this._onUpdate}, this);

        //var opts = { size: 56, align: 'middle', duration: 200 };
        //$('#menu1cart').jqDock(opts);
    },

    _onLoad: function (src, args) {
        this._updateControls();
        var obiekt = this;
        var mydialog = '#' + this.get_ns() + 'detailsdialog';


        jQuery(mydialog).dialog({
            height: 600,
            width: 800,
            modal: true,
            autoOpen: false,
            title: 'Wybierz...',
            open: function (event, ui) {
                jQuery(mydialog).html('');
                var _itemid = jQuery(mydialog).attr('data');
                dnn.xmlhttp.callControlMethod('Argip.Modules.ArgipStore.ArgipTreePreview.' + obiekt.get_id(), 'GetDeatils', { itemid: _itemid }, function (payload, ctx, req) {
                    jQuery(mydialog).html(payload);
                },
                function (payload, ctx, req) { });

            }
            //open: this._whenDialogOpenedDelegate
        });

        jQuery(".prev_dialog").click(function () {
            var tenobiekt = this;
            var itemid = jQuery(tenobiekt).attr('data');
            jQuery(mydialog).attr('data', itemid);

            jQuery(mydialog).dialog("option", "width", 800);
            jQuery(mydialog).dialog("option", "height", 600);
            jQuery(mydialog).dialog("open");
        });

    },

    _onUpdate: function (src, arg) {
        //  this._displayWait(true); 
        //  this._updateSettings();
        //  dnn.xmlhttp.callControlMethod('Argip.Modules.Argip.ArgipTreePreview.' + this.get_id(), 
        //     'UpdateSettings', {Settings:this._settings}, this._delegates._updateSuccessDelegate, this._delegates._updateFailDelegate);
    },

    _onPropChanged: function (src, args) {

    },


    //Methods
    getMessage: function (key) {
        return this._msgs[key];
    },

    getSetting: function (key, def) {
        if (this._settings[key])
            return this._settings[key];
        return def;
    },

    //Private Methods
    _updateControls: function () {

    },

    _updateSettings: function () {

    },

    _displayWait: function (show) {
        $get(this.get_ns() + 'imgAjax').className = (show ? '' : 'ceHidden');
    },

    _updateSuccess: function (payload, ctx, req) {
        this._displayWait(false);
        //alert(this.getMessage('SettingsSaved'));
        //alert(payload);
        this.set_desc(payload);
    },

    _updateFail: function (payload, ctx, req) {
        this._displayWait(false);
        alert('error: ' + payload);
    },

    dispose: function () {
        //$clearHandlers(this._updateButton);    
        //this._updateButton = null;
        this._delegates = null;
        Argip.ArgipTreePreview.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
Argip.ArgipTreePreview.registerClass('Argip.ArgipTreePreview', Sys.Component);

