var decipherCounterTimer = 0;
var decipherT = setInterval(function () {
try {
if (jQuery !== undefined) {
decipherInit(jQuery);
clearInterval(decipherT);
}
decipherCounterTimer ++;
if (decipherCounterTimer > 25) {
clearInterval(decipherT);
}
} catch (noJquery) {}
}, 100);

var decipherInit = function ($j) {
window.decipher = {
style: {
animation: "fadeIn",
animationSettings: "",
compat: "24",
popUnder: true,
newDate: false,
newTime: false,
cookieOnlyOnPop: false,
cookieOnlyOnYes: false,
cookieExpireDaysFrom: 30,
stealthReplacement: false,
yesDisableReplacement: true,
onScriptStartReplacement: false,
onDecipherReadyReplacement: true,
injectReplacement: false,
markup: unescape("\<div\ id\=\"decipherInviteBox\"\ style\=\"display\:none\;\ border\:\ 2px\ outset\ \#335EAC\;\ \ position\:\ absolute\;\ z\-index\:25000\;\ top\:\ 100px\;\ left\:\ 250px\;\ height\:\ 374px\;\ width\:\ 400px\"\>\<script\ type\=\"text\/javascript\"\>var\ yesImg\ \=\ new\ Image\(\)\,\ noImg\ \=\ new\ Image\(\)\,\ neitherImg\ \=\ new\ Image\(\)\;neitherImg\.src\=\"http\:\/\/static\.decipherinc\.com\/survey\/hpg\/hpg11004\/OneTouchIntercept\.png\"\;yesImg\.src\=\"http\:\/\/static\.decipherinc\.com\/survey\/hpg\/hpg11004\/OneTouchIntercept\-yes\.png\"\;noImg\.src\=\"http\:\/\/static\.decipherinc\.com\/survey\/hpg\/hpg11004\/OneTouchIntercept\-no\.png\"\;\<\/script\>\<img\ id\=\"onetouchimg\"\ src\=\"http\:\/\/static\.decipherinc\.com\/survey\/hpg\/hpg11004\/OneTouchIntercept\.png\"\ usemap\=\"\#decipherInviteMap\"\ \ \/\>\<map\ name\=\"decipherInviteMap\"\>\<area\ shape\=\"rect\"\ coords\=\"259\,342\,398\,363\"\ href\=\"http\:\/\/www\.onetouchdiabetes\.com\/privacy\-policy\"\ target\=\"new\"\ alt\=\"Privacy\ Policy\"\ style\=\"cursor\:\ pointer\"\ \/\>\<area\ onmouseout\=\"document\.getElementById\(\'onetouchimg\'\)\.src\=neitherImg\.src\;\"\ onmouseover\=\"document\.getElementById\(\'onetouchimg\'\)\.src\=noImg\.src\;\"\ style\=\"cursor\:pointer\"\ shape\=\"rect\"\ coords\=\"249\,275\,299\,307\"\ onclick\=\"decipher\.hide\(\)\;\"\ \/\>\<area\ onmouseout\=\"document\.getElementById\(\'onetouchimg\'\)\.src\=neitherImg\.src\;\"\ onmouseover\=\"document\.getElementById\(\'onetouchimg\'\)\.src\=yesImg\.src\;\"\ style\=\"cursor\:pointer\"\ shape\=\"rect\"\ coords\=\"306\,274\,368\,306\"\ onclick\=\"decipher\.hide\(\)\;\ decipher\.yesDisable\(\)\;\"\ \/\>\<\/map\>\<\/div\>")
},
survey: "/survey/hpg/hpg11004",
rawUrl: "http://static.decipherinc.com/survey/hpg/hpg11004",
chance: 0.20,  // percent chance that popup appears (e.g.: 0.1==10%)
markup: this.markup,
cookie: {// Cookie settings for intercept cookie
name: "decipher_seen_popup",
value: "set",
path: "/",
expire: ""
},
stealthWindow : function () {
//
// Default if function to overwrite wasn't given
if (!this.style.stealthReplacement) {
var exiturl = "/sites/default/files/stealth.html";
var win2 = window.open(exiturl,'stealth','height=280,width=280,scrollbars=yes,toolbars=no,location=no,resizable=yes,status=no');
win2.screenX = screen.width;
win2.screenY = screen.height;
if (this.style.popUnder) {
win2.blur();
if (!win2 || win2.closed) {
window.focus();
} else {
setTimeout(window.focus, 200);
}
if (win2.focus()) {
window.focus();
}
}
}
},
setCookie: function() {
//
// Set cookie based on configuration from XML
var c=this.cookie;
var now=new Date();
if (this.style.newTime) {
now.setHours();
}
if (this.style.newDate) {
now.setFullYear();
}
if (!this.style.newTime && !this.style.newDate) {
now.setTime(now.getTime()+1000*60*60*24*this.style.cookieExpireDaysFrom);
}
document.cookie=[c.name,"=",escape(c.value),"; expires=",c.expires ? c.expires : now.toGMTString(),c.path ? "; path=" + c.path : ''].join('');
},
getCookie: function(name) {
// Check if cookie is set already
var cookies = document.cookie;
var startAt = cookies.indexOf(name + "=");
if(startAt !== -1) {
startAt = startAt + name.length + 1;
var endAt = cookies.indexOf(";", startAt);
if (endAt == -1) {
endAt = cookies.length;
}
return unescape(cookies.substring(startAt, endAt));
}
return null;
},
getFakeArgs: function() {
// Find decipher script tag on HTML page
var sx, gotUrl, qmIndex;
var rex = new RegExp('http://static.decipherinc.com/apps/intercept/js/hpg/hpg11004');
var matchUrl = function (s) {
if (!s) {
return false;
}
return s.search(rex) >= 0;
};
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
sx = scripts[i];
if (matchUrl(sx.getAttribute('src'))) {
break;
}
}
gotUrl = sx.getAttribute('src');
if (gotUrl.indexOf('#') != -1) {
qmIndex = gotUrl.indexOf('#');
} else if (gotUrl.indexOf('?') != -1) {
qmIndex = gotUrl.indexOf('?');
}
if (qmIndex < 0) {
return '';
}
var ret = gotUrl.slice(qmIndex + 1);
return ret;
},
mergeUrl: function(url, args) {
//
// Get survey url with extra variables
if (url.toString().search('#')){
url = url.split('#')[0];
}
if (args == "") {
return url;
}
var qmIndex = url.indexOf('?');
if (qmIndex < 0) {
return url + '?' + args;
}
return url + '&' + args;
},
yesDisable: function() {
//
// Set intercept cookie
if (!this.style.yesDisableReplacement) {
this.setCookie();
} else {


      this.cookie.name = 'decipher_yes_to_intercept';
      this.setCookie();
      var stealthURL = window.location.protocol + '//' + window.location.host + "/sites/default/files/stealth.html";
      if (this.getFakeArgs().split('http').length == 1) {
          var exiturl = this.mergeUrl(stealthURL, this.getFakeArgs());
      } else {
          var exiturl = stealthURL;
      }
      var win2 = window.open(exiturl,'stealth','height=280,width=280,scrollbars=yes,toolbars=no,location=no,resizable=yes,status=no');
      win2.screenX = screen.width;
      win2.screenY = screen.height;
      if (this.style.popUnder) {
        win2.blur();
      }
      if (!win2 || win2.closed) {
        window.focus();
      } else {
        setTimeout(window.focus, 200);
      }
      if (win2.focus()) {
        window.focus();
      }
    

}
},
checkCookie: function() {
//
// Check if cookie is set or force=1
var fakeArgs = this.getFakeArgs();
var forceSet = fakeArgs.match('force=1');
if(this.getCookie(this.cookie.name) != this.cookie.value || forceSet) {
return false;
} else {
return true;
}
},
checkRandom: function () {
//
// Check if randomness is met
var r = Math.random();
if (r >= (1.0 - this.chance)) {// chance is equal to 1-n
return true;
} else {
return false;
}
},
inject: function () {
//
// Append decipher div(s) to the HTML Body
if (this.style.injectReplacement) {

} else {
var surveyUrl = this.mergeUrl(this.rawUrl, this.getFakeArgs());
var markup = this.style.markup.replace('__SURVEY_URL__', surveyUrl);

if (this.style.cookieOnlyOnPop) {
this.setCookie(this.cookie);
}

// if force=1 add disclaimer to intercept
if ( this.getFakeArgs().match('force=1') && this.style.compat >= 28) {
var msg = "** Forcing intercept to show! This message will go away once you <u>remove</u> 'force=1' from the JavaScript tag. **";
var forceMsg = $j('<div style="color: red; background: #FFF; font-size: 12pt; padding: 5px 5px;">' + msg + '</div>');
$j('body').append($j(markup).append(forceMsg));
} else {
$j('body').append(markup);
}
}
},
onScriptStart: function () {
// Run this code first and run other code for overwrites
if (this.style.onScriptStartReplacement) {

} else {
this.onDecipherReady();
}
},
onDecipherReady: function () {
//
// Default code to run after start
var isCookieSet = this.checkCookie();
var random = this.checkRandom();
this.jQueryVersion = $j().jquery;
if (this.style.onDecipherReadyReplacement) {


      var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
      if (is_chrome) {
          $j('#logged-in-nav').find('a').not(':first').click(function () {
          try {
              if (decipherStealthWindowOpen !== undefined && decipherStealthWindowOpen) {
                  if (decipherStealthWindow !== undefined) {
                      decipherStealthWindow.close();
                  }
              }
          } catch (e) {}
        });
      }
  
      if (isCookieSet) {
        return;
      }
      this.setCookie(this.cookie);
      if (!random){
        return;
      }
      this.inject();
      this.effects();
    

} else {
if (isCookieSet) {
return;
}
if (!random){
return;
}
if (!this.style.cookieOnlyOnPop && !this.style.cookieOnlyOnYes) {
this.setCookie(this.cookie);
}
this.inject();
this.effects();
}
},
effects: function (id, effect, options) {
//
// Pretty animations to show intercept
id = id || '#decipherInviteBox';
effect = this.style.animation;
if(effect === "slideIn") {
$j(id).show();
$j(id).animate({"left": "+="+this.style.animationSettings+"px"}, "slow");
} else {
options = options || "slow";
$j(id).fadeIn(options);
}
},
hide: function (id, effect, options) {
//
// Pretty animations to hide intercept
id = id || '#decipherInviteBox';
effect = effect || "fadeIn";
options = options || "slow";
$j(id).fadeOut(options);
}
};

$j(document).ready(function () {
decipher.onScriptStart();
});
};
