/**
@fileOverview
Part of the QlikView WorkBench Javascript Library (http://www.qlikwebworkbench.com)<br /><br />
Copyright (c) 2009 QlikTech International AB (http://www.qliktech.com)<br /><br />
All Rights Reserved. Email support@qliktech.com for licensing and support information.<br /><br />
@author <a href="mailto:email@industrialcodebox.com">Industrial CodeBox</a>
*/

Qww.Button = {};

/**
Constructs a new Qww.Button.Mgr button instance.
@class JavaScript class to manage communication with an underlying QlikView button object.<br />

@param {Object} cfg JSON object to configure ButtonMgr.

@param {String} cfg.ObjectID Id of the button in the QlikView application.

@param {String} [cfg.ApplicationID=null] Id/Name of the QlikView application. Note this is <strong>only</strong> necessary when 
multiple QlikView documents are being used on the same web page and should otherwise be set to null.

@param {Function} [cfg.OnUpdate] Function to call whenever an update is received for the button (for example 
if the button text changes). The function should accept a single parameter which is reference to the ButtonMgr itself.

@returns {Qww.Button.Mgr} Qww.Button.Mgr

@example
var buttonMgr = new Qww.Button.Mgr(
{
"ObjectID" : 'BUCLEAR',
"OnUpdate": function(btnMgr) {

alert("Qww.Button.Mgr.OnUpdate fired - .Caption = " + 
btnMgr.Caption + ", .Enabled = " + btnMgr.Enabled);        
}
});
*/
Qww.Button.Mgr = function(cfg) {

    var _isInitialized = false;

    var me = this;

    /**
    Reference to the configuration object which was used to create construct the object. This 
    allows certain properties to be updated after initial instantiation.
    @type Object
    */
    this.Cfg = cfg;

    /** 
    Whether the button is enabled (visible) within the QlikView application. This can be dependent, 
    for example, upon show and hide conditions set for the object in QlikView.
    @deprecated You should now use Mode.
    @type Bool
    */
    this.Enabled = true;

    /** 
    The state of the object. This can be dependent, for example, upon show and hide conditions set 
    for the object in QlikView.
    @type Qww.QlikViewObjectState
    */
    this.Mode = true;

    /** 
    Current Text/Caption of the button control.
    @type String
    */
    this.Caption = "";

    /**
    @ignore
    Returns an array of Qww.QvsConnectorSet objects which represents the initiasation which 
    needs to be sent to QlikView server in order to initialise this object.
    @returns {Qww.QvsConnectorSet[]} Array of Qww.QvsConnectorSet objects which represents 
    the initiasation for this object.
    */
    this.GetInitialisationSets = function() {

        var sets = [];

        sets.push(new Qww.QvsConnectorSet(cfg.ApplicationID, cfg.ObjectID, "add", "mode;action", true));

        return sets;
    };

    function initialize() {

        var btn = qwwHub.DoAvqSelect(cfg.ApplicationID, cfg.ObjectID);

        if (btn == null) {
            qwwHub.DoAllSets(me.GetInitialisationSets());
            //qwwHub.DoAvqSet(cfg.ApplicationID, cfg.ObjectID, "add", "mode;action", true);
            return false;
        }

        _isInitialized = true;

        return true; // handshake - there will not be a new update
    };

    /**
    Sends a click through to the button.
    @param {Bool} [isFinal=true] If this is the final command in this set to send to QlikView Server.
    Set this to false if you intend to call some methods on other objects and would like all the commands to 
    be sent in one request.
    */
    this.SendClick = function(isFinal) {

        if (isFinal == null)
            isFinal = true;

        Qww.Button.Mgr.SendClick(cfg.ApplicationID, cfg.ObjectID, isFinal);
    };


    this.OnAvqUpdateComplete = function() {

        if (!_isInitialized) {
            if (!initialize()) {
                return;
            }
        }

        var CH = qwwHub.DoAvqSelect(cfg.ApplicationID, cfg.ObjectID);

        if (CH == null) {
            // This might be called as the result of an OnAvqUpdateComplete
            // which might be for an unrelated control, in which case
            // the XML wont contain that for XH
            return;
        }
        else {

            this.Enabled = (CH.getAttribute("mode") == "enabled");
            this.Mode = CH.getAttribute("mode");

            this.Caption = CH.getAttribute("text");

            if (this.Cfg.OnUpdate)
                this.Cfg.OnUpdate(this);
        }
    };

    if (qwwHub)
        qwwHub.Register(this);
};

/**
Sends a click through to the button.
@param {String} applicationId Id/Name of the QlikView application <strong>(note this is only necessary when 
multiple QlikView documents are being used on the same web page and should otherwise be set to null)</strong>.
@param {String} buttonId Id of the button control in the QlikView application.
@param {Bool} [isFinal=true] If this is the final command in this set to send to QlikView Server.
Set this to false if you intend to call some methods on other objects and would like all the commands to 
be sent in one request.
*/
Qww.Button.Mgr.SendClick = function(applicationId, buttonId, isFinal) {
    if (isFinal == null)
        isFinal = true;

    qwwHub.DoAvqSet(applicationId, buttonId, "action", "", isFinal);

    qwwHub.TrackAction(applicationId, "ButtonClicked", buttonId);
};

/**
Enumeration of standard actions which can be called on an application.
@class Enumeration of standard actions which can be called on an application.<br />
@see Qww.Button.Mgr#CallStandardAction
@example
Qww.Button.Mgr.CallStandardAction(null, Qww.Button.Mgr.StandardAction.ClearAll, true);
*/
Qww.Button.Mgr.StandardAction = {

    /** 
    Clear all selections.
    @constant
    */
    ClearAll: 'CA',

    /** 
    Undo last operation.
    @constant
    */
    Back: 'BCK',

    /** 
    Redo last undone operation.
    @constant
    */
    Forward: 'FWD',

    /** 
    Lock all selections.
    @constant
    */
    LockSelections: 'LS',

    /** 
    Unlock all selections.
    @constant
    */
    UnlockSelections: 'US'
};

/**
Sends a click through to the button.
@param {String} applicationId Id/Name of the QlikView application <strong>(note this is only necessary when 
multiple QlikView documents are being used on the same web page and should otherwise be set to null)</strong>.
@param {Qww.Button.Mgr.StandardAction} action Standard action to carry out on QlikView Application.
@param {Bool} [isFinal=true] If this is the final command in this set to send to QlikView Server.
Set this to false if you intend to call some methods on other objects and would like all the commands to 
be sent in one request.
*/
Qww.Button.Mgr.CallStandardAction = function(applicationId, action, isFinal) {

    if (isFinal == null)
        isFinal = true;

    qwwHub.DoAvqSet(applicationId, "Document.ActiveSheet.StandardActions." + action, "action", "", isFinal);
    //qwwHub.DoAvqSet(applicationId, "ActiveSheet.StandardActions." + action, "action", "", isFinal);

    qwwHub.TrackAction(applicationId, "StandardAction", action);
};

