function notice(message) {
  var notice = $('flash_notice');
  if (notice) {
    notice.update(message);
    notice.show();
  }
}

function errors(message) {
  var errors = $('flash_errors');
  if (errors) {
    errors.update(message);
    errors.show();
  }
}

function login(message) {
  window.location = '/users/login';
}

function addFavorite(user_id, development_id) {
  new Ajax.Request('/users/' + user_id + '/favorites/', {method: 'post', parameters: {development_id: development_id}});
}

function addFavoriteResponse(user_id, development_id, favorite_id, development_name) {
  notice(development_name + ' has been added to your favorites.');

  var button = $('favorites_button_' + development_id);
  button.innerHTML = 'Unfavorite';
  button.onclick = function() { removeFavorite(user_id, favorite_id); }
  button.removeClassName('favorite-button-off');
  button.addClassName('favorite-button-on');
  ToolTip.onLoad();
}

function removeFavorite(user_id, favorite_id) {
  new Ajax.Request('/users/' + user_id + '/favorites/' + favorite_id, {method: 'delete'});
}

function removeFavoriteResponse(user_id, development_id, development_name) {
  notice(development_name + ' has been removed from your favorites.');

  var button = $('favorites_button_' + development_id);
  button.innerHTML = 'Favorite';
  button.onclick = function() { addFavorite(user_id, development_id); }
  button.removeClassName('favorite-button-on');
  button.addClassName('favorite-button-off');
  ToolTip.onLoad();
}

function setStyleSheetState(id, enabled) {
	var sheet = document.getElementById(id);
	if (sheet) sheet.disabled = !enabled;
}

// Observe onfocus event for all form fields in focusIdList, populating and positioning a div on the sidebar, so that 
// it's top matches the top of the first element returned by locationSelector. div has id "annotation"
function annotate(focusIdList, locationSelector, text) {
  var annotation = $('annotation');
  var annotation_wrapper = $('annotation_wrapper');

  if (!annotation_wrapper) return;
  var originTop = annotation.cumulativeOffset().top;

  focusIdList.each(function(id) {
    Event.observe($(id), 'focus', function(e) {
      // change content
      annotation.innerHTML = text;

      // move annotation to new location
      var locationTop = $$(locationSelector).first().cumulativeOffset().top;
      annotation_wrapper.style.top = Math.max(locationTop - originTop, 0) + 'px';

      // show
      annotation_wrapper.show();
    });
  });
}

// Annotate all form fields to work around browsers that do not support attribute selectors
Event.observe(window, 'load', function() {
  $$('input').each(function(input) { input.className = input.className + ' input-' + input.type; });
});
