$=jQuery;
var SITE = {}
SITE.Dom = {
   pauseMedia: function(){
   }
}
SITE.Registration = {
   ready: function(){
       //REGISTRATION
      if($('.page-id-4298').length){  
        var urlParams = new URLSearchParams(window.location.search);
         var val = urlParams.get('hayurl')
         if(val){
            $('#user_login_7-4290').val('https://haymap.com/' + val);
         }      
      }
   }
}
SITE.Events = {
   ready: function(){
	   if($('div#um_field_4290_user_login_7').length){
		   var surl = $('div#user_login_7-4290').val(); 
		   if(!surl){
			   surl = 'https://haymap.com/';
		   }
		   $('div#um_field_4290_user_login_7').attr('data-value', surl);  
		   $('body').on('input', '#user_login_7-4290', function(){
			   var $this = $(this);
			   $('div#um_field_4290_user_login_7').attr('data-value', 'http://haymap.com/'+$this.val()); 
		   });
		  
	   }
	   $('body').on('click', '.unpublish-hc-listing', function(o){
		   		o.preventDefault();
		   		var $this = $(this);
				var url = $this.find('a').attr('href');
				var nonce = $('#nvn').attr('data-did');
		   		var $section = $this.closest('.listingwrap');   
				console.info('url='+url);
				$.ajax({
					beforeSend: (xhr) => {
						//this picks up value set in functions.php to allow authentication to be passed through with function so WP knows to allow deletion.

						xhr.setRequestHeader('X-WP-Nonce', haymap_data.nonce);
					},
					url: url, //fixed value whilst testing
					type: 'GET',
					success: (response) => {
						if(response.listing_status === 'Sold Out'){
							console.info('yes');  
							$this.addClass('publish');  
							$this.find('.elementor-button-text').text('PUBLISH');
							$section.addClass('unpublished');
							$section.removeClass('published');  
						}else{
							$this.addClass('unpublish');  
							$this.find('.elementor-button-text').text('UNPUBLISH');
							$section.addClass('published');
							$section.removeClass('unpublished'); 
						}
					},
					error: (response) => {
							
					}
				});
				
	   });
	   $('body').on('click', '.publish-hc-listing', function(o){
		   		o.preventDefault();
		   		var $this = $(this);
				var url = $this.find('a').attr('href');
				var nonce = $('#nvn').attr('data-did');
				console.info('url='+url);
				$.ajax({
					beforeSend: (xhr) => {
						//this picks up value set in functions.php to allow authentication to be passed through with function so WP knows to allow deletion.

						xhr.setRequestHeader('X-WP-Nonce', haymap_data.nonce);
					},
					url: url, //fixed value whilst testing
					type: 'GET',
					success: (response) => {
						$this.html('Publish');
						if(response.status === 'success'){
							$this.addClass('publish');  
							$this.find('a').html('Publish');
						}
					},
					error: (response) => {
							
					}
				});
				
	   });
	   $('body').on('click', '.edit-hc-listing', function(){
		   		var $this = $(this);
				var id = $this.attr('data-post-id');
				var nonce = $('#nvn').attr('data-did');
				
				$.ajax({
					beforeSend: (xhr) => {
						//this picks up value set in functions.php to allow authentication to be passed through with function so WP knows to allow deletion.

						xhr.setRequestHeader('X-WP-Nonce', haymap_data.nonce);
					},
					url: "/wp-json/haymap/v1/haymap_api_get_listing?hc="+id, //fixed value whilst testing
					type: 'GET',
					success: (response) => {
						
						if(response.status === 'success'){
							console.info(status);
							$('#form-field-lisid').val(response.pid);
							$('#form-field-edithaytype').val(response.hay_type);
							$('#form-field-edithaytype2').val(response.hay_type2);
							$('#form-field-edithayquality').val(response.quality);
							$('#form-field-editpriceper').val(response.price);
							$('#form-field-editbaletype').val(response.bale_type);
							$('#form-field-editbalesize').val(response.bale_size);
							$('#form-field-editqty').val(response.quantity_available);
							$('#form-field-editsoldby').val(response.hay_type);
							$('#form-field-editpublishingoptions').val(response.post_status);
							$('#form-field-editsoldby').val(response.soldbybaleorton);
							$('#form-field-editaddress').val(response.address);
							$('#form-field-editcity').val(response.city);
							$('#form-field-editstate').val(response.state);
							$('#form-field-editzip').val(response.zip);
							$('#elistingImg .elementor-widget-container').css('background', 'url('+response.image+') no-repeat center center / cover');
							$('#edit_hay_listing_wraper').addClass('active');
						}
					},
					error: (response) => {
							
					}
				});
				
		});
	  
	   $('#addListingMessage.locked *').on('click', function(){
		 		Swal.fire({
					   icon: 'warning',
					   title: 'Verify Your Account.',
					   text: "To add listing and take advantage of other features, you must verify your account first.",
				   });   
	   });

	    $('body').on('click', '#verify_mobile button.elementor-button.elementor-size-sm.e-form__buttons__wrapper__button.e-form__buttons__wrapper__button-next', function(){
				var val = $('#form-field-mobilev').val();
				var nonce = $('#nvn').attr('data-did');
			
			$.ajax({
				beforeSend: (xhr) => {
					//this picks up value set in functions.php to allow authentication to be passed through with function so WP knows to allow deletion.

					xhr.setRequestHeader('X-WP-Nonce', haymap_data.nonce);
				},
				url: "/wp-json/haymap/v1/haymap_api_send_code?m="+val, //fixed value whilst testing
				type: 'GET',
				success: (response) => {
					if(response.success === 'success'){
					}else{						
							Swal.fire({
						   icon: 'warning',
						   title: 'Oops...',
						   text: response.message,
				   		}); 
						
						setTimeout(function(){
							$('#verify_mobile button.elementor-button.elementor-size-sm.e-form__buttons__wrapper__button.e-form__buttons__wrapper__button-next').trigger('click');
						}, 500);
					}
				},
				error: (response) => {
						$('#verify_mobile button.elementor-button.elementor-size-sm.e-form__buttons__wrapper__button.e-form__buttons__wrapper__button-previous').trigger('click');
						Swal.fire({
						   icon: 'warning',
						   title: 'Oops...',
						   text: 'There has been an error. Please try again.',
				   		}); 
				}
			});
			
				
		});
	  
	   jQuery( '.contact-seller' ).on( 'click' , function (o) {
		  	o.preventDefault();  
        	var s = $(this).attr('data-id');
		   // /wp-json/haymap/v1/haymap_api_contact_attempts
		   $.get( "/wp-json/haymap/v1/haymap_api_contact_attempts?pid="+s, function( data ) {
                 //$( "#donateModal" ).removeClass('processing'); 
                 $('#btDonation').prop('disabled',false);
                
                 if(data.allowed==='yes'){
                     console.log( 'allowed' ); // John
                     console.log( data ); // John
					 // elementor-hidden-tablet elementor-hidden-phone
            		 $('#sellerInfo').removeClass('elementor-hidden-desktop');
					 $('#sellerInfo').removeClass('elementor-hidden-tablet');
					 $('#sellerInfo').removeClass('elementor-hidden-phone');
                 }else{
                    console.log( 'error' ); // John
                    console.log( data ); // John
              		alert("Too many contacts attepted today, try back tomorrow.");
                 }
              
            }, "json");  
      } );
      jQuery( '#btnsearch' ).on( 'click' , function (o) {
		  	o.preventDefault();  
        	var s = $('#form-field-s').val();
		  	var post_type = $('#form-field-post_type').val();
		   	if(post_type == '-Select Category-'){
			    window.location.href = '/?s=' + s;
		   	}else{
		    	window.location.href = '/?s=' + s + '&post_type=' + post_type;
			}
      } );
    
      
	  jQuery( '#form-field-post_type' ).on( 'change' , function (o) {
		  var nval;
		  var val = $(this).val();
		   switch(val){
			   case 'hay_for_sale':
				   nval = 'search hay type, bale type, city or state';
				   break;
			   case 'equipment_for_sale':
				   nval = 'search equipment type, manufacturer, city or state';
				   break;
			   case 'service_listings':
				   nval = 'search service type, city or state';
				   break;
			   default:
				   nval = 'search hay type, bale type, city or state';
		   }
        	$('#form-field-s').attr('placeholder', nval);
      } ); 
      
      jQuery( '#zipToggle' ).on( 'click' , function (o) {
         o.preventDefault();  
        	$('#zipSetWrap').toggleClass('active');
         
      } ); 
      
      
   },
   load: function(){
      jQuery( document ).on( 'elementor/popup/show' , function ( event, id, instanc) {
         if(id==1192){
            $('body').addClass('haspopup'); 
         }
      } );
       jQuery( document ).on( 'elementor/popup/hide' , function ( event, id, instanc) {
         $('body').removeClass('haspopup'); 
      } );
      
    /* document.addEventListener( 'elementor/popup/show', ( event, id, instance ) => {
        console.info('pop');
         $('body').addClass('haspopup');  
     }); */
   },
   scroll: function(){
      
   }
}
$(document).ready(function(){
   SITE.Events.ready();
   SITE.Registration.ready();  
});
$(window).scroll(function(){
   SITE.Events.scroll();
});

$(window).load(function(){
   SITE.Events.load();
});