var WindowsLiveRequest = new Class({
   Extends: Request.JSON,
   
   initialize: function(url, options) {
      this.url = url;
      this.addEvent('onSuccess', this.onComplete);
      this.parent(options);
   },

   send: function(obj) {
      var queryString;
      if($defined(obj)) {
         switch($type(obj)) {
            case 'string': queryString = obj; break;
            case 'object': queryString = $H(Object).toQueryString(obj); break;
            default : queryString = 'json=' +JSON.decode(obj); break;
         }
      }  
      return this.parent(this.url, queryString);
   },

   onComplete: function() {
      this.fireEvent('onComplete', [JSON.decode(this.response.text, this.options.secure)]);
   }

});

var launchChat = function(publicId) {
   window.open("/c/windows_live/chat_box?publicId=" +publicId,'_blank','width=400,height=350,scrollbars=no');
}

var getWindowsLivePresence = function(memberIds, chatLinkText, addContactLinkText, msnScreenName) {
   var windowsLivePresenceFailure = function() {
      // does nothing for now
   };
   
   var processContact = function(contact) {
      var presence = null;
      var prop = contact.Presence.Properties.Property;
      
      // finds the Property representing PresenceStatus
      if(!$defined(prop.length)) {
         presence = prop.Value;
      } else {
         for(var i = 0; i < prop.length; ++i) {
            if($defined(prop[i].Type) && prop[i].Type == "PresenceStatus") {
               presence = prop[i].Value;
               break;
            }
         }
      }
      
      var publicId = contact.PublicID;
      
      var containerElement = $('windowslive-' +publicId);
      if($defined(containerElement)) {
         containerElement.empty();
         
         var spandex = new Element('span');
         
         // 'Chat' link
	      var chatLink = new Element('a', {
	         href : "#",
	         events : {
	            click : function() {
	               launchChat(publicId);
	            }
	         }  
	      });
	      chatLink.appendText(chatLinkText);	      
	      
         var imgStyle = "width: 16px; height: 16px; margin-right: 4px;";
         
	      if(presence == "Online") {
            containerElement.appendChild(new Element('img', { src : "http://s.bebo.com/img/windowslive/Status_Online.gif", alt : presence, style : imgStyle })); 
	         spandex.appendText(presence);
	         spandex.appendText(" ");
//            spandex.appendChild(chatLink);
//            if($defined(msnScreenName) && window.ie) {
//               spandex.appendText(" |");
//            }
	      } else if(presence == "Away" || presence == "Idle" || presence == "BeRightBack" || presence == "OutToLunch") {
 	         containerElement.appendChild(new Element('img', { src : "http://s.bebo.com/img/windowslive/Status_Away.gif", alt : presence, style : imgStyle })); 
	         spandex.appendText(presence);
	         spandex.appendText(" ");
//            spandex.appendChild(chatLink);
//            if($defined(msnScreenName) && window.ie) {
//               spandex.appendText(" |");
//            }
	      } else if(presence == "Busy" || presence == "OnThePhone") {
            containerElement.appendChild(new Element('img', { src : "http://s.bebo.com/img/windowslive/Status_Busy.gif", alt : presence, style : imgStyle })); 
	         spandex.appendText(presence);
	         spandex.appendText(" ");
//            spandex.appendChild(chatLink);
//            if($defined(msnScreenName) && window.ie) {
//               spandex.appendText(" |");
//            }
	      } else /* if(presence == "Hidden" || presence == "Offline") */ {
            containerElement.appendChild(new Element('img', { src : "http://s.bebo.com/img/windowslive/Status_Offline.gif", alt : "Offline", style : imgStyle }));
	         spandex.appendText(presence);
	      }
	      
	      containerElement.appendChild(spandex);
	      
	      if($defined(msnScreenName) && window.ie) {
	         // 'Add Contact' link
	         var addContactLink = new Element('a', {
	            href : "msnim:add?contact=" +msnScreenName
	         });
	         addContactLink.appendText(addContactLinkText);
	         spandex.appendText(" ");
	         spandex.appendChild(addContactLink);
	      }
	      
      }
   }
   
   var jsonRequest = new WindowsLiveRequest("/c/windows_live/get_presence", 
      {
         method: "get",
         secure: true, // parses results as plain old json (no javascript injection allowed)
         onComplete: function(ret) {
            if($defined(ret) && $defined(ret.GetPresenceResponse)) {
               var contact = ret.GetPresenceResponse.Contacts.Contact;
               if($defined(contact.length)) {
                  for(var i = 0; i < contact.length; ++i) {
                     processContact(contact[i]);
                  }
               } else {
                  processContact(contact);
               }
            } else {
               // On non-expected responses, do nothing
            }
         },
         onFailure: windowsLivePresenceFailure
      }
   ).send({
     MemberIds  : memberIds
   });
   return false;
}


