// JavaScript Document

//********************************
// Screenshot display functions
//********************************

// "Constant" variables, change to suit your environment
var image_width = 260;
var image_height = 156;

// Global variable used to ensure no "ghosts" remain
var visible = new Array();

function show(id, obj)
{
  // Ensure no divs remain visible if a new div is visible
  hide_all();

  var div = document.getElementById(id);

  // Get current window size
  width = window.innerWidth;
  if(width != undefined)
  {
    width = width - 16; // 16 is the width of a scroll bar in mozilla
    height = window.innerHeight;
  }
  else
  {
    width = document.body.offsetWidth - 20; // 20 is the width of a scroll bar in IE
    height = document.body.offsetHeight;
  }
  reltop = document.body.scrollTop;

  div.style.top = parseInt(findPosY(obj)) + 50;
  div.style.left = parseInt(findPosX(obj));

  // Override if window is too small
  if(height < (height - parseInt(div.style.top) + (image_height + 8)))
  {
    // do nothing
  }
  else
  {
    if((parseInt(div.style.top) + (image_height + 8)) > (reltop + height))
    {
      div.style.top = parseInt(findPosY(obj)) - (image_height + 50);
    }
  }

  // Override if window is too small
  if(width < (parseInt(div.style.left) + image_width))
  {
    // do nothing
  }
  else
  {
    if((parseInt(div.style.left) + image_width) > width)
    {
      div.style.left = parseInt(findPosX(obj)) - image_width;
    }
  }

  div.style.position = 'absolute';
  div.style.display = 'block';

  // Add to the "visible" global array to make sure it does not live past the next div to be displayed
  visible.push(div);
}

function hide_all()
{
  while(visible.length > 0)
  {
    kill = visible.pop();
    kill.style.display = 'none';
  }
}

function toggle(id, obj)
{
  // Ensure no divs remain visible if a new div is visible
  while(visible.length > 0)
  {
    kill = visible.pop();
    kill.style.display = 'none';
  }

  var div = document.getElementById(id);

  // Get current window size
  width = window.innerWidth;
  if(width != undefined)
  {
    width = width - 16; // 16 is the width of a scroll bar in mozilla
    height = window.innerHeight;
  }
  else
  {
    width = document.body.offsetWidth - 20; // 20 is the width of a scroll bar in IE
    height = document.body.offsetHeight;
  }
  reltop = document.body.scrollTop;

  if(div.style.display == 'none')
  {
    div.style.top = parseInt(findPosY(obj)) + 50;
    div.style.left = parseInt(findPosX(obj));

    // Override if window is too small
    if(height < (height - parseInt(div.style.top) + (image_height + 8)))
    {
      // do nothing
    }
    else
    {
      if((parseInt(div.style.top) + (image_height + 8)) > (reltop + height))
      {
        div.style.top = parseInt(findPosY(obj)) - (image_height + 50);
      }
    }

    // Override if window is too small
    if(width < (parseInt(div.style.left) + image_width))
    {
      // do nothing
    }
    else
    {
      if((parseInt(div.style.left) + image_width) > width)
      {
        div.style.left = parseInt(findPosX(obj)) - image_width;
      }
    }

    div.style.position = 'absolute';
    div.style.display = 'block';

    // Add to the "visible" global array to make sure it does not live past the next div to be displayed
    visible.push(div);
  }
  else
  {
    div.style.display = 'none';
  }
}

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
  {
    while(1) 
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
      {
        break;
      }
      obj = obj.offsetParent;
    }
  }
  else if(obj.x)
  {
    curleft += obj.x;
  }
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
  {
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
      {
        break;
      }
      obj = obj.offsetParent;
    }
  }
  else if(obj.y)
  {
    curtop += obj.y;
  }
  return curtop;
}

function loading(id)
{
  var div = document.getElementById(id);
  var imgs = div.getElementsByTagName('img');
  var img = imgs[0];
  var hold = img.src;
  if(!img.complete)
  {
    img.src = '/images/layout/image_loading.jpg';
  }
  img.src = hold;
}

//********************************
// Client highlight functions
//********************************

// Create a variable to hold the last highlighted class
var last_highlight = '';

function highlight_class(class_name)
{
  // If the highlighted class is the same as the one selected, the same link has been clicked twice
  var only_deselect = (last_highlight == class_name);

  // Un-highlight every client
  normal_all();

  // Don't re-highlight if the same link has been clicked twice
  if(!only_deselect)
  {
    // Save the highlighted class for later reference
    last_highlight = class_name;

    // Make every span dull
    dull_all();

    var i, As;
    var all_spans = document.getElementsByTagName('span');
    var highlight = new Array();

    for(i = 0; i < all_spans.length; i++)
    {
      if(all_spans[i].className.search(class_name) > -1)
      {
        // Save in the array to keep track of highlighted spans
        highlight.push(all_spans[i]);
      }
    }

    for(i = 0; i < highlight.length; i++)
    {
      // If the span contains an anchor tag, the anchor tag must have the new class applied to it
      As = highlight[i].getElementsByTagName('a');
      if(As.length == 1)
      {
        As[0].className += ' highlight';
      }
      else
      {
        highlight[i].className += ' highlight';
      }
    }
  }
}

function dull_all()
{
  var i, As;
  var all_spans = document.getElementsByTagName('span');

  for(i = 0; i < all_spans.length; i++)
  {
    // If the span contains an anchor tag, the anchor tag must have the new class applied to it
    As = all_spans[i].getElementsByTagName('a');
    if(As.length == 1)
    {
      As[0].className += ' dull';
    }
    else
    {
      all_spans[i].className += ' dull';
    }
  }
}

function normal_all()
{
  var i, As;
  var all_spans = document.getElementsByTagName('span');

  // Reset last_highlight
  last_highlight = '';

  for(i = 0; i < all_spans.length; i++)
  {
    // If the span contains an anchor tag, the anchor tag must have the classes removed from it
    As = all_spans[i].getElementsByTagName('a');
    if(As.length == 1)
    {
      As[0].className = As[0].className.replace('dull', '');
      As[0].className = As[0].className.replace('highlight', '');
    }
    else
    {
      all_spans[i].className = all_spans[i].className.replace('dull', '');
      all_spans[i].className = all_spans[i].className.replace('highlight', '');
    }
  }
}

