﻿/*        *        *        *        *        *        *        *         */

/*        Utility functions.                                              */

/*        *        *        *        *        *        *        *         */

function $(ctrlID) {
  var ctrl = document.getElementById(ctrlID);
  if (!ctrl)
    alert("Unexpected Error: Unable to locate control " + ctrlID);
  return ctrl;
}

function ClassExists(ctrl, name) {
	return new RegExp("\\b" + name + "\\b").test(ctrl.className);
}
function ClassAdd(ctrl, name) {
	if (!ClassExists(ctrl,name)) {
	  if ("" == ctrl.className)
	    ctrl.className = name;
    else
  	  ctrl.className += " " + name;
	}
}
function ClassRemove(ctrl, name) {
	var rep = ctrl.className.match(" " + name) ? " " + name : name;
	ctrl.className = ctrl.className.replace(rep, "");
}

function GetElementsByClassName(clsName) {
  var retVal = new Array();
  var elements = document.getElementsByTagName("*");
  var classes, j;
  for (var i = 0; i < elements.length; i++) {
    if (elements[i].className.indexOf(" ") >= 0) {
      classes = elements[i].className.split(" ");
      for (j = 0; j < classes.length; j++) {
        if (classes[j] == clsName)
          retVal.push(elements[i]);
      }
    }
    else if (elements[i].className == clsName)
      retVal.push(elements[i]);
  }
  return retVal;
}

function GetEventCtrl(e) {
  var ctrl;
  if (!e) var e = window.event;
  ctrl = e.target || e.srcElement;
  if (ctrl.nodeType == 3) ctrl = ctrl.parentNode;     // defeat Safari bug
  return ctrl;
}

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis);
function AttachEvent(obj, evt, fnc) {
	if (obj.addEventListener) {
		obj.addEventListener(evt, fnc, false);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on" + evt, fnc);
} 

function setOpacity(ctrl, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  ctrl.style.filter = "alpha(opacity:"+opacity+")";  // IE/Win
  ctrl.style.KHTMLOpacity = opacity/100;             // Safari<1.2, Konqueror
  ctrl.style.MozOpacity = opacity/100;               // Older Mozilla and Firefox
  ctrl.style.opacity = opacity/100;                  // Safari 1.2, newer Firefox and Mozilla, CSS3
}

/*        *        *        *        *        *        *        *         */

/* Preload images? */

/*        *        *        *        *        *        *        *         */

imgMenu0 = new Image(60,27);
imgMenu0.src = "Images/Menu0_Hilite.gif";
imgMenu1 = new Image(72,27);
imgMenu1.src = "Images/Menu1_Hilite.gif";
imgMenu2 = new Image(128,27);
imgMenu2.src = "Images/Menu2_Hilite.gif";
imgMenu3 = new Image(74,27);
imgMenu3.src = "Images/Menu3_Hilite.gif";
imgMenu4 = new Image(108,27);
imgMenu4.src = "Images/Menu4_Hilite.gif";
imgMenu5 = new Image(122,27);
imgMenu5.src = "Images/Menu5_Hilite.gif";
imgMenu6 = new Image(130,27);
imgMenu6.src = "Images/Menu6_Hilite.gif";
imgMenu7 = new Image(76,27);
imgMenu7.src = "Images/Menu7_Hilite.gif";
imgMenu8 = new Image(60,27);
imgMenu8.src = "Images/Menu8_Hilite.gif";
imgMenu9 = new Image(125,27);
imgMenu9.src = "Images/Menu9_Hilite.gif";

/*        *        *        *        *        *        *        *         */

/* Home page animation. */

/*        *        *        *        *        *        *        *         */

var _imgIndex = 0;
var _textIndex = -1;
var _textCounts = [3, 1, 1, 1, 1];
var _textPauses = [0, 4000, 4000, 4000, 4000];
var _timeOut = 2000;

function SwapHdrImg() {
  var imgBase, imgText, imgNew, pos;

  imgBase = $("imgSlideBase");
  imgText = $("imgSlideText");

  if ((_textIndex + 1) >= _textCounts[_imgIndex]) {
    ++_imgIndex;
    if (_imgIndex >= _textCounts.length) _imgIndex = 0;
    _textIndex = -1;

    imgNew = document.createElement("img");
    imgNew.src = imgBase.src;
    imgNew.id = "__NewImg__";
    imgNew.style.zIndex = 3;
    imgNew.style.position = "absolute";
    pos = findPos(imgBase);
    imgNew.style.left = pos[0] + "px";
    imgNew.style.top = pos[1] + "px";
    imgBase.offsetParent.appendChild(imgNew);
    imgText.style.display = "none";
    imgText.src = "Images/Slides/slide_" + (_imgIndex + 1) + "_t1.gif";

    imgBase.style.zIndex = 1;
    imgBase.src = "Images/Slides/slide_" + (_imgIndex + 1) + "_base.jpg";

    setOpacity(imgNew, 100);
    fadeOut(imgNew.id, 100, false);

    setOpacity(imgBase, 0);
    fadeIn(imgBase.id, 0);
  }
  else if (0 <= _textIndex) {
    ++_textIndex;
    imgNew = document.createElement("img");
    imgNew.src = imgText.src;
    imgNew.id = "__NewImg__";
    imgNew.style.zIndex = 3;
    imgNew.style.position = "absolute";
    pos = findPos(imgBase);
    imgNew.style.left = pos[0] + "px";
    imgNew.style.top = pos[1] + "px";
    imgBase.offsetParent.appendChild(imgNew);
    imgText.src = "Images/Slides/slide_" + (_imgIndex + 1) + "_t" + (_textIndex + 1) + ".gif";

    setOpacity(imgNew, 100);
    fadeOut(imgNew.id, 100, true);

    setOpacity(imgText, 0);
    fadeIn(imgText.id, 0);
  }
  else {
    _textIndex = 0;
    setOpacity(imgText, 0);
    imgText.src = "Images/Slides/slide_" + (_imgIndex + 1) + "_t1.gif";
    imgText.style.zIndex = 2;
    imgText.style.position = "absolute";
    pos = findPos(imgBase);
    imgText.style.left = pos[0] + "px";
    imgText.style.top = pos[1] + "px";
    imgText.style.display = "block";

    fadeIn(imgText.id, 0);
    if ((_textIndex + 1) >= _textCounts[_imgIndex])
      window.setTimeout(SwapHdrImg, _timeOut + 1400 + _textPauses[_imgIndex]);
    else
      window.setTimeout(SwapHdrImg, _timeOut + 1400);
  }
}

function fadeOut(imgName, opacity, pause) {
  var ctrl;
  ctrl = document.getElementById(imgName);
  if (opacity >= 0) {
    setOpacity(ctrl, opacity);
    opacity -= 7;
    window.setTimeout("fadeOut('"+imgName+"',"+opacity+","+pause+")", 100);
  } else {
    if (pause) {
      window.setTimeout("removeHdrImg('"+imgName+"')", _timeOut);
    }
    else {
      ctrl.parentNode.removeChild(ctrl);
      SwapHdrImg();
    }
  }
  ctrl = null;
}
function removeHdrImg(imgName) {
  var ctrl;
  ctrl = document.getElementById(imgName);
  ctrl.parentNode.removeChild(ctrl);
  SwapHdrImg();
  ctrl = null;
}

function fadeIn(imgName, opacity) {
  var ctrl;
  ctrl = document.getElementById(imgName);
  if (opacity <= 100) {
    setOpacity(ctrl, opacity);
    opacity += 7;
    window.setTimeout("fadeIn('"+imgName+"',"+opacity+")", 100);
  } else {
    setOpacity(ctrl, 100);
  }
  ctrl = null;
}

function findPos(obj) {
  var curleft = curtop = 0;
  do {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
  } while (obj = obj.offsetParent);
  return [curleft,curtop];
}

/*        *        *        *        *        *        *        *         */

/*        App functions.                                                  */

/*        *        *        *        *        *        *        *         */

var _curProductSection = 0;
var _curProductPage = 0;

function ShowProduct(section, page) {
  var ctrl, i;
  ctrl = document.getElementById("ArrowLeft");
  if (ctrl) {
    if ( (1 == section) && (1 == page) )
      ctrl.style.visibility = "hidden";
    else
      ctrl.style.visibility = "visible";
  }
  ctrl = document.getElementById("ArrowRight");
  if (ctrl) {
    if ( (4 == section) && (4 == page) )
      ctrl.style.visibility = "hidden";
    else
      ctrl.style.visibility = "visible";
  }
  if (0 != _curProductSection) {
    ctrl = GetElementsByClassName("Page" + _curProductSection);
    for (i = 0; i < ctrl.length; ++i)
      ctrl[i].style.display = "none";
  }
  ctrl = document.getElementById("Page" + section + "_Hdr");
  if (ctrl) ctrl.style.display = "block";
  ctrl = document.getElementById("Page" + section + "_" + page);
  if (ctrl) ctrl.style.display = "block";
  _curProductSection = section;
  _curProductPage = page;
  ctrl = null;
  return false;
}

function ShowNextProduct() {
  var section, page;
  if (0 == _curProductSection) return;
  section =  _curProductSection;
  page = _curProductPage;
  if (0 == page) {
    ctrl = GetElementsByClassName("Page" + section);
    for (i = 0; i < ctrl.length; ++i)
      ctrl[i].style.display = "none";
    ++_curProductSection;
    _curProductPage = 1;
    ShowAllProducts();
    return;
  }
  else if (document.getElementById("Page" + section + "_" + (page + 1))) {
    ShowProduct(section, page + 1);
  }
  else {
    if (4 == section) section = 0;
    ShowProduct(section + 1, 1);
  }
  return false;
}

function ShowPrevProduct() {
  var section, page;
  if (0 == _curProductSection) return;
  section =  _curProductSection;
  page = _curProductPage;
  if (0 == page) {
    ctrl = GetElementsByClassName("Page" + section);
    for (i = 0; i < ctrl.length; ++i)
      ctrl[i].style.display = "none";
    --_curProductSection;
    _curProductPage = 1;
    ShowAllProducts();
    return;
  }
  else if (1 == page) {
    if (1 == section)
      section = 4;
    else
      section = section - 1;
  }
  else {
    --page;
  }
  ShowProduct(section, page);
  return false;
}

function ShowAllProducts() {
  var ctrl, i;
  if (0 == _curProductSection) {
    _curProductSection = 1;
    if (0 == _curProductPage) 
      _curProductPage = 1;
  }
  if (0 == _curProductPage) 
    ShowProduct(_curProductSection, 1);
  else {
    _curProductPage = 0;
    ctrl = GetElementsByClassName("Page" + _curProductSection);
    for (i = 0; i < ctrl.length; ++i)
      ctrl[i].style.display = "block";

    ctrl = document.getElementById("ArrowLeft");
    if (ctrl) {
      if (1 == _curProductSection)
        ctrl.style.visibility = "hidden";
      else
        ctrl.style.visibility = "visible";
    }
    ctrl = document.getElementById("ArrowRight");
    if (ctrl) {
      if (4 == _curProductSection)
        ctrl.style.visibility = "hidden";
      else
        ctrl.style.visibility = "visible";
    }

    ctrl = null;
  }
  return false;
}

var _curProjectSection = 0;
var _curProjectPage = 0;

function ShowProject(section, page) {
  var ctrl, i;
  if (0 != _curProjectSection) {
    ctrl = GetElementsByClassName("Page" + _curProjectSection);
    for (i = 0; i < ctrl.length; ++i)
      ctrl[i].style.display = "none";
  }
  ctrl = document.getElementById("Page" + section + "_" + page);
  if (ctrl) ctrl.style.display = "block";
  _curProjectSection = section;
  _curProjectPage = page;
  ctrl = null;
  return false;
}

function sfHover() {
  var sfEls = document.getElementById("menu").getElementsByTagName("LI");
  for (var i = 0; i < sfEls.length; i++) {
    sfEls[i].onmouseover = function() {
      ClassAdd(this, "sfhover");
//      this.className += " sfhover";
    }
    sfEls[i].onmouseout = function() {
      ClassRemove(this, "sfhover");
//      this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
  sfEls = null;
}
if (window.attachEvent)
  window.attachEvent("onload", sfHover);
else
  window.onload = sfHover;

/*        *        *        *        *        *        *        *         */

/* Bios images. */

/*        *        *        *        *        *        *        *         */

var _imgWidth = 142;
var _imgHeight = 118;
var _divWidth;
var _divHeight;
var _curLeft;
var _curOffset;
var _curBio;
var _timer;
var _outTimer;
var _bioNames = new Array("Susanne_Flett", "Terri_LeFort",
                          "Karen_Shearing-Wittig", "Bill_Meredith",
                          "John_Stoneman", "Maide_Yazar", "Hy_Eliasoph", "Joseph_Mendez");
var _bioTitles = new Array("President", "Vice President",
                           "Senior Vice President", "Director",
                           "Vice President", "Chief Strategist", "Vice President",
                           "Vice President, Western Canada");

function Bios_Init() {
  var ctrl, ctrl2, i, left, container, slider;
  slider = $("divBiosSlider");
  container = $("divBiosContainer");
  slider.style.width = (_bioNames.length * _imgWidth) + "px";
  _curLeft = 0;
  _curBio = 0;
  _divWidth = container.offsetWidth;
  _divHeight = container.offsetHeight;

  for ( i = 0; i < _bioNames.length; ++i ) {
    ctrl = document.createElement("div");
    ctrl.id = "bio_" + i;
    ctrl.style.textAlign = "center";
    ctrl.style.width = _imgWidth + "px";
    ctrl2 = document.createElement("img");
    ctrl2.style.width = _imgWidth + "px";
    ctrl2.style.height = _imgHeight + "px";
    ctrl2.style.marginTop = "35px";
    ctrl2.src = "Images/Consultants/" + _bioNames[i] + ".jpg";
    ctrl.appendChild(ctrl2);
    ctrl2 = document.createElement("div");
    ctrl2.style.width = _imgWidth + "px";
    ctrl2.style.height = "20px";
    ctrl2.innerHTML = _bioNames[i].replace(/_/, " ") + "<br />" + _bioTitles[i];
    ctrl.appendChild(ctrl2);
    slider.appendChild(ctrl);
  }

  AttachEvent(container, 'mousemove', Bios_Mouseover);
  AttachEvent(container, 'mouseout', Bios_Mouseout);
  AttachEvent(container, 'mousedown', Bios_Mousedown);

  Bios_Set();

  ctrl = null;
  ctrl2 = null;
  slider = null;
  container = null;
}

function Bios_Mouseover(e) {
  var mouseX;
  if (!e) var e = window.event;
  mouseX = getRelativeCoordinates(e, $("divBiosContainer")).x;
  mouseX /= 1.0 * _divWidth;
  mouseX -= 0.5;
  if ( (-0.05 < mouseX ) && (0.05 > mouseX) ) {
    _curOffset = 0;
    return;
  }
  _curOffset = -12 * mouseX;
  if (!_timer) _timer = setInterval(Bios_Redraw, 10);
  clearTimeout(_outTimer);
}
function Bios_Redraw() {
  var ctrl, margin;
  ctrl = $("divBiosSlider");
  ClassAdd(ctrl, "slide-bio");
  margin = ctrl.style.marginLeft;
  if ("" == margin)
    margin = _curOffset;
  else
    margin = parseInt(margin) + _curOffset;
  if (0 < margin) margin = 0;
  if ((-1 * _bioNames.length * _imgWidth + _divWidth) > margin)
    margin = -1 * _bioNames.length * _imgWidth + _divWidth;
  ctrl.style.marginLeft = margin + "px";
  ctrl = null;
}

function Bios_StopScroll() {
  ClassRemove($("divBiosSlider"), "slide-bio");
  clearInterval(_timer);
  _timer = null;
}

function Bios_Mouseout(e) {
  if (_outTimer) clearTimeout(_outTimer);
  _outTimer = setTimeout(Bios_StopScroll, 100);
}
function Bios_Mousedown(e) {
  var ctrl;
  ctrl = GetEventCtrl(e);
  Bios_Clear();
  _curBio = parseInt(ctrl.parentNode.id.substr(4));
  Bios_Set();
  ClassRemove($("divBiosSlider"), "slide-bio");
  clearInterval(_timer);
  _timer = null;
}

function Bios_Set() {
  var ctrl;
  ctrl = $("bio_" + _curBio);
  ClassAdd(ctrl, "cur-bio");
  ctrl.childNodes[0].src = "Images/Consultants/" + _bioNames[_curBio] + "_Hilite.jpg";
  ctrl = $("biomain_" + _curBio);
  ctrl.style.display = "block";
  ctrl = null;
}

function Bios_Clear() {
  var ctrl;
  ctrl = $("bio_" + _curBio);
  ClassRemove(ctrl, "cur-bio");
  ctrl.childNodes[0].src = "Images/Consultants/" + _bioNames[_curBio] + ".jpg";
  ctrl = $("biomain_" + _curBio);
  ctrl.style.display = "none";
  ctrl = null;
}

function Bios_Prev() {
  Bios_Clear();
  _curBio -= 1;
  if (_curBio < 0) _curBio = _bioNames.length - 1;
  Bios_Set();
  return false;
}
function Bios_Next() {
  Bios_Clear();
  _curBio += 1;
  if (_curBio >= _bioNames.length) _curBio = 0;
  Bios_Set();
  return false;
}

/**
* Retrieve the coordinates of the given event relative to the center
* of the widget.
*
* @param event
*  A mouse-related DOM event.
* @param reference
*  A DOM element whose position we want to transform the mouse coordinates to.
* @return
*    A hash containing keys 'x' and 'y'.
*/
function getRelativeCoordinates(event, reference) {
  var x, y;
  event = event || window.event;
  var el = event.target || event.srcElement;
  if (!window.opera && typeof event.offsetX != 'undefined') {
    // Use offset coordinates and find common offsetParent
    var pos = { x: event.offsetX, y: event.offsetY };
    // Send the coordinates upwards through the offsetParent chain.
    var e = el;
    while (e) {
      e.mouseX = pos.x;
      e.mouseY = pos.y;
      pos.x += e.offsetLeft;
      pos.y += e.offsetTop;
      e = e.offsetParent;
    }
    // Look for the coordinates starting from the reference element.
    var e = reference;
    var offset = { x: 0, y: 0 }
    while (e) {
      if (typeof e.mouseX != 'undefined') {
        x = e.mouseX - offset.x;
        y = e.mouseY - offset.y;
        break;
      }
      offset.x += e.offsetLeft;
      offset.y += e.offsetTop;
      e = e.offsetParent;
    }
    // Reset stored coordinates
    e = el;
    while (e) {
      e.mouseX = undefined;
      e.mouseY = undefined;
      e = e.offsetParent;
    }
  }
  else {
    // Use absolute coordinates
    var pos = getAbsolutePosition(reference);
    x = event.pageX  - pos.x;
    y = event.pageY - pos.y;
  }
  // Subtract distance to middle
  return { x: x, y: y };
}
/**
 * Retrieve the absolute coordinates of an element.
 *
 * @param element
 *   A DOM element.
 * @return
 *   A hash containing keys 'x' and 'y'.
 */
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

/*        *        *        *        *        *        *        *         */
