/*
let hdrmask = $('<div/>',{class:'mask'});
$('body > header > .headerColorBar').append(hdrmask);
let frmblock = $('#frmBlock');
let frmblockHt = frmblock.height();
let frmblockpos = (frmblock.offset().top - frmblockHt);
let scrollNum = function(){
  winPos  = window.pageYOffset;
  pc = (window.pageYOffset/frmblockpos);
  hdrmask.css({left:(pc*100)+'%'});
}
$(window).on('scroll',function(){scrollNum()});
*/
//smooth scrolling
$('a[href="#locations"]').on('click', function (evt) {
  evt.preventDefault();
  $('html,body').animate({scrollTop:$('#locations').offset().top}, 500);
});

//locate
var siteid = $('body').attr('data-site');
var zipCookie = "userzip-" + siteid;
var posCookie = "position-" + siteid;
var less_than_unit = "Less than a mile";
var formZip = $('input[name=zipfld]');

formZip.on('keypress',function(e){
  if ((e.keyCode || e.which) == 13) {
    e.stopPropagation();
    e.preventDefault();
    checkValidZip();
  }
});

$('#zipOK').on('click',function(){
  checkValidZip();
});
function checkValidZip(){
  z = formZip.val();
  if(z.length == 5){
    var thisurl = rootloc + 'snippets/bp-latlngzip.php?zip=' + z;
    $.ajax({
      dataType: "text",
      url: thisurl,
      success: function(data){
        if(data.length > 3){
          $.cookie(zipCookie,z,{path : '/'});
          $('#ziperror').text('');
          $('#zipModal').modal('hide');
          $('#locateZip').val(z).trigger('change');
        }else{
          $('#ziperror').text('Unable to find zipcode "'+ z +'"');
        }
      }
    });
  }else{
    $('#ziperror').text('Please enter a five digit zipcode');
  }
}
//console.log("#46:"+typeof $.cookie(zipCookie));
if(typeof $.cookie(zipCookie) == 'undefined'){// || $.cookie(zipCookie).length < 5){
  formZip.val('');
  $('#zipModal').modal({backdrop:'static',keyboard:0});
  if('geolocation' in navigator){
    navigator.geolocation.getCurrentPosition(geosuccess,geoerror);
  };
}else if($.cookie(zipCookie) !== ''){//console.log("#47:"+typeof $.cookie(zipCookie));
  $('#locateZip').val($.cookie(zipCookie));
  var firstload=0;
  $('body').on('dealers-filtered',function(){//triggered in locate
    if(firstload === 0){
      $('#locateZip').trigger('change');
      firstload = 1;
    }
  });
}
$('#locateZip').on('change',function(){
  if($(this).val()!==''){
    $.cookie(zipCookie,$(this).val(),{path : '/'});
  }
});

function geosuccess(position){
  lat  = position.coords.latitude;
  lon = position.coords.longitude;
  var thisurl = rootloc + 'snippets/bp-latlngzip.php?lat=' + lat + '&lng=' + lon;
  $.ajax({
    dataType: "text",
    url: thisurl,
    success: function(data){
      formZip.val( data );
      $.cookie(zipCookie,data,{path : '/'});
      $('#ziperror').text('');
    }
  });
}
function geoerror() {
  $('#ziperror').text('Unable to retrieve your location');
}
$('#zipModal').find('button.close').on('click',function(){
  if($('#zipfld').val()=='') $.cookie(zipCookie,'',{path : '/'});
});
//clear bp events and replace with signal events
$(document).ajaxStop(function() { 
  $(".dealer a").each(function() {
    var onclix = $(this).attr("onclick");
    if(onclix) {
      onclix = onclix.split(";");
      var gaevent = onclix[0], kpievent = onclix[1];
      $(this).attr("onclick",kpievent+";");
    }
  });
  //something particular to shop
  $(".dealer").each(function() {
    var dealername = $(this).find(">h4").text().toLowerCase();
    if(dealername == "somenamehere") {
      
    }
  });
});
//no zip code, has selected make
$("#locateZipBtn").on("click", function() {
  var zipval = $("input#locateZip").val();
  if(zipval == "") {
    console.log("true");
  }
});

