jQuery.noConflict();
jQuery(document).ready(function() { 
	
	jQuery("q").prepend("&ldquo;").append("&rdquo;");
	jQuery("q q").prepend("&lsquo;").append("&rsquo;");
	
	// Add classes to input pseudotypes
	jQuery("input[type='text'], input[type='password']").addClass("text");
	jQuery("input[type='button'], input[type='submit'], input[type='checkbox'], input[type='image'], input[type='radio'], input[type='reset'], button").addClass("nontext");
	jQuery("input[type='button'], input[type='submit'], input[type='reset'], button").addClass("button");
	
	// Add classes to link pseudotypes
	jQuery('a[href$=".pdf"]').addClass('docpdf');
	jQuery('a[href$=".doc"]').addClass('docword');
	jQuery('a[href^="mailto:"]').addClass('mailto');
	
	// External link popper, checks href against current domain. If not matching and href contains http://, target blank it and add class external
	jQuery('a[href^=http://]').not('[href*='+window.location.host+']').attr('target','_blank').addClass("external");
	
	// Remove classes from a link containing an image
	jQuery("a img").parent().removeClass();
	
	// Add Class Last to Each Last Child
	jQuery('li:last-child').addClass('last');

	jQuery('ul.sf-menu').superfish({delay:100,animation:{opacity:'show',height:'show'},speed:'fast',autoArrows:true,dropShadows:true}); 
	jQuery("a[rel^='prettyPhoto']").prettyPhoto();
	jQuery("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	jQuery("#commentForm").validate();
	
	// Look at the URL and highlight the matching nav link
	var path = location.pathname.substring(1);
	if ( path )
		jQuery('#nav-one a[href$="' + path + '"]').addClass('selected');
		jQuery(this).parent().addClass('selected-parent');
	
	// choose text for the show/hide link - can contain HTML (e.g. an image)
	var showText='Show More Information';
	var hideText='Hide';
	// append show/hide links to the element directly preceding the element with a class of "toggle"
	jQuery('.toggle-content').prev().append('<a href="#" class="toggle-switch">'+showText+'</a>');
	// hide all of the elements with a class of 'toggle'
	jQuery('.toggle-content').hide();
	// capture clicks on the toggle links
	jQuery('a.toggle-switch').click(function() {
	// change the link depending on whether the element is shown or hidden
	jQuery(this).html (jQuery(this).html()==hideText ? showText : hideText);
	// toggle the display - uncomment the next line for a basic "accordion" style
	jQuery(this).parent().next('.toggle-content').slideToggle();
		// return false so any link destination is not followed
		return false;
	});
	
	jQuery(".sitemap .left ul").css("opacity", "0.8");
	jQuery(".sitemap .left ul ul").css("opacity", "0.5");
	jQuery(".sitemap .left ul").doFade({ fadeColor: "#362b40" });
	jQuery(".sitemap .left ul ul").doFade({ fadeColor: "#354668" });
	jQuery(".sitemap .left ul ul ul").doFade({ fadeColor: "#304531" });
	jQuery(".sitemap .left ul ul ul ul").doFade({ fadeColor: "#72352d" });
	
	cmxform();
	
	// Few setups for IE6
	if(document.all){
		//add class to drop downs and buttons 
	    jQuery('#nav li, button').hover(
			function() { jQuery(this).addClass('over'); },
			function() { jQuery(this).removeClass('over'); }
	    );
	}// if document.all
	
	// Move the label over the input and clear it on mouse click
	jQuery("#KnightLibSearch input[type='text']").inputSetter();
	
});

this.randomizer = function(){
	var length = jQuery("#randomizer li").length;
	var ran = Math.floor(Math.random()*length) + 1;
	jQuery("#randomizer li:nth-child(" + ran + ")").show();
};

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );

function cmxform(){
  // Hide forms
  jQuery( 'fieldset.cmxform' ).hide().end();
  
  // Processing
  jQuery( 'fieldset.cmxform' ).find( 'li label' ).not( '.nocmx' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end();
  
  // Show forms
  jQuery( 'fieldset.cmxform' ).show().end();
}

jQuery.fn.doFade = function(settings) {

    // if no paramaters supplied...
   settings = jQuery.extend({
      fadeColor: "black",
      duration: 200,
      fadeOn: 0.95,
      fadeOff: 0.65
   }, settings);

    var duration = settings.duration;
    var fadeOff = settings.fadeOff;
    var fadeOn = settings.fadeOn;
    var fadeColor = settings.fadeColor;

    jQuery(this).hover(function(){
     jQuery(this)
         .stop()
         .data("origColor", jQuery(this).css("background-color"))
         .animate({
             opacity: fadeOn,
             backgroundColor: fadeColor
         }, duration)
   }, function() {
     jQuery(this)
         .stop()
         .animate({
             opacity: fadeOff,
             backgroundColor: jQuery(this).data("origColor")
         }, duration)
   });

};

/*
	
	@inputClear
	apply automatic input clearing to the search input - add as needed
	$("#searchform input").inputClear();

*/
jQuery.fn.inputClear = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

// end inputClear

/*

	12.19.08 - pdf
	
	@inputSetter
	Input Setter
	pull label and insert as field. clear with click.	
	optional lower param if == 1, string toLowerCase	

	ex: $("#sitesearch input").inputSetter(1); // makes default text lowercase
		$("#sitesearch input").inputSetter(); // no lowercasing

	see custom validation for jquery.validation.js below

*/

jQuery.fn.inputSetter = function(lower) {	
	return this.each(function() {
		var $input = jQuery(this);
		var $label = jQuery("label[for='"+$input.attr("id")+"']");
		var labeltext = lower && lower==1 ? $label.text().toLowerCase() : $label.text();
		$label.hide();	
		$input.val(labeltext);		
		$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
			  .blur(function() { if (!this.value.length) { this.value = labeltext; } });		
	});
};

// end inputSetter
