// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Stop an event if global variable is set
var deaden = function(event) {
  if( deaden.enabled )
    Event.stop(event);
}
deaden.enabled = false;

if( window["ensure_help_navigation"] == undefined )
  var ensure_help_navigation = function() {};
if( window["ensure_help_relationships"] == undefined )
  var ensure_help_relationships = function() {};

if( window["ensure_meta_navigation"] == undefined )
  var ensure_meta_navigation = function() {};
if( window["ensure_meta_relationships"] == undefined )
  var ensure_meta_relationships = function() {};

function ensure_help() {
  ensure_help_navigation();
  ensure_help_relationships();
}
function ensure_meta() {
  ensure_meta_navigation();
  ensure_meta_relationships();
}

// Called at the start of a drag event
function start_effect(element) {
  element.addClassName("highlight");
  element._opacity = Element.getOpacity(element);
  Draggable._dragging[element] = true;
  new Effect.Opacity(element, {duration:0, from:element._opacity, to:0.5});
  $(element.parentNode.parentNode).addClassName( "highlight" );
}
// Called at the end of a drag event
function end_effect(element) {
  element.removeClassName("highlight");
  var toOpacity = typeof element._opacity == 'number' ? element._opacity : 1.0;
  new Effect.Opacity(element, {duration:0.5, from:0.5, to:toOpacity, 
    queue: {scope:'_draggable', position:'end'},
    afterFinish: function(){ 
      Draggable._dragging[element] = false 
    }
  });
  $(element.parentNode.parentNode).removeClassName('highlight');
}

Ajax.spinnerCount = 0;
Ajax.Responders.register({
  onCreate: function() {  
    $('spinner').show();
  },
  onComplete: function() {
    if( Ajax.activeRequestCount == 0)
      $('spinner').hide();
  }
});

// Extend InPlaceEditor to also use Ajax.Autocompleter on the text field.
Ajax.InPlaceEditorWithAutocomplete = Class.create( Ajax.InPlaceEditor, {
  initialize: function( $super, element, url, autocomplete_url, options) {
    if(!options.onEnterHover)
      options.onEnterHover = function(){};
    if(!options.onLeaveHover)
      options.onLeaveHover = function(){};
    if(!options.onComplete)
      options.onComplete = function(){};

    $super( element, url, options );

    this._autocomplete_url = autocomplete_url;

  },

  createEditField: function( $super ) {
    $super();

    var id = this.options.formId + "-" + this.options.paramName;
    this._controls.editor.id = id
    var div = document.createElement("div");
    div.id = id + "-auto_complete";
    div.className = "auto_complete";
    this._form.appendChild(div);
    this._autocomplete = new Ajax.Autocompleter(
      this._controls.editor,
      div,
      this._autocomplete_url,
      { method: 'get',
        onShow: function(element, update){ 
            Position.clone(element, update, {
              setHeight: false,
              setWidth: false,
              offsetTop: element.offsetHeight
            });
            Effect.Appear(update,{duration:0.15});
          }
      } );
  },

  leaveEditMode: function() {
    this.element.removeClassName(this.options.savingClassName);
    this.removeForm();
    this.leaveHover();
    this.element.style.backgroundColor = this._originalBackground;
    this.element.show();
    if (this.options.externalControl)
      this.options.externalControl.show();
    this._saving = false;
    this._editing = false;
    
    // These three lines were added
    if( this.options.evalScripts && this._oldInnerHTML ) {
      this.element.innerHTML = this._oldInnerHTML;
    }
    
    this._oldInnerHTML = null;
    this.triggerCallback('onLeaveEditMode');
  },

  getText: function() {
    return this.options.editStartText || "";
  }
});

Element.addMethods({
  wrap_inside: function(element, wrapper) {
    element = $(element);
    wrapper = $(wrapper);
    wrapper.innerHTML = element.innerHTML;
    element.innerHTML = "";
    element.appendChild( wrapper );
  }
});

function run_if_event_for( element, handler ) {
  return function(e) {
    var event_element = Event.element(e);
    if( $(event_element) == $(element) ) {
      handler(e);
    }
  }
}

Sortable.clear_options = function() { }
Sortable.fix_saved = function() { }
