(function() {
  /*
  	(c) 2011 Onswipe	
  */  var cookiecutter, devices, iframe, messageListener, supported, cookie_name;
  devices = ['ipad'].join('|');
  cookie_name = "onswipe_redirect";
  supported = (function() {
    return navigator.userAgent.toLowerCase().match(new RegExp(devices)) !== null;
  })();
  if (supported) {
    if (document.referrer.match(/touch.slate.com/)) {
        setCookie (cookie_name, "never", (new Date( new Date().getTime() + (1000 * 60 * 60 * 24 * 30) )).toGMTString(), "/", ".slate.com", false);
    }
    cookiecutter = "http://synapse.onswipe.com/cookie.php?host=" + location.host;
    iframe = document.createElement("IFRAME");
    iframe.src = cookiecutter;
    iframe.name = "cookiemonster";
    iframe.style.display = "none";
    document.head.appendChild(iframe);
    messageListener = function(message) {
      var form, redirect_to, stop_redirect, url, usr;
      if (message.data === "beam-me-up-scotty") {
        redirect_to = "http://synapse.onswipe.com/v1/redirect";
        url = location.href;
        stop_redirect = (function() {
          try {
             if (readCookie("onswipe_redirect")) {
             	return true;
             } else {
	            return false;
	         }
            //return url.match(/onswipe_redirect=(no|never)/)[1] || false;
          } catch (e) {
            return false;
          }
        })();
        if (!stop_redirect) {
          try {
            usr = "slate";
            if (usr != null) {
              redirect_to += "?usr=" + usr + "&url=" + url;
              return window.location = redirect_to;
            }
          } catch (e) {

          }
        } else if (stop_redirect === "never") {
          form = document.createElement('FORM');
          form.action = "" + cookiecutter + "&redirect=never";
          form.target = "cookiemonster";
          form.enctype = "application/x-www-form-urlencoded";
          form.method = "post";
          document.body.appendChild(form);
          return form.submit();
        }
      }
    };
    window.addEventListener("message", messageListener, '*');
  }
}).call(this);
