var w3c = (document.getElementById) ? true : false;
var ns4 = (document.layers) ? true : false;
var ie5 = (w3c && document.all) ? true : false;

// Gradient class for changing the color on scroll

var Gradient = {
   firstMatrix : new Array(3),
   finalMatrix : new Array(3),
   distMatrix : new Array(3),

   docHeight : null,
   i : 0,

   firstColor : null,
   finalColor : null,
   
   /**
      Reset the page length and color gradient.
   */
   update : function() {
      Gradient.setColor(Gradient.finalColor, Gradient.firstColor);		
   },

   setColor : function(input, start) {
   	  if (start == undefined) start = document.bgColor;
      Gradient.finalColor = Gradient.hexVal(input);

      // Setup scroller function
      Gradient.docHeight = parseInt(document.getElementById("main").offsetHeight) - screen.availWidth;

      Gradient.firstColor = Gradient.hexVal(start);
      Gradient.finalColor = Gradient.hexVal(Gradient.finalColor);

      Gradient.firstMatrix = Gradient.hexRGB(Gradient.firstColor);
      Gradient.finalMatrix = Gradient.hexRGB(Gradient.finalColor);


      for (var i=0;i<(Gradient.firstMatrix).length;i++)
         Gradient.distMatrix[i] = parseInt(Gradient.finalMatrix[i] - Gradient.firstMatrix[i]);

      // Add scroll event
      window.onscroll = Gradient.scroller;

   },

   scroller : function() {
      position = (ie5) ? parseInt(document.body.scrollTop) : parseInt(window.pageYOffset);

      //document.bgColor = Gradient.finalColor;
      percentage = Math.min(1, position / Gradient.docHeight);

      var newMatrix = new Array(3);

      for (var i=0;i<Gradient.distMatrix.length;i++)
         newMatrix[i] = Gradient.firstMatrix[i] + Math.round(Gradient.distMatrix[i] * percentage);

      newColor = Gradient.RGBhex(newMatrix);
      document.bgColor = newColor;
   },


   hexRGB : function(input) {

      var hex = "0123456789ABCDEF";
      var newColor = [0, 0, 0];

      for (var i=0;i<input.length;i++) {
         save = Math.floor(i/2);
         val = input.charAt(i);

         hexVal = hex.indexOf(val);

         if (i % 2 == 0) newColor[save] += parseInt(hexVal * 16);
         else newColor[save] += parseInt(hexVal);
      }

      return newColor;
   },

   RGBhex : function(input) {

      var hex = "0123456789ABCDEF";
      var newColor = new String('');

      for (i=0;i<input.length;i++) {
         var item = new String(input[i]);

         level = Math.floor(item / hex.length);
         remainder = item - (level * hex.length);

         newColor += hex.charAt(level) + hex.charAt(remainder);
      }

      return newColor;
   },

   hexVal : function(string) {
      newString = string.replace(/.*?([0-9A-F]{6,6}).*?/ig, "$1");

      return newString.toUpperCase();
   }

}

var Switcher = {

// Interactive features

   orderLinks : {
      version2: { title:"Want to Change the Price?", html:'<?= $version2; ?>', show:"version2", hide:"lastChance" },
      version1: { title:"Want More Features?", html:'<?= $version1; ?>', show:"lastChance", hide:"version2" }
   },

   update : function(value) {
      var count = 6;
      result = new Object(eval("Switcher.orderLinks."+value));
      
      // Show and hide appropriate sections
      if (result.show) {
         Switcher.showElement(result.show);
      }

      if (result.hide) {
         Switcher.hideElement(result.hide);
      }
      
      // Change order links	
      //code = '<b><a href="'+result.href+'">'+result.name+'</a></b>';
      for (var i=0;i<count;i++) document.getElementById("order["+i+"]").innerHTML = result.html;

      //document.getElementById("switcher[title]").innerHTML = '<b>'+result.title+'</b>';
   },

   hideElement : function(name) {
      var obj = document.getElementById(name);
      obj.style.visibility = "hidden";
      obj.style.position = "absolute";
   },

   showElement : function(name) {
      var obj = document.getElementById(name);
      obj.style.position = "relative";
      obj.style.visibility = "visible";
   }
}