var imageNavigator = null;



function GalleryNavigator(displayElement)
{
  if (displayElement==undefined)
  {
     alert("The display element for a GalleryNavigator must be defined.");
     return;
  }

  this.startIndex = 0;  
  this.displayElement = displayElement;
}

function GalleryNavigator_right()
{
  if (this.startIndex>=images.length)
     return;

  this.startIndex++;
  this.updateDisplay();
}

function GalleryNavigator_left()
{
  if (this.startIndex<=0)
     return;

  this.startIndex--;
  this.updateDisplay();
}

function GalleryNavigator_updateDisplay()
{
  removeAllChildrenFrom(this.displayElement);

 var maxWidth = this.displayElement.clientWidth;

  if (maxWidth==0 || maxWidth==undefined)
     maxWidth = this.displayElement.offsetWidth;

 var total = 0;
 var btn = createActiveButton("images/arrow_left.png","images/arrow_left_dark.png","imageNavigator.left()"
    ,this.startIndex>0);


  // add the shift left button.
  this.displayElement.appendChild(btn);

 total+=btn.offsetWidth;

 var expandsRight = false;

  // add the images.
  for (var i=this.startIndex;i<images.length;i++)
  {
    var img = images[i].toThumb();


      this.displayElement.appendChild(img);

       total+=img.offsetWidth;

      if (total+150>maxWidth)
      {
         if (i+1<images.length)
         {
          // alert("i = "+i+", images.length="+images.length);

           expandsRight = true;
         }

         break;
      }
  }

  // add the shift right button.
  this.displayElement.appendChild(createActiveButton("images/arrow_right.png","images/arrow_right_dark.png","imageNavigator.right()"
     ,expandsRight  && (this.startIndex<images.length)));

}

function createActiveButton(imageURI,imageOutURI,clickScript,active)
{
  var result = document.createElement("img");
   result.setAttribute("src",imageOutURI);

 if (active)
 {
   result.style.cursor = "pointer";
   result.onmouseover=new Function("this.setAttribute('src','"+imageURI+"')");
   result.onmouseout=new Function("this.setAttribute('src','"+imageOutURI+"')");
   result.onclick=new Function(clickScript);
 }

  return result;
}

GalleryNavigator.prototype.right = GalleryNavigator_right;
GalleryNavigator.prototype.left = GalleryNavigator_left;

GalleryNavigator.prototype.updateDisplay = GalleryNavigator_updateDisplay;


function Image(thumbURI,largeURI,description)
{
  this.thumbURI = thumbURI;
  this.largeURI = largeURI;
  this.description = description;
}

function Image_toThumb()
{
 var result = document.createElement("a");
  result.setAttribute("href",this.largeURI);
 var img = document.createElement("img");

  img.setAttribute("src",this.thumbURI);
  img.setAttribute("alt",this.description);
  result.appendChild(img);

 return result;
}

Image.prototype.toThumb = Image_toThumb;

function getImageBanner()
{
  return document.getElementById("galleryBanner");
}

function removeAllChildrenFrom(e)
{
  while (e.firstChild)
    e.removeChild(e.firstChild);
}

function initImageGallery()
{
  imageNavigator = new GalleryNavigator(getImageBanner());
}

function updateImageBanner()
{
  imageNavigator.updateDisplay();
}

/**
 images to be displayed in the banner
*/
var images = new Array(
new Image("images/stained%20glass%20small2.jpg","","stained glass window",""),
new Image("images/church inside_thumb.png","images/church inside_large.png",
"Central United Church Interior",
"Looking from the center of the church, this image shows how "
+"the upper and lower level seating areas are laid out.  "
+"Central United Church offers a soothing atmosphere to enjoy each Sunday."),
new Image("images/front thumb.png","images/front large.png","Central United Church front",
"The centerpiece is a Holy Cross with a copy of the Holy Bible."),
new Image("images/view from upper level_thumb.png","images/view from upper level_large.png",
"Central United Church view from upper level",
"The upper level offers a great view of the minister and the choir.  "
+"The sound travels well all through the church so you can clearly "
+"hear the minister's words and the beautiful sounds of singing and our musical instruments.")
);


