/*
 * Implement a custom Event Observer.  Makes it easeir to do
 * OSX Spotlight style searching where specific elements are
 * shown based on the previous selection.
 */

Abstract.SmartEventObserver = Class.extend(Abstract.EventObserver, {
  onElementEvent: function(event) {
    var value = this.getValue();
    if (this.lastValue != value) {
      this.callback(Event.element(event), value, event);
      this.lastValue = value;
    }
  }
});

var SmartForm = {};
SmartForm.EventObserver = Class.extend(Abstract.SmartEventObserver, {
  getValue: function() {
    return Form.serialize(this.element);
  }
});

var SmartSearch = Class.create();
SmartSearch.prototype = {
  initialize: function(form, conditions, triggersSubmit) {
    this.element = $(form);
    this.conditions = $A(conditions);
    this.triggersSubmit = triggersSubmit;
    if(!this.element) return;

    new SmartForm.EventObserver(this.element, this.onChange.bind(this));
  },

  onChange: function(element, event) {
    if (typeof this.triggersSubmit == 'object') {
      if(this.triggersSubmit.include(element.id)) {
        this.element.submit();
        return false;
      }
    }
    else {
      if(element == $(this.triggersSubmit)) {
        this.element.submit();
        return false;
      }
    }

    this.conditions.each(function(condition) {
      if(condition.keys.include($F(element))) {
        $A(condition.show).each(function(e) { $(e).show(); $(e).disabled = false;});
        $A(condition.hide).each(function(e) { $(e).hide(); $(e).disabled = true; });
      }
    }.bind(this));
    return false;
  }
}

Event.addBehavior({
  '#add-filters:click': function(event) {
    Event.stop(event);
    this.hide();
    $('filter').show();    
    $('remove-filters').show();
    ['duringlist', 'dating_to_start_list', 'dating_to_end_list', 'originlist', 'materiallist','exhibitionlist'].each(function(e) { $(e).hide(); $(e).disabled = true; });
    ['filterlist','culturelist'].each(function(e) { $(e).show(); $(e).disabled = false; });
  },
  'a.art_object_icon:click': function(event) {
    Event.stop(event);

    //Ajax call to get detailed data and update the detailed results list.
    //Whil loading show the detailed_results div (if not visible) and scroll to it upon completion.
    id = this.id.replace(/art-/,'');
    url = '/show_art_object/' + id;

    new Ajax.Updater('detailed_results', url,
                     {asynchronous:true,
                      evalScripts:false,
                      onLoading: function(){ $('detailed_results').hide();$('spinner').show();},
                      onComplete: function(){ $('spinner').hide();$('detailed_results').show();Nifty("*.rc", "transparent");new Effect.ScrollTo('random_art_object');}
                     });

    //Add/remove 'current' class to clicked link
    $$('a.art_object_icon').each(function(e){ if ($(e).hasClassName('current')) { $(e).removeClassName('current'); }});
    this.addClassName('current');

    //Make specific art object details appear (and hide the rest)
    //id = this.id.replace(/art-/,'');
    //$$('#detailed_results li.art_object_brief').each(function(e){ if ($(e).visible()) { new Effect.Fade(e, {queue: {scope: 'art_objects'} }); }});
    //new Effect.Appear('brief_' + id, {queue: {position: 'end', scope: 'art_objects'} });

    //Update the 'random_art_object' image and link from the 'results' item.
    $('random_art_object_link').href = this.href;
    $('random_art_object').src = $('full_' + id).src.replace(/_tiny/,'');

  },
  '#remove-filters:click': function(event) {
    Event.stop(event);
    this.hide();
    $('filter').hide();
    $('add-filters').show();
    $('filterlist').value = 'culture';
    ['filterlist','duringlist', 'dating_to_start_list', 'dating_to_end_list', 'originlist', 'materiallist','exhibitionlist','culturelist'].each(function(e) { $(e).hide(); $(e).disabled = true; });
  },
  '#artobjects-search': function() {
    new SmartSearch('artobjects-search', [
      {keys: ['during'],                    show: ['duringlist'],                                               hide: ['originlist','materiallist','exhibitionlist','culturelist','dating_to_start_list','dating_to_end_list']},
      {keys: ['dating_to'],                 show: ['dating_to_start_list','dating_to_end_list'],                hide: ['originlist','materiallist','exhibitionlist','culturelist','duringlist']},
      {keys: ['culture'],                   show: ['culturelist'],                                              hide: ['originlist','materiallist','exhibitionlist','duringlist','dating_to_start_list','dating_to_end_list']},
      {keys: ['exhibition'],                show: ['exhibitionlist'],                                           hide: ['originlist','materiallist','duringlist','culturelist','dating_to_start_list','dating_to_end_list']},
      {keys: ['origin'],                    show: ['originlist'],                                               hide: ['duringlist','materiallist','exhibitionlist','culturelist','dating_to_start_list','dating_to_end_list']},
      {keys: ['material'],                  show: ['materiallist'],                                             hide: ['originlist','duringlist','exhibitionlist','culturelist','dating_to_start_list','dating_to_end_list']}
    ], ['duringlist','culturelist','exhibitionlist','originlist','materiallist','dating_to_end_list'])
  }
});

//Event.onReady(function() {
//
//});