msepAPI.prototype.AjaxUserInformer = new Class(
{
      Implements: Options,

      options: {
            base: "",
            baseRaw: "",
            users: [],
            user_info: [],
            html: "",
            links: [],
            new_links: false,
            second_check: 0,
            radius: 10,
            radius_ie: 0,
            border: 1,
            borderColor: "#CCC",
            backgroundColor: "#FFF",
            photo: 1
      },

      initialize: function(options)
      {
            this.setOptions(options);

            this.html = $(document.body).innerHTML;

            this.options.radius_ie = this.options.radius*0.006;
      },

      getUsers: function()
      {
            var self = this;

            if (self.options.second_check == 0 || (self.options.second_check > 0 && self.options.html != $(document.body).innerHTML))
            {
                  self.options.second_check += 1;

                  self.options.html = $(document.body).innerHTML;

                  getElementsByAttribute(getElementsByClassName('content')[0], 'a', 'href', new RegExp("("+self.options.base+"([^\/]+)|profile\.php\\?user\=(.*?))")).each(function(el)
                  {
                        var url = el.getAttribute('href');

                        if (url_match = url.match(/profile\.php\?user\=([a-zA-Z0-9]+)/))
                        {
                              if (!self.options.users.in_array(url_match[1]))
                              {
                                    self.options.new_links = true;
                                    self.options.users.splice(0, 0, url_match[1]);
                              }
                        }
                        else if (url_match = url.match(new RegExp(self.options.base+"([^\/]+)")))
                        {
                              if (!self.options.users.in_array(url_match[1]))
                              {
                                    self.options.new_links = true;
                                    self.options.users.splice(0, 0, url_match[1]);
                              }
                        }
                  });

                  if (self.options.second_check == 0 || (self.options.second_check > 0 && self.options.new_links))
                  {
                        self.options.new_links = false;

                        var users_validate = new Request.JSON(
                        {
                              url: 'ajaxui_json.php',
                              method: 'post',
                              data: { 'task': 'ajaxui_users_validate', 'ajaxui_users': self.options.users.toString() },
                              onComplete: function(jsonObj)
                              {
                                    jsonObj.ajaxui_users.each(function(obj)
                                    {
                                          self.options.user_info[obj['user_username']] = obj;
                                    });

                                    (function()
                                    {
                                          getElementsByAttribute(getElementsByClassName('content')[0], 'a', 'href', new RegExp("("+self.options.base+"([^\/]+)|profile\.php\\?user\=(.*?))")).each(function(el)
                                          {
                                                var url = el.getAttribute('href');

                                                if (url_match = url.match(/profile\.php\?user\=([a-zA-Z0-9]+)/))
                                                {

                                                      self.addTips(el, url_match[1]);
                                                }
                                                else if (url_match = url.match(new RegExp(self.options.base+"([^\/]+)")))
                                                {
                                                      self.addTips(el, url_match[1]);
                                                }
                                          });
                                    })();
                              }
                        }).send();
                  }

                  (function()
                  {
                        getElementsByAttribute(getElementsByClassName('content')[0], 'a', 'href', new RegExp("("+self.options.base+"([^\/]+)|profile\.php\\?user\=(.*?))")).each(function(el)
                        {
                              var url = el.getAttribute('href');

                              if (url_match = url.match(/profile\.php\?user\=([a-zA-Z0-9]+)/))
                              {
                                    self.addTips(el, url_match[1]);
                              }
                              else if (url_match = url.match(new RegExp(self.options.base+"([^\/]+)")))
                              {
                                    self.addTips(el, url_match[1]);
                              }
                        });
                  })();
            }

            this.getUsers.delay(100, this);
      },

      addTips: function(el, match)
      {
            var self = this;

            if (self.options.user_info[match])
            {
                  if (el.getAttribute('alt') != 'ajaxui' && el.firstChild && ((!el.firstChild.tagName) || (el.firstChild.tagName && !el.firstChild.tagName.match(/img/i))))
                  {
                        var padding_bottom = "0px";
                        if (self.options.user_info[match]['user_tip'] != "") var padding_bottom = "6px";

                        var photo = '<div style="padding-bottom: '+padding_bottom+'"><img src="'+self.options.user_info[match]['user_photo']+'" border="0" width="150" /></div>';
                        if (self.options.user_info[match]['user_photo'] == "" || self.options.photo == 0) var photo = "";

                        if (navigator.userAgent.toLowerCase().indexOf("msie") != -1 && self.options.radius > 0)
                        {
                              var border = ((self.options.border == 0) ? "false" : "true");
                              var html = '<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" /><v:roundrect stroked="'+border+'" strokecolor="'+self.options.borderColor+'" strokeweight="'+self.options.border+'px" arcsize="'+this.options.radius_ie+'" fillcolor="'+self.options.backgroundColor+'" style="behavior:url(#default#VML); background-color: transparent; position: relative; display: block; -kself.options.html-border-radius: '+this.options.radius+'px; -moz-border-radius: '+this.options.radius+'px; -webkit-border-radius: '+this.options.radius+'px; border-radius: '+this.options.radius+'px; height: 100%; padding: 8px 10px 10px 10px; _padding-right: 7px;">'+photo+'<div style="width: 150px;">'+self.options.user_info[match]['user_tip']+'</div><div style="clear: both; line-height: 0;"></div></v>';
                        }
                        else if (navigator.userAgent.toLowerCase().indexOf("opera") != -1 && self.options.radius > 0)
                        {
                              var html = '<div style="background-image: url('+self.options.baseRaw+'include/msep/includes/corners.php?radius='+this.options.radius+'&background='+self.options.backgroundColor.replace("#", "")+'&border='+self.options.border+'&bordercolor='+self.options.borderColor.replace("#", "")+'); background-color: transparent; position: relative; display: block; height: 100%; padding: 8px 10px 10px 10px; _padding-right: 7px;">'+photo+'<div style="width: 150px;">'+self.options.user_info[match]['user_tip']+'</div><div style="clear: both; line-height: 0;"></div></div>';
                        }
                        else
                        {
                              var html = '<div style="border: '+self.options.border+'px solid '+self.options.borderColor+'; background-color: '+self.options.backgroundColor+'; position: relative; display: block; -kself.options.html-border-radius: '+this.options.radius+'px; -moz-border-radius: '+this.options.radius+'px; -webkit-border-radius: '+this.options.radius+'px; border-radius: '+this.options.radius+'px; height: 100%; padding: 8px 10px 10px 10px;">'+photo+'<div style="width: 150px;">'+self.options.user_info[match]['user_tip']+'</div><div style="clear: both; line-height: 0;"></div></div>';
                        }

                        el.setAttribute('title', html);
                        el.setAttribute('alt', 'ajaxui');

                        var userTip = new Tips(
                        {
                              fixed: true,
                              className: 'ajaxui-user-info',
                              initialize:function()
                              {
                                    this.tip.fade('hide');
                              },
                              onShow: function(toolTip)
                              {
                                    this.tip.fade('in');
                              },
                              onHide: function(toolTip)
                              {
                                    this.tip.fade('out');
                              }
                        }).attach(el);
                  }
            }
      }
});