function add_class( element, class_name ) {
  var old_class = element.getAttribute( 'class' )
        
  if ( old_class == '' || old_class == null )
    element.setAttribute( 'class', class_name )
  else if ( old_class != 'no_icon' )
    element.setAttribute( 'class', old_class + ' ' + class_name )
}

// Go through all inputs and add a class to the buttons so that they
// can be styled via CSS.
function button_classifier() {
  var inputs = document.getElementsByTagName( 'input' )
  var type, input
  
  for ( var i=0; i<inputs.length; i++ ) {
    input = inputs[i]
    type = input.getAttribute( 'type' )

    if ( !type )
      continue

    type = type.toLowerCase()
    
    if ( type == 'submit' || type == 'button' || type == 'reset' )
      add_class( input, 'submit' )
  }
}

function form_fixer() {
  var forms = document.getElementsByTagName( 'form' )
  var labels, form, label, content, span, width
  
  // Regular expression to test for the CSS class 'full'
  // on an HTML tag
  var full_regex = new RegExp( 'full', 'i' )
  
  for ( var i=0; i<forms.length; i++ ) {
    form = forms[i]
    form.style.display = 'none'
    labels = form.getElementsByTagName( 'label' )

    for ( var j=0; j<labels.length; j++ ) {
      label = labels[j]
      
      // 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 ) )
        continue
      
      content = label.innerHTML
      width = document.defaultView.getComputedStyle( label, '' ).getPropertyValue( 'width' )
      span = document.createElement( 'span' )
      span.style.display = 'block'
      span.style.width = width
      span.innerHTML = content
      label.style.display = '-moz-inline-box'
      label.innerHTML = null
      label.appendChild( span )
    }

    form.style.display = 'block'
  }
}

// Goes through all links in a page and adds CSS classes
// to them to better style outgoing links, PDF links, etc.
function link_classifier() {
  var links = document.getElementsByTagName( 'a' )
  var curr_link, uri, is_uk_link, is_mailto, is_cs_link
  var pdf_regex = new RegExp( 'pdf$', 'i' )
  var pdf_count = 0, word_count = 0
  var outside_regex = new RegExp( '^http://', 'i' )
  var uk_regex = new RegExp( 'uky\.edu', 'i' )
  var cs_regex = new RegExp( 'cs\.uky\.edu', 'i' )
  var mail_regex = new RegExp( '^mailto:', 'i' )
  var doc_regex = new RegExp( '\.doc$', 'i' )
  
  for ( var i=0; i<links.length; i++ ) {
    curr_link = links[i]
    uri = curr_link.getAttribute( 'href' )
    is_uk_link = uk_regex.test( uri )
    is_mailto = mail_regex.test( uri )
    is_word = doc_regex.test( uri )
    is_cs_link = cs_regex.test( uri )
    
    // If the link is to a PDF file...
    if ( pdf_regex.test( uri ) ) {
      // Assign a CSS class so we can style the link
      // to indicate it's pointing to a PDF file
      add_class( curr_link, 'pdf' )
      
      // If this is the first PDF link of the page...
      if ( pdf_count == 0 ) {
        // Go ahead and append a copyright statement to
        // the footer, saying that Adobe owns their PDF
        // logo, not the University or Dr. Jaromczyk
        var footer = document.getElementById( 'footer' )
        var ul = footer.getElementsByTagName( 'ul' )[0]
        var li = document.createElement( 'li' )
        li.innerHTML = 'Adobe PDF logo &copy; '
        var a = document.createElement( 'a' )
        a.setAttribute( 'href', 'http://www.adobe.com/' )
        a.innerHTML = 'Adobe'
        li.appendChild( a )
        ul.appendChild( li )
      }
      
      pdf_count += 1
    }
    
    if ( is_word ) {
      add_class( curr_link, 'word' )
      
      if ( word_count == 0 ) {
        // Go ahead and append a copyright statement to
        // the footer, saying that Microsoft owns their Word
        // logo, not the University or Dr. Jaromczyk
        var footer = document.getElementById( 'footer' )
        var ul = footer.getElementsByTagName( 'ul' )[0]
        var li = document.createElement( 'li' )
        li.innerHTML = 'Microsoft Word logo &copy; '
        var a = document.createElement( 'a' )
        a.setAttribute( 'href', 'http://www.microsoft.com/' )
        a.innerHTML = 'Microsoft'
        li.appendChild( a )
        ul.appendChild( li )                    
      }
      
      word_count += 1
    }

    // Add little outgoing-link icons via CSS classes to
    // outgoing links, but only if they're not other UK sites
    if ( outside_regex.test( uri ) && !is_uk_link && !is_word && !is_cs_link )
      add_class( curr_link, 'outside' )
    
    if ( is_uk_link && !is_mailto && !is_cs_link )
      add_class( curr_link, 'uk' )
            
    if ( is_mailto )
      add_class( curr_link, 'mail' )          
  }
}

function load_event( func ) {
  var old_onload = window.onload
        
  if ( typeof window.onload != 'function' )
    window.onload = func
  else {
    window.onload = function() {
      if ( old_onload )
        old_onload()
                        
      func()
    }
  }
}

function show_help( note ) {
  alert( note )
  return false
}

load_event( link_classifier )
load_event( button_classifier )

// We only need to "fix" the forms for Mozilla-based browsers,
// so we use the Mozilla-specific document.addEventListener
// method
if ( document.addEventListener )
  document.addEventListener( 'DOMContentLoaded', form_fixer, false )
