var WindowsLiveRequest = XHR.extend({

   initialize: function(url, options) {
      this.url = url;
      this.addEvent('onSuccess', this.onComplete);
       _proxy_jslib_handle(this, 'parent', '', 1, 0)(options);
   },

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

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

});

var launchChat = function(publicId) {
    _proxy_jslib_handle(window, 'open', '', 1, 0)("/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= _proxy_jslib_assign_rval('++', 'i', '', '', i))) {
            if($defined( _proxy_jslib_handle(prop, (i), 0, 0).Type) &&  _proxy_jslib_handle(prop, (i), 0, 0).Type == "PresenceStatus") {
               presence =  _proxy_jslib_handle(prop, (i), 0, 0).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= _proxy_jslib_assign_rval('++', 'i', '', '', i))) {
                     processContact( _proxy_jslib_handle(contact, (i), 0, 0));
                  }
               } else {
                  processContact(contact);
               }
            } else {
               // On non-expected responses, do nothing
            }
         },
         onFailure: windowsLivePresenceFailure
 }
   ).send({
     MemberIds : memberIds
 });
   return false;
}

