(function(){

  function undef(param) { return param; }
  /*
        Person -- это data wrapper over data received from DataRequest
  
        Структура данных Person:
        {
          "@create" : "Social.Person",
          id : 25346679,
          title : "Копатырь Щ",
          links : {
            alternate : "http://kopatblrb.ya.ru",
            self : "{api-host}/person/25346679",
            userpic : "http://upics.yandex.net/get/kopatblrb/normal"
            },
          content : {
            accountStatus : "normal",
            gender : "unknown",
            mood : "also unknown to me",
            qu : 10,
            levelupDay : true,
            birthday : { year:1979, month:12, day:25 }
          }
        }
  */
  
  Social.Person = function( data ) 
  {
      this._data = data;
      if (data.id == 0) return;
      this._data._userpicUrl = data.links["userpic"].replace(/normal(-[0-9a-z]+)$/, "@$1").replace(/size=normal$/, "size=@");
      this._data._userpicUrl = this._data._userpicUrl.replace(/(upics\.yandex\.net.+)\/([a-z]+)$/, "$1/@");
      if (data.content.gender == "man") data.content.gender="male";
      if (data.content.gender == "woman") data.content.gender="female";
      this._data.content.genderInt = (data.content.gender=="male")?0:(data.content.gender=="female")?1:2;
  }
  Social.Person.prototype = {
      // FTGG
      getUrl : function()
      {
          return this._data.links["alternate"];
      },
      getUserpicUrl : function( size )
      {
          if (size == undef()) size = "normal";
          return this._data._userpicUrl.replace("@", size);
      },
      getLink : function()
      {
          return "<a href='" + this.getUrl() + "'>" + this.getDisplayName() + "</a>";
      },
      compactify : function()
      {
          return { 
            id: this.getId(), 
            title: this.getDisplayName(), 
            userpic: this.getUserpicUrl("small"),
            userpics : {    "small" : this.getUserpicUrl("small"), 
                            "middle" : this.getUserpicUrl("middle"), 
                            "normal" : this.getUserpicUrl("normal") },
            href: this.getUrl(), 
            gender: this.getField("genderInt") };
      },
      // GOS compatibility
      getField : function(key)
      {
          return this._data.content[key];
      },
      getDisplayName : function()
      {
          return this._data.title;
      },
      getId : function()
      {
          return this._data.id;
      },
      isGuest : function()
      {
          return this._data.content.accountStatus == "guest";
      },
      isOwner : function(widget) 
      {
          return this.id == widget.getOwner();
      },
      isViewer : function(widget)
      {
          return this.id == widget.getViewer();
      }
  };
  
  Social.Person.Dummy = function() {}
  Social.Person.Dummy.prototype = new Social.Person({ 
      "@create" : "Social.Person",
      id : 0,
      title : "Гость",
      links : { alternate : "", self : "", userpic : "" },
      content : { gender : "it", accountStatus : "guest" }
      });
  
  Social.Person.OWNER  = new Social.Person.Dummy();
  Social.Person.VIEWER = new Social.Person.Dummy();
  Social.Person.OWNER_VIEWER_STATE = "dummy";
  Social.Person.CacheOwnerViewer = function(widget, callback)
  {
      if (Social.Person.OWNER_VIEWER_STATE == "cached")
          return callback();
      y5.Events.observe('social:Social.Person.CacheOwnerViewer', callback, y5, true);
      if (Social.Person.OWNER_VIEWER_STATE == "progress")
          return;
      Social.Person.OWNER_VIEWER_STATE = "progress";
      with (new Social.DataRequest(widget.getAuth()))
      {
          add( Get.Person(widget.getViewer()), "viewer" );
          add( Get.Person(widget.getOwner()), "owner" );
          send( function(results,status){
              Social.Person.OWNER = results["owner"];
              Social.Person.VIEWER = results["viewer"];
              var ii = ["OWNER","VIEWER"];
              for(var i=0; i<2; i++)
                if (Social.Person[ii[i]].getId() == 0) Social.Person[ii[i]] = Social.Person.Dummy();
              Social.Person.OWNER_VIEWER_STATE = "cached";
              y5.Events.notify('social:Social.Person.CacheOwnerViewer',y5,true);
          });
     }
  }
  
  Social.Person.SourceToPerson = function(source)
  {
      return new Social.Person({
          "@create" : "Social.Person",
          id : source.id,
          title : source.title,
          links : {
              alternate : "http://"+source.login+".ya.ru",
              self : "{api-host}/person/"+source.id,
              userpic : "http://upics.yandex.net/get/"+source.login+"/normal"
          },
          content : {
              accountStatus : source.hasActiveJournal?"normal":"guest",
              gender : source.sex,
              qu : source.qu,
              levelupDay : false
          }
      });
  }
  Social.Person.ContainerOwnerViewer = function()
  {
      try { if (g_globals.current_location.source && g_source) ; }
      catch(e) {}
      finally
      {
          Social.Person.OWNER_VIEWER_STATE = "progress";
          if (g_globals.current_location.source)
              Social.Person.OWNER  = Social.Person.SourceToPerson(g_globals.current_location.source);
          if (g_source)
            Social.Person.VIEWER = Social.Person.SourceToPerson(g_source);
          Social.Person.OWNER_VIEWER_STATE = "cached";
          return true;
      }
      return false;
  }
  y5.Events.observe("y5:SourcesAreReady", Social.Person.ContainerOwnerViewer, y5, true);
 

})();
