// 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.
// *---------------------------------------------------------------

var myMessenger;

// OnLoad
/*--------------------------------------------------------*/
window.onload = function(){
    myMessenger = new MessengerClient();
    myMessenger.Init();
    
    //Setup Settings
    System.Gadget.settingsUI = "Settings.html";
    System.Gadget.onSettingsClosed = SettingsClosed;
    
    var textObject = background.addTextObject("Loading", "Segoe UI", 13, "White", 5, 13); 
    textObject.addGlow( "gray", 2, 75 );
    
    textObject = background.addTextObject("Please Wait", "Segoe UI", 13, "White", 5, 30); 
    textObject.addGlow( "gray", 2, 75 );
    
    buildGadget();
}
/*--------------------------------------------------------*/

function SettingsClosed(){
	var gOpacitySetting = System.Gadget.Settings.read("gOpacity");
	if (gOpacitySetting != "")
		background.opacity = gOpacitySetting;
}
function buildGadget(){
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    var imageDPPath = new String(myMessenger.MyDisplayPicture());
    if(imageDPPath != "images/DP.png"){
        fso.CopyFile(imageDPPath, System.Gadget.path + "\\Images\\MSNDP.png");
        imageDPPath= System.Gadget.path + "\\Images\\MSNDP.png";
    }

    //System.Gadget.beginTransition();	
    background.removeObjects();
    
    var MSNImage = background.addImageObject(imageDPPath, 0, 0);
    if(imageDPPath == "images/NoDP.png"){
        MSNImage.left = -1;
	    MSNImage.top = 10;
    }else{
	    MSNImage.left = -25;
	    MSNImage.top = -15;
    }
    MSNImage.width = 30;
    MSNImage.height = 30;
    var contactBox = background.addImageObject("images/ContactBox.png", 4, 14);

    //User Status    
    var textObject = background.addTextObject(myMessenger.MyStatus(), "Segoe UI", 13, "White", 43, 15); 
    textObject.addGlow( "gray", 2, 75 );
    
    //User Display Name
    textObject = background.addTextObject(myMessenger.MyFriendlyName(), "Segoe UI", 13, "White", 43, 30); 
    textObject.addGlow( "gray", 2, 75 );
    //System.Gadget.endTransition( System.Gadget.TransitionType.morph, 1.0 );
    gTimeout = setTimeout(buildGadget, 2500);

}
function setFlyout(){
    System.Gadget.Flyout.file = "flyout.html";
    System.Gadget.Flyout.show = true;
    System.Gadget.Flyout.onShow = function(){
        addContentToFlyout();
    }
}
function addContentToFlyout(){
    try{
        if(System.Gadget.Flyout.show){
            var flyoutDoc =  System.Gadget.Flyout.document;
            var sOfflineContacts = System.Gadget.Settings.read("OfflineContacts");
            if(sOfflineContacts ==true)
                flyoutDoc.getElementById("MSNContacts").innerHTML = myMessenger.GetOnlineContacts()+myMessenger.GetOfflineContacts();
            else
	            flyoutDoc.getElementById("MSNContacts").innerHTML = myMessenger.GetOnlineContacts();
        }
    }catch(e){
        //catch slow flyout - no div object will be available.
    }
}
