var UltimateLearningPro = {
	current_page: null,
	ulp_admin_general_messages: null,
	init: function(){
		this.ulp_admin_general_messages = JSON.parse(ulp_admin_messages);

		jQuery('#ulp_course_featured-checkbox').click(function(){
			UltimateLearningPro.checkAndH('#ulp_course_featured-checkbox', '[name=ulp_course_featured]');
		});
		///not working...
		jQuery('.key-up-action').keyup(function(){});
		jQuery('.ulp-delete-parent').click(function(){
			ulpRemoveElementFromLeft(this);
		});
		jQuery('.js-ulp-instructor-become-normal-user').on('click', function(){
				var uid = jQuery(this).attr('data-uid');
				if (uid){
						jQuery.ajax({
								type : 'post',
								url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
								data : {
								        action:  'ulp_instructor_become_normal_user',
								        user_id: uid,
								},
								success: function (r) {
									jQuery('#row_'+uid).remove();
								}
						});
				}
		});
	},
	checkAndH: function(s, t){
		if (jQuery(s).is(':checked')){
			jQuery(t).val(1);
		} else {
			jQuery(t).val(0);
		}
	}
};
/// execute script
jQuery(function(){
	// Indeed_Blog_Admin.current_page = 'test';
	UltimateLearningPro.init();
});
function ulpDoModuleRemove(i){
	jQuery('#moduleid_'+i).fadeOut(500, function(){
			this.remove();
	});
}
function ulpDoModuleToggle(i){
		var display = jQuery('#ulp_module_content_inside_'+i).css('display');
		if (display=='none'){
				jQuery('#ulp_module_content_inside_'+i).slideDown( 500 );
				var str = '<i class="fa-ulp fa-toggle_up-ulp" title="' + UltimateLearningPro.ulp_admin_general_messages.toggle_section + '" ></i>';
		} else {
				jQuery('#ulp_module_content_inside_'+i).slideUp( 500 );
				var str = '<i class="fa-ulp fa-toggle_down-ulp" title="' + UltimateLearningPro.ulp_admin_general_messages.toggle_section + '" ></i>';
		}
		jQuery('#ulp_toggle_modukle_' + i ).html(str);
}

function ulpLoading(selector)
{
		jQuery(selector).html("<div class='ulp-loading-wrapp'><img src='" + decodeURI(window.ulp_url) + "/wp-content/plugins/indeed-learning-pro/assets/images/loading.gif' /></div>");
}

function removeUlpLoading(selector)
{
		jQuery(selector).empty();
}

function ulpCheckAndH(s, t){
	if (jQuery(s).is(':checked')){
		jQuery(t).val(1);
	} else {
		jQuery(t).val(0);
	}
}

function ulpUpdateNumOfEachDiv(selector){
		jQuery(selector).each(function(e, m){
			e++;
			jQuery(m).html(e);
		});
}

function ulpMakeInputhString(divCheck, hidden_input_id){
    str = jQuery(hidden_input_id).val();
    if(str==-1) str = '';
    if(str!='') show_arr = str.split(',');
    else show_arr = new Array();
		showValue = jQuery(divCheck).val();
    if(jQuery(divCheck).is(':checked')){
        show_arr.push(showValue);
    }else{
        var index = show_arr.indexOf(showValue);
        show_arr.splice(index, 1);
    }
    str = show_arr.join(',');
    if(str=='') str = -1;
    jQuery(hidden_input_id).val(str);
}
function ulpSecondMakeInputhString(divCheck, showValue, hidden_input_id){
    str = jQuery(hidden_input_id).val();
    if(str==-1) str = '';
    if(str!='') show_arr = str.split(',');
    else show_arr = new Array();
    if(jQuery(divCheck).is(':checked')){
        show_arr.push(showValue);
    }else{
        var index = show_arr.indexOf(showValue);
        show_arr.splice(index, 1);
    }
    str = show_arr.join(',');
    if(str=='') str = -1;
    jQuery(hidden_input_id).val(str);
}
function ulpMakeInputhStringWithValue(divCheck, hidden_input_id, the_value){
    str = jQuery(hidden_input_id).val();
    if (str==-1) str = '';
    if (str!='') show_arr = str.split(',');
    else show_arr = new Array();
    if(jQuery(divCheck).is(':checked')){
        show_arr.push(the_value);
    }else{
        var index = show_arr.indexOf(the_value);
        show_arr.splice(index, 1);
    }
    str = show_arr.join(',');
    if(str=='') str = -1;
    jQuery(hidden_input_id).val(str);
}
function ulpRemoveElementFromLeft(element){
	jQuery(element).parent().remove();
}
function ulpDhSelector(t, v){
	if (v){
		var d = 'visible';
	} else {
		var d = 'hidden';
	}
	jQuery(t).css('visibility', d);
}
function ulpDeleteFromTable(i, t, h, f){
	swal({
		title: "",
		text: UltimateLearningPro.ulp_admin_general_messages.are_you_sure,
		type: "warning",
		showCancelButton: true,
		confirmButtonClass: "btn-danger",
		confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
		closeOnConfirm: false
	},
	function(){
		jQuery(h).val(i);
		jQuery(f).submit();
	});
}
function ulpCheckEmailServer(){
	jQuery.ajax({
			type : 'post',
	        url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
	        data : {
	                   action: 'ulp_check_mail_server',
	               },
	        success: function (r){
							swal(UltimateLearningPro.ulp_admin_general_messages.email_works, "", "success");
	        }
	});
}
function ulpReturnNotification(){
    jQuery.ajax({
        type : "post",
        url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
        data : {
                action: "ulp_get_notification_default_by_type",
                type: jQuery('#notf_type').val(),
            },
        success: function (r) {
        	var o = jQuery.parseJSON(r);
        	jQuery('#notf_subject').val(o.subject);
        	jQuery('#notf_message').val(o.content);
        	jQuery("#notf_message_ifr" ).contents().find( '#tinymce' ).html(o.content);
        }
	});
}
function ulpDuplicatePost(pid){
	jQuery.ajax({
			type : "post",
			url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
			data : {
							action: "ulp_duplicate_post",
							post_id: pid
					},
			success: function (r) {
					location.reload(); /// refresh
			}
		});
}
function openMediaUp(target, img_target){
    //If the uploader object has already been created, reopen the dialog
  var custom_uploader;
  if (custom_uploader) {
      custom_uploader.open();
      return;
  }
  //Extend the wp.media object
  custom_uploader = wp.media.frames.file_frame = wp.media({
      title: 'Choose Image',
      button: {
          text: 'Choose Image'
      },
      multiple: false
  });
  //When a file is selected, grab the URL and set it as the text field's value
  custom_uploader.on('select', function() {
      attachment = custom_uploader.state().get('selection').first().toJSON();
      jQuery(target).val(attachment.url);
      if (img_target!=''){
      	jQuery(img_target).attr('src', attachment.url);
      	jQuery(img_target).css('display', 'block');
      }
  });
  //Open the uploader dialog
  custom_uploader.open();
}
function ulpBadgesDoDelete(badge_id){
	jQuery.ajax({
			type : "post",
			url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
			data : {
							action: "ulp_delete_badge",
							id: badge_id
					},
			success: function (r) {
					//location.reload(); /// refresh
					jQuery('#table_tr_'+badge_id).fadeOut(300);
			}
		});
}
function ulpMakeInstructorUser(uid){
	jQuery.ajax({
			type : 'post',
			url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
			data : {
							action:  'ulp_instructor_become_normal_user',
							user_id: uid,
			},
			success: function (r) {
				location.reload();
			}
	});
}
function ulpMakeUserInstructor(uid){
	jQuery.ajax({
			type : 'post',
			url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
			data : {
							action:  'ulp_user_become_instructor',
							user_id: uid,
			},
			success: function (r) {
				location.reload();
			}
	});
}
function ulpRemoveCourse(uid, cid, course_name){
	swal({
	  title: "",
	  text: UltimateLearningPro.ulp_admin_general_messages.remove_user_from_course + course_name + "?",
	  type: "warning",
	  showCancelButton: true,
	  confirmButtonClass: "btn-danger",
	  confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
	  closeOnConfirm: false
	},
	function(){
			jQuery.ajax({
					type : 'post',
					url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
					data : {
									action:  'ulp_user_remove_course',
									user_id: uid,
									course_id: cid,
					},
					success: function (r) {
						location.reload();
					}
			});
	});
}
function ulpRemoveInstructorFromCourse(uid, cid, course_name, additional_instructor){
	swal({
	  title:"",
	  text: UltimateLearningPro.ulp_admin_general_messages.remove_instructor_from_course + course_name + "?",
	  type: "warning",
	  showCancelButton: true,
	  confirmButtonClass: "btn-danger",
	  confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
	  closeOnConfirm: false
	},
	function(){
			jQuery.ajax({
					type : 'post',
					url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
					data : {
									action:  'ulp_remove_instructor_from_course',
									user_id: uid,
									course_id: cid,
									is_additional_instructor: additional_instructor
					},
					success: function (r) {
						location.reload();
					}
			});
	});
}
function ulpAdminPreviewInvoice(){
	var m = jQuery('#invoice_form').serializeArray();
	jQuery('#preview_container').html('');
	jQuery.ajax({
		  type : 'post',
		  url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
			data : {
								 action: "ulp_admin_invoice_preview",
								 metas: m
			},
			success: function (r) {
					jQuery('#preview_container').html(r);
			}
	});
}
function ulpOpenInvoiceAdmin(i, user_id){
		jQuery.ajax({
				type : "post",
				url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
				data : {
									 action: "ulp_get_invoice_popup",
									 order_id: i,
									 uid: user_id,
				},
				success: function (r) {
						jQuery('body').append(r);
				}
		});
}
function ulpClosePopup(){
		jQuery('#ulp_popup').fadeOut(500, function(){
				jQuery(this).remove();
		});
}
function ulpApMakeVisible(t, m){
		jQuery('.ulp-ap-tabs-list-item').removeClass('ulp-ap-tabs-selected-item');
		jQuery(m).addClass('ulp-ap-tabs-selected-item');
		jQuery('.ulp-ap-tabs-settings-item').fadeOut(200, function(){
				jQuery('#ulp_tab_item_' + t).css('display', 'block');
		});
}
function ulpOpenCertificateFromAdmin(certificateId){
	jQuery.ajax({
			type : "post",
			url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
			data : {
								 action: "ulp_get_certificate_popup_for_admin",
								 certificate_id: certificateId
			},
			success: function (r) {
					jQuery('body').append(r);
			}
	});
}
function ulpShowSelectorIf(target, check, value_to_compare){
		if (check==value_to_compare){
				jQuery(target).css('display', 'block');
				return;
		}
		jQuery(target).css('display', 'none');
}
function ulpResetPoints(uid){
	jQuery.ajax({
			type : "post",
			url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
			data : {
								 action: "ulp_reset_points",
								 user_id: uid
			},
			success: function (r) {
					location.reload();
			}
	});
}
function ulpRemoveCurrency(c){
	jQuery.ajax({
			type : 'post',
			url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
			data : {
								 action: 'ulp_delete_custom_currency',
								 code: c
						 },
			success: function (r) {
				if (r){
					jQuery("#ulp_div_"+c).fadeOut(300);
				}
			}
	});
}
function ulpRemoveDifficultyType(the_slug){
	jQuery.ajax({
			type : 'post',
			url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
			data : {
								 action: 'ulp_delete_course_difficulty',
								 slug: the_slug
						 },
			success: function (r) {
				if (r){
					jQuery("#ulp_div_"+the_slug).fadeOut(300);
				}
			}
	});
}
function ulpCheckFieldLimit(limit, d){
	var val = jQuery(d).val().length;
	if (val>limit){
		jQuery(d).val('');
		swal(UltimateLearningPro.ulp_admin_general_messages.error, limit + ' ' + UltimateLearningPro.ulp_admin_general_messages.limit_char, "error");
	}
}
function ulpChangePostStatus(postId, postStatus, redirectLink){
		jQuery.ajax({
				type : 'post',
				url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
				data : {
									 action: 'ulp_change_post_status',
									 post_id: postId,
									 post_status: postStatus
							 },
				success: function (r) {
						if (redirectLink==''){
							location.reload();
						} else {
							window.location.href = redirectLink;
						}
				}
		});
}
function ulpMakeExportFile(){
		var instructors = jQuery('#import_instructors').val();
		var students = jQuery('#import_students').val();
		var s = jQuery('#import_settings').val();
		var cpt = jQuery('#import_custom_post_types').val();
		jQuery('#ulpLoading_gif .spinner').css('visibility', 'visible');
		
		//console.log(u,s,pm,p);
		jQuery.ajax({
		        type : 'post',
		        url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
		        data : {
		                   action: 'ulp_make_export_file',
		                   import_instructors: instructors,
		                   import_students: students,
		                   import_settings: s,
											 import_custom_post_types: cpt
		               },
		        success: function (response) {
		        	if (response!=0){
		        		jQuery('.ulp-hidden-download-link a').attr('href', response);
		        		jQuery('.ulp-hidden-download-link').fadeIn(200);
								jQuery('#ulpLoading_gif .spinner').css('visibility', 'hidden');
		        	}
		        }
		});
}
function ulpChangeColorScheme(id, value, where ){
    jQuery('#colors_ul li').each(function(){
        jQuery(this).attr('class', 'color-scheme-item');
    });
    jQuery(id).attr('class', 'color-scheme-item-selected');
    jQuery(where).val(value);
}
function ulpChangeColorSchemeWd(id, value, where ){
		var non_selected = 'color-scheme-item';
		var selected = 'color-scheme-item-selected';
		var c = jQuery(id).attr('class');
    jQuery('#colors_ul li').each(function(){
        jQuery(this).attr('class', non_selected);
    });
    jQuery(where).val('');
    if (c==non_selected){
	    jQuery(id).attr('class', selected);
	    jQuery(where).val(value);
    }
}
function ulpCheckboxDivRelation(c, t){
		/*
		 * c = checkbox id to check
		 * t = target div
		 */
		var o = 0.5;
		if (jQuery(c).is(":checked")){
			o = 1;
		}
		jQuery(t).css("opacity", o);
}
function ulpPreviewShortcode(){
		jQuery('#preview').html('');
		jQuery("#preview").html('<div style="background:#fff;width: 100%;text-align:center;"><img src="'+window.ulp_plugin_url+'/assets/images/loading.gif" class=""/></div>');
		var meta = [];
		meta.num_of_entries = jQuery('#num_of_entries').val();
		meta.entries_per_page = jQuery('#entries_per_page').val();
		meta.order_by = jQuery('#order_by').val();
		meta.order_type = jQuery('#order_type').val();
		if (jQuery('#include_fields_label').is(':checked')){
			meta.include_fields_label = 1;
		}
		meta.theme = jQuery('#theme').val();
		meta.color_scheme = jQuery('#color_scheme').val();
		meta.columns = jQuery('#columns').val();
		if (jQuery('#align_center').is(":checked")){
			meta.align_center = 1;
		}
		if (jQuery('#slider_set').is(":checked")){
			meta.slider_set = 1;
			meta.items_per_slide = jQuery('#items_per_slide').val();
			meta.speed = jQuery("#speed").val();
			meta.pagination_speed = jQuery('#pagination_speed').val();
			meta.pagination_theme = jQuery('#pagination_theme').val();
			meta.animation_in = jQuery('#animation_in').val();
			meta.animation_out = jQuery('#animation_out').val();
			var slider_special_metas = ['bullets', 'nav_button', 'autoplay', 'stop_hover', 'responsive', 'autoheight', 'lazy_load', 'loop'];
			for (var i=0; i<slider_special_metas.length; i++){
				if (jQuery('#'+slider_special_metas[i]).is(":checked")){
					meta[slider_special_metas[i]] = 1;
				}
			}
		}
		meta.general_pagination_theme = jQuery('#general_pagination_theme').val();
		meta.pagination_pos = jQuery('#pagination_pos').val();
		meta.fields = jQuery('#ulp_grid_fields').val();
		///SHORTCODE
		switch (window.ulp_grid_entity_type){
				case 'students':
					var str = "[ulp-grid-students ";
					break;
				case 'courses':
					meta.filter_by_cats = jQuery('#ulp_filter_by_cats').val();
					if (meta.filter_by_cats==-1) meta.filter_by_cats = '';
					var str = "[ulp-grid-courses ";
					break;
		}
		for (var key in meta) {
			str += key + "='" + meta[key] +"' ";
		}
		str += ']';
	  jQuery('.the-shortcode').html(str);
	  jQuery(".php-code").html('&lt;?php echo do_shortcode("'+str+'");?&gt;');
					jQuery.ajax({
			        type : 'post',
			        url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
			        data : {
			                   action: 'ulp_ajax_do_shortcode',
												 shortcode: str
			               },
			        success: function (r) {
						//console.log(r);
			        	jQuery('#preview').html(r);
			        }
			   	});
}
function ulpSplit(v){
	if (v.indexOf(',')!=-1){
	    return v.split( /,\s*/ );
	} else if (v!=''){
		return [v];
	}
	return [];
}
function ulpExtract(t) {
    return ulpSplit(t).pop();
}
function contains(a, obj) {
    return a.some(function(element){return element == obj;})
}
function ulpLoadCourseStudents(PostId){
	jQuery('#ulp_list_course_students').slideUp( 500 );
	jQuery.ajax({
			type : 'post',
			url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
			data : {
								 action: 'ulp_ajax_edit_course_return_all_students_in_table',
								 post_id: PostId
						 },
			success: function (r) {
					jQuery('#ulp_list_course_students').html(r);
					jQuery('#ulp_list_course_students').slideDown( 500 );
			}
	});
}
function ulpAddUserToCourse(PostId){
		jQuery('#ulp_list_course_students').slideUp( 500 );
		jQuery.ajax({
				type : 'post',
				url : decodeURI(window.ulp_url)+'/wp-admin/admin-ajax.php',
				data : {
									 action: 'ulp_ajax_edit_course_add_new_student',
									 post_id: PostId,
									 username: jQuery('#ulp_new_student').val()
							 },
				success: function (r) {
						ulpLoadCourseStudents(r);
						jQuery('#ulp_new_student').val('');
				}
		});
}
function ulp_remove_course_reload_table(uid, cid, course_name){
	swal({
	  title: "",
	  text: UltimateLearningPro.ulp_admin_general_messages.remove_user_from_course + course_name + "?",
	  type: "warning",
	  showCancelButton: true,
	  confirmButtonClass: "btn-danger",
	  confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
	  closeOnConfirm: true
	},
	function(){
			jQuery.ajax({
					type : 'post',
					url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
					data : {
									action:  'ulp_user_remove_course',
									user_id: uid,
									course_id: cid,
					},
					success: function (r) {
							ulpLoadCourseStudents(cid);
					}
			});
	});
}
function ulpRemoveUserBadge(uid, badgeId){
		swal({
		  title: "",
		  text: UltimateLearningPro.ulp_admin_general_messages.remove_badge_from_user,
		  type: "warning",
		  showCancelButton: true,
		  confirmButtonClass: "btn-danger",
		  confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
		  closeOnConfirm: false
		},
		function(){
				jQuery.ajax({
						type : 'post',
						url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
						data : {
										action:  'ulp_user_remove_badge',
										user_id: uid,
										badge_id: badgeId,
						},
						success: function (r) {
							location.reload();
						}
				});
		});
}
function ulpRemoveUserCertificate(uid, certificateId){
		swal({
		  title: "",
		  text: UltimateLearningPro.ulp_admin_general_messages.remove_certificate_from_user,
		  type: "warning",
		  showCancelButton: true,
		  confirmButtonClass: "btn-danger",
		  confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
		  closeOnConfirm: false
		},
		function(){
				jQuery.ajax({
						type : 'post',
						url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
						data : {
										action:  'ulp_user_remove_certificate',
										user_id: uid,
										certificate_id: certificateId,
						},
						success: function (r) {
							location.reload();
						}
				});
		});
}

jQuery(document).ready(function(){
		jQuery('.js-ulp-do-delete-post').on('click', function(evt){
			swal({
			  title: "",
			  text: UltimateLearningPro.ulp_admin_general_messages.delete_post,
			  type: "warning",
			  showCancelButton: true,
			  confirmButtonClass: "btn-danger",
			  confirmButtonText: UltimateLearningPro.ulp_admin_general_messages.delete_it,
			  closeOnConfirm: false
			},
			function(){
					jQuery.ajax({
							type : 'post',
							url : decodeURI(window.ulp_url) + '/wp-admin/admin-ajax.php',
							data : {
											action: 'ulp_ajax_do_delete_post',
											postId: jQuery(evt.target).attr('data-id'),
							},
							success: function (r) {
								location.reload();
							}
					});
			});
		});

		// uninstall
		jQuery( '.deactivate' ).on( 'click', function(evt){
				if ( jQuery( evt.target ).attr('href').indexOf( 'indeed-learning-pro' ) > -1 ){
						if ( window.ulpKeepData == 1 ){
								var theMessage = 'Plugin data will be kept in database after you delete the plugin.';
						} else {
								var theMessage = 'Plugin data will be lost after you delete the plugin.';
						}
						var target = jQuery( evt.target ).attr('href');
						swal({
							title: theMessage,
							text: "In order to change that, go to General Settings -> Admin Workflow.",
							type: "warning",
							showCancelButton: true,
							confirmButtonClass: "btn-danger",
							confirmButtonText: "OK",
							closeOnConfirm: true
						},	function(){
								window.location.href = target;
						});
						return false;
				}
		});

		// disable notice message
		jQuery( '.ulp-js-close-admin-dashboard-notice' ).on( 'click', function(){
				var parent = jQuery(this).parent();
				parent.fadeOut( 1000 );
				jQuery.ajax({
						type : 'post',
						url : decodeURI(window.uap_url)+'/wp-admin/admin-ajax.php',
						data : {
											 action: 'ulp_close_admin_notice'
									 },
						success: function (response) {
								parent.remove();
						}
			 });
		});

});

function ulpAddZero(i)
{
		if (i < 10) {
				i = "0" + i;
		}
		return i;
}
