/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('IntraHouse');

IntraHouse.ViewFaqList = function() {
    //Call Base Method
    IntraHouse.ViewFaqList.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._helloButton = null;

    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        //        _helloSuccessDelegate: Function.createDelegate(this, this._helloSuccess),
        //        _helloFailDelegate: Function.createDelegate(this, this._helloFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad),
        //              _onGetAnswerTextSuccess: Function.createDelegate(this, this._getAnswerTextSuccess),
        _onGetAnswerTextFailed: Function.createDelegate(this, this._getAnswerTextFailed),
        componentPropChangedDelegate: Function.createDelegate(this, this._onPropChanged)
    };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

IntraHouse.ViewFaqList.prototype =
{
    //Properties
    get_ns: function() { return this.get_id() + '_'; },
    get_msgs: function() { return this._msgs; },
    set_msgs: function(value) { this._msgs = Sys.Serialization.JavaScriptSerializer.deserialize(value); },
    get_hfSearchText: function() { return $get(this.get_ns() + 'lastSearchText').value; },
    //        get_name: function() {
    //            return $get(this.get_ns() + 'lblName').innerHTML;
    //        },
    //        get_description: function() {
    //            return $get(this.get_ns() + 'lblDesc').innerHTML;
    //        },

    //Events
    initialize: function() {
        //Call Base Method    
        IntraHouse.ViewFaqList.callBaseMethod(this, 'initialize');
        //        alert(this.get_ns());
        //        //create UI
        //                this._helloButton = this._createChildControl('btnHello', 'input', 'button');
        //                this._helloButton.value = this.getMessage('btnHello'); //get localized value
        //               $get(this.get_ns() + 'lblName').parentNode.appendChild(this._helloButton);

        //hookup event handlers
        //               $addHandlers(this._helloButton, { "click": this._onHello }, this);
    },

    _onLoad: function(src, arg) {
        //page is completely loaded, you can now access any element or component
        var components = arg.get_components();
        //sample of how to do client-side IMC - look for other components we are interested in
        //in this case, we will look for other instances of the same module that are not ourselves
        //but you can communicate with any components
        for (var i = 0; i < components.length; i++) {
            if (Object.getTypeName(components[i]) == Object.getTypeName(this) && components[i].get_id() != this.get_id())
                this.add_propertyChanged(components[i]._delegates.componentPropChangedDelegate);
        }
    },

    //     _onHello: function(src, arg) {
    //            this._displayWait(true);
    //            dnn.xmlhttp.callControlMethod('IntraHouse.Modules.FaqList.ViewFaqList.' + this.get_id(),
    //                'Hello', { Name: this.get_name(), Description: this.get_description() }, this._delegates._helloSuccessDelegate, this._delegates._helloFailDelegate);

    //           this.raisePropertyChanged('SayHello');
    //        },

    _onPropChanged: function(src, args) {
        //               this.showMessage(String.format('You {0} to {1} but not to me?', args.get_propertyName(), src.get_name()));
    },
    getText: function(sender) {

        if (sender.parentNode.childNodes.length > 1) {
            var el = sender.parentNode.childNodes[1];
            if (el.style.display == 'none'/*el.style.visibility == "hidden"*/)
            //                el.style.visibility = "visible";
                el.style.display = 'block';
            else //el.style.visibility = "hidden";
                el.style.display = 'none';
            return false;
        }
        this._displayWait(true);
        var obj = this;
        var v = sender.attributes['faqID'].value;
        dnn.xmlhttp.callControlMethod('IntraHouse.Modules.FaqList.ViewFaqList.' + this.get_id(),
            'GetAnswer', { KbID: v, SearchText: this.get_hfSearchText() }, function(arg) {
                obj._getAnswerTextSuccess(arg, sender);
            }
            , this._delegates._onGetAnswerTextFailed);
        this.raisePropertyChanged('SayHello');
    },
    //Methods
    getMessage: function(key) {
        return this._msgs[key];
    },

    //    showMessage: function(msg) {
    //        $get(this.get_ns() + 'lblResponse').innerHTML = msg;
    //    },

    //Private Methods
    _createChildControl: function(id, tag, type) {
        var ctl = document.createElement(tag);
        ctl.id = this.get_ns() + id;
        if (type)
            ctl.type = type;
        return ctl;
    },

    _displayWait: function(show) {
        $get(this.get_ns() + 'imgAjax').className = (show ? '' : 'ceHidden');
    },
    _getAnswerTextSuccess: function(payload, sender, req) {
        this._displayWait(false);
        var answer = this._createChildControl('answer', 'div');
        answer.className = "ansText";
        sender.parentNode.appendChild(answer);
        answer.innerHTML = payload;
    },
    _getAnswerTextFailed: function(payload, ctx, req) {
        this._displayWait(false);
        alert('error: ' + payload);
    },
    //    _helloSuccess: function(payload, ctx, req) {
    //        this._displayWait(false);
    //        this.showMessage(payload);
    //    },

    //    _helloFail: function(payload, ctx, req) {
    //        this._displayWait(false);
    //        alert('error: ' + payload);
    //    },

    dispose: function() {
        //        $clearHandlers(this._helloButton);
        //        this._helloButton = null;
        this._delegates = null;
        IntraHouse.ViewFaqList.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
IntraHouse.ViewFaqList.registerClass('IntraHouse.ViewFaqList', Sys.Component);

