﻿var global_currentTab = 0;
var isNS = (navigator.appName == "Netscape");
var command = "";
var commandGroupId = "";
var commandTabId = "";
var commandLinkId = "";
var startX = 0;
var startY = 0;
var lastTimeoutEvent = 0;

/* ADS RELOAD *******************************************************/

  // setTimeout("reloadAds()", 10000);
function reloadAds()
{
  if (location.href.indexOf("chrjs")>0)
  {
    alert("1");
  }
  var el = document.getElementById("google_adsense");
  if (el == null) return;

  if (location.href.indexOf("chrjs")>0)
  {
    alert("2");
  }

  var value = el.innerHTML;
  el.innerHTML = "";
  el.innerHTML = value;
  setTimeout("reloadAds()", 10000);
}


/* GENERAL FUNCTIONS ************************************************/
function log(txt)
{
  var el = document.getElementById("report");
  if (el == null) return;
  var oldtxt = el.value;
  el.value = oldtxt + "\r\n" + txt;
  //el.doScroll("scrollbarPageDown");
}
function getXPos(item)
{
  xPos = eval(item).offsetLeft;
  tempEl = eval(item).offsetParent;
  while (tempEl != null) 
  {
    xPos += tempEl.offsetLeft;
    tempEl = tempEl.offsetParent;
  }
  return xPos;
}
function getYPos(item)
{
  yPos = eval(item).offsetTop;
  tempEl = eval(item).offsetParent;
  while (tempEl != null) 
  {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
  return yPos;
}
function createXMLHttpRequest() 
{
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { return new XMLHttpRequest(); } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}
function renderElement(div, xhReq, rebindBubbles)
{
  if (xhReq.readyState!=4) return;
  div.innerHTML = xhReq.responseText;
  try
  {
    SNAP_COM.shot.Rescan();
  }
  catch(e){}
}
function getAbsolutePosition(element) 
{
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) 
  {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
}
function containsDOM (container, containee) 
{
  var isParent = false;
  do 
  {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

/* COMMANDS *****************************************************************/
function addGroup(type)
{
  location.href = "AddGroup.aspx?qbid=" + global_currentTab + "&type=" + type;
}
function addLink()
{
  location.href = "AddLink.aspx?mode=0&qbid=" + global_currentTab;
}
/*function runLink(url)
{
  open("RunLink.aspx?url=" + url, "_blank");
  reloadLastVisited();
}*/
function runLink(url)
{
  var xhReq = createXMLHttpRequest();
  xhReq.open("get", "RunLink.aspx?url=" + escape(url), true);
  xhReq.onreadystatechange = function() {linkClickRegistered(xhReq);};
  xhReq.send(null);
}
function linkClickRegistered(xhReq)
{
  if (xhReq.readyState!=4) return;
  reloadLastVisited();
}
function doEdit()
{
  document.form1["TabsCtrl_Command"].value = "edit";
  document.form1.submit();
}
function shiftTabRight()
{
  document.form1["TabsCtrl_Command"].value = "str";
  document.form1.submit();
}
function shiftTabLeft()
{
  document.form1["TabsCtrl_Command"].value = "stl";
  document.form1.submit();
}
function renderTab(uid, div, tabId, publicMode, xhReq)
{
  if (xhReq.readyState!=4) return;
  div.innerHTML = "";
  div.style.marginLeft = "0px";
  div.style.marginTop = "0px";
  div.innerHTML = xhReq.responseText;
  updateGoogleAds(tabId);
  if (publicMode == 0)
  {
    loadAllRSS(uid, false);
    InitializeDD(tabId);
  }
  try
  {
    SNAP_COM.shot.Rescan();
  }
  catch(e){}  
}
function updateGoogleAds(tabId)
{
  var google_ads_marker = document.getElementById("tab"+tabId+"_adsmarker");
  var google_adsense = document.getElementById("google_adsense");
  if (google_ads_marker != null && google_adsense != null)
  {
    google_ads_marker.innerHTML = google_adsense.innerHTML;
  }
}
function loadAllRSS(uid, forceReload)
{
  for (i=0; i<g_RSSGA.length; i++)
    loadRSS(uid, g_RSSGA[i], forceReload);
}
function reloadAllRSS(uid, refreshRate)
{
  for (i=0; i<g_RSSGA.length; i++)
    loadRSS(uid, g_RSSGA[i], true);
  setTimeout("reloadAllRSS("+uid+","+refreshRate+")",refreshRate);
}
function appendElement(el, xhReq)
{
  if (xhReq.readyState!=4) return;  
  var html = el.innerHTML;
  html += xhReq.responseText;
  el.innerHTML = html;
}
function reloadLastVisited()
{
  for (i=0; i<g_LVGA.length; i++)
    doReloadLastVisited(g_LVGA[i]);  
}
function doReloadLastVisited(groupId)
{
  var el = document.getElementById("g"+groupId);
  if (el == null) return;
  var ul = null;
  var c = el.childNodes.length;
  for(i=0; i<c; i++)
  {
    ul = el.childNodes[i];
    if (ul.nodeType == 1)
      if (ul.tagName.toLowerCase() == "ul")
        break;
  }
  
  if (ul == null || ul.tagName.toLowerCase() != "ul")
    return;

  var count = el.getAttribute("count");
  if (count == null) return;
  
  //el.removeChild(ul);
  ul.innerHTML = "<img src='../images/blue/progress.gif'/>";
  
  var xhReq = createXMLHttpRequest();
  xhReq.open("get", "GetLastVisited.aspx?count=" + count, true);
  xhReq.onreadystatechange = function() {renderElement(ul, xhReq, true);};
  xhReq.send(null);
}
function loadTab(userId, tabId, tabPos, highlightGroup, publicMode)
{
  var div = document.getElementById("t" + tabId + "_body_dyn");
  if (div != null)   
  {
    var url = "../GetTab.aspx?tabId="+tabId+"&tabPos="+tabPos+"&uid="+userId+"&public="+publicMode;
    if (highlightGroup >= 0)
      url += "&hg=" + highlightGroup;
    var now = new Date(); 
    url+="&n=" + now.getTime(); 
    var xhReq = createXMLHttpRequest();
    xhReq.open("get", url, true);
    xhReq.onreadystatechange = function() {renderTab(userId, div.parentNode, tabId, publicMode, xhReq);};
    xhReq.send(null);
  }
  else
    updateGoogleAds(tabId);
  global_currentTab = tabId;
}
function loadRSS(uid, groupId, forceReload)
{
  var group = document.getElementById("g" + groupId);
  if (group == null) return;
  var tagName;
  var div = null;
  for(var k = 0; k < group.childNodes.length; k++)
  {
    var node = group.childNodes[k];
    if (node.nodeType == 1 && node.getAttribute("toupdate") != null)
    {
      if (forceReload || node.getAttribute("loaded") == null)
      {
        div = node;
        break;
      }
    } 
  }
  if (div != null)
  {
    if (forceReload) 
      div.innerHTML = "<ul><li><img src='../images/blue/progress.gif'/></li></ul>";
    var url = "GetRSS.aspx?qbid="+groupId + "&uid="+uid+(forceReload ? "&reload=true" : "");
    var now = new Date(); 
    url+="&n=" + now.getTime();
    var xhReq = createXMLHttpRequest();
    xhReq.open("get", url, true);
    xhReq.onreadystatechange = function() {renderElement(div, xhReq, false);};
    xhReq.send(null);
    div.setAttribute("loaded","true");
  }
}
function keepAlive()
{
  var xhReq = createXMLHttpRequest();
  xhReq.open("get", "KeepAlive.aspx", true);
  xhReq.send(null);
  setTimeout('keepAlive()', 600000); //10 minutes
}
function saveDDMovement(column, insertBefore, groupMoved)
{
  //alert(groupMoved);
  var now = new Date(); 
  var xhReq = createXMLHttpRequest();
  xhReq.open("get", "SaveDDMovement.aspx?col=" + column + "&ib=" + insertBefore + "&group=" + groupMoved + "&n=" + now.getTime(), false);
  xhReq.send(null);
}
function showNotes(linkId)
{
  var el = document.getElementById(linkId);
  var elBtn = document.getElementById(linkId + "b");
  if (el == null) return;
  if (el.style.display == "block")
  {
    elBtn.style.display = "inline";
    el.style.display = "none";
    //elBtn.src = "../images/blue/notes_on.gif";
    //elBtn.title = "Show notes";
  }
  else
  {
    el.style.display = "block";
    elBtn.style.display = "none";
    //elBtn.src = "../images/blue/notes_off.gif";
    //elBtn.title = "Hide notes";
  }
}
function setTab(tabId)
{
  var xhReq = createXMLHttpRequest();
  var now = new Date(); 
  try
  {
    xhReq.open("get", "SetTab.aspx?tabId=" + tabId + "&n=" + now.getTime(), true);
    xhReq.send(null);
  }
  catch(e){}
  global_currentTab = tabId;
}
function qbSearch()
{
  var val = document.getElementById("searchfield").value;
  document.form1["TabsCtrl_Command"].value = "search";
  document.form1["TabsCtrl_Element"].value = val;
  document.form1.submit();
}
function hideMenu(menuId)
{
  document.getElementById(menuId).style.display = 'none';
}
function cleanUpGadget(id)
{
  /*var el = document.getElementById(id);
  if (el == null) return;
  var imgs = el.getElementsByTagName("IMG");
  for(var i=0; i<imgs.length; i++)
  {
    var img = imgs[i];
    if (img.src != 'http://gmodules.com/ig/images/plus_google.gif') continue;
    while(img.parentElement != null && img.parentElement.tagName != "TR")
      img = img.parentElement;
    img.parentElement.style.display = 'none';
    return;
  }*/
}
function checkMouseLeave (element, evt) 
{
  if (element.contains && evt.toElement) 
    return !element.contains(evt.toElement);
  else if (evt.relatedTarget) 
    return !containsDOM(element, evt.relatedTarget);
}
function delGroup(groupId)
{
  document.form1["TabsCtrl_Element"].value = groupId;
  document.form1["TabsCtrl_Command"].value = "dg";
  document.form1.submit();
}
function sortGroup(groupId)
{
  document.form1["TabsCtrl_Element"].value = groupId;
  document.form1["TabsCtrl_Command"].value = "sg";
  document.form1.submit();
}
function moveUp()
{
  document.form1["TabsCtrl_Element"].value = commandGroupId;
  document.form1["TabsCtrl_Command"].value = "mu";
  document.form1.submit();
}
function moveDown()
{
  document.form1["TabsCtrl_Element"].value = commandGroupId;
  document.form1["TabsCtrl_Command"].value = "md";
  document.form1.submit();
}
function moveRight()
{
  document.form1["TabsCtrl_Element"].value = commandGroupId;
  document.form1["TabsCtrl_Command"].value = "mr";
  document.form1.submit();
}
function moveLeft()
{
  document.form1["TabsCtrl_Element"].value = commandGroupId;
  document.form1["TabsCtrl_Command"].value = "ml";
  document.form1.submit();
}
/*function getEventTarget(evt)
{
  try
  {
    var tgt = evt.srcElement;
    return tgt;
  }
  catch(err){}
  return evt.target;
}
function showEditGroupCommands(evt)
{
  var ev = evt || window.event;
  var target = ev.target || ev.srcElement;

  var child = findChildElement(target, "span", "groupcmds");
  if (child) child.style.visibility = "visible";
  ev.stopPropagation();
}
function hideEditGroupCommands(evt)
{
  var ev = evt || window.event;
  var target = ev.target || ev.srcElement;
  var child = findChildElement(target, "span", "groupcmds");
  if (child) child.style.visibility = "hidden";
  ev.stopPropagation();
}
function findChildElement(el, tagname, attname)
{
  var children = el.getElementsByTagName(tagname);
  for(var i=0;i<children.length;i++)
  {
    if (children[i].getAttribute(attname) == "")
      return children[i];
  }
  return null;
}*/
function openMenu(item, groupId)
{
  hideMenu('tab_edit_menu');
  hideMenu('link_edit_menu');
  commandGroupId = groupId;
  var menu = document.getElementById('edit_menu');
  if (isNS) 
  {
    menu.style.left = (getXPos(item) - 16) + "px";
    menu.style.top = (getYPos(item) - 14) + "px";
    menu.style.display = 'inline';
  }
  else 
  {
    menu.style.pixelLeft = getXPos(item) - 16;
    menu.style.pixelTop = getYPos(item) - 14;
    menu.style.display = 'inline';
  }
}
function openTabMenu(item)
{
  hideMenu('edit_menu');
  hideMenu('link_edit_menu');
  commandTabId = global_currentTab;
  var menu = document.getElementById('tab_edit_menu');
  if (isNS) 
  {
    menu.style.left = (getXPos(item) - 16) + "px";
    menu.style.top = (getYPos(item) - 0) + "px";
    menu.style.display = 'inline';
  }
  else 
  {
    menu.style.pixelLeft = getXPos(item) - 16;
    menu.style.pixelTop = getYPos(item) - 0;
    menu.style.display = 'inline';
  }
}
function openLinkMenu(item, linkId)
{
  hideMenu('edit_menu');
  hideMenu('tab_edit_menu');
  commandLinkId = linkId;
  var menu = document.getElementById('link_edit_menu');
  if (isNS) 
  {
    menu.style.left = (getXPos(item) - 1) + "px";
    menu.style.top = (getYPos(item) - 13) + "px";
    menu.style.display = 'inline';
  }
  else 
  {
    menu.style.pixelLeft = getXPos(item) - 1;
    menu.style.pixelTop = getYPos(item) - 13;
    menu.style.display = 'inline';
  }
}

function delTab(tabId)
{
  if (!tabId)
    tabId = global_currentTab;
  if (confirm(msg_delTabConfirm) == 0) return;
  document.form1["TabsCtrl_Element"].value = tabId;
  document.form1["TabsCtrl_Command"].value = "dt";
  document.form1.submit();
}
function doSave()
{
  document.form1["TabsCtrl_Command"].value = "save";
  document.form1.submit();
}
function doImport()
{
  document.form1["TabsCtrl_Command"].value = "import";
  document.form1.submit();
}
function delLink(l)
{
  document.form1["TabsCtrl_Element"].value = l;
  document.form1["TabsCtrl_Command"].value = "dl";
  document.form1.submit();
}
function doUpdateIcons()
{
  document.location.href = "UpdateIcons.aspx";
}
function addTab()
{
  document.location.href = "AddTab.aspx";
}
function editGroup(groupId)
{
  document.location.href = "AddGroup.aspx?qbid=" + groupId;
}
function editTab()
{
  document.location.href = "AddTab.aspx?qbid=" + global_currentTab + "&edit=1";
}
function editmode_addLink(groupId)
{
  if (groupId)
    document.location.href = "AddLink.aspx?mode=1&qbid=" + groupId;
  else
    document.location.href = "AddLink.aspx?mode=0&qbid=" + document.form1['TabsCtrl_CurrentTabId'].value;
}
function editLink(linkId)
{
  document.location.href = "AddLink.aspx?mode=2&qbid=" + linkId + "&edit=1";
}
function calcSelection(mode)
{
  //var groupsMode = (mode == 1);
  var someGroups = false;
  var someLinks = false;
  var list = document.getElementsByTagName("input");
  var value = "";
  for (var i = 0; i < list.length; i++)
  {
    var item = list[i];
    if (item.id.indexOf("check_")==0 && item.checked)
    {
      var id = item.id.substring(6, item.id.length);
      //if (groupsMode && isLink(id)) continue;
      //if (!groupsMode && !isLink(id)) continue;
      if (isLink(id))
      {
        someLinks = true; 
      }
      else
      { 
        someGroups = true;
      }
      id = id.substring(1);
      if (value!="") value+="|";
      value+=id;
    }
  }
  if (someGroups || someLinks)
  {
    document.form1["TabsCtrl_Element"].value = value;
    if (someGroups && !someLinks)
      return 1;
    else
    if (!someGroups && someLinks)
      return 2;
    return 3;
  }
  return 0;
}
function delSelection()
{
  document.form1["TabsCtrl_Command"].value = "delsel";
  var type = calcSelection();
  if (type == 0)
  {
    alert(msg_delErr1);
    return;
  }
  if (type == 3)
  {
    alert(msg_delErr2);
    return;
  }
  if (confirm(msg_delConfirm) == 1)
    document.form1.submit()
}
function moveSelected(mode, el)
{
  var groupsMode = (mode == 1);
  document.form1["TabsCtrl_Command"].value = (groupsMode ? "movegroupsel" : "movesel");
  
  var target = el.value;
  if (target == "---") return;
  var targetIsGroup = (target.indexOf("g")==0);
  var type = calcSelection(mode);
  if (type == 0)
  {
    alert(msg_moveErr1);
    return;
  }
  else
  if (type == 1 && !groupsMode)
  {
    alert(msg_moveErr2);
    return;
  }
  else
  if (type == 2 && groupsMode)
  {
    alert(msg_moveErr3);
    return;
  }
  else
  if (type == 3)
  {
    alert(msg_moveErr4);
    return;
  }
  document.form1["TabsCtrl_Element"].value += "|" + target;
  document.form1.submit()
}
function AddLinkToGroup()
{
  var target = document.getElementById("targetAddLink").value;
  var n = target.indexOf("g");
  var groupIndex = target.substring(n+1, target.length());
  document.location.href = "AddLink.aspx?qbid=" + document.form1['TabsCtrl_CurrentTabId'].value + "." + groupIndex;
}
function showMessage()
{
  alert(msg_startEdit);
}
function cleanHighlight(groupId)
{
  document.getElementById("g" + groupId).className = "group";
}
function moveTabRight(tabId)
{
  if (!tabId)
    tabId = commandTabId;
  document.form1["TabsCtrl_Element"].value = tabId;
  document.form1["TabsCtrl_Command"].value = "mtr";
  document.form1.submit();
}
function moveTabLeft(tabId)
{
  if (!tabId)
    tabId = commandTabId;
  document.form1["TabsCtrl_Element"].value = tabId;
  document.form1["TabsCtrl_Command"].value = "mtl";
  document.form1.submit();
}
function moveLinkUp()
{
  document.form1["TabsCtrl_Element"].value = commandLinkId;
  document.form1["TabsCtrl_Command"].value = "mlu";
  document.form1.submit();
}
function moveLinkDown()
{
  document.form1["TabsCtrl_Element"].value = commandLinkId;;
  document.form1["TabsCtrl_Command"].value = "mld";
  document.form1.submit();
}
function doDiscard()
{
  document.form1["TabsCtrl_Command"].value = "discard";
  document.form1.submit();
}
function isLink(id)
{
  var n = id.indexOf("l");
  return (n == 0);
}
function closeMessage(elId, userId, msgId)
{
  var xhReq = createXMLHttpRequest();
  xhReq.open("get", "SetMessageRead.ashx?userId=" + userId + "&msgId=" + msgId, true);
  xhReq.onreadystatechange = function() {messageClosed(xhReq, elId);};
  xhReq.send(null);
}
function messageClosed(xhReq, elId)
{
  if (xhReq.readyState!=4) return;
  document.getElementById(elId).style.display = "none";
}
function cm_getCurrentId()
{
  var id = SimpleContextMenu._attachedElement.getAttribute("qbid");
  if (id != "" && id != null) return id;
  return SimpleContextMenu._attachedElement.parentNode.getAttribute("qbid");
}
function cm_editLink()
{
  document.location.href = "AddLink.aspx?qbid=" + cm_getCurrentId() + "&mode=2";
}
function cm_deleteLink()
{
  delLink(cm_getCurrentId());
}
function cm_moveUp()
{
  commandLinkId = cm_getCurrentId();
  moveLinkUp();
}
function cm_moveDown()
{
  commandLinkId = cm_getCurrentId();
  moveLinkDown();
}
function cm_editGroup()
{
  var id = cm_getCurrentId();
  document.location.href = "AddGroup.aspx?qbid=" + id + "&edit=1";
}
function cm_addLink()
{
  var id = cm_getCurrentId();
  document.location.href = "AddLink.aspx?mode=1&qbid=" + id;
}
function cm_deleteGroup()
{
  delGroup(cm_getCurrentId());
}
function cm_editTab()
{
  var id = cm_getCurrentId();
  document.location.href = "AddTab.aspx?qbid=" + id + "&edit=1";
}
function cm_moveTabLeft()
{
  moveTabLeft(cm_getCurrentId());
}
function cm_moveTabRight()
{
  moveTabRight(cm_getCurrentId());
}
function cm_deleteTab()
{
  delTab(cm_getCurrentId());
}
function cm_moveGroupUp()
{
  commandGroupId = cm_getCurrentId();
  moveUp();
}
function cm_moveGroupDown()
{
  commandGroupId = cm_getCurrentId();
  moveDown();
}
function cm_moveGroupLeft()
{
  commandGroupId = cm_getCurrentId();
  moveLeft();
}
function cm_moveGroupRight()
{
  commandGroupId = cm_getCurrentId();
  moveRight();
}
function cm_sortGroup()
{
  commandGroupId = cm_getCurrentId();
  sortGroup(commandGroupId);
}