(function($) {
  $(document).ready(function(){
    var secure = (location.protocol == 'http:') ? ""  : ";secure";
    var Cookielaw = {
        createCookie: function (name, value, days) {
            var date = new Date(),
                expires = '';

          if (value){
              if (days) {
                  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                  expires = "; expires=" + date.toGMTString();
              } else {
                  expires = "";
              }
          } 
          else {
              if (days) {
                  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                  expires = "; expires=" + date.toGMTString();
              } else {
                  expires = "";
              }
          }
          
            document.cookie = name + "=" + value + expires + "; path=/" + secure;
        },

        createCookielawCookie: function (value) {
          var cookie_key = $('#cookie_popup_acceptButton').data('cookieKey');
          var json = {};
          // get the batch of options and set them all to false
          $('.cookie_checkboxes input[type="checkbox"]').each(function(){
            json[$(this).val()] = false;
          });

          // Now check the values that actually have been turned on/off.
              $('.cookie_checkboxes input[type="checkbox"]:checked').each(function(){
            json[$(this).val()] = true;
          });

              var objJsonB64 = btoa(JSON.stringify(json))
          this.createCookie(cookie_key, objJsonB64, 365);

            if (typeof (window.jQuery) === 'function') {
                jQuery('#cookie_popup_body').slideUp();
            } else {
                document.getElementById('cookie_popup_body').style.display = 'none';
            }
        }
    };

    $('#cookie_popup_settingsButton').on('click touchend', function(){
      $('#CookiesOptions').modal({
          backdrop: 'static',
          keyboard: false
      })
    });

    $('#cookie_popup_acceptButton, #cookie_model_saveButton').on('click touchend', function(){
      Cookielaw.createCookielawCookie(true);
      location.reload();
    });
  });
})(jQuery);
