//initialize modalShown
var modalShown = false;
var exitPopupCookieLifeSpan = 14; //days
var modalShownCookieLifeSpan = 1; //days
var modalShownCookieName = 'modalShown';

if (typeof setCookie == 'undefined') {
	function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+ d.toUTCString();
		document.cookie = cname + "=" + cvalue + ";" + expires + ";domain=.mauldineconomics.com;path=/";
	}
}
    
if (typeof getCookie == 'undefined') {
	function getCookie(cname) {
          var name = cname + "=";
          var decodedCookie = decodeURIComponent(document.cookie);
          var ca = decodedCookie.split(';');
          for(var i in ca) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
              c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
              return c.substring(name.length, c.length);
            }
          }
          return "";
    }
}

function showExitPopup() {
	var exitPopupFormName = $('#exitpopup').find('form').data('mautic-form');
  	// site go small version
  	$('#sitewrapper').addClass('modalview animate');

  	// Show the exit popup
    $('#exitpopup_bg').fadeIn();
	$('#exitpopup').fadeIn();	

	modalShown = true;
	setCookie(exitPopupFormName, 1, exitPopupCookieLifeSpan);
    setCookie(modalShownCookieName, 1, modalShownCookieLifeSpan);
}
  
function hideExitPopup() {
  	// Hide the exit popup
  	$('#exitpopup_bg').fadeOut();
    $('#exitpopup').fadeOut();
		
	// site go normal version
	$('#sitewrapper').removeClass('modalview animate');
}

$(document).ready(function() {
  var exitPopupFormName = $('#exitpopup').find('form').data('mautic-form');
  //reset modalShown based on cookie presence
  modalShown = getCookie(exitPopupFormName) != '' || getCookie(modalShownCookieName) != '';
  
  $(document).mousemove(function(e) {
   // $('#exitpopup').css('left', (window.innerWidth/2 - $('#exitpopup').width()/2));
   // $('#exitpopup').css('top', (window.innerHeight/4 - $('#exitpopup').height()/4));

    if(e.clientY <= 5 && !modalShown) {showExitPopup();}
  });

  $('#exitpopup_bg').click(hideExitPopup);

  $('a[data-dismiss=modal]').on('click', hideExitPopup);

});