/*



Dynamic Application of Functions to DOM
*/
var _debugMode = false;
document.observe('dom:loaded', init);
Event.observe(window, 'load', initAfterImages);
Event.observe(window, 'unload', function(){
  /* trying to fix ie caching stuff... */
});
/*


This is the function that applies the functions to the DOM
This occurs before initAfterImages();
*/
function init(){

  /*
  
  
  Reorder Links (reverse)
  */
  if ($('NavPrimary')){
    var NavPrimary = $('NavPrimary');
    var reversedLinks = $A();
    NavPrimary.select('li').each(function(n, i){
      reversedLinks.push(n);
    });
    reversedLinks.each(function(n, i){
      NavPrimary.insert({ top: n });
    });
  }
  /*
  
  Setting up the Angel Showcase animation
  */
  $$('#AngelShowcaseFeature img#AngelShowcaseFeatureImage', '#AngelShowcaseFeatureLocations li', '#AngelShowcaseGalleryGrid li').invoke('hide');
  /*
  
  
  Setting up the nav effect
  */
  if (ie6) {
    /* if the browser is ie6 or lower, add simple hover */
    $$('#NavPrimary li a', '#AngelShowcaseFeatureLocations li a').each(function(n, i){
      n.setStyle({ 'float': 'left' }).addClassName('IEhover');
    });
  } else {
    /* else add fancy scriptaculous hover effect */
    $$('#NavPrimary li', '#AngelShowcaseFeatureLocations li').each(function(n, i){
  
      var a = n.innerHTML;
      n.insert({ bottom: a });
      var a = n.down('a', 1).addClassName('hover').setStyle({ top: '-' + n.getHeight() + 'px', opacity: 0 });

      a.observe('mouseover', function(e){
        a.appear({
          duration: .2,
          from: a.getStyle('opacity'), to: 1,
          queue: { position: 'front', scope: 'queueScope' + i, limit: 2 },
          beforeStart: function(){
            var queue = Effect.Queues.get('queueScope' + i);
            queue.each(function(e){ e.cancel() });
          }
        });
      });
    
      a.observe('mouseout', function(e){
        new Effect.Opacity(a, {
          duration: 1,
          from: 1, to: 0,
          queue: { position: 'end', scope: 'queueScope' + i, limit: 2 }
        });
      });
    
    });
  }


  /*
  
  
  Clean up Gallery
  */
  $$('.GalleryPreview').each(function(n, i){
    n.select('.GalleryThumbnail').each(function(nn, ii){
      if (ii%3 == 2) nn.setStyle({'margin': '0px'});
    });
  });
  /*
  
  Remove extra paragraphs from gallery detail top header content
  */
  if ($('AngelShowcaseFeature') && $('AngelShowcaseFeature').down('div.description')){
    var ps = $('AngelShowcaseFeature').down('div.description').select('p');
    ps.each(function(p, i){
      if (i != 0 && i != 1 && i != 2 && i != ps.length - 1)
        p.remove();
    });
  }
  /*
  
  
  'center' images vertically when there is no title
  */
  $$('.GalleryThumbnail').each(function(n, i){
    if (!n.down('h5')) n.down('a.GalleryImage').setStyle({'top': '15px'});
  });
  
  /* hide 1st slide of home page feature */
  if ($('feature')){
    $('feature').down('.slide').hide();
  }
  
  $$('.GalleryDetailImages').each(function(ul){
    ul.select('li').invoke('setStyle', 'opacity: 0.8');
    setupOpacityHovers(ul);
  });
  
  // set the height of the mask before the page is rendered
  if ($('mask'))
    $('mask').setStyle({
      'height': ($('AngelShowcaseFeature').getHeight() - 20) + 'px'
    });
  

}
/* end init function */

/*

This function is attached to window.onload
This occurs after all images are loaded
*/
function initAfterImages (){
  
  /*
  
  Animating AngelShowcase
  */
  if($('AngelShowcaseFeatureLocations')){
    var AngelNav = $('AngelShowcaseFeatureLocations');
    /* setup */
    var AngelShell = $('AngelShowcaseFeature');

    var mask = $('mask');
    
    AngelNav.setStyle({'height' : '0px'});
    
    AngelShell.down('img').show();
    
    /* go */
    mask.morph('width: 0px;', { duration: 1.2, afterFinish: function(){ mask.remove(); } });
    AngelNav.morph('height: 70px',
    {
      duration: 1.0, delay: .6,
      afterFinish: function(){
        AngelNav.select('li').each(function(n, i){
          n.appear({ delay: i * .3, duration: .5 });
        });
      }
    });
    
    if ($('curtains'))
      $('curtains').show();
    /* fade in grid of thumbs */
    if ($('AngelShowcaseGalleryGrid')){
      var grid = $('AngelShowcaseGalleryGrid');
      var thumbs = grid.select('li');//.invoke('setStyle', 'opacity: 0');
      thumbs.each(function(thumb, i){
        thumb.down('a').insert('<div class="gridThumbBG">&nbsp;</div>');
        thumb.down('img').show();
        if (!ie6 && !Prototype.Browser.IE)
          thumb.appear({ from: 0, to: 0.8, delay: i * 0.2 + 1, duration: 0.4 });
        else
          thumb.show();
      });
      
      $$('div.gridThumbBG').invoke('setStyle', { 'opacity': '0.6' });
      
      setupOpacityHovers(grid);
    }
    
  }
  /* EndAnimating AngelShowcase */
  
  /*
  
  
  Do Home page feature
  */
  if ($('feature')) {
    var f = $('feature');
    var otherSlides = $R(2, 3).map(function(i){
      return '<a class="slide" href="/Contact-Us" style="display: none;"><img src="/images/feature#{i}.jpg" alt="" /></a>'.interpolate({i:i})
    }).join('');
    f.insert(otherSlides);
    new SlideShow(f, { fadeTime: 5 });
  }
  
  
}
/* end initAfterImages */

function setupOpacityHovers(ul){
  ul
    .observe('mouseover', function(e){
      var t = $(e.target);
      if (t.match('li') || t.descendantOf(t.up('li'))){
        Element.setStyle(t.up('li') || t, 'opacity: 1');
      }
    })
    .observe('mouseout', function(e){
      var t = $(e.target);
      if (t.match('li') || t.descendantOf(t.up('li'))){
        Element.setStyle(t.up('li') || t, 'opacity: 0.8');
      }
    });
}
