//----------------------------------------------------------
// Copyright - Gallaware, Inc. 2000
// All rights reserved
// These JavaScript functions are copyrighted by Gallaware, Inc.
// They can not be used, copied, altered, edited, or included 
// with or within any software, or published or distributed without
// the expressed written consent of Gallaware, Inc.
//----------------------------------------------------------
var xClickPanoramaArray = new Array()
var xImageButtons = false

// The Click Panorama Object
// this Object is used in conjuction with the ClickPanorama functions
// name - unique name starting with a character.  Must contain only characters, numbers or underscore
// x_length - the length of the horizontal view.  If the view contains 4 pictures horizontally
//            then this parameter would be 4
// y_length - the length of the vertical view.  If the view contains 5 pictures vertically
//            then this parameter would be 5
// x_continue - if true then when the user clicks the button to move beyond the last picture
//		to the right in the horizontal view, then the first picture is displayed. 
//		Otherwise, the view stops moving right.  The same applies when moving left.
// y_continue - if true, then when the user clicks the button to move beyond the last picture
//              to the bottom in the vertical view, then the first picture is displayed.
// 		Otherwise, the view stops moving down.  The same applies when moving up.
// startwithX - the horizontal picture index to start with.  This is a zero (0) based index.  So, if the 
// 		x_length is 4 and the starting picture is the 2nd picture, then its index would
//		be 1.
// startwithY - the vertical picture index to start with.  This is a zero (0) based index.  So, if the 
// 		x_length is 4 and the starting picture is the 2nd picture, then its index would
//		be 1.  If you have only a single horizontal panorama view, then this index would be 0.
// width - the width of the displayed picture
// height - the height of the displayed picture
// alt - the text to display as the alternate text for the image
function clickPanoramaObj(name, x_length, y_length, x_continue, y_continue, startwithX, startwithY,
			width, height, alt)
{
  var idx

  this.name = name
  this.x_length = (x_length <= 0 ? 1 : x_length)
  this.y_length = (y_length <= 0 ? 1 : y_length)

  // create the array
  // the x_length is the horiztonal length - this is always at least 1
  // y_length is the vertical length - this is always at least 1
  this.images = new Array(this.y_length)
  for (idx = 0; idx < this.y_length; idx++)
    this.images[idx] = new Array(this.x_length)

  this.isContinuousX = (typeof x_continue == "boolean" ? x_continue : false)
  this.isContinuousY = (typeof y_continue == "boolean" ? x_continue : false)

  this.startwithX = (startwithX < 0 ? 0 : startwithX)
  this.startwithY = (startwithY < 0 ? 0 : startwithY)

  this.currentX = startwithX
  this.currentY = startwithY

  this.height = height
  this.width = width

  this.alt = alt

  // add to the global array
  xClickPanoramaArray[xClickPanoramaArray.length] = this
}

// adds a picture to the clickPanoramaObj object
// pictures are added in a serial order starting with index (0,0) and ending with 
// index (n,n).  For example, the first picture is added at index (0,0).  The next 
// picture is added at (0,1)
// panoOjb - existing clickPanoramaObj
// src - the URL of the image
function addPanoImage(panoObj, imgsrc)
{
  var x_idx
  var y_idx

  for (y_idx = 0; y_idx < panoObj.images.length; y_idx++)
  {
    for (x_idx = 0; x_idx < panoObj.images[y_idx].length; x_idx++)
      if (panoObj.images[y_idx][x_idx] == null)
      {
	panoObj.images[y_idx][x_idx] = imgsrc
        return
      }
  }
}

function setPanoImage(panoObj, src, x, y)
{
  if (x > -1 && y > -1)
    if (y < panoObj.images.length && x < panoObj.images[y].length)
      panoObj.images[y][x] = src
}

// displays a series of pictures in an attempt to provide
//   a scrolling panoramic 
// border - the size of the image border
function showPano(panoObj, border, buttons, imagebuttons)
{
  var doc = document
  var myborder = 0
  var yesbuttons = true

  if (arguments.length >= 2)
    myborder = border

  if (arguments.length >= 3)
    yesbuttons = buttons

  if (arguments.length >= 4)
    xImageButtons = imagebuttons

  // start form
//  if (yesbuttons)
//    doc.write("<form>")

  // start table
  doc.write("<table cellspacing=\"0\" cellpadding=\"0\"><tr><td colspan=\"4\" align=\"center\">")
  
  // display the image
  doc.write("<img src=\"")
  doc.write(panoObj.images[panoObj.currentY][panoObj.currentX])
  doc.write("\" name=\"")
  doc.write(panoObj.name)
  doc.write("\" height=\"")
  doc.write(panoObj.height)
  doc.write("\" width=\"")
  doc.write(panoObj.width)
  doc.write("\" alt=\"")
  doc.write(panoObj.alt)
  doc.write("\" border=\"")
  doc.write(myborder)
  doc.write("\">")

  // end the image cells
  doc.write("</td></tr>")

  if (yesbuttons)
  {
    doc.write("<tr>")
    doc.write("<td align=\"center\">")

    showPanoButtons(panoObj, yesimagebuttons)

    doc.write("</td></tr>")
  }

  doc.write("</table>")

  // end the form
//  doc.write("</form>")
}

// displays the buttons separately from the images
function showPanoButtons(panoObj, imagebuttons)
{
  var doc = document
  var pre_button = ""
  var post_button = ""
  
  if (arguments.length >= 2)
    xImageButtons = imagebuttons

  if (xImageButtons)
  {
    // image buttons use links instead of 'form buttons'
    pre_button = "<a href=\"\" onClick=\""
    post_button = "</a>"
  }
  else
  {
    pre_button = "<input onClick=\""
    post_button = ">"
  }
 
  // start form and table
  doc.write("<form><table cellspacing=\"2\" cellpadding=\"0\">")
  
  // start the buttons cells
  doc.write("<tr>")
  // display the next and prev buttons
  // left button
  doc.write("<td align=\"left\" valign=\top\">")

  doc.write(pre_button)
  doc.write("clickPanoLeft('")
  doc.write(panoObj.name)
  doc.write("') ")
  if (xImageButtons)
  {
    doc.write(";return false;\"><img src=\"images/left.gif\" name=\"LeftImage\" alt=\"Left\" border=\"0\">")
  }
  else
  {
    doc.write("\" type=\"button\" ")
    doc.write(" name=\"Left\" value=\"&lt;&lt; Left\">")
  }
  doc.write("</td>")

  // up button
  if (panoObj.images.length > 1)
  {
    doc.write("<td align=\"center\" valign=\top\">")
    doc.write(pre_button)
    doc.write("clickPanoUp('")
    doc.write(panoObj.name)
    doc.write("') ")
    if (xImageButtons)
    {
      doc.write(";return false;\"><img src=\"images/up.gif\" name=\"UpImage\" alt=\"Up\" border=\"0\">")
    }
    else
    {
      doc.write("\" type=\"button\" ")
      doc.write(" name=\"Up\" value=\"Up\">")
    }
  }
  doc.write("</td>")

  // down button
  doc.write("<td align=\"center\" valign=\top\">")
  if (panoObj.images.length > 1)
  {
    doc.write(pre_button)
    doc.write("clickPanoDown('")
    doc.write(panoObj.name)
    doc.write("') ")
    if (xImageButtons)
    {
      doc.write(";return false;\"><img src=\"images/down.gif\" name=\"DownImage\" alt=\"Down\" border=\"0\">")
    }
    else
    {
      doc.write("\" type=\"button\" ")
      doc.write(" name=\"Down\" value=\"Down\">")
    }
  }
  doc.write("</td>")

  // right button
  doc.write("<td align=\"right\" valign=\top\">")
  doc.write(pre_button)
  doc.write("clickPanoRight('")
  doc.write(panoObj.name)
  doc.write("') ")
  if (xImageButtons)
  {
    doc.write(";return false;\"><img src=\"images/right.gif\" name=\"RightImage\" alt=\"Right\" border=\"0\">")
  }
  else
  {
    doc.write("\" type=\"button\" ")
    doc.write(" name=\"right\" value=\"Right &gt;&gt;\">")
  }

  //-- end table and form
  doc.write("</td></tr></table></form>")
}


// Moves the panoramic left
function clickPanoLeft(name)
{
  var panoObj = getPanoObj(name)

  if (panoObj != null)
  {
    if (panoObj.currentX > 0)
    {
      panoObj.currentX--
      if (xImageButtons)
        document.images["RightImage"].src = "images/right.gif"
    }
    else if (panoObj.isContinuousX)
      panoObj.currentX = panoObj.images[panoObj.currentY].length - 1

    document.images[name].src = panoObj.images[panoObj.currentY][panoObj.currentX]

    if (xImageButtons)
    {
      if (panoObj.currentX <= 0 && !panoObj.isContinuousX)
        document.images["LeftImage"].src = "images/blank.gif"
    }
  }
}

// Moves the panoramic right
function clickPanoRight(name)
{
  var panoObj = getPanoObj(name)

  if (panoObj != null)
  {
    if (panoObj.currentX < panoObj.images[panoObj.currentY].length - 1)
    {
      panoObj.currentX++
      if (xImageButtons)
        document.images["LeftImage"].src = "images/left.gif"
    }
    else if (panoObj.isContinuousX)
      panoObj.currentX = 0

    document.images[name].src = panoObj.images[panoObj.currentY][panoObj.currentX]

    if (xImageButtons)
    {
      if (panoObj.currentX >= panoObj.images[panoObj.currentY].length - 1 &&
		!panoObj.isContinuousX)
        document.images["RightImage"].src = "images/blank.gif"
    }
  }
}

// Moves the panoramic down
function clickPanoDown(name)
{
  var panoObj = getPanoObj(name)

  if (panoObj != null)
  {
    if (panoObj.currentY > 0)
    {
      panoObj.currentY--
      if (xImageButtons)
        document.images["UpImage"].src = "images/up.gif"
    }
    else if (panoObj.isContinuousY)
      panoObj.currentY = panoObj.images.length - 1

    document.images[name].src = panoObj.images[panoObj.currentY][panoObj.currentX]

    if (xImageButtons)
    {
      if (panoObj.currentY <= 0 && !panoObj.isContinuousY)
        document.images["DownImage"].src = "images/blank.gif"
    }
  }
}

// Moves the panoramic up
function clickPanoUp(name)
{
  var panoObj = getPanoObj(name)

  if (panoObj != null)
  {
    if (panoObj.currentY < panoObj.images.length - 1)
    {
      panoObj.currentY++
      if (xImageButtons)
        document.images["DownImage"].src = "images/down.gif"
    }
    else if (panoObj.isContinuousY)
      panoObj.currentY = 0

    document.images[name].src = panoObj.images[panoObj.currentY][panoObj.currentX]

    if (xImageButtons)
    {
      if (panoObj.currentY >= panoObj.images.length - 1 && !panoObj.isContinuousY)
        document.images["UpImage"].src = "images/blank.gif"
    }
  }
}


// this function returns the clickPanoramaObj or null from the global
// list based on the name.  If the name is not found, it return null
// name - the name given to the clickPanoramObj
function getPanoObj(name)
{
  var idx

  for (idx = 0; idx < xClickPanoramaArray.length; idx++)
  {
    if (xClickPanoramaArray[idx].name == name)
    {
      return xClickPanoramaArray[idx]
    }
  }  

  return null
}

// this function will help in pre-loading the images so that the first time
// the user moves the panorama, the image does not take forever to load.
// this function should be called after the rest of the page has loaded.
// panoObj - the clickPanoramaObj that needs its photos loaded
var imagearray = new Array()
function loadPanoImages(panoObj)
{
  var x_idx
  var y_idx
  var img

  for (y_idx = 0; y_idx < panoObj.images.length; y_idx++)
  {
    for (x_idx = 0; x_idx < panoObj.images[y_idx].length; x_idx++)
    {
      imagearray[imagearray.length] = new Image()
      imagearray[imagearray.length - 1].src = panoObj.images[y_idx][x_idx]
    }
  }
}
