jQuery(function($){
	
	// "Insert Module" modal opened
	$(document).on('mouseup', '.et-fb-button, .column-block, .et_fb_fullwidth', function (event) {
		setTimeout(function(){ 
			var modal = window.top.jQuery('.et-fb-modal-add-module-container:not(.et_fb_add_section_modal):not(.et_fb_add_row_modal)');
			if (modal.length) {
				var tabs = modal.find('.et-fb-settings-tabs-nav');
				if (tabs.length) {
					if (!tabs.find('.modules_more').length) { // If "more..." tab not yet added
					
						tabs.append('<li class="et-fb-settings-options_tab_modules_more et-fb-settings-tabs-nav-item"><a href="#" class="modules_more">More</a></li>');
						modal.find( ".et-fb-settings-options-wrap > .et-fb-settings-options" ).append('<div class="et-fb-settings-options-tab et-fb-all-modules et-fb-modules-list et-fb-settings-options et-fb-settings-options-tab-modules_more"></div>');
						
						// Add class if full-width
						var button = $(event.target);
						var fullwidth = (button.closest('.et_pb_fullwidth_section').length || button.closest('.et_fb_fullwidth').length);
						if (fullwidth) { $('.modules_more').addClass('db_module_mate_fullwidth'); }
					}
				}
			}
		}, 200);
	});
	
	// Module Mate tab closed
	$(document).on('click', '.et-fb-settings-tabs-nav-item a:not(.modules_more)', function(){
		
		// Remove the focus from the More Modules tab
		$('.et-fb-settings-options_tab_modules_more').removeClass('et-fb-settings-tabs-nav-item--active'); // Remove tab highlight
		$('.et-fb-settings-options-tab-modules_more').removeClass('et-fb-settings-options-tab--active'); // Hide more module tab
		
		var newtab = $(this).closest('.et-fb-settings-tabs-nav-item');
		newtab.addClass('et-fb-settings-tabs-nav-item--active');		
		var index = $(".et-fb-settings-tabs-nav-item").index(newtab);

		var newtabbody = $(this).closest('.et-fb-main-settings').find('.et-fb-settings-options > .et-fb-settings-options-tab').eq(index);
		newtabbody.addClass('et-fb-settings-options-tab--active').addClass('something-else'); // Show tab
		
	});
	
	// Module Mate tab opened
	$(document).on('click', '.modules_more', function(){
	
		// Highlight module tab
		$('.et-fb-settings-tabs-nav-item').removeClass('et-fb-settings-tabs-nav-item--active');
		$('.et-fb-settings-options_tab_modules_more.et-fb-settings-tabs-nav-item').addClass('et-fb-settings-tabs-nav-item--active');
	
		// Show module tab
		$('.et-fb-settings-options-tab').removeClass('et-fb-settings-options-tab--active');
		$('.et-fb-settings-options-tab-modules_more').addClass('et-fb-settings-options-tab--active');
	
		// Detect standard vs fullwidth
		var json_file = 'modules.php?fullwidth=false';
		if ($(this).hasClass('db_module_mate_fullwidth')) { 
			json_file = 'modules.php?fullwidth=true'; 
		}
		
		// Load module data
		$.getJSON(diviModuleMate.pluginDir+json_file, function( data ) {
			
			// Display modules
			var items = [];
			$.each( data, function( i, item ) {
				items.push('<li class="'+item['class']+'" data-href="'+item['url']+'" title="'+item['description']+'. Price: '+item['price']+'"><span class="et_module_title">'+item['title']+'</span></li>');
			});
			$(".et-fb-settings-options-tab-modules_more").html($("<ul/>", {"class": "db_module_mate_linked", html: items.join("")}));

			// Display disclaimer
			$(".et-fb-settings-options-tab-modules_more").append('<p class="disclaimer">Notes: Modules linked above are provided / sold by 3rd party sites. Please see these sites for suitability, purchase and installation information. Some of the links above are affiliate links and may generate a small commission which helps keep the module mate plugin free. I strive to link to the cheapest reputable source of each module, regardless of any affiliate arrangements. Some modules are only available in module packs, in which case the link will be to the cheapest module pack in which it appears. Prices listed here are subject to change - please see the relevant site for current pricing.</p>');
			
		});
	
		return false;
		
	});	
	
	// Make a click anywhere on the module box trigger the link
	$(document).on('click', '.et-fb-settings-options-tab-modules_more li', function() {
		window.open($(this).data('href'), '_blank');
	});
	
});