(function() {
  $(function() {
    var hash_sid;
    if (!window.location.search.match(/s=.+/)) {
      hash_sid = window.location.hash.replace('#', '');
      if (hash_sid === "") {
        window.location = '#' + $('body').data('sid');
      } else if ($('body').data('sid') !== hash_sid) {
        window.location = "?s=" + window.location.hash.replace('#', '');
      }
    }
    $('#send_notes').val(window.location.href).click(function() {
      this.select();
      return mixpanel.track("click_link");
    });
    if (!document.createElement("canvas").getContext) {
      $('.result .chart').hide();
      $('.result .no_canvas').show();
    }
    if ($.browser.msie && $.browser.version < 8) {
      $('.browser_update').show();
    }
    $('#notify_container').notify();
    $('#earnings_slider').slider({
      min: 1000,
      max: 12000,
      step: 100
    }).bind('slide', function(event, ui) {
      return $('input#earnings').val(ui.value);
    });
    $('#obligations_slider').slider({
      min: 0,
      max: 4000,
      step: 100
    }).bind('slide', function(event, ui) {
      return $('input#obligations').val(ui.value);
    });
    $('.main_input #credit_amount_slider').slider({
      min: window.calc.capacityVars.min,
      max: window.calc.capacityVars.max,
      step: 5000
    });
    $('.main_input #years_slider').slider({
      min: 5,
      max: 50,
      step: 1
    });
    $('.main_input #percent_slider').slider({
      min: 2,
      max: 9,
      step: 0.1
    });
    $('.main_input a.ui-slider-handle').html('<div class="top_handle"></div><div class="bottom_handle"></div>');
    $('#credit_amount_slider a.ui-slider-handle .bottom_handle').text('100 tys. PLN');
    $('#years_slider a.ui-slider-handle .bottom_handle').text('5 lat');
    $('#percent_slider a.ui-slider-handle .bottom_handle').text('6 %');
    $('#credit_amount_slider').bind('slide', function(event, ui) {
      $('a.ui-slider-handle .bottom_handle', this).text(ui.value / 1000 + ' tys. PLN');
      return $('input#credit_amount').val(ui.value);
    }).bind('mouseup', function() {
      window.calc.showCreditAmountLegend();
      return mixpanel.track('parameter_changed');
    });
    $('#years_slider').bind('slide', function(event, ui) {
      $('a.ui-slider-handle .bottom_handle', this).text(ui.value + ' lat');
      return $('input#years').val(ui.value);
    }).bind('mouseup', function() {
      window.calc.showCreditAmountLegend();
      return mixpanel.track('parameter_changed');
    });
    $('#percent_slider').bind('slide', function(event, ui) {
      $('a.ui-slider-handle .bottom_handle', this).text(ui.value + ' %');
      return $('input#percent').val(ui.value);
    }).bind('slidestart', function() {
      return $('#percent_row .legend_blue').qtip('show');
    }).bind('slidestop', function() {
      $('#percent_row .legend_blue').qtip('hide');
      window.calc.showCreditAmountLegend();
      return mixpanel.track('parameter_changed');
    });
    $('#credit_amount_slider, #years_slider, #percent_slider').bind('slidechange', function(event, ui) {
      return $(this).trigger('slide', [ui]);
    });
    window.calc.updateCapacity();
    $('#earnings, #obligations, #people, #currency').change(function() {
      return window.calc.updateCapacity();
    }).keyup(function() {
      window.calc.updateCapacity();
      return mixpanel.track('parameter_changed');
    });
    $('#people, #currency').change(function() {
      return mixpanel.track('parameter_changed');
    });
    $('#currency').change(function() {
      return window.calc.updatePercentInterval(true);
    });
    $('#earnings_slider, #obligations_slider, #years_slider, #percent_slider').bind('slide', function() {
      return window.calc.updateCapacity();
    });
    $('#earnings_slider, #obligations_slider').bind('mouseup', function() {
      return mixpanel.track('parameter_changed');
    });
    $('#percent').change(function() {
      return window.calc.updateCapacity();
    });
    window.calc.updateResult();
    $('#credit_amount_slider, #years_slider, #percent_slider').bind('slide', function() {
      return window.calc.updateResult();
    });
    $('input[name=installment_type], #percent').change(function() {
      window.calc.updateResult();
      return mixpanel.track('parameter_changed');
    });
    $('#make_note').click(function() {
      note.createNote();
      return mixpanel.track('save_note');
    });
    $('.notes_table tr.note').click(note.loadNoteEvent);
    $('#clear_notes').click(function() {
      note.clearNotes();
      mixpanel.track("clear_notes");
      return false;
    });
    $('.notes_table .note:not(.blank_note) td.date').each(function() {
      return $(this).text(note.formatTime(new Date(Date.parse($(this).text()))));
    });
    note.repairNums();
    note.loadNote(note.defaultNote, true);
    $('.result .show_plan').click(function() {
      calc.planPopup();
      window.mixpanel.track('show_plan');
      return false;
    });
    $('#credit_amount_row .legend_list div').qtip({
      position: {
        my: 'top left',
        at: 'bottom middle'
      },
      style: {
        classes: 'ui-tooltip-rounded'
      }
    });
    $('#percent_row .legend_list div').qtip({
      position: {
        my: 'top left',
        at: 'bottom middle'
      },
      style: {
        classes: 'ui-tooltip-rounded'
      }
    });
    mixpanel.last_note = note.defaultNote;
    mixpanel.track('open_site');
    $('.addthis_button_facebook').live('click', function() {
      return mixpanel.track('icon_click', {
        name: 'facebook'
      });
    });
    $('.addthis_button_twitter').live('click', function() {
      return mixpanel.track('icon_click', {
        name: 'twitter'
      });
    });
    $('.addthis_button_email').live('click', function() {
      return mixpanel.track('icon_click', {
        name: 'email'
      });
    });
    return $('.addthis_button_print').live('click', function() {
      return mixpanel.track('icon_click', {
        name: 'print'
      });
    });
  });
}).call(this);

