document.observe('dom:loaded', function(){
  
  // Recent updaters
  $('updaters-message').update(); // get rid of "loading" message
  
  new Ajax.Request('/support/updaters/updater_json.txt?id=4', {
    method:'get',
    onSuccess: function(transport){
      var json = transport.responseText.evalJSON();
      var i=0;
      if($('updaters').hasClassName('updaters-filemaker')){ max_num=6; } else { max_num=1; }
      for(i=0;i<=max_num;i++){
      // for(i=0;i<json.list.length;i++){ 
        json_obj=json.list[i];
          // truncate if more than 45 chars
          if((json_obj.name).length < 45){
            var new_name=json_obj.name;
          } else {
            var new_name=(json_obj.name).substr(0,45)+'...';
          }
        // separate out the fm and bento
        if(json_obj.type=='FileMaker'){
          console.log('filemaker');
          var updaters_content_fm=('<li class="update-'+json_obj.type+'"><a href="'+json_obj.info+'">'+new_name+'</a></li>');
        } else {
          console.log('bento');
          var updaters_content_bento=('<li class="update-'+json_obj.type+'"><a href="'+json_obj.info+'">'+new_name+'</a></li>');
        }
        // insert into page div
        if($('updaters').hasClassName('updaters-filemaker')){
          $('updaters').insert(updaters_content_fm); 
        } else {
          $('updaters').insert(updaters_content_bento);
        }
      }
       
    }
  });
  
  // KB search auto-fill
  $('kb-search-field').observe('focus',function(){
    if($(this).getValue()=='Search Knowledge Base'){ $(this).setAttribute('value',''); }
  }).observe('blur',function(){
    $(this).setAttribute('value','Search Knowledge Base');
  })
  
  // submit search
  $('submit-link').observe('click', function(e) {
    Event.stop(e);
    var search_for = $('kb-search-field').getValue();
		location.href='http://help.filemaker.com/app/answers/list/kw/'+search_for;
  });
  $('kb-search-form').observe('submit', function(e) {
    Event.stop(e);
    var search_for = $('kb-search-field').getValue();
		location.href='http://help.filemaker.com/app/answers/list/kw/'+search_for;
  });
  
}); 
