﻿
/**
*
* @TODO: remove globals   gameURL 202,  objMessenger 98 119 142 163 185, popupHeight 206, popupWidth 205, screen 214, strApplicationID 94, strMessengerID 115 138 159 181
*
* @jslint          2009-01-28
*
*/

MSLM.messengerapi = {

    __version: 1.0, // class version
    __class: 'MSLM.liveapi', // class name

    // set default options
    _oDefaults: {
        elApplicationID: "#MSLM_application_id",
        elWindowsLiveID: "#MSLM_live_id",
        elApplication: ".MSLM_open_application",
        elAddContact: ".MSLM_add_contact",
        elStartChat: ".MSLM_start_chat",
        elStartVoiceChat: ".MSLM_start_voice_chat",
        elStartVideoChat: ".MSLM_start_video_chat",
        elGameDemo: ".MSLM_open_gamedemo"
    },

    /*** Setup */
    _setUp: function(oOptions) {
        var _scope = MSLM.messengerapi;
        // merge options and defaults
        _scope._oOpt = jQuery.extend({}, _scope._oDefaults, oOptions);
        _scope._oDefaults = null;

        // addEventHandler for Applications
        if (jQuery.browser.msie) {
            jQuery(_scope._oOpt.elApplication)
                .unbind('click.application', MSLM.messengerapi.openMessengerApplicationByID)
                .bind('click.application', { applicationID: MSLM.messengerapi._getApplicationID() }, MSLM.messengerapi.openMessengerApplicationByID);
        }

        // addEventHandler for AddingContacts
        jQuery(_scope._oOpt.elAddContact)
            .unbind('click.messengerContact', MSLM.messengerapi.addMessengerContactByID)
            .bind('click.messengerContact', { messengerID: MSLM.messengerapi._getMessengerID() }, MSLM.messengerapi.addMessengerContactByID);

        // addEventHandler for starting Chat
        jQuery(_scope._oOpt.elStartChat)
            .unbind('click.messengerChat', MSLM.messengerapi.addMessengerContactByID)
            .bind('click.messengerChat', { messengerID: MSLM.messengerapi._getMessengerID() }, MSLM.messengerapi.startChatByID);

        // addEventHandler for starting Voice Chat
        jQuery(_scope._oOpt.elStartVoiceChat)
            .unbind('click.messengerVoiceChat', MSLM.messengerapi.addMessengerContactByID)
            .bind('click.messengerVoiceChat', { messengerID: MSLM.messengerapi._getMessengerID() }, MSLM.messengerapi.startVoiceChatByID);

        // addEventHandler for starting Video Chat
        jQuery(_scope._oOpt.elStartVideoChat)
            .unbind('click.messengerVideoChat', MSLM.messengerapi.addMessengerContactByID)
            .bind('click.messengerVideoChat', { messengerID: MSLM.messengerapi._getMessengerID() }, MSLM.messengerapi.startVideoChatByID);

        // addEventHandler for GameDemos
        jQuery(_scope._oOpt.elGameDemo)
            .unbind('click.gameDemo', MSLM.messengerapi.openGameDemo)
            .bind('click.gameDemo', MSLM.messengerapi.openGameDemo);
    },


    _getApplicationID: function() {
        var _scope = MSLM.messengerapi;
        return (jQuery(_scope._oOpt.elApplicationID)).val();
    },

    _getMessengerID: function() {
        var _scope = MSLM.messengerapi;
        return (jQuery(_scope._oOpt.elWindowsLiveID)).val();
    },

    /* check support of ActiveXObject "MSNMessenger.P4QuickLaunch" */
    _getMSNMessengerObject: function() {
        try {
            var objectMSN = new ActiveXObject("MSNMessenger.P4QuickLaunch");
            return objectMSN;
        }
        catch (e) {
            if (jQuery.browser.msie) {
                MSLM.messengerapi._openMSNVersionLayer();
            }
            return null;
        }
    },

    _openMSNVersionLayer: function() {
        MSLM.modal.showByClass('.MSLM_modal_msnversion');
        return false;
    },

    /* LiveMessenger - OpenApplication (in >=IE6)  */
    openMessengerApplicationByID: function(pApplicationID) {

        if (typeof (pApplicationID) == 'object') {
            strApplicationID = pApplicationID.data.applicationID;
        }
        else { strApplicationID = pApplicationID; }

        objMessenger = MSLM.messengerapi._getMSNMessengerObject();
        if (objMessenger != null) {
            if (strApplicationID != "") {
                objMessenger.LaunchApp(strApplicationID, '');
            }
            else {
                objMessenger.LaunchApp('', '');
            }
        }
        return false;
    },


    // msnim:add - Allow your visitors to add you to their MSN contacts immediately. Example: <a href="msnim:add?contact=email@address.com ">Add me to your MSN Messenger contact list<
    addMessengerContactByID: function(pMessengerID) {

        if (typeof (pMessengerID) == 'object') {
            strMessengerID = pMessengerID.data.messengerID;
        }
        else { strMessengerID = pMessengerID; }

        objMessenger = MSLM.messengerapi._getMSNMessengerObject();

        if (objMessenger != null) {
            window.location = 'msnim:add?contact=' + strMessengerID;
        }
        else {
            // open Layer AddContact and replace [Windows Live ID] with current Messenger ID
            var modalLayerAddContact = jQuery('.MSLM_modal_addcontact p');
            modalLayerAddContact.html(modalLayerAddContact.html().replace(/\[(.*?)\]/g, '[' + strMessengerID + ']'));
            MSLM.modal.showByClass('.MSLM_modal_addcontact');
        }
        return false;
    },


    // msnim:chat - Clicking this link will open a conversation with email@address.com and allows instant chatting. Example: <a href="msnim:chat?contact=email@address.com ">Click here to chat</a>
    startChatByID: function(pMessengerID) {

        if (typeof (pMessengerID) == 'object') {
            strMessengerID = pMessengerID.data.messengerID;
        }
        else { strMessengerID = pMessengerID; }

        objMessenger = MSLM.messengerapi._getMSNMessengerObject();
        if (objMessenger != null) {
            window.location = 'msnim:chat?contact=' + strMessengerID;
        }
        else {
            // open Layer AddContact and replace [Windows Live ID] with current Messenger ID
            MSLM.modal.showByClass('.MSLM_modal_bodyscouts_chat');
        }
        return false;
    },

    // msnim:voice - Allow your visitors to start Voice chatting with you in just one click. Example: <a href="msnim:voice?contact=email@address.com ">Start a Voice chat with me</a>
    startVoiceChatByID: function(pMessengerID) {

        if (typeof (pMessengerID) == 'object') {
            strMessengerID = pMessengerID.data.messengerID;
        }
        else { strMessengerID = pMessengerID; }

        objMessenger = MSLM.messengerapi._getMSNMessengerObject();
        if (objMessenger != null) {
            window.location = 'msnim:voice?contact=' + strMessengerID;
        }
        else {
            // open Layer AddContact and replace [Windows Live ID] with current Messenger ID
            var modalLayerAddContact = jQuery('.MSLM_modal_addcontact p');
            modalLayerAddContact.html(modalLayerAddContact.html().replace(/\[(.*?)\]/g, '[' + strMessengerID + ']'));
            MSLM.modal.showByClass('.MSLM_modal_addcontact');
        }
        return false;
    },


    // msnim:video - Launches a Video conversation straight from a web page. Example: <a href="msnim:video?contact=email@address.com ">Start a Video chat with me</a>.
    startVideoChatByID: function(pMessengerID) {

        if (typeof (pMessengerID) == 'object') {
            strMessengerID = pMessengerID.data.messengerID;
        }
        else { strMessengerID = pMessengerID; }

        objMessenger = MSLM.messengerapi._getMSNMessengerObject();
        if (objMessenger != null) {
            window.location = 'msnim:video?contact=' + strMessengerID;
        }
        else {
            // open Layer AddContact and replace [Windows Live ID] with current Messenger ID
            var modalLayerAddContact = jQuery('.MSLM_modal_addcontact p');
            modalLayerAddContact.html(modalLayerAddContact.html().replace(/\[(.*?)\]/g, '[' + strMessengerID + ']'));
            MSLM.modal.showByClass('.MSLM_modal_addcontact');
        }
        return false;
    },


    /* Open Game Demo in PopUp Window */
    openGameDemo: function() {
        var _scope = MSLM.messengerapi;
        gameURL = jQuery(_scope._oOpt.elGameDemo).attr("href");

        // default popup size
        popupWidth = 450;
        popupHeight = 450;

        // overwrite popup size if defined in popupurl e.g. *.html#500,500
        if (gameURL.lastIndexOf("#") != -1) {
            popupWidth = gameURL.split("#")[1].split(",")[0];
            popupHeight = gameURL.split("#")[1].split(",")[1];
        }

        var popupWindow = window.open(gameURL, 'GameDemo', 'left = ' + ((screen.width / 2) - (popupWidth / 2)) + ', top = ' + ((screen.height / 2) - (popupHeight / 2)) + ', width= ' + popupWidth + ', height= ' + popupHeight + ', status=no, scrollbars=no, resizable=no, menubar=no, location=no');
        popupWindow.focus();
        return false;
    },

    /*** Get singleton instance  */
    getInstance: function(oOptions) {
        return MSLM.messengerapi.initialize(oOptions);
    },

    /*** Constructor   */
    initialize: function(oOptions) {
        // fake singleton
        if (this._oOpt) { return this; }
        // setup 
        this._setUp(oOptions);
        // fake singleton
        return this;
    }

};

// initialize onDOMReady
jQuery(function() {
    MSLM.messengerapi.initialize();
});