/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
(function ($) {
  $(document).ready(function () {
    //ajax call url and nonce 
    var AjaxURL =  egpr_admin_params.ajax_url;
    var AjaxNonce =  egpr_admin_params.ajax_nonce;

    /* 
    * Case 1: Tab Settings
    */
    $('ul.egpr-nav-tabs li a').click(function(){
        var tab_id = $(this).attr('data-tab');
        $('ul.egpr-nav-tabs li').removeClass('current');
        $('.egpr-tab-content').hide().removeClass('current');
        $(this).parent().addClass('current');
        $("#"+tab_id).fadeIn('300').addClass('current');
        if(tab_id == "aboutus" || tab_id == "howtouse" || tab_id == "shortcode_usage"){
          $('.egpr-form-actions-wrap').hide();
        }else{
           $('.egpr-form-actions-wrap').show();
        }
     });
     
     /*
      * Account API Type 
      */
    $('body').on('change', '#account_type', function () {
         var acctype = $(this).val();
         if(acctype != ''){
             if(acctype == "verified"){
                 $('.verified_baccount').removeClass('hide_tmp');
                 $('.nonverified_baccount').addClass('hide_tmp');
             }else{
                 $('.verified_baccount').addClass('hide_tmp');
                 $('.nonverified_baccount').removeClass('hide_tmp');
             }
         }else{
             $('.verified_baccount').addClass('hide_tmp');
             $('.nonverified_baccount').addClass('hide_tmp');
         }
         
     });
     /*
      * Floating Review Template Type
      */

     $('body').on('change', '#floating_review_layout', function () {
         var review_layout = $(this).val();
         $('.review_preview').addClass('hide_tmp');
         $('#floating_'+review_layout).removeClass('hide_tmp');
     });
     
       /*
       * Display specific pages options 
       */
        $('body').on('change', '#egpr_freview_display', function () {
            if ($(this).val() == 'specific_pages') {
                $('.egpr-option-select-specific-page').slideDown(200);
            } else {
                $('.egpr-option-select-specific-page').slideUp(200);
            }
        });
        
        $(window).load(function ()
            {
                if($('body').hasClass('post-new-php') || $('body').hasClass('post-php')){
                    if($('body').hasClass('post-type-egprreviews')){
                        initMap();  
                    }
                }
             
            });
            

     var edit_placeid = $('#egpr_meta_place_id').val();
    // This function is called when the user clicks the UI button requesting
      function servicesPlaceId(request,service,map,infowindow){
          service.getDetails(request, function(place, status) {
              if (status === google.maps.places.PlacesServiceStatus.OK) {
                var marker = new google.maps.Marker({
                  map: map,
                  position: place.geometry.location
                });
                marker.setVisible(true);
                   infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
                    'Place ID: ' + place.place_id + '<br>' +
                    place.formatted_address + '</div>');
                   infowindow.open(map, marker);
              }
            });
    }
      // This sample uses the Place Autocomplete widget to allow the user to search
      // for and select a place. The sample then displays an info window containing
      // the place ID and other information about the place that the user has
      // selected.

      // This example requires the Places library. Include the libraries=places
      // parameter when you first load the API. For example:
      // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -30.8688, lng: 151.2195},
         // center: {lat: 27.7172, lng: 85.3240},
          zoom: 13
        });

         var infowindow = new google.maps.InfoWindow();
         var service = new google.maps.places.PlacesService(map);
         if(edit_placeid != ''){
             var request = {
                placeId: edit_placeid
              };
             servicesPlaceId(request,service,map,infowindow);
         }

        var input = document.getElementById('egpr-pac-input');
        console.log(input);

        var autocomplete = new google.maps.places.Autocomplete(input);
        console.log(autocomplete);
        autocomplete.bindTo('bounds', map);

        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

        var infowindow = new google.maps.InfoWindow();
        var infowindowContent = document.getElementById('infowindow-content');
        infowindow.setContent(infowindowContent);
        var marker = new google.maps.Marker({
          map: map
        });
        marker.addListener('click', function() {
          infowindow.open(map, marker);
        });

        autocomplete.addListener('place_changed', function() {
          infowindow.close();
          var place = autocomplete.getPlace();
          if (!place.geometry) {
            return;
          }

          if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
          } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);
          }

          // Set the position of the marker using the place ID and location.
          marker.setPlace({
            placeId: place.place_id,
            location: place.geometry.location
          });
          marker.setVisible(true);
         // console.log(place.geometry.location.lat());
          //console.log(place.geometry.location.lng());
          $('#infowindow-content').css('display','block');
          infowindowContent.children['place-name'].textContent = place.name;
          infowindowContent.children['place-id'].textContent = place.place_id;
          $('#egpr_meta_location_name').val(place.name);          
          $('#egpr_meta_place_id').val(place.place_id);
          infowindowContent.children['place-address'].textContent = place.formatted_address;
          $('#egpr_meta_formated_address').val(place.formatted_address);
          $('#egpr_meta_phone_no').val(place.formatted_phone_number);
          infowindow.open(map, marker);
        });

        var input = document.getElementById('egpr-pac-input');
          google.maps.event.addDomListener(input, 'keydown', function(event) { 
          if (event.keyCode === 13) { 
            event.preventDefault(); 
        }
        }); 
     }
     
     /*
      * Post type Metabox : Review Template Preview 
     */
       $('.egpr-template-selection').on('change', function() {
            var template_value = $(this).val();
            $('.egpr-temp-common').hide();
            $('#egpr-demo-' + template_value).show();
        });
        var inline_view = $(".egpr-template-selection option:selected").val();
        $('.egpr-temp-common').hide();
        $('#egpr-demo-' + inline_view).show();
        
    /*
      * Post type Metabox : Badge Template Preview 
     */
       $('.egpr-badge-selection').on('change', function() {
            var template_value = $(this).val();
            $('.egpr-temp-common-badge').hide();
            $('#egpr-demobadge-' + template_value).show();
        });
        var inline_view3 = $(".egpr-badge-selection option:selected").val();
        $('.egpr-temp-common-badge').hide();
        $('#egpr-demobadge-' + inline_view3).show();
        
          /*
      * Post type Metabox : Header Template Preview 
     */
       $('.egpr-header-selection').on('change', function() {
            var template_value = $(this).val();
            $('.egpr-temp-common-header').hide();
            $('#egpr-headerdemo-' + template_value).show();
        });
        var inline_view2 = $(".egpr-header-selection option:selected").val();
        $('.egpr-temp-common-header').hide();
        $('#egpr-headerdemo-' + inline_view2).show();
        
        
        /**
         * Layout type show & hide configuration
         *
         */
        $(".egpr-layout-type").change(function() {
           if ($(this).val() === "list")
            {
               $('.egpr-slider-setting-wrapper').slideUp('slow');
            }
            else if($(this).val() === "slider")
            {
               $('.egpr-slider-setting-wrapper').slideDown('slow');
            }
            else
            {
              $('.egpr-slider-setting-wrapper').hide();
            }
        });
        var selected_layout_type = $(".egpr-layout-type option:selected").val();
        if (selected_layout_type === "list")
        {
             $('.egpr-slider-setting-wrapper').hide();
        }else if(selected_layout_type === "slider"){
             $('.egpr-slider-setting-wrapper').show();
        }else{
             $('.egpr-slider-setting-wrapper').hide();
        }
        
        $('body').on('click', '.egpr-slider-toogle-outer-wrap', function() {
            $(this).closest('.egpr-layout-setting-wrap').find('.egpr-slider-setting-inner-wrap').slideToggle();
            $(this).find('.dashicons').toggleClass('dashicons-arrow-down dashicons-arrow-up');
        });
       
        $('.egpr-color-picker').wpColorPicker();
        
          $('#egpr_meta_custom_enable').click(function() {
             $('.egpr-show-custom-options').slideToggle();
          });
          
           $('#egpr_meta_show_badge').click(function() {
             $('.egpr-show-badge-options').slideToggle();
          });
           $('#egpr_meta_show_header').click(function() {
             $('.egpr-show-header-options').slideToggle();
          });
            $('#egpr_meta_show_reviews').click(function() {
             $('.egpr-show-reviews-options').slideToggle();
          });
          
          
          $('.egpr_slider_pager_type').change(function(){
             $('.pager_type_preview').slideToggle(); 
          });
          
        $(".slider_controls_type").change(function() {
           if ($(this).val() === "arrow")
            {
               $('.arrowcolor_hide').slideDown('slow');
            }
            else
            {
              $('.arrowcolor_hide').slideUp('slow');
            }
        });
         $(".slider_controls_show").change(function() {
           if ($(this).val() === "true")
            {
               $('.controls_type_preview').slideDown('slow');
            }
            else
            {
              $('.controls_type_preview').slideUp('slow');
            }
        });
          
          
    $('.egpr-shortcode-wrap,.egpr-shortcode-display-value').click(function(){
        $(this).focus();
        $(this).select();
        document.execCommand('copy');
        $(this).siblings('.egpr-copied-info').show().delay(1000).fadeOut();
    });
     $('.egpr-shortcode-wrap2,.egpr-shortcode-display-value2').click(function(){
        $(this).focus();
        $(this).select();
        document.execCommand('copy');
        $(this).siblings('.egpr-copied-info2').show().delay(1000).fadeOut();
    });
    
      $('.egpr-review-csettings').children('.egpr-custom-header').click(function () {
          $(".default-reviews-settings").not($(this).toggleClass('egpr-open-menu-list').next('.default-reviews-settings').slideToggle(300)).hide(300);
            $(this).find('i').toggleClass('fa-sort-down fa-short-up');
        });    
        
         $('.rate_us_settings').children('.egpr-custom-header').click(function () {
          $(".default-rateus-settings").not($(this).toggleClass('egpr-open-menu-list').next('.default-rateus-settings').slideToggle(300)).hide(300);
          $(this).find('i').toggleClass('fa-sort-down fa-short-up');
        });

        $('.viewallreviews_settings').children('.egpr-custom-header').click(function () {
          $(".default-viewall-settings").not($(this).toggleClass('egpr-open-menu-list').next('.default-viewall-settings').slideToggle(300)).hide(300);
          $(this).find('i').toggleClass('fa-sort-down fa-short-up');
        });
         
         $('.slider_custom_settings').children('.egpr-custom-header').click(function () {
          $(".default-slider-settings").not($(this).toggleClass('egpr-open-menu-list').next('.default-slider-settings').slideToggle(300)).hide(300);
          $(this).find('i').toggleClass('fa-sort-down fa-short-up');
        });  

      /*
      * Google Font Switching
      */
      $('#egpr_meta_reviewer_font_family').change(function () {
          var label_font = $(this).val();
          $("#egpr-label-font-style").html('');
          $("#egpr-label-font-style").html('#egpr-label-font-preview {font-family: "' + label_font + '" !important; }');      
          if(label_font != "default" && label_font != ''){
          WebFont.load({
              google: {
                  families: [label_font]
              }
          });
          } 
      });

      var fontfamily = $( "#egpr_meta_reviewer_font_family option:selected" ).val();
      if(typeof fontfamily != 'undefined'){
          $("#egpr-label-font-style").html('#egpr-label-font-preview {font-family: "' + fontfamily + '" !important; }');      
         if(fontfamily != "default" && fontfamily != ''){
         WebFont.load({
                google: {
                    families: [fontfamily]
                }
            });
         }
      }
     $("#egpr_meta_enable_banimation").change(function() {
        if($(this).prop('checked') == true) {
                $('.epgr-badge-animation').slideDown('slow');
        } else {
               $('.epgr-badge-animation').slideUp('slow');
        }
    });

      if($('#egpr_meta_enable_banimation').attr('checked')) {
       $('.epgr-badge-animation').show();
     } else {
       $('.epgr-badge-animation').hide();
     }

     $("#egpr_meta_enable_ranimation").change(function() {
        if($(this).prop('checked') == true) {
                $('.epgr-review-animation').slideDown('slow');
        } else {
               $('.epgr-review-animation').slideUp('slow');
        }
    });

     if($('#egpr_meta_enable_ranimation').attr('checked')) {
       $('.epgr-review-animation').show();
     } else {
       $('.epgr-review-animation').hide();
     }

     $(".egpr-link-type").change(function() {
           if ($(this).val() === "default")
            {
               $('.egpr-custom-link-rateus').slideUp('slow');
            }
            else
            {
               $('.egpr-custom-link-rateus').slideDown('slow');
            }
           
        });
        var link_type = $(".egpr-link-type option:selected").val();
        if (link_type === "default")
        {
            $('.egpr-custom-link-rateus').hide();
        }else{
              $('.egpr-custom-link-rateus').show();
        }

  });//$(function () end
}(jQuery));


