// Contact picker vendor site source code.
// This code should be served from the vendor website so that it can be called
// by script in the vendor's web pages.


//don't bother loading Atlas just for registerNamespace
//registerNamespace("Microsoft.Gadget");
if (!window.Microsoft) {window.Microsoft={};}
if (!window.Microsoft.Gadget) {window.Microsoft.Gadget={};}

Microsoft.Gadget.ContactSelectorPartner = function(p_el, 
                                           privacyPolicyURL,
                                           dataDesired,
                                           resultsCallback_,
                                           channelEndpointURL)
{

    var self = this;
    self.resultsCallback = resultsCallback_;
    
    this.msgHandler = function (srcname, srcurl, msg)
    {
      var args = msg.split(",");
      if (!args) return;
      var cmd = decodeURIComponent(args[0]);
      if (cmd == "DATA")
      {
        var contacts = [];
        for (var i = 1; i < args.length; i++)
        {
          props = args[i].split("&");
          if (props && (props.length > 0) && (props[0].length > 0))
          {
            var item = new Object();
            for (var j = 0; j < props.length; j++)
            {
              var pair = props[j].split("=");
              item[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
            }
            contacts.push(item);
          }
        }
        if (self.resultsCallback)
          self.resultsCallback(cmd, contacts);
      }
      else
      {
        if (self.resultsCallback)
          self.resultsCallback(cmd);
      }
    };

    if (!p_el.id)
    {
      throw Error("Host iframe needs an id");
    }
        
    var frm = document.createElement("iframe");
    frm.id = Microsoft.Internal.uniqueID(p_el.id);
    frm.name = frm.id;
    frm.src = "http://dev.live.com/scripts/AddressManager/ContactSelector.html" +
//        "http://127.0.0.1/live/ContactSelector.html" + 
        "#policyUrl="+encodeURIComponent(privacyPolicyURL) +
        "&columns="+encodeURIComponent(dataDesired) +
        "&contextID="+encodeURIComponent(frm.id) +
        "&domain="+encodeURIComponent(window.location.hostname) + 
        "&channelEndpoint="+encodeURIComponent(channelEndpointURL);
     frm.frameBorder = "0";
     frm.scrolling = "no";
     frm.style.width = "100%";
     frm.style.height = "100%";
     frm.style.border = "none";
     p_el.appendChild(frm);
    
  // construct unique iframe in our domain to act as msg conduit
    this.port = document.createElement("div");
    this.port.id = Microsoft.Internal.uniqueID("hport");
    this.port.style.width = "0px";
    this.port.style.height = "0px";
    this.port.style.visibility = "hidden";
    this.port.style.display = "none";
    p_el.appendChild(this.port);
    
    Microsoft.Internal.ChannelMux.register_iframe(this.port.id);

    Microsoft.Internal.ChannelMux.register_recver(frm.id, 
      "http://dev.live.com/scripts/AddressManager/channel.htm",
//      "http://127.0.0.1/live/channel.htm", 
      this.msgHandler);    
};


