// JavaScript Document

/*  Begin fade effects */

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); });
}
/* End fade effects */

/* *****
 * User defined fade objects and messages
 *
 * These messages are used in fades triggered by mouseovers and
 * mouseouts on table cells.  They are the simplest type of fade and
 * require no extra Javascript code.
 */
 
var fader = new Array();

fader[0] = new fadeObject('fade0', 'dddddd', '000000', 20, 20);
fader[0].msg[0] = "";
fader[0].msg[1] = "ccSPF offers a true R-value because of it's ability to withstand external environmental pressues such as wind or extreme temperatures. ccSPF insulation has an R-value close to 7.0 per inch depending on which product is used. Please refer to our <a href=/services/sprayfoam/>spray foam</a> insulation section and view the manufactures data sheets for more information.";
fader[0].msg[2] = "Yes. Mold and moisture problems are a common place with many insulation systems. Moisture can become trapped if the permeability of a surface is too low. Poly sheeting, which is often used in fiberglass and cellulose insulation systems, can trap moisture from escaping because of it's low perm rating.  It is also easy for a penetration to occur in the poly sheeting, for example, from a staple or nail. This penetration will cause moisture to flow in and build up if it does not have another way to exit. ccSPF insulation qualifies as a vapor barrier/retarder at two inches or more but doesn't trap moisture like poly sheeting.";
fader[0].msg[3] = "The ccSPF is a two component mixture that is sprayed onto a substrate by a certified <a href=http://www.sprayfoam.org/>SPFA</a> professional. The insulation is sprayed on a exposed surface usually after the electrical and plumbing services are in place. As the mixture comes together, it expands up to 100 times its initial volume in seconds, filling any cavities, voids, or gaps.";
fader[0].msg[4] = "No, foam insulation has been thoroughly tested and is not a concern to electrical authorities nor is it corrosive to any metals whatsoever.";
fader[0].msg[5] = "No, it is an inert substance and it is hypoallergenic.";
fader[0].msg[6] = "Indefinitely. It is a formulation of polymers.";
fader[0].msg[7] = "Fiberglass and cellulose insulation are not capable of stopping air flow. Spay foam insulation will end air flow. Fiberglass and cellulose insulation lose their R-value during extreme temperatures and ccSPF does not. ccSPF is  mold &amp; moisture resistant unlike fiberglass &amp; cellulose. Fiberglass batts have an R-value of 3.5 per inch thickness. Cellulose has an R-value of about 3 to 4 per inch thickness. Spray foam insulation has an R-value of about 7 per inch depending on manufacturers specifications. Cellulose and fiberglass insulation can settle over time which can allow air flow to occur. Spray-foam will add structural strength to your building framing.";
fader[0].msg[8] = "Absolutely Not. It does not emit volatile organic compounds or use zero-ozone-depleting technology.";
fader[0].msg[9] = "No, it is an inert substance.  It also leaves no spaces for rodents or insects to live. It will even keep the critters out of your below grade area by sealing them out!";
fader[0].msg[10] = "For new construction or renovations, the insulation is installed after the windows, doors and roof systems are in, the electrical, framing and plumbing inspections are complete, and after any other electrical or mechanical system located behind the drywall is installed. It is the last installation to take place before drywall installation. On existing homes, the spray foam insulation can be installed to the attic, roof line, and sub-flooring to seal in the house. It is more cost effective to leave existing interior wall insulation in place.";
fader[0].msg[11] = "An outlet can be easily added because ccSPF insulation can be drilled, scraped or shaven. Depending on the framework you use, you may even have space to add wiring or an outlet over the insulation after it is in place. Contact us today to learn more.";
fader[0].msg[12] = "People from varying industries including building scientists, engineers, architects, insulation and energy experts, green home specialists, mold experts, and the USGBC.";
fader[0].msg[13] = "Your building does need to be ventilated, but engineers and building scientists (as well as the US Department of Energy) advise that you seal the structure as tight as possible and provide necessary ventilation through your air conditioning and heating system. Many systems employ an air exchanger, which pre-conditions incoming outside air to control humidity and energy efficiency. This type of design along with spray-foam creates a true Thermal Envelope.";
fader[0].msg[14] = "It has been around for more than 30 years for commercial and industrial uses. It has become very popular for residential use in the last 10 years.";

/* option to fade into another space such as fade1 below

fader[1] = new fadeObject('fade1', 'bbbbbb', '000000', 20, 20);
fader[1].msg[1] = "dfdfdf";
fader[1].msg[2] = "We have two ears and one mouth so we may listen more and talk the less.<br />- Epictetus";
fader[1].msg[3] = "It is better to be violent, if there is violence in our hearts, than to put on the cloak of nonviolence to cover impotence.<br />- Mahatma Gandhi";
fader[1].msg[4] = "Don't part with your illusions. When they are gone you may still exist, but you have ceased to live.<br />- Mark Twain";

*/


/* *****
 * The code below describes how to make a throbbing or automatic fade
 * sequence of messages. 
 */
 
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) ? 100 : 5000);

  // 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;
}

fader[2] = new fadeObject('fade2', 'bbbbbb', '000000', 30, 30);
fader[2].msg[1] = "This is an automatically fading message";
fader[2].msg[2] = "By designing your own looping functions...";
fader[2].msg[3] = "You can co-ordinate many separate fade effects";
fader[2].msg[4] = "Including a simple list of messages like this one";
fader[2].msg[5] = "View the source to find out how!";

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