/**
 *  Table of contents
 *    openAttribWin(uri)
 *    openPreviewWin(id)
 *    toggleDL(id)
 *
 *    cookie processing
 */

var imgWin;

/**
 *  Opens window and loads the specified document.
 *  Function closes any window it previously opened. Call using:
 *    <code>href="javascript: void openAttribWindow('somefile.html');"</code>
 *
 *  @param  {String} uri  Location of doc to load.
 *  @type void
 */
function openAttribWin(uri)
{
  try {
    imgWin.close();
  }
  catch (e) {}

  var features = "width=640,height=480";

  imgWin = window.open(uri, "attribwin", features);
}

/**
 *  Opens the image window or the edit-image page depending on 'editimgchkbox'.
 *  Only occurs in preview_artist.php
 *
 *  @param  string id
 *  @type   void
 */
function openPreviewWin(id)
{
  if (document.getElementById("editimgchkbox") &&
      document.getElementById("editimgchkbox").checked) {
    window.open(staging_dir + "../admin/registry/regimage_edit.php?id=" + id,
      "editimgwin");
  }
  else {
    openAttribWin("artwork.php?id=" + id);
  }
}

/**
 *  Toggles display of dl elements.
 *
 *  @param  {String}  id  identifier of dl element to toggle
 *  @return {boolean}     if toggle success true else false
 */
function toggleDL(id)
{
  var dls;
  var element;

  // get a collection of all dl elements
  if (document.all) {
    dls = document.all.tags("dl");
  }
  else if (document.getElementById) {
    dls = document.getElementsByTagName("dl");
  }
  else {
    return false;
  }

  // kill display of all dl elements
  for (var i = 0; i < dls.length; i ++) {
    dls[i].style.display = "none";
  }

  // delete cookie value
  document.cookie = "activeid=";

  // if a different item has been clicked, show it
  if (id == activeId) {
    activeId = null;
  }
  else {
    activeId = id;

    // toggle display of target dl element
    if (document.all) {
      element = document.all[id];

      if (element.style.display == "none") {
        element.style.display = "block";
      }
      else {
        element.style.display = "none";
      }
    }
    else if (document.getElementById) {
      element = document.getElementById(id);

      if(element.style.display == "none") {
        element.style.display = "block";
      }
      else {
        element.style.display = "none";
      }
    }
    else {
      return false;
    }
    document.cookie = "activeid=" + id;
  }
  colAdjust();
  return true;
}

/**
 *  If activeid cookie exists, retrieve and assign to var activeId.
 */
var activeId = "";
var cookies = document.cookie.split(";");
var cookiePieces;

for (var i = 0; (i < cookies.length) && (activeId == ""); i++) {
  cookiePieces = cookies[i].split("=");

  if (cookiePieces[0].substring(0, 1) == " ") {
    cookiePieces[0] = cookiePieces[0].substring(1, cookiePieces[0].length);
  }
  if (cookiePieces[0] == "activeid") {
    activeId = decodeURIComponent(cookiePieces[1]);
  }
}
