/* ********************************************************************
* Buffered Text-fade Effect - v3.0.1
* - Copyright 2008 - Licenced for free distribution under the BSDL
*   - http://www.opensource.org/licenses/bsd-license.php
*
*   This program is licenced under the BSDL and may be distributed far
* and wide, anywhere on the planet and beyond (maybe!)  If you happen
* to get a kick out of this script, please drop me a note at:
* wyvern@greywyvern.com and tell me where you gave it a good home and
* plenty of bytes to eat, hmmm? :)  I'd be eternally grateful.
*
* Description:
*   A javascript object which allows you to direct text fades within an
* HTML page.  Fades received before the previous fade has completed are
* queued rather than displayed immediately.  This keeps all fade
* animations smooth.
*
* Changelog:
*  3.0.1 - Changed timeout values to 10ms to keep Opera from queuing
*
*  3.0  - Buffer for each fadeObject is stored separately for stability
*         and speed.
*       - Major structure changes.  Different arguments
*
* Support:
*   Opera   - Yes
*   IE 6    - Yes
*   IE 5.5  - Yes
*   IE 5.01 - Fails
*   Firefox - Yes
*   Safari  - Yes
*/

function fadeObject(id, c1, c2, s1, s2) {
  var self 		 = this;
  this.id      = id;
  this.elem    = false;
  this.colour  = {
    stt: [parseInt(c1.substr(0, 2), 16), parseInt(c1.substr(2, 2), 16), parseInt(c1.substr(4, 2), 16)],
    end: [parseInt(c2.substr(0, 2), 16), parseInt(c2.substr(2, 2), 16), parseInt(c2.substr(4, 2), 16)],
    now: [parseInt(c1.substr(0, 2), 16), parseInt(c1.substr(2, 2), 16), parseInt(c1.substr(4, 2), 16)]
  };
  this.steps   = [s1, s2];
  this.dir     = false;
  this.active  = false;
  this.queue   = [];
  this.msg     = [];
  this.message = 0;
  function d2h(num) {
    num = Math.round(num);
    return ((num < 16) ? "0" : "") + num.toString(16);
  }
  this.fade = function(message, direction) {
    this.elem = this.elem || document.getElementById(this.id);
    this.queue.push([message, direction]);
    for (var x = 0; x < this.queue.length; x++) {
      for (var y = x + 1; y < this.queue.length; y++) {
        if (this.queue[x][0] == this.queue[y][0] && this.queue[x][1] != this.queue[y][1]) {
          this.queue.splice(x, 1);
          this.queue.splice(y - 1, 1);
        }
      }
    }
    if (!this.active) setTimeout(function() { self.fadeLoop(); }, 10);
  };
  this.fadeLoop = function() {
    if (!this.active && this.queue.length) {
      if (this.dir && this.message != this.queue[0][0]) this.queue.unshift([this.message, false]);
      var msg = this.queue.shift();
      if (this.msg[msg[0]]) {
        this.active = true;
        this.elem.innerHTML = this.msg[this.message = msg[0]];
        this.dir = msg[1];
      }
    }
    if (this.dir) {
      var c1 = this.colour.stt, c2 = this.colour.end, s = this.steps[0];
    } else var c1 = this.colour.end, c2 = this.colour.stt, s = this.steps[1];
    for (var x = 0, cnow = "", inc = 0; x < 3; x++) {
      this.colour.now[x] += inc = (c2[x] - c1[x]) / s;
      cnow += this.colour.now[x] = (inc < 0) ? Math.max(this.colour.now[x], c2[x]) : Math.min(this.colour.now[x], c2[x]);
    } this.elem.style.color = "#" + d2h(this.colour.now[0]) + d2h(this.colour.now[1]) + d2h(this.colour.now[2]);
    if (cnow == c2.join("")) {
      this.active = false;
      if (!this.queue.length) {
        if (!this.dir) {
          if (this.msg[0]) {
            this.queue.push([0, true]);
            setTimeout(function() { self.fadeLoop(); }, 10);
          } else this.elem.innerHTML = "&nbsp;";
        }
      } else setTimeout(function() { self.fadeLoop(); }, 10);
    } else setTimeout(function() { self.fadeLoop(); }, 10);
  };
  if (window.addEventListener) {
    window.addEventListener('load', function() { self.fade(0, true); }, false); 
  } else if (window.attachEvent)
    window.attachEvent('onload', function() { self.fade(0, true); });
}

var hash = new Array();

function throb(item) {

  // If the hash array does not have an entry for this item, initialise it at 2
  if (!hash[item]) hash[item] = 2;

  // Send a fade command, using the hash array to tell us what parameters we should use
  fader[item].fade(Math.floor(hash[item] / 2), !(hash[item] % 2));

  // Call this function again for this same item after a certain amount of time
  setTimeout(function() { throb(item); }, (hash[item] % 2) ? 0 : 6000);

  // If we have exceeded the number of messages in this fader, start over again at 2
  if (++hash[item] > fader[item].msg.length * 2 - 1) hash[item] = 2;
}

var fader = new Array();

fader[1] = new fadeObject('fade1', 'ffffff', 'ff0000',200, 75);
fader[1].msg[1] = "\"SAS was very impressive\"";
fader[1].msg[2] = "\"your wonderful support\"";
fader[1].msg[3] = "\"smooth as clockwork\"";
fader[1].msg[4] = "\"did a brilliant job\"";
fader[1].msg[5] = "\"A fantastic effort\"";

fader[2] = new fadeObject('fade2', 'ffffff', '003366',400, 125);
fader[2].msg[1] = "\"Your organizational skills very much came into their own, and your preparatory work showed through at every level.  To describe all of your efforts as superb would be no exaggeration whatsoever\" - British Council for Offices";
fader[2].msg[2] = "\"The team's attention to detail and creative approach to problem solving made for an incredibly successful event\" - Scope Ketchum";
fader[2].msg[3] = "\"The whole thing ran seamlessly, the seminars, lunches, technical tours and above all the dinner.  A credit to SAS Event Management.\" - Argent";
fader[2].msg[4] = "\"In every respect I found SAS to be extremely professional, attentive to detail and highly responsive to our needs.  They provide an excellent service with a smile and a wicked sense of humour\" - Blue Circle Industries plc";
fader[2].msg[5] = "\"Once again you and your team did a wonderful job in organising and promoting the show.  The organisation was flawless and the event was very productive indeed\" - Mattel Toys Europe plc";

fader[3] = new fadeObject('fade3', 'ffffff', '00a56d',300, 100);
fader[3].msg[1] = "\"the sight of your friendly, helpful and welcoming staff throughout was always reassuring\" - RIBA";
fader[3].msg[2] = "\"SAS were outstanding.  They are always brilliant but this was them at their best.\" - Colliers CRE";
fader[3].msg[3] = "\"SAS is an organisation which is customer-led in every respect\" - Audi";
fader[3].msg[4] = "\"The logistical and management skills provided by SAS could not be beaten.\" - bennett interior design";
fader[3].msg[5] = "\"The conference was the best organised I have attended or presented at\" - Arup";


// Start this fader
setTimeout(function() { throb(1); }, 1000);
setTimeout(function() { throb(2); }, 1000);
setTimeout(function() { throb(3); }, 1000);


