(function ($) {

	$(document).ready(function () {
		var $project_details_location_chooser = $('#_extra_project_details_location'),
			$extra_page_post_settings = $('#extra-page-post-settings'),
			$sidebar_chooser = $extra_page_post_settings.find('select[name="_extra_sidebar"]').parents('.form-field'),
			$sidebar_control_field = $extra_page_post_settings.find('.extra-sidebar-control-field'),
			$sidebar_location_chooser = $extra_page_post_settings.find('select[name="_extra_sidebar_location"]');

		if ($sidebar_location_chooser.length > 0) {

			$sidebar_location_chooser.change(function () {
				if ('none' === $sidebar_location_chooser.val() || $sidebar_location_chooser.val() === '' && $sidebar_location_chooser.data('location') === 'none') {
					$sidebar_chooser.addClass('sidebar-hidden').slideUp();
				} else {
					$sidebar_chooser.removeClass('sidebar-hidden');
					if (!$sidebar_chooser.hasClass('project-sidebar-hidden')) {
						$sidebar_chooser.slideDown();
					}
				}
			}).trigger('change');
		}

		if ($project_details_location_chooser.length > 0) {
			$project_details_location_chooser.change(function () {
				if ('sidebar' === $(this).val()) {
					$sidebar_chooser.removeClass('project-sidebar-hidden');
					if (!$sidebar_chooser.hasClass('sidebar-hidden')) {
						$sidebar_chooser.slideDown();
					}
				} else {
					$sidebar_chooser.addClass('project-sidebar-hidden').slideUp();
				}

				if ('single_col' === $(this).val()) {
					$sidebar_control_field.slideUp();
				} else {
					$sidebar_control_field.not('.sidebar-hidden').slideDown();
				}
			}).trigger('change');
		}
	});

})(jQuery);
