/*************************************************************
/* Messenger Presence Script
/* Matthew Cosier
/* Wed, Feb 7th, 2007
/*
/* Simply link to this script in your HTML document / ASP.NET page
/* This will inject presense support into the current content page
/* All you need to do to integrate with the presence awareness
/* is to make a call to LinkToInstantMessage, or GetOnlineStatusImage
/* You can also reference myStatus variable, or myContacts variable within your other scripts on the containing page
/* You can also determine if the presence ability is supported by checking the isSupported variable for a value of 1
/**************************************************************/

//document.write("<OBJECT classid=clsid:B69003B3-C55E-4B48-836C-BC5946FC3B28 codeType=application/x-oleobject id=IMessenger width=0 height=0></OBJECT>");

var isSupported = 1;
var IMessenger;
try {
    IMessenger = new ActiveXObject("Messenger.UIAutomation.1");
    var email = IMessenger.MyContacts.Item(0).Property(1);
    isSupported = 1;
}
catch (ex)
{
  isSupported = 0;
}

var myStatus;
var myContacts;
var numContacts;

if (isSupported == 1)
{
    // if we are supported, grab all of the data...
    myStatus = IMessenger.MyStatus;
    myContacts = IMessenger.MyContacts;
    numContacts = myContacts.Count;
}

function findUserByEmail(emailAddress)
{
  for(var i = 0; i < myContacts.Count; i++)
  {
     if(myContacts.Item(i).Property(1) == emailAddress)
        return myContacts.Item(i);
  }
  
  return null;
}

function onlineStatusByEmail(email) {

    var u = findUserByEmail(email);
    if(u == null)
    {
        //alert('Sorry, could not find that user!');
       return;
    }

    switch (u.Status) {
        case 0x0000: return "unknown";
        case 0x0001: return "offline";
        case 0x0002: return "online";
        case 0x0006: return "invisible";
        case 0x000A: return "busy";
        case 0x000E: return "brb";
        case 0x0012: return "idle";
        case 0x0022: return "away";
        case 0x0032: return "phone";
        case 0x0042: return "lunch";
        default: return "failure";
    }
}

function onlineStatusByUser(user) {

    if(user == null)
    {
      return "unknown";
    }
    
    switch (user.Status) {
        case 0x0000: return "unknown";
        case 0x0001: return "offline";
        case 0x0002: return "online";
        case 0x0006: return "invisible";
        case 0x000A: return "busy";
        case 0x000E: return "brb";
        case 0x0012: return "idle";
        case 0x0022: return "away";
        case 0x0032: return "phone";
        case 0x0042: return "lunch";
        default: return "failure";
    }
}

function GetImageByStatus(status)
{
    switch (status)
    {
        case 0x0000: return "Images/o_imnhdr.gif";
        case 0x0001: return "Images/o_imnoff.gif";
        case 0x0002: return "Images/o_imnon.gif";
        case 0x0006: return "Images/o_imnhdr.gif";
        case 0x000A: return "Images/o_imnbusy.gif";
        case 0x000E: return "Images/o_imnaway.gif";
        case 0x0012: return "Images/o_imnaway.gif";
        case 0x0022: return "Images/o_imnaway.gif";
        case 0x0032: return "Images/o_imnaway.gif";
        case 0x0042: return "Images/o_imnaway.gif";
    }    
}

function GetImageByStatusByUser(user)
{
    if (user == null)
    {
      return "Images/o_imnhdr.gif";
    }
    
    switch(user.Status)
    {
        case 0x0000: return "Images/o_imnhdr.gif";
        case 0x0001: return "Images/o_imnoff.gif";
        case 0x0002: return "Images/o_imnon.gif";
        case 0x0006: return "Images/o_imnhdr.gif";
        case 0x000A: return "Images/o_imnbusy.gif";
        case 0x000E: return "Images/o_imnaway.gif";
        case 0x0012: return "Images/o_imnaway.gif";
        case 0x0022: return "Images/o_imnaway.gif";
        case 0x0032: return "Images/o_imnaway.gif";
        case 0x0042: return "Images/o_imnaway.gif";
    }    
}

function GetEmailFromUser(user)
{
    return user.Property(1);
}

function SendInstantMessage(email)
{
    var user = findUserByEmail(email);
    if (user == null)
    {
        alert('Sorry, but you cant send an instant message to this user!');
    }
    else
    {
        IMessenger.InstantMessage(user);
    }
}

