jQuery( document ).ready( function( $ ) {

	'use strict';

	/*--------------------------------------------------------------
	Convert parent dropdown menu to select2 menu
	--------------------------------------------------------------*/

	if ( $( 'select#parent_id' ).length > 0 ) {	
		var parent_id = $( 'select#parent_id' );
		if ( !$.isEmptyObject( parent_id ) ) {
			parent_id.select2({
				width: '100%'
			});  
		}
	}
	
	/*--------------------------------------------------------------
	Show/hide meta boxes depending on page template
	--------------------------------------------------------------*/

	if ( document.getElementById( 'page_template' ) != null ) {
	
		// Settings upon page load
		var value = document.getElementById( 'page_template' ).value;
		
		if ( value.indexOf( 'hub-template.php' ) >= 0 ) {
			$( '#gp_hubsdiv, div[id*="tagsdiv-"]' ).show();
		} else if ( value.indexOf( 'review-template.php' ) >= 0 ) {
			$( '#gp_hubsdiv, #tagsdiv-post_tag' ).show();		
		} else {
			$( '#gp_hubsdiv, div[id*="tagsdiv-"]' ).hide();
		}
			
		// Settings when changing menu
		document.getElementById( 'page_template' ).onchange = function ( value ) {
			var value = document.getElementById( 'page_template' ).value;	
			if ( value.indexOf( 'hub-template.php' ) >= 0 ) {
				$( '#gp_hubsdiv, div[id*="tagsdiv-"]' ).show();	
			} else if ( value.indexOf( 'review-template.php' ) >= 0 ) {
				$( '#gp_hubsdiv, #tagsdiv-post_tag' ).show();				
			} else {	
				$( '#gp_hubsdiv, div[id*="tagsdiv-"]' ).hide();
			}				
		};
		
	}

});