/*


Dynamic Application of Functions to DOM
*/
var _debugMode = false;
document.observe('dom:loaded', init);
Event.observe(window, 'load', initAfterImages);

new SlideShow('home-slideshow', {
  crossFade: true,
  startHidden: false,
  slidesSelector: ' li',
  slideDuration: 4,
  pauseOnMouseover: false
});



function init () {
  
  if (!$('print-logo')) $('head').down().insert('<img id="print-logo" src="/images/print-logo.jpg" width="294" height="90"/>');
  
  [$$('.tab'), $('logo'), $('close-button')].compact().flatten().invoke('pngHack');
  if (Prototype.Browser.IE) $('logo').observe('click', function() {
    window.location = "/";
  });
  
  // var flash = $('flash');
  // if (flash && flash.down('#message').descendants().length > 1) {
  //   Prototype.Browser.IE ? flash.show() : flash.appear();
  //   $('backdrop').show();
  //   var executer = new PeriodicalExecuter(growl_hide, 20);
  //   flash.observe('mouseover', growl_hover.curry(executer)).observe('mouseout', growl_hover.curry(executer)).observe('click', growl_hide.curry(executer));
  // }
  
  $$('#primary table tr:nth-child(even), #primary table tr:nth-child(even), #secondary table tr:nth-child(even), #secondary table tr:nth-child(even)').invoke('addClassName', 'alt');
  
  var variation = $('variation');
  if (variation) $('add').observe('click', function(e) {
    if (variation.value != "label") return;
    e.stop();
    variation.up().highlight({duration: 3, startcolor: '#FF0000', endcolor: '#FFEACA', restorecolor: '#FFEACA'});
  });
  
  $('show-signup').observe('click', function(e){
    e.stop();
    $('email-sign-up').appear({duration: .5});
  });

  
  // hide press/philanthropy images while loading
  // $$('div.spotlight-item .top img').invoke('hide');

  $('close').observe('click', function(e){
    $('email-sign-up').fade({duration: .5});
  });

  // gift ideas view
  $$('.category-container .tab').each(function(tab, i){
    // if something like '$25 or less'
    var h3 = tab.down('h3');
    var h3i = h3.innerHTML;
    if (h3.innerHTML.startsWith('$') && h3.innerHTML.include(' or less')) {
      // remove ' or less' from the h3
      h3.remove();
      tab.insert('<h3>' + h3i.gsub(' or less', '') + '</h3>')
    } else {
      // else remove the p
      tab.down('p').remove();
      h3.setStyle('font-size: 24px; line-height: 37px; padding-right: 20px');
    }
  });
  
  // limiting the gift message textareas to 255 chars
  $$('textarea.gift_message').each(function(textarea, i){
    textarea.previous('.character-count').update(255 - textarea.getValue().length);
    textarea.observe('keyup', function(e){
      if (this.getValue().length > 255) {
        this.value = this.getValue().slice(0, 255);
        textarea.highlight({ startcolor: '#ff0000', endcolor: '#ffffff', restorecolor: '#ffffff' });
      }
      else this.previous('.character-count').update(255 - this.getValue().length);
    });
  });

  var dateChooser = $('date-chooser');
  if (dateChooser && Prototype.Browser.IE) dateChooser.observe('click', function(e){
    dateChooser.down('input').checked = true;
  });
  
  if (location.pathname == '/') $('cocoDesign').show();
  
}

function growl_hover(executer, e) {
  var flash = $('flash');
  if (e.type == 'mouseover') {
    executer.stop();
    flash.addClassName('hover');
  }
  else {
     flash.removeClassName('hover');
     executer = new PeriodicalExecuter(growl_hide, 5);
  }
}

function growl_hide(executer) {
  if (Prototype.Browser.IE) {
    $('flash').hide();
    $('backdrop').hide();
  }
  else $('flash').fade({afterFinish: function(){$('backdrop').hide()}});
  executer.stop();
}

function initAfterImages () {
  // Center images on the press and philanthropy pages vertically in the white space
  var centerImageVertically = function(img, i){
    var imgHeight = img.getHeight();
    var shellHeight = 70;
    var margin = 12;
    var newMarginTop = margin + (shellHeight - imgHeight) / 2;
    img.setStyle({ marginTop: newMarginTop + 'px' });//.show();
  }
  $$('div.spotlight-item .top img').each(centerImageVertically);
}
