var connectorName = "CenerTCPMessenger.exe";
var connectorPort = "2181";


var OnlineContacts = "";
var OfflineContacts = "";




// OnLoad
/*--------------------------------------------------------*/
window.onload = function(){
	//System.Gadget.visibilityChanged = checkVisibility;	
	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 xmlhttp = new XMLHttpRequest();

    /* Get Data */
    /*----------------------------------------------------------------------------*/
    xmlhttp.open("GET", "http://localhost:"+connectorPort +"/GetAllData",true);

    xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) 
	if(xmlhttp.status == 200) {


    System.Gadget.beginTransition();	
    background.removeObjects();

var UsersDP;
var UsersStatus;
var UsersName;
var rXML = xmlhttp.responseXML;
  

if(xmlhttp.responseText != "0" && rXML.xml !="")
{   
    UsersDP = rXML.getElementsByTagName('DisplayPicturePath')[0].text;
    UsersStatus = rXML.getElementsByTagName('UserStatus')[0].text;

    OnlineContacts = rXML.getElementsByTagName('OnlineContacts')[0].text;
    OfflineContacts = rXML.getElementsByTagName('OfflineContacts')[0].text;
    UsersName = new String(rXML.getElementsByTagName('DisplayName')[0].text);
}
else
{
    UsersDP = "images/NoDP.png";
    UsersStatus = "Offline";

    OnlineContacts = "";
    OfflineContacts = "";
    UsersName = new String("");

}
	if(UsersName.length > 10)
	   UsersName = UsersName.substring(0,9) +"...";



    var MSNImage = background.addImageObject(UsersDP, 0, 0);
    if(UsersDP == "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", 0, 0);
    contactBox.left = 4;
    contactBox.top = 14;

    var textObject = background.addTextObject(UsersStatus, "Segoe UI", 13, "White", 43, 15); 
    textObject.addGlow( "gray", 2, 75 );
    textObject = background.addTextObject(UsersName, "Segoe UI", 13, "White", 43, 30); 
    textObject.addGlow( "gray", 2, 75 );
    System.Gadget.endTransition( System.Gadget.TransitionType.morph, 1.0 );


        }
	else
	{
		System.Shell.execute(System.Gadget.path +"\\Connector\\CenerTCPMessenger.exe");
	}
    }
    xmlhttp.send(null);
    /*----------------------------------------------------------------------------*/
    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 = OnlineContacts+OfflineContacts;

	    else
		flyoutDoc.getElementById("MSNContacts").innerHTML = OnlineContacts;

	    
        }
    }
    catch(e)
    {
        //catch slow flyout - no div object will be available.
    }
}
