var $interestListSourceInput,
    $interestListSourceOtherInput,
    $neighborhoodArrow,
    $neighborhoodDetails,
    $neighborhoodTitle;

function updateSourceOptions () {

  // add or remove options based on which neighborhoods selected
  var otherSelected = ($interestListSourceInput.val() == 'other');
  $interestListSourceInput.removeOption('other');
  for (var neighborhoodId in neighborhoodInputs) {
    for (var i in interestListSources[neighborhoodId]) {
      if ($("#" + neighborhoodInputs[neighborhoodId]).is(":checked")) {
        $interestListSourceInput.addOption(i, interestListSources[neighborhoodId][i], false);
      } else {
        $interestListSourceInput.removeOption(i);
      }
    }
  }
  $interestListSourceInput.addOption('other', "Other", otherSelected).trigger('change');

  // check if select has any options
  if ($interestListSourceInput.children().length > 2) {
    if ($interestListSourceInput.is(":hidden")) {
      $interestListSourceInput.val('').trigger('change').parent().slideDown("fast");
    }
  } else {
    $interestListSourceInput.val('').parent().slideUp("fast");
    $interestListSourceOtherInput.parent().slideUp("fast");
  }
}

function checkSelectedSource() {
  if ($interestListSourceInput.val() == 'other') {
    $interestListSourceOtherInput.parent().slideDown("fast");
  } else {
    $interestListSourceOtherInput.val('').trigger('change').parent().slideUp("fast");
  }
}

$(document).ready(function() {

  $interestListSourceInput = $("#InterestListInterestListSourceId");
  $interestListSourceOtherInput = $("#InterestListInterestListSourceOther");
  $neighborhoodArrow = $('#neighborhood-arrow');
  $neighborhoodDetails = $('#neighborhood-details');
  $neighborhoodTitle = $('#neighborhood-title');

  if (jQuery.browser.msie) {
    if (jQuery.browser.version.substr(0,1) == "8") {
      $neighborhoodTitle.css('margin-bottom', $neighborhoodTitle.css('margin-bottom') == '0px' ? '1px' : '0px');
    }
    if (jQuery.browser.version.substr(0,1) == "7") { 
      var $len = $neighborhoodDetails.children('.neighborhood-sub-text').children('.input').children('.checkbox').length;
      $neighborhoodDetails.css('height', (31 + $len * 25) + 'px');
    }
    if (jQuery.browser.version.substr(0,1) == "6") {
      $neighborhoodDetails.css('height', $neighborhoodDetails.outerHeight());
    }
  }

  $interestListSourceInput.parent().hide();
  $interestListSourceOtherInput.parent().hide();
  $("#InterestListAddForm input[type=submit]")
    .css('cursor', 'pointer')
    .click(function () {
      $(this)
        .css('opacity', 0.5)
        .css('cursor', 'auto')
        .attr('disabled', true);
      $('#InterestListAddForm').submit();
    });

  $('div.input.expandable').addClass('expanded').click(function() {
    var me = $(this);
    if (me.is(':animated')) { return; }
    
    var is_expanded = me.hasClass('expanded');
    
    // Toggle off
    if (is_expanded) {
      me.find('img').attr('src', '/img/neighborhood_on.gif');
      $('#' + me.attr('rel')).slideUp();
      me.removeClass('expanded');
    }
    
    // Toggle on
    else {
      me.find('img').attr('src', '/img/neighborhood_off.gif');
      $('#' + me.attr('rel')).slideDown();
      me.addClass('expanded');
    }
  });

  updateSourceOptions();
  checkSelectedSource();

  var noneSelected = true;
  for (var id in neighborhoodInputs) {
    var nInput = $("#" + neighborhoodInputs[id]);
    nInput.parent().click(updateSourceOptions);
    if (nInput[0].checked) noneSelected = false;
  }
  if (noneSelected) {
    $('#neighborhood-title').click();
  }

  $interestListSourceInput.change(checkSelectedSource);


});

