document.observe('dom:loaded', domLoaded);

Event.observe(window, 'load', windowLoaded);

function domLoaded () {  
  //someone clicks on a donation amount link
  var donationAmount = String(window.location).include('donation') ? String(window.location).split('=')[1].split('#')[0] : null;
  if (donationAmount && $('order_amount')) $('order_amount').value = donationAmount;
  
  
  // Hide last pipe character of tertiary nav on landing pages.
  if ($$('p.landingPageNav')[0]){
    var landingPageNav = $$('p.landingPageNav').first();
    var landingPageNavArray = landingPageNav.innerHTML.strip().split(' |');
    landingPageNav.update(
      landingPageNavArray.without(landingPageNavArray.last()).join(' |')
    );
  }

  // Make last word red in page titles
  if ($('pageHeader')){
    var pageHeader = $('pageHeader');
    var words = pageHeader.innerHTML.split(' ');
    if (words.length <= 1) return;
    words[words.length - 1] = '<span class="red">' + words.last() + '</span>';
    pageHeader.update(words.join(' '));
  }
  
  //if the secondary column isn't being used get rid of it
  if ($('secondary')) {
    if ($('secondary').innerHTML.strip().empty()) {
      $('primary').setStyle({ 
        width: '100%',
        borderRight: 'none'
        });
    }
  }
  
  // if($('gallery')) {
  //   $$('img').invoke('uniformScale', 300);
  // }
  
  if ($('searchForm')) $('searchForm').observe('submit', openResultsWindow);
  
  
  if($('column-wrapper')) {
    var titles = $$('#column-wrapper h1');
    // titles.invoke('redify_every_other_word');
  }
  
  //style and add money symbol on the ad landing pages in ie
  if (Prototype.Browser.IE) {
    var stats = $('stats');
    if (stats) {
      stats.select('a').invoke('insert', {top: '<span>$</span>'})
    }
  }
}

function windowLoaded () {
  // Control minimum height of content box so it is always lower than the navigation.
  if ($('wrapper')) {
    var wrapper = $('wrapper');
    var primary = $('primary');
    var secondary = $('secondary');
    var cHeight = (wrapper.getHeight() > 600) ? wrapper.getHeight() : 600;
    
    if (wrapper) {
      wrapper.setStyle({height: cHeight + 'px'});
      if (primary) {
        primary.setStyle({height: '350px'});
        if (secondary && secondary.getHeight() > primary.getHeight())
          primary.setStyle({height: secondary.getHeight() + 'px'});
      }
    }
  }

  $('shell').setStyle('background: url(../images/background.jpg) no-repeat;');
  
  

}

function openResultsWindow(e) {
  $(e).stop();
  // window.open('http://www.google.com/search?q=' + $('q').value + '&sitesearch=waterfrontmission.org');
}

Element.addMethods('img', {
  uniformScale: function(el, scaledSize) {
    if (el.getWidth() > scaledSize) {  
      var el = $(el);
      var height = el.getHeight();
      var width = el.getWidth();
      var ratio = (height > width) ? width/height : height/width;
      var scaledSize = scaledSize || 300;
    
      var base = scaledSize + 'px';
      var modified = scaledSize * ratio + 'px';
    
      if (height > width)
        return el.setStyle({ height: base, width: modified });
      else
        return el.setStyle({ width: base, height: modified });
    }
  }
});

Element.addMethods({
  redify_every_other_word: function(el) {
    var text = el.innerHTML.split(' ');
    for (var i = text.length - 1; i >= 0; i--){
      if (i%2 != 0) text[i] = '<span class="red">' + text[i] + '</span>';
    };
    el.innerHTML = text.join(' ');
  }
})

function cl (str) {
  if (console) console.log(str);
}