(function() {
  Synerge = { 
    startHomeGallery: function() {},    // This is a stub - overriden function in gallery.js
    startWorkPage: function() {},       // This is a stub - overriden function in work.js
    startPortfolioPage: function() {},  // This is a stub - overriden function in portfolio.js
    toString: function() { return '[Synerge]'; } 
  };

  var m_nodes,
      m_imgCache,
      m_gallery,
      BANNER_RX= /^(home|contact|design|services|web development|industries|info|marketing|process|results|team)$/i,
      INNER_BG_RX = /^(chain|gears|monkeys|phone_cord|puzzle|rope_ladder)$/,
      INNER_BG = {
        team: 'monkeys',
        services: 'chain',
        'web development': 'gears',
        industries: 'rope_ladder',
        process: 'gears',
        work: 'chain',
        info: 'puzzle',
        contact: 'phone_cord'
      },
      IMG_LOCN = '/images/',
      IMG_LIST = [ 'request_quote_hover.png', 'nav_left_bg.png', 'nav_right_bg.png', 'nav_middle_bg.png', ],
      INNER_IMG_LIST = [ 'see_full_testimonial_hover.png', 'more_blog_hover.png' ],
      HOME_IMG_LIST = [ 'module_arrow_long_grey.png' ];

  function cacheImages( aImgList) {
    var img, mustPush = !!m_imgCache;
    if ( !Neaux.isArray(aImgList)) throw new Error('Image List must be an array');
    if ( aImgList.length == 0) return;
    if ( !m_imgCache) m_imgCache = new Array(aImgList.length);
    for ( var i=0, len=aImgList; i < len; i++) {
      img = new Image;
      img.src = IMG_LOCN + aImgList[i];
      if ( mustPush)
        m_imgCache[i] = img;
      else
        m_imgCache.push(img);
    }
  }

  function swapImage( aElm, aHoverImgName) {
    var theImg = Neaux.$(aElm), theImgName, theHoverImg;
    if ( !theImg || typeof theImg.tagName == 'undefined' || theImg.tagName != 'IMG') return;
    theImgName = theImg.src;
    aHoverImgName = IMG_LOCN + aHoverImgName;
    theHoverImg = new Image;
    theHoverImg.src = aHoverImgName;
    Neaux.on( theImg, 'mouseover,mouseout', function(aEvt) {
      theImg.src = ( aEvt.getType() == 'mouseover') ? theHoverImgName : theImgName;
    });
  }

  function analyzeNav( aMenu) {
    var theMenu = Neaux.$(aMenu), thePath = window.location.href, theNodes = [],
        theAnchors, thePtr, theLI, homePage;

    if ( !theMenu) return null;

    // Locate the anchor that contains the current page's URL
    theAnchors = theMenu.getElementsByTagName('a');
    for ( var i=0, len=theAnchors.length; i < len; ++i)
      if ( thePath == theAnchors[i].href) break;      

    // For a page not in the menu, return null
    if ( i == theAnchors.length) return null;
    thePtr = theAnchors[i];   // SAVE THIS FOR PAGE TITLE
    theID = theMenu.id;
    while( thePtr && thePtr.id != theID) {
      if ( thePtr.nodeName == 'LI') {
        theNodes.push(thePtr);
        thePtr.getElementsByTagName('a')[0].className = 'active';
      }
      thePtr = thePtr.parentNode;
    }
    homePage = theMenu.getElementsByTagName('li')[0];
    if ( homePage != theNodes[theNodes.length-1]) theNodes.push(homePage);

    return (m_nodes = theNodes.reverse());
  }

  function setPageTitle( aElm) {
    var theElm;
    if ( !m_nodes || !(theElm = Neaux.$(aElm))) return;
    theElm.innerHTML = m_nodes[m_nodes.length-1].getElementsByTagName('a')[0].innerHTML;
  }

  function setBreadcrumbs( aElm) {
    var theElm, theFrag, theClone, theText;
    if ( !m_nodes || !(theElm = Neaux.$(aElm))) return;
    theFrag = document.createDocumentFragment();
    for ( var i=0, len=m_nodes.length; i < len; i++) {
      if ( i > 0) theFrag.appendChild(document.createTextNode(' > '));
      theClone = m_nodes[i].getElementsByTagName('a')[0].cloneNode(true);
      theFrag.appendChild(theClone);
    }
    theElm.appendChild(theFrag);
  }

  function setSideNav( aSection, aNav, aTab) {
    var theSection, theNav, theUL, theTab, $$ = Neaux.$, theNodes = m_nodes || [], theText;
    if ( !(theSection = $$(aSection)) || !(theNav = $$(aNav)) ) return;

    // This is only applicable to top-level menu (i.e.; level 1) items with children
    // So if that is the case, we are interested in the first UL child element
    if ( theNodes.length > 1) theUL = theNodes[1].getElementsByTagName('ul');
    if ( theUL && theUL.length) {
      theUL = theUL[0]; // Save this for tab placement below
      // Clone the UL from the main nav (So we dont corrupt the main nav) and attach it to the nav container
      theNav.appendChild(theUL.cloneNode(true));

      // Get the name of the top menu element & build a tab image string based on the name & place accordingly
      if ( theTab = $$(aTab)) {
        // Makin' a lot of assumptions here...
        theElm = theUL.parentNode.getElementsByTagName('span'); // Collection of spans
        if ( theElm.length) {
          theText = theElm[0].innerHTML;
          theTab.src = IMG_LOCN+'sidetab_'+theText.toLowerCase() +'.png';
          theTab.alt = theText;
          theTab.parentNode.href = theUL.parentNode.getElementsByTagName('a')[0].href;
        }
      }
    }
    else
      theSection.style.display = 'none';
  }

  function setBannerImage( aImg) {
    //TODO: OVERRIDE, IF IMAGE NOT PLACEHOLDER, MAKE EDITABLE REGION override if 'Templates' not in src
    var i = m_nodes ? m_nodes.length : -1, a, sp, txt, theImg = Neaux.$(aImg);
    if ( !m_nodes || !theImg) return;
    while( i--) {
      a = m_nodes[i].getElementsByTagName('a')[0];
      sp = a.getElementsByTagName('span');
      a = sp.length ? sp[0] : a;
      txt = a.innerHTML;
      if ( txt.match(BANNER_RX)) break;
    }
    theImg.src = txt ? IMG_LOCN+txt.toLowerCase()+'_banner_text.png' : IMG_LOCN+'default_banner_text.png';
    theImg.alt = '';  // ???
  }

  function setInnerBackground( aContainer) {
    var i, a, sp, txt, theElm = Neaux.$(aContainer), theOvr = Synerge.innerPageOverride;
    if ( !theElm) return;
    if ( theOvr && theOvr.match(INNER_BG_RX)) {
      theElm.style.backgroundImage = 'url('+IMG_LOCN+theOvr+'_bg.png)';
      return;
    }
    if ( !m_nodes) return;
    i = m_nodes.length;
    while( i--) {
      a = m_nodes[i].getElementsByTagName('a')[0];
      sp = a.getElementsByTagName('span');
      a = sp.length ? sp[0] : a;
      txt = a.innerHTML.toLowerCase();
      if ( typeof INNER_BG[txt] != 'undefined') break;
    }
    if ( typeof INNER_BG[txt] != 'undefined') theElm.style.backgroundImage = 'url('+IMG_LOCN+INNER_BG[txt]+'_bg.png)';
  }

  function getDeltaTime( aStart) {  // FOR DEBUGGING
    var t0 = aStart.valueOf(), t1 = new Date();
    return (t1-t0);
  }

  /* ORIGINALLY THE REQUEST A QUOTE FORM WAS A POPUP - OBSOLETE
  function initRequestQuoteForm() {
    var ovl = Neaux.$('nw-overlay'), btnRequest = Neaux.$('btnRequest');
    if (ovl) ovl.style.display = 'none';
    if (!ovl || !btnRequest) return;
    Neaux.on( btnRequest, 'click', function( aEvt) {
      var bdy = document.body, top, left;
      ovl.style.visibility = 'hidden';
      ovl.style.display = 'block';
      top =  (bdy.offsetHeight - ovl.offsetHeight) / 2,
      left = (bdy.offsetWidth - ovl.offsetWidth) / 2;
      if ( top < 0) top = 0;
      if ( left < 0) left = 0;
      //ovl.style.top = top + 'px';
      ovl.style.left = left + 'px';
      ovl.style.visibility = 'visible';
      aEvt.preventDefault();
      Neaux.$('nw-close').onclick = function() { 
        ovl.style.display = 'none';
        this.onclose = null;
        return true;
      };
    });
  }
  */

  Neaux.main = function() { 
    //var d0 = new Date(), d1;    // FOR DEBUGGING
    var imgList = IMG_LIST;
    analyzeNav('nav_main');
    setPageTitle('pageTitle');
    setBreadcrumbs('breadcrumbs');
    setSideNav('sidebarNavContainer', 'nav_sidebar', 'sidebar_tab');
    setBannerImage('bannerTextImage');
    setInnerBackground('innerContainer');
    //initRequestQuoteForm();
    if ( Neaux.$('innerPage')) imgList.concat(INNER_IMG_LIST);
    if ( Neaux.$('homePage')) imgList.concat(HOME_IMG_LIST);
    cacheImages(imgList);
    Synerge.startHomeGallery();
    Synerge.startWorkPage();
    Synerge.startPortfolioPage();
    //var d1 = new Date(); alert(d1-d0);  // FOR DEBUGGING
    //var x = ''; for( var i=0,len=m_nodes.length;i<len;i++) x += m_nodes[i].getElementsByTagName('a')[0].innerHTML + '\n'; alert(x);
  };
}());
//<!-- End of synerge.js ->
