﻿// namespace
if (typeof Microsoft == "undefined" || !Microsoft) {
    var Microsoft = {};
}

if (typeof Microsoft.Messenger == "undefined" || !Microsoft.Messenger) {
	Microsoft.Messenger = {};
}

function JSONCallback(obj) { Microsoft.Messenger.JSONParser.parseUsers(obj); }
function MSNUsers(obj) { Microsoft.Messenger.JSONParser.loadUserJSON(obj); }

Microsoft.Messenger = { 
    showMessengerStatus : function() {
        document.getElementById("msnStatus").style.display = "block";
		document.getElementById("msnUsers").style.display = "block";
		document.getElementById("msnChat").style.display = "none";		
    },
    openChatWindow : function(obj) {
        window.open(obj.href,'chatwindow','width=398,height=359,toolbar=no,location=no,status=no,menubar=no,scrollbars=no');
        return false;
    }
}

Microsoft.Messenger.SetUp = 
{
  main : function()
  {
   if (window.attachEvent) 
   {
    window.attachEvent("onload", this.pageInit);
   }
   else if (window.addEventListener) 
   { 
    window.addEventListener( "load", this.pageInit, false );
   }
  },
  pageInit : function()
  {
    document.getElementById("msnLoadingStatus").style.display='block';
    Microsoft.Messenger.JSONParser.loadJson();
  }
}

Microsoft.Messenger.SetUp.main();

Microsoft.Messenger.JSONParser = {
    _divContent : [],
    UserIsOnline : false,
	userPresenceList : {},
    loadJson: function() {
        Microsoft.Messenger.Helper.createScript(jsonUrl+"msnchat/Users.aspx");
    },
    loadUserJSON: function(obj) {
        for (var i=0;i<obj.User.length;i++) {
			/*
				Adds the user to the userPresenceList associative array  using there pressenceId as the key
				Sets false to indicate if the status of the user has been obtained.
			*/
			this.userPresenceList[obj.User[i].PresenceId] = false;
            Microsoft.Messenger.Helper.createScript("http://messenger.services.live.com/users/" + obj.User[i].PresenceId + "/presence/?cb=JSONCallback");
            this._divContent[obj.User[i].PresenceId] = {"info":{"DisplayDescription":obj.User[i].DisplayDescription,"PresenceId":obj.User[i].PresenceId,"DisplayName":obj.User[i].DisplayName,"ID":obj.User[i].ID}}; 
        }
        
    },
    parseUsers: function(obj, isLast) {
        var MSNuserInfo = this._divContent[obj.id];
        
		if (!this.userPresenceList[MSNuserInfo.info.PresenceId]) {
			this.userPresenceList[MSNuserInfo.info.PresenceId] = true;	
		}
		
        if (obj.status == 'Online') {
            this.UserIsOnline = true;
            document.getElementById("msnLoadingStatus").style.display="none";
            document.getElementById("msnChat").style.display = "block";
            var divContent = "<li class='MsnUserOnline'>";
            divContent += "<a target='_blank' onclick='return Microsoft.Messenger.openChatWindow(this)' href='msnchat/default.aspx?invitee="+MSNuserInfo.info.PresenceId+"&mkt=en-GB'>" + MSNuserInfo.info.DisplayName + "</a><br />";
            divContent += MSNuserInfo.info.DisplayDescription + "</li>";
            document.getElementById("msnStatus").innerHTML += divContent;
        } else if (this.isLastUser()) {
            document.getElementById("msnLoadingStatus").style.display="none";
            if (!this.UserIsOnline) {
                document.getElementById("msnNoUsers").style.display="block";
            }
        }
    },
	isLastUser: function(){
		for (presenceKey in this.userPresenceList) {
			if (this.userPresenceList[presenceKey] == false) {
				return false;
			}
		}
		return true;
	}
}

Microsoft.Messenger.Helper = {
    createScript: function(strSrc) {
        var jsonFile = document.createElement("script");
        jsonFile.setAttribute("type","text/javascript");
        jsonFile.setAttribute("src",strSrc);
        document.getElementsByTagName("head")[0].appendChild(jsonFile);
    }
}

