/*

So. Documentation. This script may seem kind of odd, but trust me, it all makes sense. See, the tried-and-true techniques for image preloading 
don't work reliably in Microsoft Internet Explorer. So the old img.src = "foo"; img.src = 
"bar"; trick won't fly. Instead, we use a seemingly complex double-buffering scheme that 
actually works really well. Here's the idea: First we load an image into one buffer (that is, one img element) and dissolve it in. That process is 
guaranteed to take two seconds, because the dissolve is defined as being of two seconds' duration. Meanwhile, we start loading the next image into the other buffer, which is currently set to display: none. After two seconds, the first buffer has fully dissolved in, and we hold for four seconds. Then we dissolve the first buffer out again, which takes another two seconds.

That's a total of eight seconds. Eight seconds to load a 30 - 50 KB image into a buffer. 5k/s is fairly reasonable.

Once that eight-second, dissolve-in-hold-dissolve-out cycle has finished, we repeat it using the other buffer. (We maintain a global variable, called creatively enough Ã¢â‚¬Å“buffer,Ã¢â‚¬Â that points to whatever the current buffer is. Utility functions called otherBuffer() and swapBuffers() handle the magic.)

*/

var images = new Array;
images.push( ["/images/profile_JenniferSm.jpg", "Jennifer", "Recent Grad", "My name is Jennifer. I'm 21 years old and I just graduated from college...", "schoolsout/"] );
images.push( ["/images/profile_KatieSm.jpg", "Katie", "The Job Hunt", "I'm Katie. I'm 19 years old and I'm looking for my first real job...", "jobhunt/"] );
images.push( ["/images/profile_AshleySm.jpg", "Ashley", "Cash Crunch", "I'm Ashley and I teach at an elementary school. I'm on a pretty tight budget so I need help budgeting for my living expenses.", "beyond/"] );
images.push( ["/images/profile_JacobSm.jpg", "Jacob", "New Family", "I'm Jacob and I'm 24. Luckily I have insurance--I don't know what I'd do without it...", "benefitsiq/"] );
images.push( ["/images/profile_BrandonSm.jpg", "Brandon", "First Job", "I'm Brandon. I'm almost 23 and I really wanted to find a job with benefits...", "workit/"] );
images = shuffle(images);

delay = 4000; 

imagePointer = 0;
buffer = "profileImage1";

addLoadEvent(thisOnload);

function thisOnload() {
  loadImageIntoBuffer();
  dissolveBufferIn();
}

// -------------------------------------
// The real work happens in this section
// -------------------------------------

function loadImageIntoBuffer(buf) {
  //console.debug("loading " + images[imagePointer][1] + " into buffer " + buf);
  buf ? buf = buf : buf = buffer;
  $(buf).setAttribute("src", images[imagePointer][0]);
  $(buf).setAttribute("alt", images[imagePointer][0]);
}

function dissolveBufferIn() {
  //console.debug("dissolving buffer " + buffer + " in");
  assignText();
  new Effect.Appear( $(buffer), {duration: 0.1, queue: "end"} );
  setTimeout( "dissolveBufferOut()", delay );
  incrementImagePointer();
  loadImageIntoBuffer(otherBuffer());
}

function dissolveBufferOut() {
  //console.debug("dissolving buffer " + buffer + " out");
  new Effect.Fade( $(buffer), {duration: 0.1, afterFinish:dissolveBufferIn} );
  swapBuffers();
}

// -----------------------------
// Utility functions and whatnot
// -----------------------------

function assignText() {
  $("profileTitle").firstChild.nodeValue = images[imagePointer][2];
  $("profileText").firstChild.nodeValue = images[imagePointer][3];
  $("profileLink").setAttribute('href', images[imagePointer][4]);
}

function shuffle(v) {
  for (var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
  return v;
}

function incrementImagePointer() {
  imagePointer++;
  if (imagePointer == images.length) {
    imagePointer = 0;
  }
}

function swapBuffers() {
  buffer == "profileImage1" ? buffer = "profileImage2" : buffer = "profileImage1";
/*  //console.debug("swapped buffers: buffer = " + buffer);*/
}

function otherBuffer() {
  buffer == "profileImage1" ? buf = "profileImage2" : buf= "profileImage1";
  return buf;
}
