// Turn off AJAX for local file browsing
if ( location.protocol.substr(0,4)  === 'file' ||
     location.protocol.substr(0,11) === '*-extension' ||
     location.protocol.substr(0,6)  === 'widget' ) {

	// Start with links with only the trailing slash and that aren't external links
	var fixLinks = function() {
		$( "a[href$='/'], a[href='.'], a[href='..']" ).not( "[rel='external']" ).each( function() {
			this.href = $( this ).attr( "href" ).replace( /\/$/, "" ) + "/index.html";
		});
	};

	// Fix the links for the initial page
	$( fixLinks );

	// Fix the links for subsequent ajax page loads
	$( document ).on( 'pagecreate', fixLinks );

	// Check to see if ajax can be used. This does a quick ajax request and blocks the page until its done
	$.ajax({
		url: '.',
		async: false,
		isLocal: true
	}).error(function() {
		// Ajax doesn't work so turn it off
		$( document ).on( "mobileinit", function() {
			$.mobile.ajaxEnabled = false;
			
			var message = $( '<div>' , {
				'class': "ui-footer ui-bar-e",
				style: "overflow: auto; padding:10px 15px;",
				'data-ajax-warning': true
			});
			
			message
			.append( "<h3>Note: Navigation may not work if viewed locally</h3>" )
			.append( "<p>The AJAX-based navigation used throughout the jQuery Mobile docs may need to be viewed on a web server to work in certain browsers. If you see an error message when you click a link, try a different browser or <a href='https://github.com/jquery/jquery-mobile/wiki/Downloadable-Docs-Help'>view help</a>.</p>" );
			
			$( document ).on( "pagecreate", function( event ) {
				$( event.target ).append( message );
			});
		});
	});
}


// display the version of jQM
$( document ).on( "pageinit", function() {
	var version = $.mobile.version || "dev",
		words = version.split( "-" ),
		ver = words[0],
		str = (words[1] || "Final"),
		html = "Version " + ver;

	if( str.indexOf( "rc" ) == -1 ){
		str = str.charAt( 0 ).toUpperCase() + str.slice( 1 );
	} else {
		str = str.toUpperCase().replace(".", "");
	}

	if ( $.mobile.version && str ) {
		html += " " + str;
	}

	$( ".jqm-version" ).html( html );
});


// global search
$( document ).on( "pageinit", ".jqm-demos", function() {
	$( this ).find( ".jqm-search ul" ).listview({
		globalNav: "docs",
		inset: true,
		theme: "d",
		dividerTheme: "d",
		icon: false,
		filter: true,
		filterReveal: true,
		filterPlaceholder: "Search...",
		autodividers: true,
		autodividersSelector: function ( li ) {
    		return "";
  		}
	});
	
	$( this ).find( ".jqm-header .jqm-search-link" ).on( "click", function() {
		$( this ).parent( ".jqm-header" ).toggleClass( "jqm-search-toggle" );
	});
	
	$( this ).find( ".jqm-header:not(.jqm-search-toggle) .jqm-search-link" ).on( "click", function() {
		$( this ).parent( ".jqm-header" ).find( ".jqm-search .ui-input-text" ).focus();
	});
});

@@ -2626,23 +2626,31 @@
 le%22 );%0A%09
-%7D);%0A%09%0A%09
+%09%0A%09%09var type = 
 $( this 
@@ -2643,36 +2643,38 @@
 ype = $( this ).
-find
+parent
 ( %22.jqm-header:n
@@ -2675,14 +2675,23 @@
 ader
-:not(.
+%22 ).hasClass( %22
 jqm-
@@ -2707,55 +2707,45 @@
 ggle
-) .jqm-search-link%22 ).on( %22click%22, function() %7B
+%22 ) ? %22searchshow%22 : %22searchhide%22;%0A%09%09
 %0A%09%09$
@@ -2799,36 +2799,305 @@
 arch
- .ui-input-text%22 ).focus();%0A
+%22 ).trigger( type );%0A%09%7D);%0A%09%0A%09$( this ).find( %22.jqm-header .jqm-search%22 )%0A%09%09.on( %22searchshow searchhide%22, function( event ) %7B%0A%09%09%09if ( event.type === %22searchshow%22 ) %7B%0A%09%09%09%09$( this ).find( %22.ui-input-text%22 ).focus();%0A%09%09%09%7D else %7B%0A%09%09%09%09$( this )%0A%09%09%09%09%09.find( %22.ui-input-clear%22 ).trigger( %22click%22 );%0A%09%09%09%7D%0A%09
 %09%7D);

clear search when hiding the search bar