﻿function InitPhotos(photoWin, photos)
{
  // [deprecated]
  var photoWin = document.getElementById(photoWin);
  var caption = document.getElementById("caption");

  var btnNext = document.getElementById("btnNext");
  var btnPrev = document.getElementById("btnPrev");

  var index = 0;
  var maxIndex = (photos.length - 1);

  var btnNextColor = btnNext.style.color;
  var btnPrevColor = btnPrev.style.color;

  var photoDiv = document.getElementById("photos");

  if (photos.length > 0)
  {
    photoDiv.style.display = "block";

    changePhoto = function(i)
    {
      if ((i > -1) && (i < photos.length))
      {
        photoWin.alt = photos[i].alt;
        photoWin.src = photos[i].src;
        photoWin.title = photoWin.alt;
        caption.innerHTML = photoWin.alt;
      }

      if (i > (maxIndex - 1))
      {
        btnNext.style.color = "#f2f2e6";
        btnNext.style.cursor = "default";
        btnNext.style.textDecoration = "none";
      }
      else
      {
        btnNext.style.color = btnNextColor;
        btnNext.style.cursor = "pointer";
        btnNext.style.textDecoration = "underline";
      }

      if (i < 1)
      {
        btnPrev.style.color = "#f2f2e6";
        btnPrev.style.cursor = "default";
        btnPrev.style.textDecoration = "none";
      }
      else
      {
        btnPrev.style.color = btnPrevColor;
        btnPrev.style.cursor = "pointer";
        btnPrev.style.textDecoration = "underline";
      }
    }

    changePhoto(index);

    btnNext.onclick = function()
    {
      index += 1;
      if (index < photos.length) { changePhoto(index); } else { index -= 1; }
      return false;
    }

    btnPrev.onclick = function()
    {
      index -= 1;
      if (index > -1) { changePhoto(index); } else { index += 1; }
      return false;
    }
  }
}

function InitPhotoGallery(dl)
{
  var photos = $("#photos");
  var dlPhotos = $("#" + dl + " a");
  var latestPhoto = $("img.photo-latest");
  var latestPhotoCaption = $("#caption");

  dlPhotos.lightBox({
    imageBlank: "/scripts/jquery-lightbox/images/lightbox-blank.gif",
    imageLoading: "/scripts/jquery-lightbox/images/lightbox-ico-loading.gif",
    imageBtnClose: "/scripts/jquery-lightbox/images/lightbox-btn-close.gif",
    imageBtnPrev: "/scripts/jquery-lightbox/images/lightbox-btn-prev.gif",
    imageBtnNext: "/scripts/jquery-lightbox/images/lightbox-btn-next.gif"
  });

  var firstPhoto = dlPhotos.find("img");

  latestPhoto.attr("src", firstPhoto.attr("src"));
  latestPhoto.attr("alt", firstPhoto.attr("alt"));
  latestPhoto.attr("title", firstPhoto.attr("alt"));
  latestPhoto.css("display", "block");
  latestPhoto.css("border", "2px solid #000");

  var caption = firstPhoto.attr("alt");

  if (dlPhotos.length > 1)
  {
    caption += "<span style=\"color: #999; font-size: .8em; display: block;\">(click the image to view more photos)</span>";

    latestPhoto.css("cursor", "pointer");

    latestPhoto.click(function()
    {
      $("#photo").hide();
      photos.show();
    });
  }

  latestPhotoCaption.html(caption);

  photos.hide();
}

function OpenWindow(url, n, w, h, sb, r, mb, tb, s, loc, dir)
{
  var screenWidth = window.screen.availWidth;
  var screenHeight = window.screen.availHeight;

  var offsetXWinXP = 5;
  var offsetYWinXP = 18;

  var halfScreenWidth = Number(window.screen.availWidth / 2);
  var halfScreenHeight = Number(window.screen.availHeight / 2);

  var halfWindowWidth = Number(w / 2);
  var halfWindowHeight = Number(h / 2);

  var diffWidth = Number(halfScreenWidth - halfWindowWidth);
  var diffHeight = Number(halfScreenHeight - halfWindowHeight);

  diffWidth -= offsetXWinXP;
  diffHeight -= offsetYWinXP;

  var windowName = n;

  if (windowName.length == 0)
  {
    //The windowName is a zero-based integer corresponding to the number of milliseconds
    //since January 1, 1970, to the date specified by the instance of the Date object;
    //therefore, the windowName will almost always be unique.
    var dtm = new Date();
    windowName = dtm.getTime();
  }

  //if (url == null) { url = "" };
  if (w == null) { w = 640 };
  if (h == null) { h = 480 };
  if (sb == null) { sb = "no" };
  if (r == null) { r = "no" };
  if (mb == null) { mb = "no" };
  if (tb == null) { tb = "no" };
  if (s == null) { s = "no" };
  if (loc == null) { loc = "no" };
  if (dir == null) { dir = "no" };

  var features = new StringBuilder();
  features.Append("directories=" + dir + ",");
  features.Append("location=" + loc + ",");
  features.Append("menubar=" + mb + ",");
  features.Append("resizable=" + r + ",");
  features.Append("scrollbars=" + sb + ",");
  features.Append("status=" + s + ",");
  features.Append("toolbar=" + tb + ",");
  features.Append("width=" + w + ",");
  features.Append("height=" + h + ",");
  features.Append("top=" + diffHeight + ",");
  features.Append("left=" + diffWidth);

  return window.open(url, windowName, features.ToString());
}

// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value)
{
  this.strings = new Array("");
  this.Append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.Append = function(value)
{
  if (value)
  {
    this.strings.push(value);
  }
}

// Appends the given value to the end of this instance with a line break.
StringBuilder.prototype.AppendLine = function(value)
{
  if (value)
  {
    this.strings.push(value + "\n");
  }
}

// Clears the string buffer
StringBuilder.prototype.Clear = function()
{
  this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.ToString = function()
{
  return this.strings.join("");
}

