var SearchFields = function(panel){
  this.panel = panel;
  this.wwtypes = ['find', 'location'];
};

SearchFields.prototype.getPositionForType = function(type){
  var panel = this.panel;
  var index = panel.items.indexOf(jQuery.grep(panel.items.items, function(f){
    if(typeof(f.wwtype) != 'undefined' && type == f.wwtype){
      return f;
    }
  }).pop());
  if(index == -1 && type == 'location'){
    var finds = jQuery.grep(panel.items.items, function(f){
      if(typeof(f.wwtype) != 'undefined' && f.wwtype == 'find'){
        return f;
      }
    });
    var last_find = finds.pop();
    index = panel.items.indexOf(last_find);
  }
  return index + 1;
};

SearchFields.prototype.add = function(field){
  var panel = this.panel;
  if(Ext.isDefined(field.wwtype)){
    var indexToInsertAt = this.getPositionForType(field.wwtype);
    panel.insert(indexToInsertAt, field);
  }
  else{
    panel.add(field);
  }
};
