﻿// References for IntelliSense support in Visual Studio 2008
/// <reference path=".\IntelliSense\sscorlib.js" />
/// <reference path=".\IntelliSense\Microsoft.Live.Messenger.documentation.js" />
  
var user = null;
var hostName = null;
var signInControl = null;
var signInControlVisible = true;

// Initializes the SignInControl and hooks up SignInControl events
function onLoad()
{
    var hostUrl = window.location.href;
    var index = hostUrl.lastIndexOf("/");
    hostUrl = hostUrl.substring(0, index);
    
    var channelUrl = hostUrl + "/Channel.html";
    var privacyUrl = hostUrl + "/Privacy.html";

    // Create the sign in control and associate it with the DIV in the main page.
    signInControl = new Microsoft.Live.Messenger.UI.SignInControl(
        "SignInControlDiv", 
        privacyUrl,
        channelUrl,        
        "en-us");             
            
    // Attach to the AuthenticationCompleted event which will get raised once the user has authenticated.
    signInControl.add_authenticationCompleted(onAuthenticationCompleted);
}

// Called once the user has provided their Windows Live credentials and authenticated
function onAuthenticationCompleted(sender, e)
{
    var continueSignIn = true;
    
    if (e.get_status() & Microsoft.Live.Core.AuthenticationStatus.unsupportedBrowser)
    {
        continueSignIn = confirm("Your browser is not supported. Do you want to continue anyway?");        
    }

    if (!continueSignIn)
    {
        log("User chose not to continue sign-in.");
        return;
    }

    user = new Microsoft.Live.Messenger.User(e.get_identity());
    
    // Attach to the SignInCompleted event that will be raised once Sign-in completes. 
    user.add_signInCompleted(onSignInCompleted);
    
    user.signIn(null);    
}

// Called once the user has signed into the Messenger service.
function onSignInCompleted(sender, e)
{
    if (e.get_resultCode() == Microsoft.Live.Messenger.SignInResultCode.success)
    {
        log("Sign-in Succeeded.");
        
        if (shouldHideSignInControl)
        {
            showSignInControlLinks(signInControl);
        }
        
        // Hide the Sign-in control after sign in has succeeded.
        if (shouldHideSignInControl)
        {
            signInControl.hide();
            signInControlVisible = false;
        }        
    }
    else if (e.get_resultCode() == Microsoft.Live.Messenger.SignInResultCode.failure)
    {
        log("ERROR: Sign-in failed.");
    }        
}

// Provides logging functionality within the loaded HTML page.
function log(logEntry)
{
    var statusDiv = document.getElementById("StatusDiv");
    
    statusDiv.innerHTML += "<BR>" + logEntry;
}

// Shows the SignInControlLinks at the top of the page.
function showSignInControlLinks(signInControl)
{
    var signInControlLinksDiv = document.getElementById("SignInControlLinksDiv");
    
    if (signInControlLinksDiv)
    {
        signInControlLinksDiv.innerHTML = "";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_aboutMessengerUrl() + "\">About Messenger</a>  ";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_changeAccountUrl(); + "\">Change Account</a>  ";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_privacyUrl() + "\">Privacy</a>  ";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_reportAbuseUrl() + "\">Report Abuse</a>  ";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_settingsUrl() + "\">Settings</a>  ";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_signUpUrl() + "\">Sign Up</a>  ";
        signInControlLinksDiv.innerHTML += "<a target=\"_blank\" href=\"" + signInControl.get_links().get_termsOfUseUrl() + "\">Terms of Use</a>";     
    }
}

function toggleSignInControlVisible()
{
    if (signInControlVisible)
    {
        signInControl.hide();
        signInControlVisible = false;
    }
    else
    {
        signInControl.show();
        signInControlVisible = true;
    }
}