var currPhoto = 0;
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
function getElemsByRel(rel)
{
  aElements = document.getElementsByTagName("a");
  relElems = [];
  for (i = 0; i < aElements.length; i++) 
  {
    //if (aElements[i].hasAttribute("rel") && aElements[i].rel == rel) 
    //{
    if (aElements[i].getAttribute("rel") && aElements[i].getAttribute("rel") == rel) 
    {
      //var hrefURL = aElements[i].href;
      var hrefURL = aElements[i].rev;
      //alert('hrefURL = ' + hrefURL);
      var photoFunction = 'showPhoto("' + hrefURL + '")';
      aElements[i].onclick = new Function("return " +  photoFunction);
    }
  }
}

function showPhoto(url)
{
  url = url.replace("http://www.fedilphotography.com", "");
  currPhoto = galleryArray.indexOf(url);
  //alert('Setting currPhoto(' + url + ') = ' + currPhoto);
  var photoImg = document.getElementById('currentPhoto');
  photoImg.src = url;
  var newImg = new Image();
  newImg.src = photoImg.src;
  //alert('Width:' + newImg.width + ' style:' + newImg.style.width);
  //alert('Width:' + photoImg.width + ' style:' + photoImg.style.width);
  if (newImg.width > "800")
  {
    photoImg.style.width = "800px";
    photoImg.style.height = "";
    if (newImg.height > "600")
    {
      photoImg.style.width = "";
      photoImg.style.height = "600px";
    }
  }
  else
  {
    if (newImg.height > "600")
    {
      photoImg.style.width = "";
      photoImg.style.height = "600px";
    }
    else
    {
      photoImg.style.width = "";
      photoImg.style.height = "";
    }
  }
  //this.style.opacity = "1.0";
  highlightThumb(url);

  return false;
}
function highlightThumb(url)
{
  var thumbList = document.getElementById("gallery").getElementsByTagName("img");
  for (var i = 0; i < thumbList.length; i++)
  {
    var thumbSrc = thumbList[i].src;
    if (thumbSrc.match(url))
    {
      //alert("Match");
      thumbList[i].style.opacity = "1.0";
      thumbList[i].id = "selected";
    }
    else
    {
      //alert(thumbSrc + ' ' + url);
      thumbList[i].style.opacity = "";
      thumbList[i].id = "";
    }
  }
}
function nextPhoto()
{
  var photoURL;
  if (currPhoto == galleryArray.length - 1)
  {
    photoURL = galleryArray[0];
  }
  else
  {
    photoURL = galleryArray[currPhoto + 1];
  }
  showPhoto(photoURL);
}
function prevPhoto()
{
  var photoURL;
  if (currPhoto == 0)
  {
    photoURL = galleryArray[galleryArray.length - 1];
  }
  else
  {
    photoURL = galleryArray[currPhoto - 1];
  }
  showPhoto(photoURL);
}
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
  with (thisform)
  {
  if (validate_required(name,"Please enter your name!")==false)
    {name.focus();return false}
  if (validate_required(email,"Email must be filled out!")==false)
    {email.focus();return false}
  if (validate_email(email,"Invalid email address!")==false)
    {email.focus();return false}
  if (validate_required(body,"You must enter something in the message!")==false)
    {body.focus();return false}
  }
}
function submitForm(thisform)
{
  document.contact.submit();
}

