﻿if (window.MyBulletinBoard === undefined) {
    debugger; // should have loaded MyBulletinBoard.js first
}

MyBulletinBoard.onConsentComplated = function(e) {
    if (!MyBulletinBoard.isLoggedIn() || !e) {
        return;
    }
    var consentToken = e.get_consentToken();
    if (!consentToken) {
        return;
    }
    var userName = MyBulletinBoard.user.name;
    var postData = 'u=' + (userName) + 
        '&ct=' + consentToken +
        '&s=' + (MyBulletinBoard.user.sessionKey);
    var request = new MyBulletinBoard.AsyncServerCall(
        'UpdateConsentTokenHandler.ashx',
        postData,
        this.onUpdateConsentTokenComplete);
    request.send();
}

MyBulletinBoard.onUpdateConsentTokenComplete = function(responseBody, status) {
    if (status != 200 || !responseBody) {
        
        return;
    }
    /*document.getElementById('dvSignIn').style.display = 'none';
    document.getElementById('dvPhotoFrame').style.display = '';
    document.getElementById('dvLogout').style.display = 'block';*/
}

MyBulletinBoard.onMessengerSigninCompleted = function(e) {
    if (!e || !MyBulletinBoard.isLoggedIn()) {
        return;
    }
    if (e.get_resultCode() !== 0) {
        return;
    }
    var user = Microsoft.Live.Messenger.UI.Tags.TagsFactory.get_user();
    if (!user) {
        return;
    }
    if (MyBulletinBoard.user.cid) {
        // The cid is already in the DB, no need to persist it
        return;
    }
    MyBulletinBoard.user.cid = user.get_address().get_cid();
    if (MyBulletinBoard.user.cid) {
        window.setTimeout(
            function() {
                MyBulletinBoard.uploadUserCid();
            },
            1);
    }
}

MyBulletinBoard.uploadUserCid = function() {
    if (!MyBulletinBoard.isLoggedIn() || !MyBulletinBoard.user.cid) {
        return;
    }
    var userName = MyBulletinBoard.user.name;
    var postData = 'u=' + (userName) +
        '&cid=' + MyBulletinBoard.user.cid +
        '&s=' + (MyBulletinBoard.user.sessionKey);
    var request = new MyBulletinBoard.AsyncServerCall(
        'UpdateUserCidHandler.ashx',
        postData,
        null);
    request.send();    
}

MyBulletinBoard.onLoaderReady = function() {
    //Commented to stop show messages.
    //MyBulletinBoard.loadInitialMessagesPayload();
    window.setTimeout(
        MyBulletinBoard.startRefreshInterval,
        1000);
}

// extend the MyBulletinBoard.updateLoggedInStyle function
extend_updateLoggedInStyle = function() {
    if (MyBulletinBoard.updateLoggedInStyle) {
        var f = MyBulletinBoard.updateLoggedInStyle;
        MyBulletinBoard.updateLoggedInStyle = function() 
        {
            f.call(this);
           /* if (MyBulletinBoard.isLoggedIn() && !MyBulletinBoard.user.consentToken) {
                document.getElementById('dvSignIn').style.display = '';
                document.getElementById('btnLogin').style.display = 'none';
                document.getElementById('btnSignIn').style.display = '';
                document.getElementById('dvLogout').style.display = 'none';
            }
            else {
                document.getElementById('btnSignIn').style.display = 'none';
                document.getElementById('dvSignIn').style.display = 'none';
                document.getElementById('dvLogout').style.display = 'block';
            }
            if (!MyBulletinBoard.isLoggedIn())
            {
                document.getElementById('dvLogout').style.display = 'none';
                document.getElementById('dvSignIn').style.display = '';
                document.getElementById('btnLogin').style.display = '';
                document.getElementById('btnSignIn').style.display = 'none';
            }*/
        }
    }
}
extend_updateLoggedInStyle();

// Extend the logout function
extend_logOut = function() {
    if (MyBulletinBoard.logOut) {
        var f = MyBulletinBoard.logOut;
        MyBulletinBoard.logOut = function() {
            f.call(this);
            var msgrUser = Microsoft.Live.Messenger.UI.Tags.TagsFactory.get_user();
            if (msgrUser && msgrUser.get_status() === Microsoft.Live.Messenger.UserStatus.signedIn) {
                msgrUser.signOut(Microsoft.Live.Messenger.SignOutLocations.local);
            }
        }
    }
}
extend_logOut();

// Extend MyBulletinBoard.displayUserMessage
extend_displayUserMessage = function() {
    if (MyBulletinBoard.displayUserMessage) {
        var f = MyBulletinBoard.displayUserMessage;
        MyBulletinBoard.displayUserMessage = function(userName, cid, created, message) {
            var row = f.call(this, userName, cid, created, message);
            if (!cid) {
                return;
            }
            var r = row;
            for (var i = 0; i < r.childNodes.length; i++) {
                var colId = r.childNodes[i].getAttribute('id');
                if (colId && colId.indexOf('profileCol') >= 0) {
                    var placeHolder = r.childNodes[i].childNodes[0];
                    var msgrProfileTag = Microsoft.Live.Messenger.UI.Tags.TagsFactory.createTag(
                        'profile',
                        { "cid": cid });
                    placeHolder.appendChild(msgrProfileTag);
                    if (!MyBulletinBoard.isLoggedIn()) {
                        return;
                    }
                    if (MyBulletinBoard.user.cid == cid) {
                        return;
                    }
                    var msgrifTag = Microsoft.Live.Messenger.UI.Tags.TagsFactory.createTag(
                        'if',
                        { "cid": cid, "condition": "online" });
                    var imButton = document.createElement('button');
                    imButton.innerHTML = 'IM now';
                    imButton.onclick = function() {
                        var msgrUser = Microsoft.Live.Messenger.UI.Tags.TagsFactory.get_user();
                        if (!msgrUser || msgrUser.get_status() !== Microsoft.Live.Messenger.UserStatus.signedIn) {
                            return;
                        }
                        var contact = msgrUser.get_contacts().findByCid(cid);
                        if (!contact) {
                            contact = msgrUser.get_applicationContacts().findByCid(cid);
                        }
                        if (contact) {
                            msgrUser.get_conversations().create(contact);
                        }
                    }
                    msgrifTag.style.display = 'none';
                    msgrifTag.appendChild(imButton);
                    placeHolder.appendChild(msgrifTag);
                    return;
                }
            }
        }
    }
}
extend_displayUserMessage();


