var mediums = ["aim","wlm",/*"icq",*/"yahoo","jabber"];

function imName(im)
{
   switch(im)
   {
      case "jabber":
         return "Jabber / Google Talk";
      case "yahoo":
         return "Yahoo!";
      case "wlm":
         return "Windows Live Messenger";
      default:
         return im.toUpperCase();
   }
}

function refreshStatus()
{
   jQuery.get("/admin/status", null, function (st)
   {
      st.wlm.status = st.wlm.status.toLowerCase();
      st.wlm.id = st.wlm.id.toLowerCase();

      st.aim = st.aim.response.data.users[0]
      st.aim.id = st.aim.aimId;
      st.aim.status = st.aim.state;

      if ((st.wlm.status == "away") && (st.aim.status == "away"))
      {
         st.icq.status = "away";

         if (st.yahoo.status == "online")
         {
            st.yahoo.status = "away";
         }
      }

      var l_TimeStamp = 0;
      var med;
      for (var i = 0; (med = mediums[i]); ++i)
      {
         var img = document.getElementById("IM_" + med.charAt(0));
         if (img)
         {
            img.name = "";
            img.stat = "offline";
            if (st[med])
            {
               img.name = st[med].id;
               img.stat = st[med].status;
               switch (img.stat)
               {
                  case "online":
                  case "away": break;
                  default: img.stat = "offline"; break;
               }
            }

            img.title = img.alt = imName(med) + " : " + img.stat + (img.name ? ' as ' + img.name : '' );
            img.src = "/images/im/" + (window.bigStatus ? "big/" : "") + med + img.stat + ".png";
            if (window.bigStatus)
            {
               img.parentNode.nextSibling.nextSibling.innerHTML = "(currently " + img.stat + (img.stat != "offline" ? " as \"" + img.name + '"' : "") + ")";
            }
         }
      }
   }, "json");
}

function initStatus()
{
   window.bigStatus = window.location.href.indexOf("contactus") != -1;
   var med;
   for (var i = 0; (med = mediums[i]); ++i)
   {
      var img = document.getElementById("IM_" + med.charAt(0));
      if (img)
      {
         img.med = imName(med);
         img.title = img.alt;
         img.onclick = function()
         {
            var tx = "Niversoft's support is currently " + this.stat + " on " + this.med + (this.stat != "offline" ? " as \"" + this.name + "\".\n" : ".\n");
            if (this.stat == "offline")
            {
               tx += "Please try again later";
            }
            else if (this.stat == "away")
            {
               tx += "The support may not be available, you may not receive an immediate answer.";
            }
            else
            {
               tx += "If you need support with Niversoft's products, the IM is the easiest and fastest way to get some.\n\n";
               tx += "Please try to contact \"" + this.name + "\" with your " + this.med + " client.";
            }
            alert(tx);
         };
      }
   }
   refreshStatus();
   window.setInterval("refreshStatus()", 60000);
}
initStatus();