//
// Filename: global.js
// Copyright (c) 2000-2008 Fox Smart, Inc.  All Rights Reserved.
//
// Revision History
//
// Name              Date         Description
// ===========================================================================
// Andrew Pach       APR-27-2006  Initial Creation
//

// Preload all the immages
function MM_preloadImages()
{
   var d=document;
   if (d.images)
   {
      if (!d.MM_p)
         d.MM_p=new Array();
      var i,j=d.MM_p.length, a=MM_preloadImages.arguments;
      for (i=0; i<a.length; i++)
         if (a[i].indexOf("#")!=0)
         {
            d.MM_p[j]=new Image;
            d.MM_p[j++].src=a[i];
         }
   }
}

// Find an object
function MM_findObj(n, d)
{
   var p,i,x;
   if (!d)
      d=document;
   if ((p=n.indexOf("?"))>0&&parent.frames.length)
	{
      d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
   }
   if (!(x=d[n])&&d.all)
      x=d.all[n];
   for (i=0;!x&&i<d.forms.length;i++)
      x=d.forms[i][n];
   for (i=0;!x&&d.layers&&i<d.layers.length;i++)
      x=MM_findObj(n,d.layers[i].document);
   if (!x && d.getElementById)
      x=d.getElementById(n);
   return x;
}

// Swap an image and restore the previous image
function MM_swapImgRestore()
{
   var i,x,a=document.MM_sr;
   for (i=0; a&&i < a.length&&(x=a[i])&&x.oSrc; i++)
      x.src=x.oSrc;
}

// Swap an image
function MM_swapImage()
{
   var i,j=0,x,a=MM_swapImage.arguments;
   document.MM_sr=new Array;
   for (i=0; i < (a.length-2); i+=3)
      if ((x=MM_findObj(a[i]))!=null)
      {
         document.MM_sr[j++]=x;
         if (!x.oSrc)
           x.oSrc=x.src;
         x.src=a[i+2];
      }
}

// ***** Montage Script for Updating Random Images on a Tag *****

// Cache arrays for montage
var tagArray = null;
var imgArray = null;

// Update a tag with a random image after a random amount of time.
// 'tags' should be an array of tag Ids for the tag whose image should be updated.
// 'imgs' should be an array of image locations for the images to randomly display.
// 'minIntervals' should be an array of minimum intervals in milliseconds to wait before changing a montage image.
// 'maxIntervals' should be an array of maximum intervals in milliseconds to wait before changing a montage image.
function montage(tags, imgs, minIntervals, maxIntervals)
{
  // alert("Montage function called.");

  // Store the passed in images in a local array
  tagArray = new Array(tags.length);
  imgArray = new Array(imgs.length);
  minIntervalArray = new Array(minIntervals.length);
  maxIntervalArray = new Array(maxIntervals.length);
  for (i=0; i < imgs.length; i++)
  {
    tagArray[i] = tags[i];
    imgArray[i] = imgs[i];
    minIntervalArray[i] = minIntervals[i];
    maxIntervalArray[i] = maxIntervals[i];
  }

  // Call montage update to perform the main processing
  montageUpdate();
}

// Montage variables for image loading callbacks
var montageNewImageAllowed = true;
var montageNewTag = null;
var montageNewUrl = null;
var montageNewImageCounter = 0;

// Perform a montage update
function montageUpdate()
{
  // Come up with a random number to select a unique animation to use
  var cycle = Math.floor(Math.random() * imgArray.length);
  // alert("Selected position in the image array: " + cycle);

  // Check the semiphore to see if we are allowed to display a new image
  if (montageNewImageAllowed == true)
  {
    // Reset the counter
    montageNewImageCounter = 0;

    // Set a blocking semaphore to ensure no other images are changed until we are
    // finished with this one.
    montageNewImageAllowed = false;

    // Get the tag for the image we want to update
    montageNewTag = window.document.getElementById(tagArray[cycle]);

    // Come up with a URL for the newly selected image.
    // We will append a "?variable=" plus a random number to ensure the URL is
    // unique each time.  This will ensure the image is reloaded since some browsers will
    // cache the image and thus cause any animated GIF's to no longer animate.
    montageNewUrl = imgArray[cycle] + "?variable="+Math.random()*99999

    // Preload the newly selected image and set a callback to display the image once it
    // has finished loading.
    var loadImage=new Image;
    loadImage.onload = montageNextImageLoadedCallback
    loadImage.onerror = montageNextImageLoadErrorCallback
    loadImage.onabort = montageNextImageLoadAbortCallback
    loadImage.src=montageNewUrl;
  }
  else
  {
    // Update the counter
    montageNewImageCounter = montageNewImageCounter + 1;
    // alert("Incrementing counter to " + montageNewImageCounter);

    // If we reached our wait limit, just allow a new image to be displayed
    if (montageNewImageCounter > 1)
    {
       // alert("Counter limit hit.  Resetting the image loader.");
       montageNewImageCounter = 0;
       montageNewImageAllowed = true;
    }
  }

  // Come up with a random amount of time to wait before being called again
  delayMillis = minIntervalArray[cycle] +
     (maxIntervalArray[cycle] - minIntervalArray[cycle]) * Math.random();
  // alert("Delay: " + delayMillis);

  // Ask to be called back again in the specific number of milliseconds
  setTimeout("montageUpdate()", delayMillis);
}

// Callback function that will display the next image after it has finished loading
function montageNextImageLoadedCallback()
{
  // The image has finished loading so display it in the new tag
  montageNewTag.style.backgroundImage = "url(" + montageNewUrl + ")";

  // Unblock the semiphone to allow the next image to be displayed
  montageNewImageAllowed = true;
}

// Callback function in case the next image didn't load properly
function montageNextImageLoadErrorCallback()
{
  // Unblock the semiphone to allow the next image to be displayed
  // There's nothing else we can really do since we can't display the next image.
  montageNewImageAllowed = true;
}

// Callback function in case the next image load got aborted by the user
function montageNextImageLoadAbortCallback()
{
  // Unblock the semiphone to allow the next image to be displayed
  // There's nothing else we can really do since we can't display the next image.
  montageNewImageAllowed = true;
}

// Sets the status bar text
function setStatus(statusText)
{
	window.status = statusText;
	return true;
}

// Submits a form after setting a field to a specified value.
function submitForm(formObject, submitFieldId, submitFieldValue)
{
   // Lookup the field
   var submitField = document.getElementById(submitFieldId);

   // Set the field value on the desired field
   submitField.value = submitFieldValue;

   // Submit the form
   formObject.submit();
}
