// The functions in this file depend on the Prototype Javascript
// library, so be sure to include it in your HTML page *before*
// including this file.

/////////////////////////////////////////////////////////////////////

// For all elements in the page with a class name of 'hide', this
// will hide them from the user.  This is useful in case we want
// something hidden from Javascript users (maybe because we'll
// display it to them later when they click a link, or whatever)
// but that we want fully displayed for someone without Javascript.
// This way, non-JS folks can still see everything and it won't
// get hidden, because they wouldn't be able to use JS later to
// show the hidden item to them.
function hider() {
  var elements = $$( '*.hide' )

  elements.each(
    function ( element ) {
      element.hide()
    }
  )
}

// Used with an HTML select menu so that a new URL will load in
// the current window when the select menu is changed.
function menu_changer( select ) {
  var index = select.selectedIndex
  var url = select.options[index].value
  document.location = url
}

// Accepts an ID of an element in a page, the link whose text should
// be changed from Show to Hide and vice versa, and an anchor so
// the user's view will be adjusted to that location in the page
function toggle_display( id, link, anchor ) {
  var show_regex = new RegExp( '^show', 'i' )
  var hide_regex = new RegExp( '^hide', 'i' )
  var link_text = link.innerHTML

  // Element is hidden, we should show it
  if ( show_regex.test( link_text ) ) {
    Effect.SlideDown( id )
    link.innerHTML = 'Hide'

  // Element is shown, we should hide it
  } else {
    Effect.SlideUp( id )
    link.innerHTML = 'Show'
  }

  window.location.hash = anchor
  return false
}

// Mozilla browsers don't handle the CSS display property
// inline-block correctly, so this function makes it behave
// like it does in Safari, IE, etc.
function form_fixer() {
  // Get an array of all form elements in the HTML page
  var forms = $$( 'form' )

  // Various variables we'll use later
  var labels, form, label, content, span, width

  // Regular expression to test for the CSS class 'no_fix'
  // on an HTML element.  If an element has this class,
  // then we don't 'fix' that form to use inline-block
  // correctly.
  var no_fix_regex = new RegExp( 'no_fix', 'i' )
  
  // Regular expression to test for the CSS class 'full'
  // on an HTML element
  var full_regex = new RegExp( 'full', 'i' )
  
  // Go through all the forms in the page...
  forms.each(
    function ( form ) {
      if ( no_fix_regex.test( form.className ) )
	return

      // Hide the form so the user doesn't potentially see us working
      // on it--gotta be discreet about our cosmetic surgery.
      form.hide()

      // Get an array of all label elements within this form
      labels = form.select( 'label' )

      // Go through each label...
      labels.each(
	function ( label ) {
	  // If the label has a CSS class of "full", we want
	  // to skip it because the label should take up the
	  // full width of its area
	  if ( full_regex.test( label.className ) )
	    return
	  
	  // Here the magic happens!
	  content = label.innerHTML
	  if (document.defaultView) {
	    width = document.defaultView.getComputedStyle( label, '' ).getPropertyValue( 'width' )
	    label.style.display = '-moz-inline-box'
	  } else {
	    width = label.currentStyle['width']
	  }
	  span = document.createElement( 'span' )
	  span.style.display = 'block'
	  span.style.width = width
	  span.innerHTML = content
	  label.innerHTML = null
	  label.appendChild( span )
	}
      )

      // Display our fixed-up form again
      form.show()
    }
  )
}

// When the page loads, fix all forms for Mozilla-based browsers
// and hide elements that are marked as hideable (see function
// comments for more information)

if (window["Event"] !== undefined) {
  Event.observe( window, 'load', function() {
    form_fixer()
    hider()
  } );
}
