/*


Dynamic Application of Functions to DOM
*/
var _debugMode = false;
document.observe('dom:loaded', init);
Event.observe(window, 'load', initAfterImages);


function init () {
  //apply css3 styles to ie6
  applyCSS3toIE6();
  
  //apply css3 selectors to all to ie6 and ie
  applyCSS3toIE();
  
  //check if the callout has any content in it, if so show it
  var callout = $('callout');
  if (callout && !callout.cleanWhitespace().innerHTML == '') callout.show();
  
  // setupHomeNewsletterLink();
  
  // setupYoutubeLinks();
  
  showSubNav();
  
  setupNavDropDown();
  
  formatTitleInSticky();
  
  setupWelcomeVideo();
}

function initAfterImages () {
  
}

function applyCSS3toIE6() {
  //check if it is ie6
  if (window.XMLHttpRequest || !Prototype.Browser.IE) return;
  //make 32bit pngs transparent and links inside clickable
  $$('#feature ul#stickies li').invoke('pngHack').invoke('observe', 'click', function(e) {
    window.location = e.element().down('a').getAttribute('href');
  }).invoke('observe', 'mouseenter', function(e) {
    window.status = e.element().down('a').getAttribute('href');
  }).invoke('observe', 'mouseleave', function(e) {
    window.status = '';
  });
  
  $$('.side-section input[type="submit"]').invoke('addClassName', 'submit');
  $$('.side-section input[type="text"]').invoke('addClassName', 'text');  
}

function applyCSS3toIE() {
  if (!Prototype.Browser.IE) return
  
  //add last child class
  $$('.side-section > *:last-child').invoke('addClassName', 'last-child');

}

function setupHomeNewsletterLink() {
  var link = $('newsletter-link');
  if (!link) return;
  var input = $('newsletter-input');
  link.observe('click', function(e) {
    e.stop();
    e.element().blur();
    Effect.ScrollTo(input, {duration: .25, offset: -80, afterFinish: function() {
      input.focus()
      input.select();      
    }});

  })
}

function setupYoutubeLinks() {
  // <object width="425" height="344">
  //   <param name="movie" value="http://www.youtube.com/v/TpJk-Wd5rDQ&hl=en&fs=1"></param>
  //   <param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
  //   <embed src="http://www.youtube.com/v/TpJk-Wd5rDQ&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
  // </object>
  var youtubeTemplate = new Template(
    '<object id="youtube-video" width="425" height="344"><param name="movie" value="#{url}"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="#{url}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>'
  );
  
  //Dom element to insert youtube video
  var main = $('main');
  var existingYoutubeVideo;
  
  //youtube link looks like http://www.youtube.com/v/TpJk-Wd5rDQ&amp;hl=en&amp;fs=1
  $$('#main a[href^="http://www.youtube.com/"]').invoke('observe', 'click', function(e) {
    var aLink = e.element().up();
    e.stop();
    e.element().blur();
    existingYoutubeVideo = $('youtube-video');
    if(existingYoutubeVideo) existingYoutubeVideo.remove();
    main.insert({bottom: youtubeTemplate.evaluate({ url: aLink.readAttribute('href') }) });
  });
}

function showSubNav() {
  var subNav = $('sub-nav');
  if (!subNav) return;
  if (subNav.select('ul').size() > 0) subNav.show();
}

function setupNavDropDown() {
  var target;
  $$('.drop-down').invoke('observe', 'mouseenter', function(e) {
    if (!e.element().down('div')) return;
    target = e.element().down('div').addClassName('down').next('ul').show();
  }).invoke('observe', 'mouseleave', function(e){
    if (!target) return;
    target.hide();
    target.previous('div').removeClassName('down');
  });
}

function formatTitleInSticky() {
  $$('#stickies h6').select(function(el){
    return el.getHeight() > 14;
  }).invoke('setStyle', {marginTop: '13px'});
}

function setupWelcomeVideo() {
  if (!$('feature')) return;
  $$('.video a')[0].observe('click', function(e) {
    e.stop();
    e.element().blur();
    $('welcome-video').appear({duration: .25});
  });
  $('welcome-video').observe('click', function(e) {
    e.element().match('small') ? e.element().up().hide() : e.element().hide();
  });
}