/*
** Sets the text node value of the supplied element object
** to the specified text string.  The optional index parameter
** allows for the element's nth text node to be updated.
**
** e     - the element object to be updated
** txt   - the new text string
** index - the index of the text node within the attribute - default 0
*/
function setText(e, txt, index) {
   if (typeof e == 'string') {
      e = document.getElementById(e);
   }

   if ((!e) || (!e.nodeType) || (e.nodeType != 1)) return;
   if (!txt) txt = "";
   if (!index) index = 0;
   for (var i = 0, c = 0; i < e.childNodes.length; i++) {
      var node = e.childNodes[i];
      if (node.nodeType == 3) {
         if (c == index) {
            node.nodeValue = txt;
            return;
         }
         c++;
      }
   }
}



function addEvent(target, eventType, functionRef, capture)
{
   if (typeof target == 'string')
   {
      var target = document.getElementById(target);
   }

   if (target)
   {
      if (typeof target.addEventListener != 'undefined')
      {
         // self.alert("Adding .addEventListener() ...");
         target.addEventListener(eventType, functionRef, capture);
      }
      else if (typeof target.attachEvent != 'undefined')
      {
         // self.alert("Adding .attachEvent() ...");
         target.attachEvent("on" + eventType, functionRef);
      }
      else
      {
         // self.alert("Adding DOM event to event list ...");
         if (typeof target["on" + eventType] == 'function')
         {
            var oldfunc = target["on" + eventType];
            target["on" + eventType] = function(e) {
               oldfunc(e);
               functionRef(e);
            }
         } else {
            target["on" + eventType] = functionRef;
         }
      }
   }
}



function navTo(contentName, title)
{
   setText('pageHeading', title);

   var iCanvass = frames['iCanvass'];

   if ((contentName != null) && (contentName.length > 0))
   {
      if (typeof iCanvass == 'object')
      {
         iCanvass.location.href = '/Content/' + contentName + '.html';
      }

      var leftImage = document.getElementById('topLeftImage');
      var mainImage = document.getElementById('mainImage');

      if ((typeof leftImage == 'object') && (typeof mainImage == 'object'))
      {
         leftImage.style.backgroundImage = "url(/css/img/" + contentName + "Left.jpg)";
         mainImage.style.backgroundImage = "url(/css/img/" + contentName + "Main.jpg)";
      }
   }
}


function preLoad()
{
   var imgNames = ["AboutUs", "Plumbing", "Drainage", "ContactUs"];
   var imgObjects = [];

   for (var i = 0; i < imgNames.length; i++)
   {
      imgObjects[i*2] = new Image();
      imgObjects[i*2].src = "/css/img/" + imgNames[i] + "Left.jpg";

      imgObjects[(i*2)+1] = new Image();
      imgObjects[(i*2)+1].src = "/css/img/" + imgNames[i] + "Main.jpg";
   }
}



addEvent(window, "load", preLoad, false);