var JobViewer;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
JobViewer = (function() {
  function JobViewer() {
    this.showJobDetails = __bind(this.showJobDetails, this);
    this.changeSection = __bind(this.changeSection, this);
    this.buildNavigationMenu = __bind(this.buildNavigationMenu, this);    $.ajaxSetup({
      cache: false
    });
    this.listen();
  }
  JobViewer.prototype.listen = function() {
    $('#careers #positions .openPositions a').click(this.showJobDetails);
    $('#positionDetailsOverlay').click(__bind(function(event) {
      if ($(event.srcElement).parents('#positionDetailsOverlay').length === 0) {
        return this.displayModal(false);
      }
    }, this));
    return $('#positionDetailsModal #jobDetails').click(__bind(function(event) {
      event.preventDefault();
      event.stopPropagation();
      return false;
    }, this));
  };
  JobViewer.prototype.buildNavigationMenu = function() {
    var headings, modalWindow, totalHeadings;
    modalWindow = $('#positionDetailsModal');
    headings = modalWindow.find('h2');
    totalHeadings = headings.length;
    headings.each(__bind(function(index, element) {
      var identifier, navigationItem, sectionName;
      sectionName = $(element).html();
      identifier = sectionName.toLowerCase().split(' ').join('');
      $(element).parent('div').attr('class', identifier);
      navigationItem = $(modalWindow).find('.navigation .template').clone().removeClass('template');
      navigationItem.find('a').attr('href', identifier).html(sectionName);
      if (index === 0) {
        navigationItem.addClass('first').find('a').addClass('selected');
      }
      if (index === (totalHeadings - 1)) {
        navigationItem.addClass('last');
      }
      navigationItem.find('a').click(this.changeSection);
      return $(modalWindow).find('.navigation').append(navigationItem);
    }, this));
    return $('#positionDetailsModal .navigation li:not(.template):eq(0) a').click();
  };
  JobViewer.prototype.changeSection = function(event) {
    var section;
    event.preventDefault();
    event.stopPropagation();
    section = $(event.currentTarget).attr('href');
    $(event.target).parents('.navigation').find('a.selected').removeClass('selected');
    $(event.target).addClass('selected');
    $('#jobDetails > div:not(.' + section + ')').hide();
    $('#jobDetails .' + section).show();
    return false;
  };
  JobViewer.prototype.displayModal = function(shouldDisplay) {
    var modalWindow, overlay;
    overlay = $('#positionDetailsOverlay');
    modalWindow = $('#positionDetailsModal');
    if (shouldDisplay) {
      overlay.css({
        height: $(document).height()
      });
      modalWindow.css({
        top: $(window).scrollTop()
      });
      overlay.fadeIn(200);
      $(document).bind('keydown', __bind(function(event) {
        if (event.keyCode === 27) {
          return this.displayModal(false);
        }
      }, this));
      return $(window).scroll(__bind(function(event) {
        if ($(document).scrollTop() < ($('#positionDetailsModal').offset().top - ($(window).height() * 0.8))) {
          return this.displayModal(false);
        }
      }, this));
    } else {
      return overlay.fadeOut(200, __bind(function() {
        var navigation;
        navigation = $('#positionDetailsModal #jobDetails .navigation').clone();
        navigation.find('li:not(.template)').remove();
        $('#positionDetailsModal #jobDetails').remove();
        return $('#positionDetailsModal').append(navigation);
      }, this));
    }
  };
  JobViewer.prototype.showJobDetails = function(event) {
    var jobURL, navigation;
    event.preventDefault();
    event.stopPropagation();
    jobURL = $(event.currentTarget).attr('href');
    this.displayModal(true);
    navigation = $('#positionDetailsModal .navigation').clone();
    $('#positionDetailsModal').load(jobURL + ' #jobDetails', __bind(function() {
      $('#positionDetailsModal #jobDetails h1').after(navigation);
      return this.buildNavigationMenu();
    }, this));
    return false;
  };
  return JobViewer;
})();
$(function() {
  return new JobViewer;
});
