﻿
var FriendPathController = {
     ExtractToArray : function (liststring)
    {  
        retVal = {};
        if (liststring != "" && liststring.indexOf (",") > 0)
            retVal = this.oc (liststring.split (","));
        return retVal;
    },

    oc : function (a)
    {
      var o = {};
      for(var i=0;i<a.length;i++)
      {
        o[a[i]]='';
      }
      return o;
    },

    BuildUserToUserPath : function (fromname, toname, strUserListFrom, strUserListTo)
    {
        var userlistfromArray = this.ExtractToArray (strUserListFrom);
        var userlisttoArray = this.ExtractToArray (strUserListTo);
        var match = ["",""];
        var count = 0;
        var isyourfiend = false;
        if (toname != fromname)
        {
            if (toname in userlistfromArray)
            {
                isyourfiend = true;
            }
            if (!isyourfiend)
            {
                for (x in userlistfromArray)
                {
                    if (String (x) != "")
                    {
                        if (x in userlisttoArray)
                        {
                            if (AJAXController.hasFirebug()) console.log ("Match: " + String (x));
                            match[count] = String (x);
                            count ++;
                            if (count >= 2)
                                break;
                        }
                    }
                }
                if (match[0] != "")
                {
                    this.BuildHTML (fromname, toname, match, count);
                } else 
                {
                    var outputobj = $("user_connection");
                    var li = document.createElement('li');
                    li.className = "user_bridge";
                    outputobj.appendChild (li);
                    li.innerHTML = "<a href='/" + toname + "'>" + toname + "</a> không nằm trong mạng của bạn. ";
                }
            } else 
            {
                var outputobj = $("user_connection");
                var li = document.createElement('li');
                li.className = "user_bridge";
                outputobj.appendChild (li);
                li.innerHTML = "<a href='/" + toname + "'>" + toname + "</a> đang trong friendlist của bạn. ";
            }
        } else 
        {
            var outputobj = $("user_connection");
            var li = document.createElement('li');
            li.className = "user_bridge";
            outputobj.appendChild (li);
            li.innerHTML = "Đây là trang của bạn.";
        }
    },

    BuildHTML : function (fromname, toname, matcharray, nummatch)
    {
        //<li class="user_bridge"><a href="#">Me</a> > <a href="#">FirstOne</a> > <span class="user_this">ThisUser</span></li>
        var outputobj = $("user_connection");
        for (i=0;i<nummatch;i++)
        {
            var li = document.createElement('li');
            li.className = "user_bridge";
            outputobj.appendChild (li);
            
            var afrom = document.createElement('a');
            afrom.innerHTML = fromname;
            afrom.setAttribute("href", "/" + fromname);
            afrom.onmouseover = function ()
                            {
                                this.className = "user_this";
                            }
            afrom.onmouseout = function ()
                            {
                                this.className = "";
                            }
            li.appendChild (afrom);
            
            var arrow = document.createElement('span');
            arrow.innerHTML = " &gt; ";
            li.appendChild (arrow);
            
            var firstOne = document.createElement('a');
            firstOne.innerHTML = matcharray[i];
            firstOne.setAttribute("href", "/" + matcharray[i]);
            firstOne.onmouseover = function ()
                            {
                                this.className = "user_this";
                            }
            firstOne.onmouseout = function ()
                            {
                                this.className = "";
                            }
            li.appendChild (firstOne);
            
            var arrow1 = document.createElement('span');
            arrow1.innerHTML = " &gt; ";
            li.appendChild (arrow1);
            
            var ato = document.createElement('a');
            ato.innerHTML = toname;
            ato.setAttribute("href", "/" + toname);
            ato.onmouseover = function ()
                            {
                                this.className = "user_this";
                            }
            ato.onmouseout = function ()
                            {
                                this.className = "";
                            }

            li.appendChild (ato);
        }
        if (AJAXController.hasFirebug()) console.log ("Build utouPath HTML finish");
    }
}