// Image preloading plugin
// From: http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
jQuery.preloadImages = function() {
	for ( var i = 0; i < arguments.length ; i++ ) {
		jQuery( "<img>" ).attr( "src" , arguments[i] );
	}
}

jQuery( document ).ready( function($) {

	// Search form
	if ( $( "input#asl_searchquery" ).length ) {
		$( "input#asl_searchquery" ).css({ 'color':'#999', 'font-style':'italic' }).val( "Type your search here" ).blur( function() {
			if ( !$(this).val() ) $( this ).css({ 'color':'#999', 'font-style':'italic' }).val( "Type your search here" );
		}).focus( function() {
			if ( $(this).val() == "Type your search here" ) $( this ).css({ 'color':'#333', 'font-style':'normal' }).val( "" );
		});
	}
	
	// Main nav drop-downs
	if ( !( $.browser.msie && parseInt($.browser.version) < 7 ) ) {

		// Preload images
		$.preloadImages( "/wp-content/themes/slt-custom/img/nav-arrow5.gif" );
		var navTimers = [];
		$( "#nav-main ul.nav > li" ).hover(
			function () {
				if ( $( this ).children( "ul" ).length ) {
					// Get unique id for this element and use it to assign timeout
					var id = jQuery.data( this );
					var $this = $( this );
					navTimers[id] = setTimeout( function() {
						$this.children( 'ul' ).fadeIn( 300 );
						navTimers[id] = "";
					}, 300 );
				}
			},
			function () {
				if ( $( this ).children( "ul" ).length ) {
					var id = jQuery.data( this );
					// If there's a timer running for this element...
					if ( navTimers[id] != "" ) {
						// Clear timeout for this element
						// i.e. don't show subnav if cursor moves away quickly
						clearTimeout( navTimers[id] );
					} else {
						// Else hide the subnav
						$( this ).children( "ul" ).fadeOut( 200 );
					}
				}
			}
		);
		
	}
	
});