var Notice = {
  alert: function(message) {
    $('alert').update(message);
    new Effect.Appear('alert', { duration: 0.15 });
    setTimeout(this.clear, 4000)
  },
  clear: function() {
    $('alert').update('');
    new Effect.Fade('alert', { duration: 0.15 })
  }
}

document.observe("dom:loaded", function() {
	setTimeout(hideFlashes, 10000);
	QueryBox.initialize($('q'));
	// sets up a new toggler for on/off items in
	// the main searchbox in the header
	// argument 1 => the DOM id of the searchbox
	// argument 2 => the CSS class of the togglers (the links)
	new SearchTextToggler('q', 'toggle');
	
	// all links with a class of "ex" will replace the
	// value in the main search box with the link text
	// (note: emphasis on 'replace' - no appending text)
	$$('a.ex').each(function(e) {
	  $(e).observe('click', function() {
	    $('q').value = e.innerHTML;
	  });
	  return false;
	})
	
	// ip-options
	$$('.ip-options #options input[type="checkbox"]').each(function(e) {
	  $(e).observe('click', handleOption);
	});
	
	// saved searches
	SavedSearch.initialize();
	
	// inline search examples
	Cheatsheet.initialize();
	
	// sneaky sidebar
	Sidebar.initialize();
});

// used to select IP options
function handleOption() {
  $$('.ip-options #options input[type="checkbox"]').each(function(e) { 
    e.checked = false; 
  });
  
  $$('.ip-options #options li').each(function(e) { 
    $(e).removeClassName('selected'); 
  });
  
  this.checked = true;
  $(this).up('li').addClassName('selected');
}

// fades a "flashed" message on page load
function hideFlashes() {
	$$('.notice').each(function(e)	{ if (e) fadeElement(e); } );
	$$('.warning').each(function(e)	{ if (e) fadeElement(e); } );
	$$('.error').each(function(e)		{ if (e) fadeElement(e); } );
}
function fadeElement(e) { 
	Effect.Fade(e, { duration: 2.0 }); 
}

// used in the main search to show the
// ajax spinner during search requests
function toggleLoader() {
  $('search_loading').toggle();
  $('search_submit').toggle();
   
  var body_height = $('body').getHeight();
  $('content-loading').setStyle({minHeight: body_height + 'px'})
  
  $('content-loading').toggle();
  $('body').toggle();
}