/* PAGE INITIALIZATION FUNCTIONS */
var addEvent = function(obj, type, fn) { //http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
  } else if (obj.attachEvent) {
    obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
  }
}
addEvent(window, 'load', preProcessPage);

var com_sonoa_bannerTimeout;

function preProcessPage() {
	if (!document.getElementsByTagName || (!document.getElementById || (!document.createElement || !document.createTextNode))) return false;
	processPage();
}

function processPage() {
  //page hooks go here
  processBlocks();
  processFormInputs();
  processLinks();
  processBanners();
  processDivs();
  imagePreload();
}

/* INDIVIDUAL ELEMENT INITIALIZATIONS */
function processDivs() {
  var theDivs = document.getElementsByTagName("div");
  var fixDiv = function(theElement) {
    if (hasClassHook(theElement)) {
      if (/js_iconLink/.test(theElement.className)) {
        theElement.onclick = function() {
          doIconLink(this);
          return false;
        }
        theElement.style.cursor = 'pointer';
      }
    }
  }
  forEach(theDivs,fixDiv);
}

function doIconLink(theElement) {
  var theLinks = theElement.getElementsByTagName("a");
  if (theLinks.length > 0) {
    document.location.href = theLinks[0].href;
  }
}

function processBanners() {
  var theBanners = grabBanners();
  if (theBanners) {
    var bannerBase = 999;
    for (var i=0; i<theBanners[0].length; i++) {
      var thisBanner = theBanners[0][i];
      thisBanner.style.zindex = (bannerBase + i);
      var bannerLinks = thisBanner.getElementsByTagName("a");
      thisBanner.actionLink = false;
      for (var j=0; j<bannerLinks.length; j++) {
        var thisLink = bannerLinks[j];
        if (thisLink.className && /action_button/.test(thisLink.className)) {
          thisBanner.actionLink = thisLink.getAttribute('href');
        }
      }
      if (thisBanner.actionLink) {
        thisBanner.onclick = function() {
          document.location.href = this.actionLink;
          return false;
        }
      }
    }
    com_sonoa_bannerTimeout = setTimeout(timerSwitchBanner,6000);
  }
}

function timerSwitchBanner() {
  var newBannerNum = -1;
  var theBanners = grabBanners();
  if (theBanners) {
    for (var i=0; i<theBanners[0].length; i++) {
      if (theBanners[0][i] == theBanners[2]) {
        newBannerNum = i;
      }
    }
    if (newBannerNum != -1) {
      switchBanner(newBannerNum);
    }
  }
  com_sonoa_bannerTimeout = setTimeout(timerSwitchBanner,6000);
  return false;
}

function buttonSwitchBanner(theElement) {
  clearTimeout(com_sonoa_bannerTimeout);
  var newBannerNum = -1;
  var listParent = getParentElement(theElement, 'ul');
  if (listParent.nodeName.toLowerCase() == 'ul') {
    var listItems = listParent.getElementsByTagName('li');
    for (var i=0; i<listItems.length; i++) {
      var newLink = listItems[i].getElementsByTagName('a')[0];
      if (newLink == theElement) {
        newBannerNum = i;
      }
    }
    if (newBannerNum != -1) {
      switchBanner(newBannerNum);
    }
  }
  return false;
}

function switchBanner(bannerNumber) {
  var theBanners = grabBanners();
  if (theBanners) {
    for (var i=0; i<theBanners[0].length; i++) {
      var thisBanner = theBanners[0][i];
      thisBannerId = thisBanner.getAttribute('id');
      if (i == bannerNumber) {
        new Effect.Appear(thisBannerId);
      } else {
        new Effect.Fade(thisBannerId);
      }
    }
  }
}

function grabBanners() {
  var bannerArray = false;
  if (document.getElementById('banner_container') && document.getElementById('banner_container_inner')) {
    var bannerContainer = document.getElementById('banner_container');
    var bannerContainerInner = document.getElementById('banner_container_inner');
    var allBanners = new Array();
    bannerArray = new Array();
    var thisBanner;
    var nextBanner;
    var allDivs = bannerContainerInner.getElementsByTagName('div');
    var pushBanner = function(theElement) {
      if (theElement.className && /banner_item/.test(theElement.className)) {
        allBanners.push(theElement);
        if (theElement.style.display != 'none') {
          thisBanner = theElement;
        }
      }
    }
    forEach(allDivs,pushBanner);
    for (var i=0; i<allBanners.length; i++) {
      var theElement = allBanners[i];
      if (theElement == thisBanner) {
        var nextNum = (i + 1);
        if (nextNum >= allBanners.length) {
          nextBanner = allBanners[0];
        } else {
          nextBanner = allBanners[nextNum];
        }
      }
    }
    bannerArray.push(allBanners);
    bannerArray.push(thisBanner);
    bannerArray.push(nextBanner);
  }
  return bannerArray;
}

function stopBubble(e) {
  if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}


function processLinks() {
  var theAnchors = document.getElementsByTagName('a');
  var fixAnchor = function(theElement) {
    addEvent(theElement,'click',stopBubble);
    if (theElement.getAttribute('name')) {
      setAnchor(theElement);
    }
    if (hasClassHook(theElement)) {
      if (/js_switchBanner/.test(theElement.className)) {
        theElement.onclick = function() {
          return buttonSwitchBanner(this);
        }
      }
      if (/js_bgLink/.test(theElement.className)) {
        var parentDiv = getParentElement(theElement,"div");
        if (parentDiv) {
          parentDiv.onclick = function() {
            document.location.href = theElement.href;
            return false;
          }
        }
      }
    }
  }
  forEach(theAnchors,fixAnchor);
}
function processFormInputs() {
  var theFormInputs = document.getElementsByTagName('input');
  var fixInput = function(theElement) {
    if (theElement.getAttribute('type') && (theElement.getAttribute('type') == 'submit')) {
      var newLink = document.createElement('a');
      newLink.setAttribute('href','#');
      newLink.className = theElement.className;
      addClass(newLink,'image_button');
      var link_inner = document.createElement('span');
      if (theElement.className && /noText/.test(theElement.className)) {
        link_inner.innerHTML = "&nbsp;";
        link_inner.className = 'noText';
      } else {
        link_inner.innerHTML = theElement.getAttribute('value');
      }
      newLink.appendChild(link_inner);
      newLink.onclick = function() {
        return doFormSubmit(this);
      }
      addClass(theElement,'no_show');
      theElement.parentNode.insertBefore(newLink,theElement);
    }
    if (hasClassHook(theElement)) {
      if (/js_clearInput/.test(theElement.className)) {
        theElement.onclick = function() {
          var initialValue = getExtensionFromClass(theElement,'js_initialValue_');
          if (initialValue) {
            initialValue = initialValue.replace("_", " ").toLowerCase();
          }
          if (this.value.toLowerCase() == initialValue) {
            this.value = "";
          }
        }
      }
    }
  }
  forEach(theFormInputs,fixInput);
}
function doFormSubmit(theElement) {
  var parentForm = getParentElement(theElement, 'form');
  if (parentForm.nodeName.toLowerCase() == 'form') {
    parentForm.submit();
    return false;
  }
}
function processBlocks() {
  if (document.getElementById('blocks')) {
    var blockHolder = document.getElementById('blocks');
    var allBlocks = new Array();
    var holderChildren = blockHolder.getElementsByTagName('div');
    var maxHeight = 0;
    var checkBlock = function(theElement) {
      if (theElement.className && /block_inner/.test(theElement.className)) {
        allBlocks.push(theElement);
        var thisHeight = theElement.offsetHeight;
        if (thisHeight > maxHeight) {
          maxHeight = thisHeight;
        }
      } 
    }
    forEach(holderChildren,checkBlock);
    var setBlock = function(theElement) {
      theElement.style.minHeight = maxHeight + 'px';
    }
    forEach(allBlocks,setBlock);
  } else if (document.getElementById('main_content_blocks')) {
    var blockHolder = document.getElementById('main_content_blocks');
    var allBlocks = new Array();
    var holderChildren = blockHolder.getElementsByTagName('div');
    var maxHeight = 0;
    var checkBlock = function(theElement) {
      if (theElement.className && /block/.test(theElement.className)) {
        allBlocks.push(theElement.getElementsByTagName('div')[0]);
        var thisHeight = theElement.offsetHeight;
        if (thisHeight > maxHeight) {
          maxHeight = thisHeight;
        }
      } 
    }
    forEach(holderChildren,checkBlock);
    var setBlock = function(theElement) {
      theElement.style.minHeight = maxHeight + 'px';
    }
    forEach(allBlocks,setBlock);
  }
}
function setAnchor(theElement) {
  var thePayload = document.createTextNode(theElement.innerHTML);
  theElement.innerHTML = '';
  theElement.parentNode.insertBefore(thePayload,theElement);
}

/* UTILITY FUNCTIONS */
function forEach(array, action) {
  for (var i=0; i < array.length; i++) {
    action(array[i]);
  }
}

function removeChildNodes(theElement) {
  while (theElement.hasChildNodes()) {
    theElement.removeChild(theElement.firstChild);
  }
}

function getParentElement(theElement, targetNodeName) {
  var bodyNode = document.getElementsByTagName("body")[0];
  var currentParent = theElement;
  var targetNodeName = targetNodeName.toLowerCase();
  while ((currentParent.nodeName.toLowerCase() != targetNodeName) && (currentParent != bodyNode)) {
    currentParent = currentParent.parentNode;
  }
  return currentParent;
}

function stripWhitespace(theString) {
	return theString.replace(/^\s*|\s*$/g,'');
}

function addClass(theElement,theClass) {
	if (!theElement.className) {
		theElement.className = theClass;
	} else if (theElement.className.indexOf(theClass) == -1) {
		theElement.className += (" " + theClass);
	}
}

function removeClass(theElement,theClass) {
  if (theElement.className && theElement.className.indexOf(theClass) != -1) {
    theElement.className = stripWhitespace(theElement.className.replace(theClass,""))
  }
}

function hasClassHook(theElement) {
  return (theElement.className && /js_/.test(theElement.className));
}

function getExtensionFromClass(theElement,theKey) {
	var theExtension = false;
	if (theElement.className) {
    var findExtension = function(thisClass) {
      if (thisClass.indexOf(theKey) == 0) {
        var tempName = thisClass.substring(theKey.length);
        if (tempName.length > 0) {
          theExtension = tempName;
        }
      }
    }
    forEach(theElement.className.split(' '),findExtension);
  }
	return theExtension;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return [curleft,curtop];
}

function imagePreload() {
  var imageHolder = document.createElement("div");
  imageHolder.style.display = "none";
  var imageDirectory = './images/';
  var hoverImages = new Array();
  hoverImages.push('banner_button_large_active.png');
  hoverImages.push('banner_icon_demo_active.png');
  hoverImages.push('banner_icon_try_active.png');
  hoverImages.push('banner_icon_contact_active.png');
  hoverImages.push('banner_circle_filled.png');
  hoverImages.push('banner_small_back_active.png');
  hoverImages.push('banner_small_arrow_active.png');
  var fixImage = function(theElement) {
    var newImage = document.createElement("img");
    newImage.setAttribute("src",imageDirectory + theElement);
    newImage.setAttribute("alt","preloaded image");
    imageHolder.appendChild(newImage);
  }
  forEach(hoverImages,fixImage);
  document.getElementsByTagName("body")[0].appendChild(imageHolder);
}