(function() {
  function startHomeGallery() {
    var theContainer = Neaux.$('gallery_images'), curImg=0, isStopped = false, theOffset = 0, 
        theImages, theImage, theTabs, theTimer, theTween,
         HOME_DURATION = 3000,
        HOME_INITIAL_DELAY = 4000,
        HOME_DELAY = 4000
        HOME_ROLLOVER_DELAY = 150,
        WORK_MAX_TABS = 5;
    if ( !theContainer) return;
  
    theContainer.style.marginLeft = '0px';  // FOR NOW
    theImages = theContainer.getElementsByTagName('img');
    theTabs = Neaux.$$('#gallery_nav a');
    for ( var i=0, len = theImages.length; i < len; i++) {
      theImage = theImages[i];
      theImage.style.left = theOffset + 'px';
      theImage.style.visibility = 'visible';
      theImage.__nwid = i;
      theTabs[i].__nwid = i;
      theOffset += 680;
    }
    if ( typeof curImg != 'undefined') theTabs[curImg].className = 'active';
  
    function _setTab( aNewImg) {
      if ( isStopped && theTimer) {
        clearTimeout( theTimer);
        theTimer = null;
      }
      if ( aNewImg == curImg) return;
      if ( theTween && theTween.isTweenInProgress()) theTween.stop();
      theTween = new Neaux.Tween( function() {
        var start, delta;
        return {
          auto: true, // DOESN'T WORK!!!
          listeners: {
            start: function() {
              theTabs[curImg].className = '';
              start = parseInt(theContainer.style.marginLeft);
              delta = (680 * -aNewImg) - start;
            },
            change: function( aEvt) { 
              var offset = start + delta * aEvt.tween;
              theContainer.style.marginLeft = offset + 'px';
            },
            finish: function() {
              var nextImg = aNewImg + 1;
              theTimer = null;
              theTabs[aNewImg].className = 'active';
              curImg = aNewImg;
              if ( !isStopped && (nextImg < theImages.length) )
                theTimer = Neaux.Function.delay( function() { _setTab(nextImg); }, HOME_DELAY);
            }
          }
        }
      }());
      theTween.start();
    }
  
    function _maybeSetTab(aElt) {
      var timerID = null;
      aElt.onmouseout = function() { 
        clearTimeout(timerID);
        timerID = null;
        aElt.onmouseout = null;
      };
      function timerHandler() {
        timerID = null;
        aElt.onmouseout = null;
        isStopped = true; 
        _setTab(aElt.__nwid);
      }
      timerID = setTimeout( timerHandler, HOME_ROLLOVER_DELAY);
    }
  
    //Neaux.on( theTabs, 'mouseover', function(aEvt) { isStopped = true; _setTab(this.__nwid); } ); 
    Neaux.on( theTabs, 'mouseover', function(aEvt) { _maybeSetTab(this); } ); 
    theTimer = Neaux.Function.delay( function() { _setTab(1); }, HOME_INITIAL_DELAY);
  
  }
  Synerge.startHomeGallery = startHomeGallery;
})();

/* ORIGINAL HOME PAGE ROTATOR w/ FADE
  //
  // startHomeGallery initiates and manages rotation of several banner images on the home page.
  //
  function startHomeGallery() {
    var theContainer = Neaux.$('home_gallery'), doFade = !Neaux.isWebKit, isStopped = false, 
        theTimer, theDelay, curImg=0, prevImg, theImage, theImages, theTabs, zIndex = 900;
    if ( !theContainer) return;

    // For each image in the rotation and each tab associated with the image, place the array index
    // in the dom object so we can access the corresponding tab for each image later on. 
    theImages = theContainer.getElementsByTagName('img');
    theTabs = Neaux.$$('#gallery_nav a');
    for ( var i=0, len = theImages.length; i < len; i++) {
      theImage = theImages[i];
      theImage.__nwid = i;
      theTabs[i].__nwid = i;
      //if ( theImage.style.visibility == 'visible') curImg = i;  // TODO: NEED TO USE CURRENT STYLE, NOT ELEMENT
    }
    if ( typeof curImg != 'undefined') theTabs[curImg].className = 'active';

    // _setTab is called to set the current banner image in the "gallery". This is called by the 
    // image rotator or by the click listener associated with a tab.
    function _setTab( aNewImg) {
      var theTarget, nextImg;
      //window.status = 'new: '+aNewImg+' --- '+'cur: '+curImg;
      if ( isStopped && theTimer) {
        clearTimeout( theTimer);
        theTimer = null;
        doFade = false;
      }
      if ( aNewImg == curImg) return;
      if ( doFade) {
        theTarget = theImages[aNewImg];
        theTarget.style.visibility = 'hidden';
        if ( typeof theTarget.style.filter == 'undefined')
          theTarget.style.opacity = 0;
        else {
          if ( !theTarget.currentStyle || !theTarget.currentStyle.hasLayout) theTarget.style.zoom = 1;
          theTarget.style.filter = 'alpha(opacity=0)';
          theTarget.style.zIndex = ++zIndex;
        }
        theTarget.style.visibility = 'visible';
        // DOESN'T WORK IN WEBKIT
        m_transition = new Neaux.Transition( {
            target: theTarget, 
            property: 'opacity', from: 0, to: 1,
            duration: HOME_DURATION,
            auto: true,   // DOESN'T WORK
            listeners: {
              finish: function() {
                if ( isStopped) return;
                theImages[aNewImg].style.visibility = 'visible';
                theImages[curImg].style.visibility = 'hidden';
                theTabs[aNewImg].className = 'active';
                theTabs[curImg].className = '';
                curImg = aNewImg;
                nextImg = curImg + 1;
                if ( nextImg < theImages.length) 
                  theTimer = Neaux.Function.delay( function() { _setTab(nextImg); }, HOME_DELAY);
              }
            }
        });
        //m_transition.__debug = true;
        m_transition.start();
      }
      else {
        theImages[aNewImg].style.visibility = 'visible';
        theImages[curImg].style.visibility = 'hidden';
        theTabs[aNewImg].className = 'active';
        theTabs[curImg].className = '';
        curImg = aNewImg;
        nextImg = curImg + 1;
        if ( !isStopped && nextImg < theImages.length) 
          theTimer = Neaux.Function.delay( function() { _setTab(nextImg); }, HOME_DELAY);
      }
    } // End of _setTab function

//complete: function( aEvt) { _setTab(aEvt.image.__nwid);  if ( aEvt.image.__nwid == (theImages.length - 1)) this.stop(); }
    Neaux.on( theTabs, 'mouseover', function(aEvt) { isStopped = true; _setTab(this.__nwid); } ); 
    theDelay = doFade ? HOME_INITIAL_DELAY : HOME_DELAY;
    theTimer = Neaux.Function.delay( function() { _setTab(1); }, theDelay);
  } // End of startHomeGalleryRotator function
*/
//<!-- End of gallery.js ->
