init_form = function() {
  // replace the generic button with a styled link
  $('submit-newsletter').remove();
  new Insertion.Bottom('form-newsletter', '<a href="#" class="submit" id="submit-newsletter">Submit</a>');
  
  var start_value = $('email').getValue();

  $('email').observe( 'focus', function(el) {
    if (this.getValue() == start_value) Event.element(el).clear();
    Event.stop(el);
  });

  $('email').observe( 'blur', function(el) {
    if (this.getValue() == '') Event.element(el).value = start_value;
    Event.stop(el);
  });

  $('submit-newsletter').observe( 'click', function(el) {
    $('form-newsletter').request({
      onComplete: function(response) {
        $('email').value = "Thanks for signing up";

        new PeriodicalExecuter( function(pe) { 
          $('form-newsletter').reset();
          pe.stop();
        }, 4);
      }
    });
    
    Event.stop(el);
  });
}

init_filters = function() {
  $$('ul.filters a').invoke( 'observe', 'click',  function(el) {
    new Ajax.Updater('sub-navigation', Event.element(el).href, {
      parameters: { ajax: true },
      onComplete: init_filters
    });

    Event.stop(el);
  });
}

init_external_links = function() {
  $$('.external').invoke( 'observe', 'click',  function(el) {
    window.open(Event.element(el).href); 
    Event.stop(el);
  });
}

init_player_btn = function() {
  $$('.audio-player').invoke('observe','click', function(el) {
    var win = window.open(Event.element(el).href, 'player', 'width=400, height=515, directories=no, location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no');
    win.focus();
    Event.stop(el);
  });
}

set_container_height = function() {
  var h = Math.max($('column-left').getHeight(), $('column-middle').getHeight())

  if (h > $('container').getHeight()) {
    $('container').setStyle({ height: h });     
  }
}

fix_image_size = function() {
  var max_width = 405;
  
  $$('.post img').each( function(el) {
    if (el.getWidth() > max_width) {
      el.width = max_width;
    }
  });
}

debug = function(msg) {
  console.log(msg);
}


var HighlightTableRows = Class.create();
HighlightTableRows.prototype = {
  initialize: function(rows, selector) {
    rows.invoke( 'observe', 'mouseover', function(el) {
      Event.element(el).up(1).toggleClassName(selector);
    });

    rows.invoke( 'observe', 'mouseout', function(el) {
      Event.element(el).up(1).toggleClassName(selector);
    });
  }
}


Event.observe( window, 'load', function() {
  init_form();
  //init_filters();
  init_external_links();
  init_player_btn();
  set_container_height();
  fix_image_size();
});

