// Class "MessengerClient"
// Version 1.0 JS
// Created On 10/03/2007
// Modified On 10/03/2007
// Created by Thomas Pleasance(tom@cener.co.uk)
// *---------------------------------------------------------------
// *    Please do not use/modify this code without my permission.
// *---------------------------------------------------------------

function MessengerClient()
{
    this.cMessenger=null;
    this.Init=Init;
    
    this.MyStatus=MyStatus;
    this.MyStatusCode=MyStatusCode;
    this.MyFriendlyName=MyFriendlyName;
    this.MyDisplayPicture=MyDisplayPicture;
    this.OpenOptions=OpenOptions;
    this.ShowContactWindows=ShowContactWindows;
    this.SetMyStatus=SetMyStatus;
    this.GetOnlineContacts=GetOnlineContacts;
    this.GetOfflineContacts=GetOfflineContacts;
}
//Public
function Init(){
    this.cMessenger=null;
    try{
        this.cMessenger=document.createElement("object");
        this.cMessenger.classid="clsid:B69003B3-C55E-4B48-836C-BC5946FC3B28";
        document.appendChild(this.cMessenger);
        return true;
    }catch(e){
        return false;
    }
}
function MyStatus(){
    return getStatus(this.cMessenger.MyStatus);
}
function MyStatusCode(){
    return this.cMessenger.MyStatus;
}
function MyFriendlyName(){
    return this.cMessenger.MyFriendlyName;
}
function MyDisplayPicture(){
     var fileName = this.cMessenger.MyProperty(2);
     if(fileName=="")
        return "images/NoDP.png"
     else
        return fileName;
}
function SetMyStatus(StatusCode){
    this.cMessenger.MyStatus = StatusCode; 
}
function OpenOptions(){
    this.cMessenger.OptionsPages(0,0);
}
function ShowContactWindows(EmailAddress){
    this.cMessenger.InstantMessage(EmailAddress);
}
function GetOnlineContacts(){
var outputText="";
var myContact;
var myContacts=this.cMessenger.MyContacts;
    for(var i=0;i<myContacts.Count;i++){
        myContact=myContacts(i);
        if(myContact.Status != 1){
            if (myContact.Blocked)
                outputText = outputText + "<div class=\"MSNLine\" onmouseover=\"this.className='MSNLineOn';\" onmouseout=\"this.className='MSNLineOff'\" ondblclick=\"jShowContactWindows('"+myContact.SigninName+"')\"><img src=\"images\\Status/Online Blocked.png\" alt=\"Online Blocked\" align=\"absmiddle\" />"+ShortEn(myContact.FriendlyName)+"</div>";
            else
                outputText = outputText + "<div class=\"MSNLine\" onmouseover=\"this.className='MSNLineOn';\" onmouseout=\"this.className='MSNLineOff'\" ondblclick=\"jShowContactWindows('"+myContact.SigninName+"')\"><img src=\"images\\Status/"+getStatus(myContact.Status)+".png\" alt=\""+getStatus(myContact.Status)+"\" align=\"absmiddle\" />"+ShortEn(myContact.FriendlyName)+"</div>";
        }
    }
    return outputText;
}
function GetOfflineContacts(){
var outputText="";
var myContact;
var myContacts=this.cMessenger.MyContacts;
    for(var i=0;i<myContacts.Count;i++){
        myContact=myContacts(i);
        if(myContact.Status == 1){
            if (myContact.Blocked)
                outputText = outputText + "<div class=\"MSNLine\" onmouseover=\"this.className='MSNLineOn';\" onmouseout=\"this.className='MSNLineOff'\" ondblclick=\"jShowContactWindows('"+myContact.SigninName+"')\"><img src=\"images\\Status/Offline Blocked.png\" alt=\"Offline Blocked\" align=\"absmiddle\" />"+ShortEn(myContact.FriendlyName)+"</div>";
            else
                outputText = outputText + "<div class=\"MSNLine\" onmouseover=\"this.className='MSNLineOn';\" onmouseout=\"this.className='MSNLineOff'\" ondblclick=\"jShowContactWindows('"+myContact.SigninName+"')\"><img src=\"images\\Status/"+getStatus(myContact.Status)+".png\" alt=\""+getStatus(myContact.Status)+"\" align=\"absmiddle\" />"+ShortEn(myContact.FriendlyName)+"</div>";
        }
    }
    return outputText;
}

//Private
function getStatus(status){
    switch(status){
        case 1: return "Offline";
        case 2: return "Online";
        case 6: return "Invisible";
        case 10: return "Busy";
        case 14: return "Be Right Back";
        case 18: return "Idle";
        case 34: return "Away";
        case 50: return "On the Phone";
        case 66: return "Out to Lunch";
        default: return "Unknown";
    }
}
function ShortEn(Str){
    Str = new String(Str);
    if (Str.length > 20)
        return (Str.substring(0, 20) + "...");
    return Str;
}