jQuery( 'document' ).ready( function () {
	jQuery( '#submit-pac-da' ).on( 'click', function ( e ) {
		wpNavMenu.registerChange();
		pacDaAddWidgettoMenu();
	} );
	function pacDaAddWidgettoMenu( ) {
		description = jQuery( '#pac-da-html' ).val();
		menuItems = { };
		processMethod = wpNavMenu.addMenuItemToBottom;
		var t = jQuery( '.pac-da-div' );
		t.find( '.spinner' ).show();
		re = /menu-item\[([^\]]*)/;
		m = t.find( '.menu-item-db-id' );
		listItemDBIDMatch = re.exec( m.attr( 'name' ) ), listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt( listItemDBIDMatch[1], 10 );
		menuItems[listItemDBID] = t.getItemData( 'add-menu-item', listItemDBID );
		menuItems[listItemDBID]['menu-item-description'] = description;
		if ( menuItems[listItemDBID]['menu-item-title'] === '' ) {
			menuItems[listItemDBID]['menu-item-title'] = '(Untitled)';
		}
		nonce = jQuery( '#pac_da_sc_menu_nonce' ).val();
		params = {
			'action': 'pac_da_description_hack',
			'description-nonce': nonce,
			'menu-item': menuItems[listItemDBID]
		};
		jQuery.post( ajaxurl, params, function ( objectId ) {
			jQuery( '#pac-da-div .menu-item-object-id' ).val( objectId );
			wpNavMenu.addItemToMenu( menuItems, processMethod, function () {
				t.find( '.spinner' ).hide();
				jQuery( '#pac-da-title' ).val( '' ).blur();
				jQuery( '#pac-da-html' ).val( '' );
			} );
		});
	}
});
