/* Локализация //he.wikipedia.org/wiki/MediaWiki:Gadget-Dwim.js */

if ( mw.user.options.get( 'skin' ) === 'vector' ) {
	$( function() {
		'use strict';

		setTimeout( function() {

			var hes = "qwertyuiop[]asdfghjkl;'zxcvbnm,./`QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?~#^йцукенгшщзхъфывапролджэячсмитьбю.ёЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,Ё№:",
				hebeng = function ( str ) {
					return ( str || '' ).replace( /./g, function( c ) {
						var ic = hes.indexOf( c );

						return ic + 1 ? hes.charAt( ( ic + 70 ) % 140 ) : c;
					} );
				};
			
		var $searchBoxes = $(
			'#searchInput, #searchInput2, #powerSearchText, #searchText, [name=search]'
		);
		// $searchBoxes.suggestions may not be defined depending on implementation.
		if ( !$searchBoxes.suggestions ) {
			return;
		}
			

			$searchBoxes.suggestions( {
				fetch: function( query ) {
					var $this = $( this ),
						apiUrl = mw.util.wikiScript( 'api' );

					if ( query.length !== 0 ) {
						var params = {
							action: 'opensearch',
							search: query,
							redirects: 'return',
							suggest: true
						};

						$.ajax( {
							data: params,
							url: apiUrl,
							dataType: 'json',
							success: function( data ) {
								if ( $.isArray( data ) && 1 in data ) {
									var orig = data[1];

									$this.suggestions( 'suggestions', orig );

									if ( data[1].length < 10 ) {
										params.search = hebeng( query );
										
										if ( params.search === query ) {
											return;
										}

										params.limit = 10 - data[1].length;

										$.ajax( {
											data: params,
											url: apiUrl,
											dataType: 'json',
											success: function( data ) {
												if ( $.isArray( data ) && 1 in data ) {
													$this.suggestions(
														'suggestions',
														orig.concat( data[1] )
													);
												}
											}
										} );
									}
								}
							}
						} );
					}
				}
			} );
		}, 1200 ); // setTimeout
	} ); // document.ready
} // skin === vector