﻿var _MW = new Object();


//var _MWAPIURL = "http://localtest.mindwallet.com:54501";

_MW.apps = new Array();

_MW.registerApp = function (newObject) {
    newObject.appName = newObject.appName.toUpperCase();
    if (_MW != null && _MW.apps != null && _MW.apps[newObject.appName] == null) {
        _MW.apps[newObject.appName] = newObject;
        _MW.apps[newObject.appName].ActivationNodes = new Array();
    }

    //start listening
    //setTimeout(_MW.pingApp(_MW.apps[newObject.appName], CurrentSheetID, CurrentParentID), 120000);
};

_MW.showAppMessage = function (App, oNode, Message) {
    var AppMessage = $('<div/>', { "class": "AppMessage",
        "text": Message
    });
    $('.node' + oNode.Position.PositionID).find('.ActionBar').append(AppMessage);
    AppMessage.fadeOut(10000, function () { $(this).remove(); });
    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + App.appName, 'DisplayedMessage', CurrentSheetID, 1]);


};

_MW.showAppCard = function (App, oNode, url) {
    var CardName = 'appCard' + App.appName + oNode.Position.PositionID;
    var AppCards = $('#' + CardName);
    var AppCard;

    //url="http://localtest.mindwallet.com:54501/Home/fbquick/787541c7-0860-41ea-a21a-2322e06675c3/180836785287887";
    if (AppCards.length == 0) {
        AppCard = $('<div/>', { "class": "AppCard", "id": 'appCard' + App.appName + oNode.Position.PositionID
        });
        AppCard.append("<iframe src='" + url + "'/>");
        $('.node' + oNode.Position.PositionID).children('.SubNodes').before(AppCard);
    }
    else {
        AppCard = AppCards.first();
        AppCard.find('iframe').attr('src', url);
    }




    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + App.appName, 'DisplayedAppCard', CurrentSheetID, 1]);
};

_MW.removeAppCard = function (App, oNode) {
    var CardName = 'appCard' + App.appName + oNode.Position.PositionID;
    var AppCards = $('#' + CardName);
    var AppCard;

    //url="http://localtest.mindwallet.com:54501/Home/fbquick/787541c7-0860-41ea-a21a-2322e06675c3/180836785287887";
    if (AppCards.length == 0) {

    }
    else {
        AppCards.remove();
    }




    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + App.appName, 'RemovedAppCard', CurrentSheetID, 1]);
};

_MW.updateText = function (App, oNode, text) {


    oNode.NewValue = (oNode.Dirty != null && oNode.Dirty ? oNode.NewValue : oNode.NodeText) + text;
    MarkDirty(oNode);
    //TestNodeChange(oNode);
    $('.node' + oNode.Position.PositionID).children('.nodeContent').text($('.node' + oNode.Position.PositionID).children('.nodeContent').first().text() + text);




    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + App.appName, 'AppUpdatedText', CurrentSheetID, 1]);
};

_MW.getToken = function (app, oNode) {
    var SubmitData;

    if (oNode.Apps == null || oNode.Apps[app.appName] ==null || oNode.Apps[app.appName].PublicToken == null) {
        SubmitData = { appName: app.appName,
            positionID: oNode.Position.PositionID,
            nodeID: oNode.NodeID,
            itemKey: oNode.Position.ItemKey
        };
    }
    else {
        SubmitData = { appName: app.appName,
            positionID: oNode.Position.PositionID,
            nodeID: oNode.NodeID,
            itemKey: oNode.Position.ItemKey,
            publicToken: oNode.Apps[app.appName].PublicToken
        };
    }

    return $.ajax({
        url: '/api/apptoken',
        cache: false,
        type: 'post',
        data: SubmitData,
        success: function (data) {
            //set the tokens
            app.ActivationNodes[oNode.Position.PositionID].AccessToken = data.Token;
            app.ActivationNodes[oNode.Position.PositionID].AccessTokenOwner = data.isOwner;
            if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + app.appName, 'TokenRequest', CurrentSheetID, 1]);
        },
        error: ProcessAddedNodeError
    });



};

_MW.activateApp = function (app, oNode,data) {
    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + app.appName, 'ActivateApp', CurrentSheetID, 1]);

    return $.ajax({
        url: app.ActivateURL,
        dataType: "jsonp",
        crossDomain: true,
        cache: false,
        type: 'post',
        data: { appName: app.appName,
            positionID: oNode.Position.PositionID,
            itemKey: oNode.Position.ItemKey,
            token: app.ActivationNodes[oNode.Position.PositionID].AccessToken,
            data: data
        },

        error: ProcessAddedNodeError
    });


};

_MW.deleteToken = function (app, oNode) {
    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + app.appName, 'DeactivateApp', CurrentSheetID, 1]);

    return $.ajax({
        url: '/api/apptoken',
        cache: false,
        type: 'delete',
        data: { appName: app.appName,
            positionID: oNode.Position.PositionID,
            nodeID: oNode.NodeID,
            itemKey: oNode.Position.ItemKey,
            token: app.ActivationNodes[oNode.Position.PositionID].AccessToken
        },
        success: function (data) {
            //set the tokens
            delete app.ActivationNodes[oNode.Position.PositionID].AccessToken;
            delete app.ActivationNodes[oNode.Position.PositionID].AccessTokenOwner;
            if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + app.appName, 'TokenInvalidated', CurrentSheetID, 1]);
        },
        error: ProcessAddedNodeError
    });


};

_MW.pingApp = function (app,itemKey, currentParent) {
    if (typeof _gaq != "undefined" && _gaq) _gaq.push(['_trackEvent', 'app.' + app.appName, 'PingApp', CurrentSheetID, 1]);

    return $.ajax({
        url: app.PingURL,
        dataType: "jsonp",
        crossDomain: true,
        cache: false,
        type: 'post',
        data: { appName: app.appName,
            currentParent: oNode.Position.PositionID,
            itemKey: oNode.Position.ItemKey,
            token: app.ActivationNodes[oNode.Position.PositionID].AccessToken
        },
        success:  function(){ setTimeout(app.pingApp(app, CurrentSheetID, CurrentParentID), 120000);},

        error: function(){ setTimeout(app.pingApp(app, CurrentSheetID, CurrentParentID), 120000);}
    });


};
