$(document).ready(
   function() {
	   
	   document.onkeydown = KeyPress;
	   
	   
	  
	   
	$('select').niceSelect();

	   

	   
	 $("p,h1,h2,h3,h4,h5,h6,li,dd,dt,a,.breadcrumb").each(function(){
			 var thisCaller = $(this);
			 var myHtml = $(this).html();
			//console.log(myHtml);
			var myNewHtml = myHtml.replace(/(<sup>)?(&reg;|®|&amp;reg;|\u00AE)(<\/sup>)?/gi, '<sup>&reg;</sup>');
			//console.log(myNewHtml);
			
			thisCaller.html(myNewHtml);
	  });
			   
		  
	 
	 var header = $(".topSection");
	 var menu = $("ul#menu-primary");
	 var menuUtility = $("ul#menu-utility");
	 $(window).scroll(function() {
		 var scroll = $(window).scrollTop();
		 
		 if (scroll >= 200) {
			 header.addClass("stuck");
			 menu.addClass("stuck");
			 menuUtility.addClass("stuck");
		 } else {
			 header.removeClass("stuck");
			 menu.removeClass("stuck");
			 menuUtility.removeClass("stuck");
		 }
	 });

	 $('input#toggleAnimation:checkbox').change(
	 function(){
		 if ($(this).is(':checked')) {
			//console.log("Checked!");
			 $('video').each(function() {
				 $(this).get(0).play();
			 });
		 }
		
		else { 
			//console.log("Unchecked!");
			$('video').each(function() {
				$(this).get(0).pause();
			});
		}
	 });

	 if($('.owl-carousel.owl-carousel-bottom').length > 0 ){
			 console.log("run");
			$(".owl-carousel.owl-carousel-bottom").owlCarousel({
				dots: false,
				autoplay: true,
				loop:true,
				margin: 30,
				responsiveClass:true,
				navText : ['<i class="fas fa-angle-left" aria-hidden="true"></i>','<i class="fas fa-angle-right" aria-hidden="true"></i>'],
				nav: true,
				loop: true,
				responsive:{
					0:{
						items:1

					},
					
					300:{
						items:2

					},
					
					600:{
						items:3
					},
					1000:{
						items:5
					}
				}

			});
		}


	new WOW().init();
	
	
	
	
	
	var docWidth = document.documentElement.offsetWidth;
	
	[].forEach.call(
	  document.querySelectorAll('*'),
	  function(el) {
		if (el.offsetWidth > docWidth) {
		  //console.log("Offset: " + el.outerHTML);
		  //alert("Offset: " + el.outerHTML);
		}
	  }
	);

	var wpcf7Elms = document.querySelectorAll('.wpcf7:not(.useDefaultc7)');

wpcf7Elms.forEach(function(wpcf7Elm) {
    wpcf7Elm.addEventListener('wpcf7mailsent', function(event) {
		console.log(event);
		autoHideMessage=0;
		currentModalForm=event.detail.unitTag;
		console.log('#'+event.detail.unitTag+' .wpcf7-response-output');
		$('#'+event.detail.unitTag+' .wpcf7-response-output').show();
		 $(".ezmodal").ezmodal("hide");
	
		var $element = $('#'+event.detail.unitTag+' .wpcf7-response-output');
		console.log("Starting timer");
    var timer = setTimeout(function() {
        $element.hide();
		autoHideMessage=1;
		console.log("Timer code called");
	}, 3000);
        
      
    }, false);

	wpcf7Elm.addEventListener('wpcf7invalid', function(event) { 
			autoHideMessage=1;
			currentModalForm=event.detail.unitTag;
			console.log('#'+event.detail.unitTag+' .wpcf7-response-output');
			$('#'+event.detail.unitTag+' .wpcf7-response-output').show();
			
		 }, false);

	wpcf7Elm.addEventListener('wpcf7mailfailed', function(event) { 
			autoHideMessage=1;
			currentModalForm=event.detail.unitTag;
			console.log('#'+event.detail.unitTag+' .wpcf7-response-output');
			$('#'+event.detail.unitTag+' .wpcf7-response-output').show();
		 }, false);
});

$('.ezmodal').ezmodal({
        'onClose': function () {
             console.log("Modal Closed");
			//  console.log("Hide: "+autoHideMessage);
			 if(autoHideMessage)
			{
				console.log("Hide: "+autoHideMessage);
				console.log('#'+currentModalForm+' .wpcf7-response-output');
				if(currentModalForm!="")
				{
					$('#'+currentModalForm+' .wpcf7-response-output').hide();
				}
			}
			
        }
    });

	
   

	
	}
);

var autoHideMessage=1;

var currentModalForm="";

function KeyPress(e) {
	  var evtobj = window.event? event : e
	  if (evtobj.keyCode == 90 && evtobj.ctrlKey) {
		  $( "#wpadminbar" ).toggle();
	  }
}

// Get the button:
let mybutton = document.getElementById("btt");

// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
	//mybutton.style.display = "block";
  } else {
	//mybutton.style.display = "none";
  }
}
 
function topFunction() {
	document.body.scrollTop = 0; // For Safari
	document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}





function showAllNewMakesOptions(strMake) {
	console.log(strMake);
	console.log("showAllNewMakesOptions");
	var optionsToShow = ["0", "Sea Ray", "EdgeWater", "Cobia", "Azimut", "Jupiter"];
	 $('#makes').next('.nice-select').find('li.option').each(function() {
      var optionValue = $(this).attr('data-value');
	  //console.log(optionValue);
      if(optionsToShow.indexOf(optionValue) !== -1) {
        $(this).show();
		//console.log(optionValue);
		//console.log(strMake);
      } else {
		  //console.log(optionValue);
		  //console.log(strMake);
        $(this).hide();
      }
    });
	if (!optionsToShow.includes(strMake)) {
		$('#makes').next('.nice-select').find('.option').first().addClass('selected').trigger('click');
	}
    $('select').niceSelect('update');
	//$('select').niceSelect('update');
    }

 function showAllMakesOptions(strMake) {
	 console.log("showAllMakesOptions");
	 //console.log(strMake);
     $('#makes option').show();
     $('select').niceSelect('update');
    }

function imageExists(image_url){
	try {

	var http = new XMLHttpRequest();

	http.open('HEAD', image_url, false);
	http.send();

	return http.status != 404;
	 } catch (error) {
		// Handle the error gracefully
		console.error(image_url+" An error occurred while checking the image existence:", error);
		return false; // Return false indicating image does not exist
	}

}

function setSelectedBoat(fieldtoUpdate,boatId,boatName)
{

console.log(fieldtoUpdate);
console.log(boatId);
console.log(boatName);
SelectedBoatId=boatId;
SelectedBoat=boatName;
document.getElementById(fieldtoUpdate).value=SelectedBoat;
document.getElementById("comment").value=SelectedBoat;
document.getElementById("commenttrade").value=SelectedBoat;
$('select').niceSelect();


}

var SelectedBoat="";
var grecaptcharesponse="";
function escapeHtml(text) {
  var map = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#039;'
  };

  return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}

Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))), 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };

function cnull(theval)
{
	if(theval)
	{
		return theval;
	}
	else
	{
		return "";
	}
}

function cnullengfuel(theval)
{
	//alert("here");
	if(theval.Engines)
	{
		return (theval.Engines[0].Fuel)?theval.Engines[0].Fuel:"";
		
	}
	else
	{
		return "";
	}
}

function cnullengtype(theval)
{
	if(theval.Engines)
	{
		if(theval.Engines[0].PropellerType)
		{
		return (theval.Engines[0].Type)?theval.Engines[0].Type+ " "+ theval.Engines[0].PropellerType:"";
		}
		else
		{
		return (theval.Engines[0].Type)?theval.Engines[0].Type:"";
		}
		
	}
	else
	{
		return "";
	}
}

function cnullengpower(theval)
{
	if(theval.Engines)
	{
		return (theval.Engines[0].EnginePower)?theval.Engines[0].EnginePower:"";
	}
	else
	{
		return "";
	}
}

function cnullengmodel(theval)
{
	if(theval.Engines)
	{
		
			return (theval.Engines[0].Make)?theval.Engines[0].Make+" "+theval.Engines[0].Model:"";
		
	}
	else
	{
		return "";
	}
}

function cnullloc(theval)
{
	if(theval)
	{
		if(theval=="Unknown")
		{
			return "";
		}
		else
		{
			return theval;
		}
	}
	else
	{
		return "";
	}
}

function getValue(field)
{
return document.getElementById(field).value;
}

function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    return elt.options[elt.selectedIndex].text;
}

function setValue(field,data)
{
	if(data!=undefined)
	{
		document.getElementById(field).value=data;
	}
	else
	{
		document.getElementById(field).value="";
	}
}

function resetdropdown(field)
{
	var dd = document.getElementById(field);
	dd.selectedIndex = 0;
	 $('select').niceSelect('update');
}

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}


function addunitmeasure(thevalue,theunit)
	{
		if(thevalue)
		{
			if(thevalue!="")
			{
				return thevalue + theunit;
			}
			else
			{
				return "";
			}
		}
		else
		{
			return "";
		}
	}


