(function ($) {
  "use strict";
  
  $(function () {
    $('input.icheck').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' /* optional */
    });
  });


var base_url = $('#base_url').attr('href');
var csrf_value = $('#csrf_value').attr('href');
var yes = $('#yes').attr('href');
var no = $('#no').attr('href');
var are_you_sure = $('#are_you_sure').attr('href');
var cancel = $('#cancel').attr('href');
var success_alert = $('#success').attr('href');
var success_msg = $('#success_msg').attr('href');
var error_alert = $('#error').attr('href');
var error_msg = $('#error_msg').attr('href');
var label = $('#label').attr('href');
var value = $('#value').attr('href');

jQuery(document).ready(function(){
  //active date picker
    $('.datepicker').datepicker({
        format: "yyyy-mm-dd",
        rtl: true,
    });

    // current time for clock
    $(function(){
      var datetime = null;
      var update = function () {
          datetime.html(moment().format('h:mm:ss a'));
      };

      $(document).ready(function(){
          datetime = $('#time');
          update();
          setInterval(update, 1000);
      });

    });

    $('.tinymce').summernote({
    fontSizes: ['8', '9', '10', '11', '12', '14', '18','20','30','50'],
    height: 100,
    codemirror: { // codemirror options
      theme: 'monokai',
      mode: 'text/html',
      lineNumbers: true,
      htmlMode: true,
    },
    toolbar: [
      ['style', ['style']],
      ['font', ['bold', 'italic', 'underline', 'clear']],
      ['fontname', ['fontname']],
      ['color', ['color']],
      ['para', ['ul', 'ol', 'paragraph']],
      ['height', ['height']],
      ['table', ['table']],
      ['insert', ['link', 'picture', 'video', 'hr']],
      ['view', ['fullscreen', 'codeview']],
      ['fontsize', ['fontsize']],
      ['help', ['help']]
    ],
    // callbacks: {
    //     onImageUpload: function(files, editor, welEditable) {
    //         sendFile(files[0], editor, welEditable);
    //     }
    // }
  });

  function sendFile(image) {
        var data = new FormData();
        data.append("file", image);
        //if you are using CI 3 CSRF
        data.append('csrf_test_name', csrf_value);
        $.ajax({
            data: data,
            type: "POST",
            url: `${base_url}/admin/dashboard/summernote_image_upload`,
            cache: false,
            contentType: false,
            processData: false,
            success: function (url) {
                var image = url;
                $('.tinymce').summernote("insertImage", image);
            },
            error: function (data) {
                console.log(data);
            }
        });
    }


    // active imageuploadify
    $(document).ready(function() {
      $('.image_upload').imageuploadify();
    });

    // data table 1
    $('#example1').DataTable({
        'lengthChange': true,
    });

     // data table 2
    $('#example2').DataTable({
        'lengthChange': true,
    });

    // select2
     $('.select2').select2();

    // full calendar 
    $('#calendar').datepicker("setDate", new Date());

  /**
    ** tag selector
  **/
     $('.chosen-select').chosen({width:"100%",
        max_selected_options:4,
        placeholder_text_multiple:"Select Tags",
        
      });


     //Timepicker
  $('.timepicker').timepicker({
      showInputs: false,
      defaultTime: '10:00',
      format: 'hh:mm',
      use24hours: true,
      showMeridian: false,    
      minuteStepping:10,
  });

});
$(function() {
  $(document).on('change','.email_option',function(){
      var id = $(this).val();
      if(id==2){
        $('.smtpArea').slideDown();
      }else{
        $('.smtpArea').slideUp();
      }
  });
});

$(".skill_form, .validForm").validate({
   errorPlacement: function(error,element) {
    return false;
  }
});

 //color picker with addon
  $('.my-colorpicker1').colorpicker();
  $('.my-colorpicker2').colorpicker();

  $(function () {
    $('[data-toggle="tooltip"]').tooltip();
  });

$(function(){
   $('.hide_details').hide();
  $(document).on('click', '.details_check', function(event) {
    $('.hide_details').toggle();
  });

});


$(function(){
  $(document).on('click', '.check_password', function(event) {
    var val = $(this).val();
      $('.show_password').slideToggle();
  });

});


// Admin login
$(function(){
  $('#user_login_form').on('submit',function(e) {
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
                window.location = json.url;
                $(".reg_msg").slideDown().html(json.msg); 
            }else if(json.st == 2) {
                $(".reg_msg").slideDown().html(json.msg);
            }else{
                $(".reg_msg").slideDown().html(json.msg);
            }
            setTimeout(function(){ $('.reg_msg').fadeOut();}, 2000);
        },'json');
        return false;
    });
 });



// Registration form
$(function(){
  $(document).on('submit', '#user_insert_form', function(e) {
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
                $(".reg_msg").slideDown().html(json.msg); 
                $("#user_insert_form")[0].reset(); 
            }else{
                $(".reg_msg").slideDown().html(json.msg);
            }
            setTimeout(function(){ $('.reg_msg').fadeOut();}, 2000);
        },'json');
        return false;
    });
 });







//default alert for action
$(function(){
  var sure_alert = $('.sure-msg').data('msg');
  $(document).on('click','.action_btn',function(){
    var link = $(this).attr('href');
    var msg = $(this).data('msg');
    swal({
        title: sure_alert,
        text: msg,
        type: "warning",
        showCancelButton: true,
        cancelButtonText: cancel,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: yes,
        cancelButtonText: no,
        closeOnConfirm: false,
      }, function(){                      
          window.location.href =link;
      });
    return false;
  });
});

// change Password form
$(function(){
  $(document).on('submit', '#change_pass_form', function(e) {
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
                $(".reg_msg").slideDown().html(json.msg); 
                $("#change_pass_form")[0].reset(); 
            }else{
                $(".reg_msg").slideDown().html(json.msg);
            }
            setTimeout(function(){ $('.reg_msg').fadeOut();}, 2000);
        },'json');
        return false;
    });
 });


// ajax image upload
$('.upload_img').on('change',(function(e) {
    e.preventDefault();
    var url = $(this).attr('action');
    var formData = new FormData(this);
    $('.upload_progress').slideDown();
    $.ajax({
        type:'POST',
        url:url,
        data:formData,
        dataType:'json',  
        cache:false,
        contentType: false, 
        processData: false,
        xhr: function () {
          var xhr = new window.XMLHttpRequest();
          xhr.upload.addEventListener("progress", function (evt) {
              if (evt.lengthComputable) {
                  var percentComplete = evt.loaded / evt.total;
                  percentComplete = parseInt(percentComplete * 100);
                  $('.myprogress').text(percentComplete + '%');
                  $('.myprogress').css('width', percentComplete + '%');
              }
          }, false);
          return xhr;
      },
          
        success:function(data){
          if(data.st == 1){
           $('.uploaded_img').attr('src', base_url+data.img);
           $('.upload_progress').fadeOut();
          }else{
            $('.upload_progress').hide();
            $("html, body").animate({ scrollTop: 0 }, 600);
            $('#successMessage').html(`<div class="alert alert-danger alert-dismissible custom_alert" id="successMessage">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <h4>
                    <i class="icon fa fa-close"></i>
                    Error!
                </h4>
                ${data.msg}
              </div>`);
          }
          
        },
        error: function(e){
            console.log(e.error);
        }
    });
}));


// ajax image upload
$('.upload_pdf').on('change',(function(e) {
    e.preventDefault();
    var url = $(this).attr('action');
    var formData = new FormData(this);
    $('.pdf_upload_progress').slideDown();
    $.ajax({
        type:'POST',
        url:url,
        data:formData,
        dataType:'json',  
        cache:false,
        contentType: false, 
        processData: false,
        xhr: function () {
          var xhr = new window.XMLHttpRequest();
          xhr.upload.addEventListener("progress", function (evt) {
              if (evt.lengthComputable) {
                  var percentComplete = evt.loaded / evt.total;
                  percentComplete = parseInt(percentComplete * 100);
                  $('.myprogress').text(percentComplete + '%');
                  $('.myprogress').css('width', percentComplete + '%');
              }
          }, false);
          return xhr;
      },
          
        success:function(data){
          if(data.st == 1){
           $('.uploaded_pdf').slideDown();
           $('.pdf_upload_progress').fadeOut();
           $("html, body").animate({ scrollTop: 0 }, 600);
           $('#successMessage').html(`<div class="alert alert-success alert-dismissible custom_alert" id="successMessage">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <h4>
                    <i class="icon fa fa-check"></i>
                    Success!
                </h4>
                ${data.msg}
              </div>`);
          }else{
            $('.pdf_upload_progress').hide();
          }
          
        },
        error: function(){
            console.log("error");
        }
    });
}));

//delete gallery image
$(function(){
  $(document).on('click','.img_delete',function(){
    var conf = confirm('Are You Want to delete this image');
    var id = $(this).attr('data-id');
      if(conf){
        var url = base_url+'admin/profile/delete_portfolio/'+id;
         $.post(url, {'csrf_test_name': csrf_value }, function(json){
          if(json.st == 1){
            $('#hide_image_'+id).slideUp();
            MSG(true,json.msg);
          }
       },'json');

      return false;
      }
  });
});


// social area
$(function(){
  $(document).on('click', '.social_label', function(e) {
     var val = $(this).val();
     $(`.site_field_${val}`).toggleClass('hidden');
     var p =$(this).parent().toggleClass('label_active');
  });
})


// social area
$(function(){
  $(document).on('click', '.toggle_btn', function(e) {
    $('.toggle_display').slideToggle();
  });
})





$(function(){
   var max_fields      = 6; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    
    var x = 1; //initlal text box count
    $(add_button).on('click', function(e) {
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append(`<div class="form-group about_me_group hide_${x}">
                  <input type="text" name="label[]" id="name" class="form-control mr-10" placeholder="Label">

                  <input type="text" name="value[]" id="value" class="form-control " placeholder="Value" >
                  <a href="javascript:;" data-id='${x}' class="remove_field">
                    <i class="fa fa-close"></i>
                  </a>
                </div>`); //add input box
        }else{
          alert('Sorry Fields Limit exists');
        }
    });
    
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); 
        var id = $(this).attr('data-id');
        $('.hide_'+id).remove();
        x--;
    });

});


$(function(){
   var max_fields      = $('.vcard_limit').data('limit'); //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_icon_btn"); //Add button ID
    var currentField = $('.input_fields_wrap .addField').length;
    var limitText = $('.limit_text').data('msg');
    var x = currentField; //initlal text box count
    $(add_button).on('click', function(e) {
      var id = $(this).data('id');
      var placeholder = $(this).data('placeholder');
      var icon = $(this).data('icon');
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append(`<div class="form-group hide_${x}" >
                        <div class="addField">
                          <div class="addFieldHeading">
                            <div class="d handle">
                              <i class="fa fa-reorder"></i>
                              <h4><span class="mr-2">${icon}</span> ${placeholder}</h4>
                            </div>
                             <a href="javascript:;" data-id='${x}' class="remove_field text-danger">
                              <i class="fa fa-close"></i>
                            </a>
                          </div>
                          <div class="addFieldDetails">
                            <input type="text" name="label[${id}]" class="form-control" value="" placeholder="${label}" required>
                            <input type="text" name="value[${id}]" class="form-control" value="" placeholder="${value}" required>
                            <input type="hidden" name="site_id[]" value="${id}">
                          </div>
                        </div>
                      </div>
              `); //add input box
        }else{
          swal({
            title: "Alert",
            text: limitText,
            type: "warning",
            showCancelButton: false
          },function(){

          });
        }
    });
    
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); 
        var id = $(this).attr('data-id');
        $('.hide_'+id).remove();
        x--;
    });

});


$(function(){

  var ul_sortable = $('.input_fields_wrap');

  ul_sortable.sortable({
    opacity: 1,
    tolerance: 'pointer',
    cursor: 'move',
    handle: '.handle',
    revert: 100,
    update: function(event, ui) {
        console.log(event)
      }

     });
  ul_sortable.disableSelection();
})



$(function(){
    $(document).on('submit','.vcard-form-submit',function(){
       $('.submit_btn').prop('disabled', true);
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
               $('.submit_btn').prop('disabled', false);
                MSG(true,success_msg);
            }else{
               $('.submit_btn').prop('disabled', false);
                 MSG(true,success_msg);
            }
        },'json');
        return false;
    });
 });


$(function() {
  $(document).on('change','.layout_action',function(){
     var $this = $(this);
      var type = $(this).data('type');
      var value = $(this).val();
      remove_checked(type);
      var url = `${base_url}admin/profile/change_layouts/${type}/${value}`;
      $.post(url, {'csrf_test_name': csrf_value }, function(json){
        if(json.st == 1){
          MSG(true,success_msg);
          $this.addClass('active');
          $this.attr('checked',true);

        }
      },'json');
      return false;
  });


  function remove_checked(data){
    $(`.${data}`).removeClass('active');
    $(`.${data}`).removeAttr('checked');
    return true;
  }



});

/**
  ***  accordion
**/ 


$(function(){
    $(".accordions").on("click", ".page_accordion_header", function() {
        $(this).toggleClass("active").next().slideToggle(300);
        $(this).toggleClass('arrow_up').toggleClass('arrow_down');
    });
})

$(function() {
  $(document).on('click','.package_head',function(){
      var id = $(this).val();
      $(`.show_body_${id}`).slideDown();
  });
});


$(function() {
  $(document).on('click','.email_option',function(){
      var id = $(this).val();
      if(id==2){
        $('.smtp_area').slideDown();
      }else{
        $('.smtp_area').slideUp();
      }
  });
});

/**
  ** Settings ==> registration / email verification
**/

$(function() {
  $(document).on('change','.setting_option',function(){
      var type = $(this).data('type');
      var value = $(this).data('value');
      var setData = this;
      var url = `${base_url}admin/dashboard/setting_status/${type}/${value}`;
      $.post(url, {'csrf_test_name': csrf_value }, function(json){
        if(json.st == 1){
          if(value==1){
            MSG(true,success_msg);
            $(setData).data('value',0);
         }else{
            MSG(true,success_msg);
            $(setData).data('value',1);
         }
         
        }
      },'json');
      return false;
  });
});



/**
  ** change all items status using ajax
**/

$(function() {
  $(document).on('click','.change_status',function(event){
      var setData = this;
      var id = $(this).data('id');
      var status = $(this).data('status');
      var table = $(this).data('table');
      var url = `${base_url}admin/dashboard/change_content_status/${id}/${status}/${table}`;
      $.post(url, {'csrf_test_name': csrf_value }, function(json){
        if(json.st == 1){
           if(status==1){
            $(setData).data('status',0);
            $(setData).html(`<i class="fa fa-close"></i>&nbsp; Hide`).removeClass('label-success').addClass('label-danger');
              MSG(true,success_msg);
           }else{
            $(setData).data('status',1);
            $(setData).html(`<i class="fa fa-check"></i>&nbsp; Live`).removeClass('label-danger').addClass('label-success');
            MSG(true,success_msg);
           }
        }
      },'json');
      return false;
  });
});

function MSG(valid,msg){
  if(valid){
      $.notify({
        icon: 'fa fa-check',
        title: success_alert,
        message:msg
      },{
        type: 'success'
      },{
          animate: {
            enter: 'animated fadeInRight',
            exit: 'animated fadeOutRight'
          }
      });
  }else{
      $.notify({
        icon: 'fa fa-close',
        title: error_alert,
        message:msg
      },{
        type: 'danger'
      },{
          animate: {
            enter: 'animated fadeInRight',
            exit: 'animated fadeOutRight'
          }
      });
  }
}

/**
  ** ajax submit form
**/
$(function(){
    $(document).on('submit','.form-submit',function(){
      $('.submit_btn').prop('disabled', true);
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
                $(".reg_msg").html(json.msg).slideDown(); 
                $(".form-submit")[0].reset(); 
                $('.customModal').modal('hide');
                $('.submit_btn').prop('disabled', false);
            }else{
                $(".reg_msg").html(json.msg).slideDown();
            }
            setTimeout(function(){ $('.reg_msg').fadeOut();}, 2000);
        },'json');
        return false;
    });
 });


$(function(){
    $(document).on('submit','#videUpload_form',function(){
      $('.submit_btn').prop('disabled', true);
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
                MSG(true,json.msg)
                $("#videUpload_form")[0].reset(); 
                $('.submit_btn').prop('disabled', false);
                  location.reload();
            }else{
              MSG(false,json.msg)
            }
            setTimeout(function(){ $('.reg_msg').fadeOut();}, 2000);
        },'json');
        return false;
    });
 });


$(function(){

/**
  ** readURL
**/
  function service_URL(input) {
    if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function(e) {
          $('.service_icon_preview').attr('src', e.target.result);
          $('.serviceImg').slideDown();
      }
      reader.readAsDataURL(input.files[0]);
    }
  }

  var _URL = window.URL || window.webkitURL;
  $(".service_img").on('change',function(e){
      var container = this;
      var max_width = $(this).data('width');
      var max_height = $(this).data('height');
      var file, img;
      if ((file = this.files[0])) {
          img = new Image();
          img.onload = function () {
            var height = this.height;
            var width = this.width;
            if(max_width != 0 && max_height !=0){
              if(width <= max_width && height <= max_height){
                service_URL(container);
                $('.img_error').html('');
              }else{
                $('.img_error').html(`Image Dimention should be less than ${max_width} x ${max_height} px`);
                $('.service_img').val('');
                $('.serviceImg').slideUp();
              }
            }else{
                service_URL(container);
                $('.img_error').html('');
            }
            
          };
          img.src = _URL.createObjectURL(file);
      }
  });
});




  function pwa_url(input) {
    if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function(e) {
          $('.service_icon_preview').attr('src', e.target.result);
          $('.serviceImg').slideDown();
      }
      reader.readAsDataURL(input.files[0]);
    }
  }

  var _URL = window.URL || window.webkitURL;
  $(".pwa_img").on('change',function(e){
      var container = this;
      var max_width = $(this).data('width');
      var max_height = $(this).data('height');
      var file, img;
      if ((file = this.files[0])) {
          img = new Image();
          img.onload = function () {
            var height = this.height;
            var width = this.width;
            if(max_width != 0 && max_height !=0){
              if(width == height){
                pwa_url(container);
                $('.img_error').html('');
              }else{
                $('.img_error').html(`Image Dimention should be less than ${max_width} x ${max_height} px and must be square`);
                $('.pwa_img').val('');
                $('.serviceImg').slideUp();
              }
            }else{
                pwa_url(container);
                $('.img_error').html('');
            }
            
          };
          img.src = _URL.createObjectURL(file);
      }
  });



// /*=============================================
// ***********  favicon uploader ********************
// ================================================== */
$(function(){

/**
  ** readURL
**/
  function readURL(input) {
    if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function(e) {
          $('.fav_icon_preview').attr('src', e.target.result);
      }
      reader.readAsDataURL(input.files[0]);
    }
  }

  var _URL = window.URL || window.webkitURL;
  $(".load_img").on('change',function(e){
      var container = this;
      var file, img;
      if ((file = this.files[0])) {
          img = new Image();
          img.onload = function () {
            var height = this.height;
            var width = this.width;
            if(width <= 80 && height <= 80){
              readURL(container);
              $('.img_error-2').html('');
            }else{
              $('.img_error-2').html(`Image Dimention should be less than 35 x 35`);
              $('.load_img').val('');
            }
            
          };
          img.src = _URL.createObjectURL(file);
      }
  });
});


/**
  ** toggle schedule option
**/
$(function(){
  $(document).on('click', '.schedule_input', function(event) {
      $('.schedule_post_view').slideToggle();
  });
})


// feature type changes
$(function(){
  $(document).on('change', '.account_duration', function(event) {
      var val = $(this).val();
      if(val=='free' || val=='trial'){
        $('.feature_price').slideUp();
      }else{
        $('.feature_price').slideDown();
      }

      if(val=='trial'){
        $('.trial_text').slideDown();
      }else{
         $('.trial_text').slideUp();
      }
  });
})


$(document).ready(function(){
    $('.layout-images').on('change', function(){
        var fields = $(this).data('id');
        var fileInput = $(this)[0];

        if(fileInput.files.length > 0 ){
            var formData = new FormData();
            $.each(fileInput.files, function(k,file){
                formData.append('file', file);
            });
            formData.append('csrf_test_name', csrf_value);

            $.ajax({
                method: 'post',
                url:base_url+"admin/profile/upload_banner_img/"+fields,
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function(data){
                  if(data.st==1){
                    $(`#${fields}`).attr("src",base_url+data.url);
                    $(`.banner_empty_text.${fields} `).slideUp();
                    $(`.banner_image.${fields} `).slideDown();
                     $(`#${fields} `).show();
                     MSG(true,'Banner image uploaded successfully')
                    console.log(response);
                  }else{
                    $('.reg_msg').html(data.msg).slideDown();
                  }
                }
            });
        }else{
            console.log('No Files Selected');
        }
    });

});


//delete banner img
$(function(){
  $(document).on('click','.banner_img_del',function(){
    var conf = confirm('Are You Want to delete this image');
    var id = $(this).attr('data-id');
      if(conf){
        var url = base_url+'admin/profile/delete_layout_banner/'+id;
         $.post(url, {'csrf_test_name': csrf_value }, function(json){
          if(json.st == 1){
            $(`.banner_empty_text.${id} `).slideDown();
            $(`.banner_image.${id} `).slideUp();
            // $('#hide_image_'+id).slideUp();
            MSG(true,json.msg);
          }
       },'json');

      return false;
      }
  });
});

  $(document).on('change','.toggle_feature',function(){
      var toggle_feature = $(this);
      var id = toggle_feature.data('id');
      var value = toggle_feature.data('value');
      var url = `${base_url}admin/dashboard/features_toggle/${id}/${value}`;
      $.post(url, {'csrf_test_name': csrf_value }, function(json){
        if(json.st == 1){
          toggle_feature.attr('data-value',json.value);
          MSG(true,success_msg);
        }
      },'json');
      return false;
  });


//Reset Password
$(function() {
  $(document).on('click','.reset_password',function(){
      var id = $(this).data('id');
      var msg = $(this).data('msg');
        swal({
          title: are_you_sure,
          text: msg,
          type: "warning", 
          showCancelButton: true,
          cancelButtonText: cancel,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: yes,
          closeOnConfirm: false
        },
        function(){ 

           
            var url = `${base_url}admin/dashboard/reset_password/${id}`;
            $.post(url, {'csrf_test_name': csrf_value }, function(json){
                if(json.st == 1){     
                    swal({
                      title: "Success",
                      text: "1234 is your new Password",
                      type: "success",
                      showCancelButton: false
                    },function(){
                      
                    });
                }
            },'json');

        });
      return false;
  });
});


$(function(){

  var _URL = window.URL || window.webkitURL;
  $(".ads_img").on('change',function(e){
      var container = this;
      var id = $(this).data('id');
      var max_width = 1200;
      var max_height = 250;
      var file, img;
      if ((file = this.files[0])) {
          img = new Image();
          img.onload = function () {
            var height = this.height;
            var width = this.width;
            if(max_width != 0 && max_height !=0){
              if(width <= max_width && height <= max_height){
                $('.img_error_'+id).html('');
                $('#ads_form_'+id).submit();
              }else{
                $('.img_error_'+id).html(`Image Dimention should be less than ${max_width} x ${max_height} px`);
                $('.ads_img').val('');
                $('#ads_details_'+id).slideDown();
              }
            }else{
                service_URL(container);
                $('.img_error_'+id).html('');
            }
            
          };
          img.src = _URL.createObjectURL(file);
      }
  });
});


$(function(){
  $(document).on('click', '.is_video', function(event) {
      var val = $(this).val();
      if(val==0){
        $('.image_link').slideDown();
        $('.video_link').slideUp();
      }else{
        $('.video_link').slideDown();
        $('.image_link').slideUp();
      }
  });
});


$(function(){
  $(document).on('click', '.icon_dropdown li', function(event) {
      $('.icon_dropdown li').removeClass('active');
      var val = $(this).data('value');
      $('.preview_icon').html(val);
      $('.icon').val(val);
      $(this).addClass('active');
  });
})

  $('.iconGroup').on('change', function(e) {
    var icon = e.icon;
    $('.icon').val(`<i class="${icon}"></i>`);
  });
// $(document).on('click', '.btn-icon', function(event) {

//       var val = $(this).data('value');
//       alert(val);
//       $('.preview_icon').html(val);
//       $('.icon').val(val);
//       $(this).addClass('active');
//   });


$(function(){
  $(document).on('click', '.tab_li', function(event) {
      var val = $(this).data('value');
      if(val=='icon'){
         $(`.service_type`).val(1);
       }else if(val=='image'){
        $(`.service_type`).val(2);
       }else if(val=='video'){
        $(`.service_type`).val(3);
       }else{
        $(`.service_type`).val(1);
       }
      
  });
})



    $(document).on('submit','.ads_forms',function(e) {
        e.preventDefault();
        var type_id = $(this).data('type');
        var url = `${base_url}admin/dashboard/upload_ads/${type_id}`;

        var formData = new FormData(this);
        $('#single_progress_'+type_id).slideDown();
        $.ajax({
            type:'POST',
            url:url,
            data:formData,
            dataType:'json',  
            cache:false,
            contentType: false, 
            processData: false,
            xhr: function () {
              var xhr = new window.XMLHttpRequest();
              xhr.upload.addEventListener("progress", function (evt) {
                  if (evt.lengthComputable) {
                      var percentComplete = evt.loaded / evt.total;
                      percentComplete = parseInt(percentComplete * 100);
                      $('.myprogress').text(percentComplete + '%');
                      $('.myprogress').css('width', percentComplete + '%');
                  }
              }, false);
              return xhr;
          },
              
            success:function(json){
              if(json.st == 1){
                  $('#single_progress_'+type_id).slideUp();
                  $(".ads_preview_"+type_id).attr("src", base_url+json.img).show();
                  $("#ads_details_"+type_id).hide();
              }else{
                $('#single_progress_'+type_id).hide();
                $("html, body").animate({ scrollTop: 0 }, 600);
                  MSG(false,json.msg);
              }
              
            },
            error: function(){
                console.log("error");
            }
        });
    });


    
   $('#stars li').on('mouseover', function(){
    var onStar = parseInt($(this).data('value'), 10); // The star currently mouse on
   
    // Now highlight all the stars that's not after the current hovered star
    $(this).parent().children('li.star').each(function(e){
      if (e < onStar) {
        $(this).addClass('hover');
      }
      else {
        $(this).removeClass('hover');
      }
    });
    
  }).on('mouseout', function(){
    $(this).parent().children('li.star').each(function(e){
      $(this).removeClass('hover');
    });
  });
  
  
  /* 2. Action to perform on click */
  $('#stars li').on('click', function(){
    var onStar = parseInt($(this).data('value'), 10); // The star currently selected
    var stars = $(this).parent().children('li.star');
    
    for (var i = 0; i < stars.length; i++) {
      $(stars[i]).removeClass('selected');
    }
    
    for (var i = 0; i < onStar; i++) {
      $(stars[i]).addClass('selected');
    }
    
    // JUST RESPONSE (Not needed)
    var ratingValue = parseInt($('#stars li.selected').last().data('value'), 10);
    var msg = "";
    if (ratingValue > 1) {
        msg = ratingValue;
    }
    else {
        msg = ratingValue;
    }
    responseMessage(msg);
    
  });
  
  function responseMessage(msg) {
    $('.rating').val(msg);
  }  
$(document).on('click','.loginBTN',function(){
    var user = $(this).data('user');
    var pass = $(this).data('pass');
    $('.usermail').val(user);
    $('.pass').val(pass);
    $('#user_login_form').submit();
});


$('.copy').on('click', function(e) {
  e.preventDefault();
  var id = $(this).data('id');
  var copyText = $(this).data('link');

  var textarea = document.createElement("textarea");
  textarea.textContent = copyText;
  textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand("copy"); 
  document.body.removeChild(textarea);

      var url = `${base_url}/profile/count_copy/${id}`;
        $.get(url, {'csrf_test_name': csrf_value }, function(json){
            if (json.st == 1) {
              $(this).addClass('d_bg');
              $('.copy_alert').slideDown();
            }
        },'json');
     ;

  setTimeout(function(){ $('.copy_alert').slideUp(); $('.copy').removeClass('d_bg');}, 2000);
});


$(function(){
  $(document).on('keyup','.remove_space',function(){
      var val = $(this).val();
      if(val==''){
        return;
      }

      if(val.match(/[^\w]/g)){
        $(".alert_msg").html('No space allowed').addClass('error');
        var newName =  val.replace(/[^\w]/g, "");
        $(this).val(newName);
        return;
      }else{
        $(".alert_msg").html('').removeClass('error');
      }

      return;
  });
});


$(document).on('click', '.is_profile', function(event) {
    if ($(this).is(':checked')) {
      $('.profile_features').slideDown();

    }else{
      $('.profile_features').slideUp();
    }
    
    
  });


$(function(){
    $(document).on('submit','.submitForm',function(){
       $('.submitBtn').prop('disabled', true);
        var url = $(this).attr('action');
        $.post(url, $(this).serialize(), function(json){
            if (json.st == 1) {
               $('.submitBtn').prop('disabled', false);
                MSG(true,json.msg);
                if(json.url !='' || json.url !=undefined){
                   window.location = json.url;
                }
            }else{
               $('.submitBtn').prop('disabled', false);
                 MSG(false,json.msg);
            }
        },'json');
        return false;
    });
 });


 $(document).on('keyup','#username',function(){
      var val = $(this).val();
      if(val==''){
        return;
      } 

      if(val.length < 3){
        $(".alert_msg").html('name must be 3').removeClass('succes').addClass('error');
        return;
      }

      if(val.match(/\s/g)){
        $(".alert_msg").html('No space allowed').removeClass('succes').addClass('error');
         var newName = val.replace(/\s/g,'');
        $(this).val(newName);
        return;
      }else{
        $(".alert_msg").html('').removeClass('error');
      }

      if(val.match(/[^A-Za-z0-9]/)){
        $(".alert_msg").html('English letter only').removeClass('succes').addClass('error');
         var newName = val.replace(/[^A-Za-z0-9]/g,'');
        $(this).val(newName);
        return;
      }else{
        $(".alert_msg").html('').removeClass('error');
      }
      
      $('.submitBtn').prop('disabled', true);
      $('.register_loader').slideDown();
      var value = encodeURIComponent(val);
        var url =`${base_url}admin/home/check_username/${value}`;
         $.get(url, {'csrf_test_name': csrf_value }, function(json){
          if(json.st == 1){
            setTimeout(function(){ 
              $('.register_loader').slideUp();
              $('.alert_msg').html(json.msg).removeClass('error').addClass('success');
              $('.submitBtn').prop('disabled', false);
            }, 2000);
          }else{
            setTimeout(function(){ 
              $('.register_loader').slideUp();
              $('.alert_msg').html(json.msg).removeClass('succes').addClass('error');
              $('.submitBtn').prop('disabled', true);
            }, 2000);
          }
       },'json');

      return false;
  });



 $(document).on('change','.profileImg_upload',(function(e) {
    e.preventDefault();
    var url = $(this).attr('action');
    var formData = new FormData(this);
    $('.upload_progress').slideDown();
    $.ajax({
        type:'POST',
        url:url,
        data:formData,
        dataType:'json',  
        cache:false,
        contentType: false, 
        processData: false,
        xhr: function () {
          var xhr = new window.XMLHttpRequest();
          xhr.upload.addEventListener("progress", function (evt) {
              if (evt.lengthComputable) {
                  var percentComplete = evt.loaded / evt.total;
                  percentComplete = parseInt(percentComplete * 100);
                  $('.myprogress').text(percentComplete + '%');
                  $('.myprogress').css('width', percentComplete + '%');
              }
          }, false);
          return xhr;
      },
          
        success:function(data){
          if(data.st == 1){
            if(data.type==1){
              $('.uploaded_img').attr('src', base_url+data.img);
            }else{
              $('.coverPhoto.vcardCover').css('background-image', `url(${base_url+data.img})`);
            }
           
           $('.upload_progress').fadeOut();
          }else{
            $('.upload_progress').hide();
            $('#successMessage').html(`<div class="alert alert-danger alert-dismissible custom_alert" id="successMessage">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <h4>
                    <i class="icon fa fa-close"></i>
                    Error!
                </h4>
                ${data.msg}
              </div>`);
          }
          
        },
        error: function(e){
            console.log(e.error);
        }
    });
}));


}(jQuery)); 




