//----------------------------------------------------------
// 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.
//----------------------------------------------------------

// Image processing utilities
//
// Functions included:
//  imageNoOp()
//  imageStartSwap()
//  swapImage()
//  nextImage()
//  prevImage()
//  changeImage()
//  imageAdd()
//  imageInfo()
//  addToPreload()
//  preloadAllImages()
//  preloadImage()
//  imageLinkEx()
//  opacity()
//  changeOpac()
//  blendimage()
//  startBlending()

// global variables
var preImageArray = new Array()
var imageArray = new Array()
var imageIdx = 0;

var imagecont = true

function imageNoOp() {
  
}

function imageStartSwap(dtime,cont)
{
  imageIdx = 0
  if (cont != null)
    imagecont = cont

  if (imageArray.length > 0)
    setTimeout(imageSwap, dtime)
}

function swapImage(name, src)
{
  eval("document." + name + ".src='" + src + "'");
}

function swapImageById(name, src)
{
  document.getElementById(name).src = src;
}


function imageSwap()
{
 if (imageIdx < imageArray.length)
  {
    swapImage(imageArray[imageIdx].iname, imageArray[imageIdx].src);
    imageIdx++;
  }

  if (imageIdx < imageArray.length)
    setTimeout(imageSwap, imageArray[imageIdx].dtime);
  else if (imagecont)
  {
    imageIdx = 0;
    setTimeout(imageSwap, imageArray[imageIdx].dtime);
  }
}

function nextImage(imgPrevId, imgNextId, imgEnlargeId) 
{
  if (imageIdx < imageArray.length - 1)
  {
    imageIdx++;
//    swapImage(imageArray[imageIdx].iname, imageArray[imageIdx].src);
    swapImageById(imageArray[imageIdx].iname, imageArray[imageIdx].src);


    if (imageIdx == imageArray.length - 1) {
	document.getElementById(imgNextId).style.visibility = "hidden";
    }

    if (imageIdx > 0)
	document.getElementById(imgPrevId).style.visibility = "visible";  

    showEnlargeUrl(imgEnlargeId, imageArray[imageIdx]);
  } 
}

function prevImage(imgPrevId, imgNextId, imgEnlargeId)
{
  if (imageIdx > 0)
  {
    imageIdx--;
//    swapImage(imageArray[imageIdx].iname, imageArray[imageIdx].src);
    swapImageById(imageArray[imageIdx].iname, imageArray[imageIdx].src);

    if (imageIdx == 0) {
	document.getElementById(imgPrevId).style.visibility = "hidden";
    }

    if (imageIdx < imageArray.length - 1)
	document.getElementById(imgNextId).style.visibility = "visible";  

    showEnlargeUrl(imgEnlargeId, imageArray[imageIdx]);
  }
}

function showEnlargeUrl(imgEnlargeId, imgInfo) 
{
    if (imgEnlargeId != null)
    {
      var imgEl = document.getElementById(imgEnlargeId);
      if (imgInfo.enlargeUrl != null) {
	imgEl.style.visibility = "visible";
        imgEl.href = imgInfo.enlargeUrl;
      }  else
	imgEl.style.visibility = "hidden";
    }

}

function changeImage(imgobject, src)
{
  imgobject.src = src
}


function imageAdd(name, src, dtime, eUrl)
{
  if (imageArray == null)
    imageArray = new Array()

  imageArray[imageArray.length] = new imageInfo(name, src, dtime, null, null, null, eUrl)
}

// This function is the imageInfo object.  It is used to hold information about an image
// without having that image load into memory.  Using the JavaScript Image object would
// load the image into memory as soon as the 'src' attribute is set.  This object allows
// the loading to be put off until later in the loading of the page.
// param - name The name of the image
// param - src The URL of the source of the image
// param - dtime The display time when the image is used in a fade or rotating
// param - ht The height of the image
// param - wd The width of the image
// param - alt The alternate text of the image

function imageInfo(name, src, dtime, ht, wd, alt, eUrl)
{
  this.iname = name;
  this.src = src;
  this.dtime = dtime;
  if (ht != null)
	this.height = ht;
  if (wd != null)
	this.width = wd;
  if (alt != null)
	this.alt = alt;
  if (eUrl != null)
	this.enlargeUrl = eUrl;
}

// This function adds image information to an array.  This array is then used
// with the 'preloadAllImages()' function.  This function should be used in the 'onLoad' 
// for the body.
// param - imgname The image name
// param - src The URL to the image
// param - ht The height of the image
// param - wd The width of the image
// param - alt The alternate text for the image

function addToPreload(imgname, src, ht, wd, alt)
{
  preImageArray[preImageArray.length] = new imageInfo(imgname, src, 0, ht, wd, alt)
}

// preloads an images that reside in the 'preImageArray' into memory.  
// This function should be used in the 'onLoad' event of the body tag.

function preloadAllImages()
{
  var idx
  var image

  for (idx = 0; idx < preImageArray.length; idx++)
    preloadImage(preImageArray[idx].src, preImageArray[idx].height, preImageArray[idx].width)
}

// preloads an image into memory.  Allows 'rollover' images to perform the rollover faster
// param - src The URL of the image
var imageArray_x = new Array()
function preloadImage(src, ht, wd)
{
  var image

  if (ht != null && wd != null)
  {
    if (ht > 0 && wd > 0)
      image = new Image(wd, ht)
    else
      image = new Image()
  }
  else
    image = new Image()

  image.src = src
  imageArray_x[imageArray_x.length] = image
}

//
// This function creates an HREF with an image.  It optionally allows the image
// to be rolled over using the mouseOver and mouseOut HREF attributes.  When a 
// rollover image is used, the second image is placed into an array.  This array
// does not 'pre-load' the images right away.  So, you will need to call preloadAllImages()
// after the page has loaded to make the second images load faster.
// param - name The name of the IMAGE which is referenced in the href
// param - link The URL of the HREF
// param - img1 The initial image to show in the link
// param - img2 If present, the image to show for the mouseOver
// param - ht The height of the image
// param - wd The width of the image
// param - alt The ALTernate text to display

function imageLinkEx(name, link, img1, img2, ht, wd, alt, target, x_over, x_out)
{
  var over_text = ""
  var out_text = ""

  switch (arguments.length)
  {
    case 8:
      x_over = ""
    case 9:
      x_out = ""
  }

  document.write("<a href=\"", link, "\"")

  if (img2.length > 1)
  {
    over_text = "changeImage(" + name + ",\"" + img2 + "\")"
    out_text = "changeImage(" + name + ",\"" + img1 + "\")"
//    document.write(" onMouseover='changeImage(",name, ",\"", img2, "\")' onMouseout='changeImage(", name, ",\"", img1 ,"\"")
    addToPreload(name, img2, ht, wd, alt)    

//    document.write(")' ")
  }

  if (x_over.length > 0)
    over_text += (";" + x_over)

  if (x_out.length > 0)
    out_text += (";" + x_out)

  if (over_text.length > 0)
    document.write(" onMouseover='" + over_text + "'")

  if (out_text.length > 0)
    document.write(" onMouseout='" + out_text + "'")
  
  if (target != null && target.length > 0)
    document.write(" target=\"", target, "\"")

  document.write("><img border='0' name='", name, "' src='", img1, "' ")

  if (wd > 0)
    document.write(" width='", wd, "' ")

  if (ht > 0)
    document.write(" height='", ht, "' ")

  document.write(" alt='", alt, "'></A>")
}

function showImage(src, w, h, name, border, alt, align, vspace, hspace)
{
  var wd = "" 
  var ht = ""
  var iname = ""
  var bordersize = ""
  var alter_text = ""
  var vertspace = ""
  var horzspace = ""
  var align_it = ""

  switch (showImage.arguments.length)
  {
    case 9:
      horzspace = " hspace=\"" + hspace + "\" "     
    case 8:
      vertspace = " vspace=\"" + vspace + "\" "
    case 7:
      align_it = " align=\"" + align + "\" "
    case 6:
      alter_text = " alt=\"" + alt + "\" "
    case 5:
      bordersize = " border=\"" + border + "\" "
    case 4:
      iname = " name=\"" + name + "\" "
    case 3:
      ht = " height=\"" + h + "\" "
    case 2:
      wd = " width=\"" + w + "\" "
  }

  document.write("<img src=\"", src, "\" ", wd, ht, iname, bordersize, alter_text, 
		vertspace, horzspace, align_it, ">")
  
}
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    
    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
    
    //make image transparent
    changeOpac(0, imageid);
    
    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for(i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
        timer++;
    }
}

/* 
// The startBlending function req's a div like the one below:
<div id="blenddiv" style="background-image: url(images/firstimage.jpg); background-repeat: no-repeat;" >
    <img id="blendimage" src="images/firstimage.jpg" style="filter: alpha(opacity=0); -moz-opacity: 0; opacity: 0;" alt="alt wording" />
</div>

*/
function startBlending(imagename, showtime, blendtime) {

	blendimage('blenddiv',imagename, imageArray[imageIdx++].src, blendtime);

	if (imageIdx == imageArray.length)
		imageIdx = 0;

	setTimeout("startBlending('" + imagename + "'," + showtime + "," + blendtime + ")", showtime);
}


var divBlendingArray = new Array();
var divBlendingIdx = 0;

function addBlendingDivId(divId, hide)
{
  divBlendingArray[divBlendingArray.length] = divId;
}


// Div blending
function initBlendingDivs(showtime, blendtime)
{
	setTimeout("startBlendingDivs(" + showtime + "," + blendtime + ")", showtime);
}

function startBlendingDivs(showtime, blendtime) 
{
	var curDiv;
	var nextDiv;

	curDiv = divBlendingArray[divBlendingIdx++];
	if (divBlendingIdx == divBlendingArray.length)
		divBlendingIdx = 0;
	nextDiv = divBlendingArray[divBlendingIdx];

	blendDivs(curDiv, nextDiv, blendtime);

	setTimeout("startBlendingDivs(" + showtime + "," + blendtime + ")", showtime);
}

function blendDivs(divIdOut, divIdIn, millisec)
{
    var speed = Math.round(millisec / 100);
    var timer = 0;
    
    //fade out and in
    for(i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + (100 - i) + ",'" + divIdOut + "')",(timer * speed));
	setTimeout("changeOpac(" + i + ",'" + divIdIn + "')",(timer * speed));
        timer++;
    }
}

