/* copyright (c) Robert E Walker 2006 */


/********************************/
// Initialization

if ('undefined' == typeof(basepath))
{
 var basepath = '';
}// if 

// used to pause other processes (ie: video playback) until
// the cycle link is fulling initialized.
var cycleLinkIsLoaded = false;

// Data for the video testimonials link.
var videoLinks = [
                  {'href':basepath + 'testimonials/coad_d_myjob.html',
                   'thumbnail':basepath + 'testimonials/images/coad_d_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;I love coaching.'),
                   'width':150},

                  {'href':basepath + 'testimonials/coad_d_coaching.html',
                   'thumbnail':basepath + 'testimonials/images/coad_d_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;It&rsquo;s a proprietary \nprocess ...'),
                   'width':150},

                  {'href':basepath + 'testimonials/coad_d_50hires.html',
                   'thumbnail':basepath + 'testimonials/images/coad_d_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;What I&rsquo;m most \nproud about ...'),
                   'width':150},

                  {'href':basepath + 'testimonials/elmandjra_m_ind_video.html',
                   'thumbnail':basepath + 'testimonials/images/elmandjra_m_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;It&rsquo;s a very stable \nindustry.'),
                   'width':150},
                   
                  {'href':basepath + 'testimonials/gilhooly_e_ind_video.html',
                   'thumbnail':basepath + 'testimonials/images/gilhooly_e_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;It&rsquo;s a growing \nindustry.'),
                   'width':160},
                   
                  {'href':basepath + 'testimonials/lippert_t_ind_video.html',
                   'thumbnail':basepath + 'testimonials/images/lippert_t_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;It&rsquo;s a terrific place \nfor sales people.'),
                   'width':150},
                   
                  {'href':basepath + 'testimonials/moberg_t_ind_video.html',
                   'thumbnail':basepath + 'testimonials/images/moberg_t_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;It&rsquo;s a great \nindustry \nto work in.'),
                   'width':150},
                   
                  {'href':basepath + 'testimonials/elmandjra_m_video.html',
                   'thumbnail':basepath + 'testimonials/images/elmandjra_m_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;Large company discipline, \nsmall company experience ...'),
                   'width':160},
                   
                  {'href':basepath + 'testimonials/lippert_t_video.html',
                   'thumbnail':basepath + 'testimonials/images/lippert_t_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;My track record with \nDEC is 100%.'),
                   'width':150},
                   
                  {'href':basepath + 'testimonials/moberg_t_video.html',
                   'thumbnail':basepath + 'testimonials/images/moberg_t_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;We&rsquo;ve had a great \ntrack record ...'),
                   'width':150},
                   
                  {'href':basepath + 'testimonials/gilhooly_e_video.html',
                   'thumbnail':basepath + 'testimonials/images/gilhooly_e_video_thumb.jpg',
                   'caption':makeSpan('&ldquo;He has integrity ... and \nreally knows the business.'),
                   'width':150}
                   
                 ];
// Create a new CycleLink and attach to the document.
function makeCycleLink (elementId)
{
 try
 {
  var element = document.getElementById(elementId);
  document.cycleVideoLink = new CycleLink(element);
  document.cycleVideoLink.linksList = randomizeArray(videoLinks);
//  document.cycleVideoLink.linksList = videoLinks;
  document.cycleVideoLink.nextLink();
  cycleLinkIsLoaded = true;
 }
 catch (error)
 {
  alert (error.name +': '+ error.message);
 }// try
}// function makeCycleLink

/********************************/
// Object CycleLink

/********************************/
// class methods
CycleLink.prototype.nextLink = CycleLinkNextLink;
CycleLink.prototype.displayLink = CycleLinkDisplayLink;

// class properties
CycleLink.prototype.link;
CycleLink.prototype.thumbnail;
CycleLink.prototype.caption;
CycleLink.prototype.linksList;
CycleLink.prototype.currentLink = -1;
CycleLink.prototype.captionedImage;
CycleLink.prototype.frequency = 4000;

/********************************/
function CycleLink (linkObject)
{
 if (isLinkObject(linkObject))
 {
  this.link = linkObject;
 }
 else
 {
  throw new Error('CycleLink. Failed to create new CycleLink: linkObject is not an anchor.');
 }// if

 this.thumbnail = getThumbnail(this.link, 'video_thumb');
 this.caption = getCaption(this.link, 'video_caption');
 
}// function CycleLink

function CycleLinkNextLink ()
{
 this.currentLink++;
 if (this.currentLink >= this.linksList.length)
 {
  this.currentLink = 0;
 }// if
 this.displayLink(this.currentLink);
 setTimeout('document.cycleVideoLink.nextLink()', this.frequency);
}// function CycleLinkNextLink

function CycleLinkDisplayLink (index)
{
 try
 {
  this.link.setAttribute ('href', this.linksList[index].href);
  this.thumbnail.src = this.linksList[index].thumbnail;
  replaceAllChildNodes(this.linksList[index].caption, this.caption);
  this.link.style.width = this.linksList[index].width + 'px';
 }
 catch (error)
 {
  throw new Error('CycleLink.displayLink: failed to display link ' + index + ':: ' + error.name + ": " + error.message);
 }// try
}// function CycleLinkDisplayLink

/********************************/
// Object CaptionedImage

// NOTE: No longer being used.

/********************************/
// class methods

// class properties
CaptionedImage.prototype.image;
CaptionedImage.prototype.caption;

/********************************/
function CaptionedImage (element)
{
 if (element.hasChildNodes())
 {
  this.image = null;
  this.caption = null;
  var children = element.childNodes;
  var child;
  var count = 0;
  while (((null == this.image) && (null == this.caption)) || (count < children.length))
  {
   child = children[count];
   switch (child.id)
   {
    case 'thumb':
    {
	 this.image = child;
     break;
    }
    case 'caption':
    {
     this.caption = child;
    }
   }// switch
   count++;
  }// while
 }
 else
 {
  throw new Error('CaptionedImage. Failed to create new CaptionedImage: the element has no child elements');
 }// if
}// function CaptionedImage


/********************************/
// Utilities

/********************************/
function replaceAllChildNodes (sourceElement, destinationElement)
{
 removeAllChildNodes(destinationElement);
 // copy all child nodes from source to destination
 var newChildren = sourceElement.childNodes;
 for (var index = 0; index < newChildren.length; index++)
 {
  destinationElement.appendChild(newChildren[index].cloneNode(true));
 }// for
}// replaceAllChildNodes

/********************************/
function removeAllChildNodes (element)
{
 while (element.firstChild)
 {
  element.removeChild(element.firstChild);
 }// while
}// function removeAllChildNodes

/********************************/
function makeSpan (text)
{
 // opening quote
 text = text.replace('&ldquo;',String.fromCharCode(8220));
 // apostrophe
 text = text.replace('&rsquo;',String.fromCharCode(8217));
 // split at newline
 var substrings = text.split('\n');
 var newText = document.createElement('span');
 var linebreak = document.createElement('br');
 // reassemble with replaced linebreak
 for (var index=0; index < substrings.length; index++)
 {
  newText.appendChild(document.createTextNode(substrings[index]));
  // don't put linebreak after last substring
  if (index < substrings.length -1)
  {
   newText.appendChild(linebreak);
  }// if
 }// for
 return newText;
}// function makeSpan

/********************************/
function isLinkObject (element)
{
 // Returns true if element is an anchor element, otherwise
 // returns false.
 var result = false;
 if ('object' == typeof element)
 {
  if ('A' == element.tagName)
  {
   result = true;
  }
 }// if
 return result;
}// function isLinkObject
 
/********************************/
function getThumbnail (element, thumbnail_id)
{
 var thumbnail;
 if (thumbnail = getChildElementById(element, thumbnail_id))
 {
  return thumbnail;
 }
 else
 {
  throw new Error('getThumbnail. Failed to find child element with id='+thumbnail_id);
 }// if
}// function getThumbnail

/********************************/
function getCaption (element, caption_id)
{
 var caption;
 if (caption = getChildElementById(element, caption_id))
 {
  return caption;
 }
 else
 {
  throw new Error('getCaption. Failed to find child element with id='+caption_id);
 }// if
}// function getCaption

/********************************/
function getChildElementById (element, child_id)
{
 var foundChild = null;
 if (element.hasChildNodes())
 {
  var children = element.childNodes;
  var child;
  var count = 0;
  var done = false;
  while (!done)
  {
   child = children[count];
   if (child_id == child.id)
   {
    foundChild = child;
    done = true;
   }
   else
   {
	count++;
	if (count >= children.length)
	{ 
	 done = true;
	}// if
   }// if
  }// while
 }// if
 return foundChild;
}// function getChildById

/********************************/
function randomizeArray (anArray)
{
 if ('object' == typeof anArray)
 {
  var index;
  var resultArray = new Array();
  while (anArray.length > 0)
  {
   index = Math.round(randomRange(0, anArray.length-1));
   resultArray[resultArray.length] = anArray.splice(index, 1)[0]; // index [0] is kludge to prevent nested objects in resultArray.
  }// while
  return resultArray;
 }
 else
 {
  throw new Error('randomizeArray: failed to randomize array; input is ' + typeof anArray + ' but should be object.');
 }// if
}// function randomizeArray

/********************************/
function randomRange (min, max)
{
 return (Math.random() * (max-min)) + min;
}// function randomRange