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

/* fade flashes automatically */
Event.observe(window, 'load', function() {
  $A(document.getElementsByClassName('notice-alert')).each(function(o) {
    o.opacity = 100.0
    Effect.Fade(o, {duration: 8.0})
  });
});

function set_course_begin(course_id, availability) {
  if (!$('price_form' + course_id)) {
    new Ajax.Request('/public/price_calculator/' + course_id, {asynchronous:false, evalScripts:true});
  }
  $('price-calculator-title' + course_id).focus();
  f = $('price_form' + course_id);
  sel = f.booking_course_availability_id;
  for (var i = 0; i < sel.length; i++) {
    if (sel.options[i].value == availability) {
      sel.options[i].selected = true;
      f.onsubmit();
      return true;
    }
  }
}

function submit_coursefinder() {
    Element.hide('coursefinder_submit');
    Element.show('spinner');
    $('coursefinder_form').submit();
    $('coursefinder_form').getElementsBySelector('select').each(function(e){$(e).disabled=true})
    $('coursefinder_form').getElementsBySelector('input').each(function(e){$(e).disabled=true})
}

DelayedObserver = Class.create();
DelayedObserver.prototype = {
  initialize: function(element, ev, frequency, callback){
    this.element = $(element);
    this.frequency = frequency;
    this.event = ev;
    this.callback = callback;
    this.timer = false;
    this.registerObserver();
  },

  registerObserver: function(){
    Event.observe(this.element, this.event, this.onTimeoutEvent.bind(this));
  },

  doCallback: function() {
    this.callback(this.element);
  },

  onTimeoutEvent: function(){
    clearTimeout(timer);
    timer = setTimeout(this.doCallback.bind(this), this.frequency * 1000);
  }
};

WelcomeAnim = Class.create();
WelcomeAnim.prototype = {
  initialize: function(){
    this.active = 0;
    this.panel = 1;
    this.run = true;
    this.frequency = 1.3;
  },

  setMenu: function(name){
    this.run = false;
    this.showMenu(name);
  },

  showMenu: function(name){
    for (var x = 1; x <= 6; x++) {
      this.disableMenu(x);
    }
    this.enableMenu(name);
  },

  enableMenu: function(name){
    $('welcome_ma_'+name).src = "/images/hp/welcome_ma_"+name+".png";
  },

  disableMenu: function(name){
    $('welcome_ma_'+name).src = $('welcome_m_'+name).src;
  },

  show: function(name){
    if (this.active == name)
      return false;
    var queue = Effect.Queues.get('global');
    queue.each(function(effect) { effect.cancel(); });
    Effect.Fade('step' + this.active, { duration: 0.5 });
    Effect.Appear('step' + name, { duration: 0.4 });
    this.active = name;
  },

  start: function(){
    if (!this.run)
      return;
    this.show(this.panel);
    this.showMenu(this.panel);
    this.panel++;
    if (this.panel == 7) {
      this.run = false;
      setTimeout("anim.reset()", this.frequency * 1000);
    } else {
      setTimeout("anim.start()", this.frequency * 1000);
    }
  },

  reset: function(){
    this.panel = 0;
    this.show(this.panel);
    for (var x = 1; x <= 6; x++) {
      this.enableMenu(x);
    }
  }
};

var anim = new WelcomeAnim();
var timer = null;

function anim_start() {
  anim.start();

  new DelayedObserver('welcome_ma_1', 'mouseover', 0.6, function(){
    anim.show(1);
  });
  new DelayedObserver('welcome_ma_2', 'mouseover', 0.6, function(){
    anim.show(2);
  });
  new DelayedObserver('welcome_ma_3', 'mouseover', 0.6, function(){
    anim.show(3);
  });
  new DelayedObserver('welcome_ma_4', 'mouseover', 0.6, function(){
    anim.show(4);
  });
  new DelayedObserver('welcome_ma_5', 'mouseover', 0.6, function(){
    anim.show(5);
  });
  new DelayedObserver('welcome_ma_6', 'mouseover', 0.6, function(){
    anim.show(6);
  });
}

Ajax.CachedAutocompleter = Class.create();

Object.extend(Object.extend(Ajax.CachedAutocompleter.prototype, Autocompleter.Base.prototype), {
  initialize: function(element, update, url, options) {
    this.baseInitialize(element, update, options);
    this.options.asynchronous = true;
    this.options.onComplete = this.onComplete.bind(this);
    this.options.defaultParams = this.options.parameters || null;
    this.url = url;
    this.cache = {};
  },

  getUpdatedChoices: function() {
    var t = this.getToken();
    if (this.cache[t]) {
      this.updateChoices(this.cache[t]);
    } else {
      entry = encodeURIComponent(this.options.paramName) + '=' +
      encodeURIComponent(t);

      this.options.parameters = this.options.callback ?
      this.options.callback(this.element, entry) : entry;

      if(this.options.defaultParams)
        this.options.parameters += '&' + this.options.defaultParams;
        new Ajax.Request(this.url, this.options);
      }
  },

  onComplete: function(request) {
    this.updateChoices(this.cache[this.getToken()] = request.responseText);
  },

  // Page jump fix
  markPrevious: function() {
    if (this.index > 0) {
      this.index--;
    } else {
      this.index = this.entryCount-1;
      this.update.scrollTop = this.update.scrollHeight;
    }

    selection = this.getEntry(this.index);
    selection_top = selection.offsetTop;

    if (selection_top < this.update.scrollTop) {
      this.update.scrollTop = this.update.scrollTop - selection.offsetHeight;
    }
  },

  markNext: function() {
    if(this.index < this.entryCount-1) {
      this.index++;
    } else {
      this.index = 0;
      this.update.scrollTop = 0;
    }

    selection = this.getEntry(this.index);
    selection_bottom = selection.offsetTop+selection.offsetHeight;

    if(selection_bottom > this.update.scrollTop+this.update.offsetHeight){
      this.update.scrollTop = this.update.scrollTop+selection.offsetHeight;
    }
  },

  updateChoices: function(choices) {
    if (!this.changed && this.hasFocus) {
      this.update.innerHTML = choices;
      Element.cleanWhitespace(this.update);
      Element.cleanWhitespace(this.update.down());

      if (this.update.firstChild && this.update.down().childNodes) {
        this.entryCount =
          this.update.down().childNodes.length;
        for (var i = 0; i < this.entryCount; i++) {
          var entry = this.getEntry(i);
          entry.autocompleteIndex = i;
          this.addObservers(entry);
        }
      } else {
        this.entryCount = 0;
      }

      this.stopIndicator();
      this.update.scrollTop = 0;
      this.index = 0;

      if (this.entryCount==1 && this.options.autoSelect) {
        this.selectEntry();
        this.hide();
      } else {
        this.render();
      }
    }
  }
});
