jQuery(document).ready(function() {
 
	dap_postmeta_mediauploader();
	dap_member_tiny_editor1();
    jQuery('#dap_manage_pay_per_post_table').find('tbody').show();
    jQuery('#dap_manage_pay_per_post_table').DataTable(
        {
           "bSort": false, pageLength: 10, 
        } 
    );
    
       /******Create page*******/
	jQuery(document).on('click', '.clicktoaddimage', function() {  
		 	jQuery('#getImagesettingpoup1').trigger('click');
		 	var resource_id = jQuery(this).attr("data-id");
		 	jQuery('#resource_id').val(resource_id);
		 	jQuery('.image_url_outer').css("display", "flex");
		 	jQuery('.teaser_content_outer').css("display", "none");
		 	
		 	jQuery('#modalImageSetting h4.modal-title').text('Image Settings'); 
		 	var teaser_data = jQuery('#row_'+resource_id+ ' .teaser_data').html();
		    tinymce.get('teaser_content').setContent(teaser_data);
		 	
		 	var resource_name = jQuery('#row_'+resource_id+ ' .resource_name').text();
			jQuery('#post_title').val(resource_name);
			
			jQuery('#attr_checked').val(1);
		 			 	
	});
	jQuery(document).on('click', '.clicktoaddimage1', function() {  
		    jQuery('#modalImageSetting h4.modal-title').text('Content Settings'); 
			
		 	jQuery('#getImagesettingpoup2').trigger('click');
		 	var resource_id = jQuery(this).attr("data-id");
		 	jQuery('#resource_id').val(resource_id);
		 	
		 	jQuery('.image_url_outer').css("display", "none");
		 	jQuery('.teaser_content_outer').css("display", "flex");
		 	
		 	var image_url = jQuery('#row_'+resource_id+ ' .image_data').attr("src");	
		 	jQuery('#image_url').val(image_url);
		 	
		 	var resource_name = jQuery('#row_'+resource_id+ ' .resource_name').text();
			jQuery('#post_title').val(resource_name);
			
			jQuery('#attr_checked').val(2);
		 
	});
	jQuery(document).on('click', '.clicktoaddimage2', function() {  
		    jQuery('#modalImageSetting h4.modal-title').text('Content Settings'); 
		 	jQuery('#getImagesettingpoup2').trigger('click');
		 	var resource_id = jQuery(this).attr("data-id");
		 	jQuery('#resource_id').val(resource_id);
		 	
		 	jQuery('.image_url_outer').css("display", "flex");
		 	jQuery('.teaser_content_outer').css("display", "flex");
		 	var image_url = jQuery('#row_'+resource_id+ ' .image_data').attr("src");		 		  
		 	jQuery('#image_url').val(image_url);
		 	
		 	var resource_name = jQuery('#row_'+resource_id+ ' .resource_name').text();
			jQuery('#post_title').val(resource_name);
						 
			var teaser_data = jQuery('#row_'+resource_id+ ' .teaser_data').html();
		    tinymce.get('teaser_content').setContent(teaser_data);
			jQuery('#attr_checked').val(3);
			
			var resource_name = jQuery('#row_'+resource_id+ ' .resource_name').text();
			jQuery('#post_title').val(resource_name);
			
	});
	
	jQuery(document).on('click', '.savedata', function() { 
			var image_url = jQuery('#image_url').val();
			var resource_id = jQuery('#resource_id').val();
			
			var post_title = jQuery('#post_title').val();
			
			var teaser_content =tinymce.get('teaser_content').getContent();
			jQuery("#teaser_content_clone").html(teaser_content);
			
			var teaser_content_length = jQuery("#teaser_content_clone").text().length;
			if(teaser_content_length > 130 ){
				swal('Max allowed characters in the description is 130.');
				return false;
			}
			
			var teaser_content1 = teaser_content;
			teaser_content = encodeURIComponent(teaser_content);
			
			var attr_checked = jQuery('#attr_checked').val();
			
			if(attr_checked ==1){
				if (image_url == '' || typeof image_url == undefined || typeof image_url == 'undefined') {					 
						swal('Please enter or upload image.');				 
				}
			}
			
			 
			jQuery('.savedata').text("Saving...");
				jQuery.post(ajaxurl, {
					action: 'dapSavePostMetaData',
					image_url: image_url,
					resource_id: resource_id,
					teaser_content: teaser_content,
					post_title: post_title,
				}, function(response) {
					console.log(response);	
					jQuery('.warning_cls').show(); 		 
				    jQuery('.savedata').text("Save");
				    
				    if(attr_checked ==3){ 
						jQuery('#row_'+resource_id+ ' .resource_name').text(post_title);				
							
						 
						if (image_url == '' || typeof image_url == undefined || typeof image_url =='undefined') {		
									 
						}else{
							jQuery('#row_'+resource_id).find('.image_btn_'+resource_id).hide();	
							jQuery('#row_'+resource_id+ ' .image_url_outer').css("display", "flex");	
							jQuery('#row_'+resource_id+ ' .image_data').attr("src", image_url).show();			 			 
						}
						if (teaser_content1  == '' || typeof teaser_content1  == undefined || typeof teaser_content1  =='undefined' ) {		
							
						}else{		
							jQuery('#row_'+resource_id).find('.teaser_btn_'+resource_id).hide();
							jQuery('#row_'+resource_id+ ' .teaser_content_outer').css("display", "flex");	 
							jQuery('#row_'+resource_id+ ' .teaser_data').html(teaser_content1).show(); 	 			 
						}
						
					}else if(attr_checked ==2){
						jQuery('#row_'+resource_id+ ' .teaser_data').html(teaser_content1).show();
						
						jQuery('#row_'+resource_id).find('.teaser_btn_'+resource_id).hide();	
					}else{
						jQuery('#row_'+resource_id+ ' .image_data').attr("src", image_url).show();		
						jQuery('#row_'+resource_id).find('.image_btn_'+resource_id).hide();		
					}
				   
				    setTimeout(function(){  jQuery('.warning_cls').hide();  }, 2000);
				    tinymce.get('teaser_content').setContent('');
				    jQuery('#modalImageSetting').modal('hide');
 
				});
	 

	}); 
    
    
});

function dap_postmeta_mediauploader(){
	
	var dap_postmeta_img;
	
	jQuery(document).on('click','.dap_postmeta_img_upload',function() {  
		
		if (dap_postmeta_img) {
			dap_postmeta_img.open();
			return;
		}
		dap_postmeta_img = wp.media.frames.file_frame = wp.media({
			title: 'Choose Image',
			button: {
				text: 'Choose Image'
			},
			multiple: false
		});
		dap_postmeta_img.on('select', function() {
			attachment = dap_postmeta_img.state().get('selection').first().toJSON();	
			//jQuery('#login_template_wrapper .login_img img').attr('src',attachment.url);
			 
			jQuery('#image_url').val(attachment.url);
			
		});
		dap_postmeta_img.open();
	});
	
	
}


function dap_member_tiny_editor1() {
		tinymce.init({
        selector: '.dap_member_tiny_editor1',
        inline: true,
        height: 500,
        theme: 'modern',
        force_br_newlines: true,
        force_p_newlines: false,
        resize: "both",
        object_resizing: "img",
        forced_root_block: '',
        fontsize_formats: "8pt 9pt 10pt 11pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 22pt 24pt 26pt 30pt",
        theme_advanced_fonts: "Andale Mono=andale mono,times;" + "Arial=arial,helvetica,sans-serif;" + "Arial Black=arial black,avant garde;" + "Book Antiqua=book antiqua,palatino;" + "Comic Sans MS=comic sans ms,sans-serif;" + "Courier New=courier new,courier;" + "Georgia=georgia,palatino;" + "Helvetica=helvetica;" + "Impact=impact,chicago;" + "Symbol=symbol;" + "Tahoma=tahoma,arial,helvetica,sans-serif;" + "Terminal=terminal,monaco;" + "Times New Roman=times new roman,times;" + "Trebuchet MS=trebuchet ms,geneva;" + "Verdana=verdana,geneva;" + "Webdings=webdings;" + "Wingdings=wingdings,zapf dingbats",
        plugins: ['advlist autolink lists link image charmap print preview hr anchor pagebreak', 'searchreplace wordcount visualblocks visualchars code fullscreen', 'insertdatetime media nonbreaking save table contextmenu directionality', 'emoticons template paste textcolor colorpicker textpattern imagetools'],
        toolbar1: 'insertfile undo redo | styleselect | bold italic |  fontselect | fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
        toolbar2: 'print preview media | forecolor backcolor emoticons | codesample',
        image_advtab: true,
        templates: [{
            title: 'Test template 1',
            content: 'Test 1'
        }, {
            title: 'Test template 2',
            content: 'Test 2'
        }]
    });
}
