//! Copyright (c) Microsoft Corporation. All rights reserved.
//! Microsoft.Live.Messenger.Samples.ContactList.js
//!


Type.createNamespace('Microsoft.Live.Messenger.Samples');

////////////////////////////////////////////////////////////////////////////////
// Microsoft.Live.Messenger.Samples._strings

Microsoft.Live.Messenger.Samples._strings = { 
    appearOffline: 'Offline',
    away: 'Away',
    beRightBack: 'Be right back',
    busy: 'Busy',
    idle: 'Away',
    inACall: 'In a call',
    offline: 'Offline',
    online: 'Online',
    outToLunch: 'Out to lunch',
    tooltipContact: '{0}{1} ({2}) <{3}>'
};


////////////////////////////////////////////////////////////////////////////////
// Microsoft.Live.Messenger.Samples._contactPanel

Microsoft.Live.Messenger.Samples._contactPanel = function Microsoft_Live_Messenger_Samples__contactPanel(element, contacts) {
    /// <summary>
    /// Represents a UI control that renders a contact list.
    /// </summary>
    /// <param name="element" type="Object" domElement="true">
    /// The associated DOM element.
    /// </param>
    /// <param name="contacts" type="Microsoft.Live.Messenger.SortedContactCollection">
    /// The associated contacts.
    /// </param>
    /// <field name="_contacts$2" type="Microsoft.Live.Messenger.SortedContactCollection">
    /// The associated contacts.
    /// </field>
    /// <field name="_contactItems$2" type="Array">
    /// The associated contact UI elements.
    /// </field>
    Microsoft.Live.Messenger.Samples._contactPanel.constructBase(this, [ element ]);
    Debug.assert(element);
    Debug.assert(contacts);
    this._contacts$2 = contacts;
    this._contacts$2.add_collectionChanged(Delegate.create(this, this._onOnlineContactsChanged$2));
    this._contactItems$2 = [];
    this._initializeItems$2();
}
Microsoft.Live.Messenger.Samples._contactPanel.prototype = {
    _contacts$2: null,
    _contactItems$2: null,
    
    dispose: function Microsoft_Live_Messenger_Samples__contactPanel$dispose() {
        /// <summary>
        /// Disposes the control.
        /// </summary>
        Microsoft.Live.Messenger.Samples._contactPanel.callBase(this, 'dispose');
        this._contacts$2.remove_collectionChanged(Delegate.create(this, this._onOnlineContactsChanged$2));
        this._disposeItems$2();
    },
    
    _initializeItems$2: function Microsoft_Live_Messenger_Samples__contactPanel$_initializeItems$2() {
        /// <summary>
        /// Initializes each of the contact items.
        /// </summary>
        Debug.assert(!this._contactItems$2.length);
        var index = 0;
        var $enum1 = this._contacts$2.getEnumerator();
        while ($enum1.moveNext()) {
            var contact = $enum1.get_current();
            this._addContactItem$2(index, contact);
            index++;
        }
    },
    
    _disposeItems$2: function Microsoft_Live_Messenger_Samples__contactPanel$_disposeItems$2() {
        /// <summary>
        /// Disposes each of the contact items.
        /// </summary>
        var $enum1 = this._contactItems$2.getEnumerator();
        while ($enum1.moveNext()) {
            var item = $enum1.get_current();
            item.dispose();
        }
        this._contactItems$2.clear();
    },
    
    _onOnlineContactsChanged$2: function Microsoft_Live_Messenger_Samples__contactPanel$_onOnlineContactsChanged$2(sender, e) {
        /// <summary>
        /// Occurs when the associated collection changes.
        /// </summary>
        /// <param name="sender" type="Object">
        /// </param>
        /// <param name="e" type="Microsoft.Live.Core.NotifyCollectionChangedEventArgs">
        /// </param>
        switch (e.get_action()) {
            case Microsoft.Live.Core.NotifyCollectionChangedAction.add:
                var index = e.get_newStartingIndex();
                var $enum1 = e.get_newItems().getEnumerator();
                while ($enum1.moveNext()) {
                    var contact = $enum1.get_current();
                    this._addContactItem$2(index, contact);
                    index++;
                }
                break;
            case Microsoft.Live.Core.NotifyCollectionChangedAction.remove:
                var index = e.get_oldStartingIndex();
                var $enum2 = e.get_oldItems().getEnumerator();
                while ($enum2.moveNext()) {
                    var contact = $enum2.get_current();
                    this._removeContactItem$2(index, contact);
                    index++;
                }
                break;
            case Microsoft.Live.Core.NotifyCollectionChangedAction.reset:
                this._disposeItems$2();
                this._initializeItems$2();
                break;
        }
    },
    
    _addContactItem$2: function Microsoft_Live_Messenger_Samples__contactPanel$_addContactItem$2(index, contact) {
        /// <summary>
        /// Adds a new contact item at the provided index.
        /// </summary>
        /// <param name="index" type="Number" integer="true">
        /// </param>
        /// <param name="contact" type="Microsoft.Live.Messenger.Contact">
        /// </param>
        var item = $('ContactListItemTemplate').cloneNode(true);
        item.id = '';
        if (this.get_domElement().children.length === index) {
            this.get_domElement().appendChild(item);
        }
        else {
            var current = this.get_domElement().children[index];
            this.get_domElement().insertBefore(item, current);
        }
        this._contactItems$2.insert(index, new Microsoft.Live.Messenger.Samples._contactPanelItem(item, contact));
    },
    
    _removeContactItem$2: function Microsoft_Live_Messenger_Samples__contactPanel$_removeContactItem$2(index, contact) {
        /// <summary>
        /// Removes an existing contact item at the provided index.
        /// </summary>
        /// <param name="index" type="Number" integer="true">
        /// </param>
        /// <param name="contact" type="Microsoft.Live.Messenger.Contact">
        /// </param>
        var item = this.get_domElement().children[index];
        var contactItem = ScriptFX.UI.Control.getControl(item);
        if (!contactItem) {
            Debug.fail('Unable to remove contact');
        }
        else {
            contactItem.dispose();
            this._contactItems$2.remove(contactItem);
        }
    }
}


////////////////////////////////////////////////////////////////////////////////
// Microsoft.Live.Messenger.Samples._contactPanelItem

Microsoft.Live.Messenger.Samples._contactPanelItem = function Microsoft_Live_Messenger_Samples__contactPanelItem(itemElement, contact) {
    /// <summary>
    /// Represents a contact panel item.
    /// </summary>
    /// <param name="itemElement" type="Object" domElement="true">
    /// The associated DOM element.
    /// </param>
    /// <param name="contact" type="Microsoft.Live.Messenger.Contact">
    /// The associated contact.
    /// </param>
    /// <field name="_eltStatus$2" type="Object" domElement="true">
    /// The associated status element.
    /// </field>
    /// <field name="_eltDisplayName$2" type="Object" domElement="true">
    /// The associated display name element.
    /// </field>
    /// <field name="_eltSeparator$2" type="Object" domElement="true">
    /// The associated separator element.
    /// </field>
    /// <field name="_eltPersonalMessage$2" type="Object" domElement="true">
    /// The associated personal message element.
    /// </field>
    /// <field name="_contact$2" type="Microsoft.Live.Messenger.Contact">
    /// The associated contact.
    /// </field>
    /// <field name="_presence$2" type="Microsoft.Live.Messenger.ContactPresence">
    /// The contact's presence.
    /// </field>
    /// <field name="_status$2" type="Microsoft.Live.Messenger.PresenceStatus">
    /// The current status.
    /// </field>
    Microsoft.Live.Messenger.Samples._contactPanelItem.constructBase(this, [ itemElement ]);
    this._contact$2 = contact;
    this._contact$2.add_propertyChanged(Delegate.create(this, this._onContactChanged$2));
    this._presence$2 = this._contact$2.get_presence();
    this._presence$2.add_propertyChanged(Delegate.create(this, this._onPresenceChanged$2));
    this._eltStatus$2 = this.get_domElement().getElementsByTagName('img')[0];
    var items = this.get_domElement().getElementsByTagName('span');
    this._eltDisplayName$2 = items[0];
    this._eltSeparator$2 = items[1];
    this._eltPersonalMessage$2 = items[2];
    this._reset$2();
}
Microsoft.Live.Messenger.Samples._contactPanelItem.prototype = {
    _eltStatus$2: null,
    _eltDisplayName$2: null,
    _eltSeparator$2: null,
    _eltPersonalMessage$2: null,
    _contact$2: null,
    _presence$2: null,
    _status$2: 0,
    
    dispose: function Microsoft_Live_Messenger_Samples__contactPanelItem$dispose() {
        /// <summary>
        /// Disposes the item.
        /// </summary>
        if (this.get_isDisposed()) {
            return;
        }
        this._contact$2.remove_propertyChanged(Delegate.create(this, this._onContactChanged$2));
        this._presence$2.remove_propertyChanged(Delegate.create(this, this._onPresenceChanged$2));
        this.get_domElement().parentNode.removeChild(this.get_domElement());
        Microsoft.Live.Messenger.Samples._contactPanelItem.callBase(this, 'dispose');
    },
    
    _reset$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_reset$2() {
        /// <summary>
        /// Updates everything.
        /// </summary>
        this._updateStatus$2(this._presence$2.get_status());
        this._updateBlockedStatus$2();
        this._updateType$2();
        this._updateDisplayName$2();
        this._updatePersonalMessage$2();
        this._updateTooltips$2();
    },
    
    _updateStatus$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_updateStatus$2(newStatus) {
        /// <summary>
        /// Updates the status.
        /// </summary>
        /// <param name="newStatus" type="Microsoft.Live.Messenger.PresenceStatus">
        /// </param>
        ScriptFX.UI.Element.removeCSSClass(this._eltStatus$2, this._getCssClass$2(this._status$2));
        ScriptFX.UI.Element.addCSSClass(this._eltStatus$2, this._getCssClass$2(newStatus));
        this._status$2 = newStatus;
    },
    
    _updateType$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_updateType$2() {
        /// <summary>
        /// Updates the address type.
        /// </summary>
        switch (this._contact$2.get_currentAddress().get_type()) {
            case Microsoft.Live.Messenger.IMAddressType.mobileNetwork:
            case Microsoft.Live.Messenger.IMAddressType.telephone:
                ScriptFX.UI.Element.addCSSClass(this._eltStatus$2, 'Mobile');
                break;
            default:
                ScriptFX.UI.Element.removeCSSClass(this._eltStatus$2, 'Mobile');
                break;
        }
    },
    
    _updateBlockedStatus$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_updateBlockedStatus$2() {
        /// <summary>
        /// Updates the blocked status.
        /// </summary>
        if (this._contact$2.get_isBlocked()) {
            ScriptFX.UI.Element.addCSSClass(this._eltStatus$2, 'Blocked');
        }
        else {
            ScriptFX.UI.Element.removeCSSClass(this._eltStatus$2, 'Blocked');
        }
    },
    
    _updateDisplayName$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_updateDisplayName$2() {
        /// <summary>
        /// Updates the display name.
        /// </summary>
        while (this._eltDisplayName$2.children.length > 0) {
            this._eltDisplayName$2.removeChild(this._eltDisplayName$2.firstChild);
        }
        this._eltDisplayName$2.appendChild(Microsoft.Live.Messenger.MessengerUtility.emoticonEncode(this._contact$2.get_displayName()));
    },
    
    _updatePersonalMessage$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_updatePersonalMessage$2() {
        /// <summary>
        /// Updates the personal message.
        /// </summary>
        while (this._eltPersonalMessage$2.childNodes.length > 0) {
            this._eltPersonalMessage$2.removeChild(this._eltPersonalMessage$2.firstChild);
        }
        var personalMessage = this._presence$2.get_personalMessage();
        this._eltSeparator$2.style.display = (String.isNullOrEmpty(personalMessage)) ? 'none' : 'inline';
        this._eltPersonalMessage$2.appendChild(Microsoft.Live.Messenger.MessengerUtility.emoticonEncode(personalMessage));
    },
    
    _updateTooltips$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_updateTooltips$2() {
        /// <summary>
        /// Updates the various tooltips.
        /// </summary>
        var status = this._getStatusString$2(this._status$2);
        this._eltStatus$2.title = status;
        var personalMessage = this._presence$2.get_personalMessage();
        var displayName = this._contact$2.get_displayName();
        this._eltPersonalMessage$2.title = personalMessage;
        if (!String.isNullOrEmpty(personalMessage)) {
            personalMessage = ' - ' + personalMessage;
        }
        this.get_domElement().title = String.format(Microsoft.Live.Messenger.Samples._strings.tooltipContact, displayName, personalMessage, status, this._contact$2.get_currentAddress().get_address());
    },
    
    _onContactChanged$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_onContactChanged$2(sender, e) {
        /// <summary>
        /// Occurs when properties of the contact change.
        /// </summary>
        /// <param name="sender" type="Object">
        /// </param>
        /// <param name="e" type="Microsoft.Live.Core.PropertyChangedEventArgs">
        /// </param>
        switch (e.get_propertyName()) {
            case 'IsBlocked':
                this._updateBlockedStatus$2();
                break;
            case 'DisplayName':
                this._updateDisplayName$2();
                this._updateTooltips$2();
                break;
        }
    },
    
    _onPresenceChanged$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_onPresenceChanged$2(sender, e) {
        /// <summary>
        /// Occurs when the contact's presence changes.
        /// </summary>
        /// <param name="sender" type="Object">
        /// </param>
        /// <param name="e" type="Microsoft.Live.Core.PropertyChangedEventArgs">
        /// </param>
        switch (e.get_propertyName()) {
            case 'Status':
                this._updateStatus$2(this._presence$2.get_status());
                this._updateTooltips$2();
                break;
            case 'PersonalMessage':
                this._updatePersonalMessage$2();
                this._updateTooltips$2();
                break;
        }
    },
    
    _getCssClass$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_getCssClass$2(status) {
        /// <summary>
        /// Gets the CSS class for the status.
        /// </summary>
        /// <param name="status" type="Microsoft.Live.Messenger.PresenceStatus">
        /// </param>
        /// <returns type="String"></returns>
        switch (status) {
            case Microsoft.Live.Messenger.PresenceStatus.online:
                return 'Online';
            case Microsoft.Live.Messenger.PresenceStatus.offline:
                return 'Offline';
            case Microsoft.Live.Messenger.PresenceStatus.appearOffline:
                return 'AppearOffline';
            case Microsoft.Live.Messenger.PresenceStatus.away:
                return 'Away';
            case Microsoft.Live.Messenger.PresenceStatus.beRightBack:
                return 'BeRightBack';
            case Microsoft.Live.Messenger.PresenceStatus.busy:
                return 'Busy';
            case Microsoft.Live.Messenger.PresenceStatus.idle:
                return 'Idle';
            case Microsoft.Live.Messenger.PresenceStatus.inACall:
                return 'InACall';
            case Microsoft.Live.Messenger.PresenceStatus.outToLunch:
                return 'OutToLunch';
            default:
                Debug.fail('Unexpected PresenceStatus');
                return 'Offline';
        }
    },
    
    _getStatusString$2: function Microsoft_Live_Messenger_Samples__contactPanelItem$_getStatusString$2(status) {
        /// <summary>
        /// Gets the status in string form.
        /// </summary>
        /// <param name="status" type="Microsoft.Live.Messenger.PresenceStatus">
        /// </param>
        /// <returns type="String"></returns>
        switch (status) {
            case Microsoft.Live.Messenger.PresenceStatus.online:
                return Microsoft.Live.Messenger.Samples._strings.online;
            case Microsoft.Live.Messenger.PresenceStatus.offline:
                return Microsoft.Live.Messenger.Samples._strings.offline;
            case Microsoft.Live.Messenger.PresenceStatus.appearOffline:
                return Microsoft.Live.Messenger.Samples._strings.appearOffline;
            case Microsoft.Live.Messenger.PresenceStatus.away:
                return Microsoft.Live.Messenger.Samples._strings.away;
            case Microsoft.Live.Messenger.PresenceStatus.beRightBack:
                return Microsoft.Live.Messenger.Samples._strings.beRightBack;
            case Microsoft.Live.Messenger.PresenceStatus.busy:
                return Microsoft.Live.Messenger.Samples._strings.busy;
            case Microsoft.Live.Messenger.PresenceStatus.idle:
                return Microsoft.Live.Messenger.Samples._strings.idle;
            case Microsoft.Live.Messenger.PresenceStatus.inACall:
                return Microsoft.Live.Messenger.Samples._strings.inACall;
            case Microsoft.Live.Messenger.PresenceStatus.outToLunch:
                return Microsoft.Live.Messenger.Samples._strings.outToLunch;
            default:
                Debug.fail('Unexpected PresenceStatus');
                return Microsoft.Live.Messenger.Samples._strings.offline;
        }
    }
}


////////////////////////////////////////////////////////////////////////////////
// Microsoft.Live.Messenger.Samples.ContactList

Microsoft.Live.Messenger.Samples.ContactList = function Microsoft_Live_Messenger_Samples_ContactList() {
    /// <summary>
    /// The application's main scriptlet.
    /// </summary>
    /// <field name="_signInControlID" type="String" static="true">
    /// Sign in control DIV
    /// </field>
    /// <field name="_onlineContactsID" type="String" static="true">
    /// Online contacts DIV
    /// </field>
    /// <field name="_offlineContactsID" type="String" static="true">
    /// Offline contacts DIV
    /// </field>
    /// <field name="_current" type="Microsoft.Live.Messenger.Samples.ContactList" static="true">
    /// Current instance of the class.
    /// </field>
    /// <field name="_signInControl" type="Microsoft.Live.Messenger.UI.SignInControl">
    /// The associated sign in control.
    /// </field>
    /// <field name="_eltOnlineContacts" type="Object" domElement="true">
    /// The DOM element associated with the online contact list control.
    /// </field>
    /// <field name="_eltOfflineContacts" type="Object" domElement="true">
    /// The DOM element associated with the offline contact list control.
    /// </field>
    /// <field name="_onlineContacts" type="Microsoft.Live.Messenger.Samples._contactPanel">
    /// The associated online contact list control.
    /// </field>
    /// <field name="_offlineContacts" type="Microsoft.Live.Messenger.Samples._contactPanel">
    /// The associated offline contact list control.
    /// </field>
    /// <field name="_user" type="Microsoft.Live.Messenger.User">
    /// The associated user.
    /// </field>
    var path = window.location.href;
    var index = path.lastIndexOf('/');
    path = path.substring(0, index);
    var privacyUrl = path + '/' + 'Privacy.html';
    var channelUrl = path + '/' + 'Channel.html';
    this._signInControl = new Microsoft.Live.Messenger.UI.SignInControl(Microsoft.Live.Messenger.Samples.ContactList._signInControlID, privacyUrl, channelUrl, null);
    this._signInControl.add_authenticationCompleted(Delegate.create(this, this._onAuthenticationCompleted));
    this._eltOnlineContacts = $(Microsoft.Live.Messenger.Samples.ContactList._onlineContactsID);
    this._eltOfflineContacts = $(Microsoft.Live.Messenger.Samples.ContactList._offlineContactsID);
}
Microsoft.Live.Messenger.Samples.ContactList.onLoad = function Microsoft_Live_Messenger_Samples_ContactList$onLoad() {
    /// <summary>
    /// The main entrypoint for the application.
    /// </summary>
    Microsoft.Live.Messenger.Samples.ContactList._current = new Microsoft.Live.Messenger.Samples.ContactList();
}
Microsoft.Live.Messenger.Samples.ContactList.prototype = {
    _signInControl: null,
    _eltOnlineContacts: null,
    _eltOfflineContacts: null,
    _onlineContacts: null,
    _offlineContacts: null,
    _user: null,
    
    _onAuthenticationCompleted: function Microsoft_Live_Messenger_Samples_ContactList$_onAuthenticationCompleted(sender, e) {
        /// <summary>
        /// Occurs when the user has authenticated with Windows Live.
        /// </summary>
        /// <param name="sender" type="Object">
        /// </param>
        /// <param name="e" type="Microsoft.Live.Core.AuthenticationCompletedEventArgs">
        /// </param>
        this._user = new Microsoft.Live.Messenger.User(e.get_identity());
        if (this._onlineContacts) {
            this._onlineContacts.dispose();
        }
        if (this._offlineContacts) {
            this._offlineContacts.dispose();
        }
        this._onlineContacts = new Microsoft.Live.Messenger.Samples._contactPanel(this._eltOnlineContacts, this._user.get_onlineContacts());
        this._offlineContacts = new Microsoft.Live.Messenger.Samples._contactPanel(this._eltOfflineContacts, this._user.get_offlineContacts());
        this._user.add_signInCompleted(Delegate.create(this, this._onSignInCompleted));
        this._user.add_signedOutRemotely(Delegate.create(this, this._onSignedOutRemotely));
        this._user.signIn(null);
    },
    
    _onSignInCompleted: function Microsoft_Live_Messenger_Samples_ContactList$_onSignInCompleted(sender, e) {
        /// <summary>
        /// Occurs when the user has been signed into the .NET Messenger Service.
        /// </summary>
        /// <param name="sender" type="Object">
        /// </param>
        /// <param name="e" type="Microsoft.Live.Messenger.SignInCompletedEventArgs">
        /// </param>
        if (e.get_resultCode() !== Microsoft.Live.Messenger.SignInResultCode.success) {
            return;
        }
    },
    
    _onSignedOutRemotely: function Microsoft_Live_Messenger_Samples_ContactList$_onSignedOutRemotely(sender, e) {
        /// <summary>
        /// Occurs when the user is signed out.
        /// </summary>
        /// <param name="sender" type="Object">
        /// </param>
        /// <param name="e" type="Microsoft.Live.Messenger.SignedOutRemotelyEventArgs">
        /// </param>
        if (this._onlineContacts) {
            this._onlineContacts.dispose();
        }
        if (this._offlineContacts) {
            this._offlineContacts.dispose();
        }
    }
}


Microsoft.Live.Messenger.Samples._contactPanel.createClass('Microsoft.Live.Messenger.Samples._contactPanel', ScriptFX.UI.Control);
Microsoft.Live.Messenger.Samples._contactPanelItem.createClass('Microsoft.Live.Messenger.Samples._contactPanelItem', ScriptFX.UI.Control);
Microsoft.Live.Messenger.Samples.ContactList.createClass('Microsoft.Live.Messenger.Samples.ContactList');
Microsoft.Live.Messenger.Samples.ContactList._signInControlID = 'pnlSignInControl';
Microsoft.Live.Messenger.Samples.ContactList._onlineContactsID = 'pnlOnlineContacts';
Microsoft.Live.Messenger.Samples.ContactList._offlineContactsID = 'pnlOfflineContacts';
Microsoft.Live.Messenger.Samples.ContactList._current = null;

// ---- Do not remove this footer ----
// Generated using Script# v0.5.1.0 (http://projects.nikhilk.net)
// -----------------------------------
