myMoro = {
  iphoneUrl: 'https://mobileroadie.com/app-single?id=highline',
  ipadUrl: 'https://mobileroadie.com/app-single?id=highline',
  androidUrl: 'https://mobileroadie.com/app-single?id=highline',
  appName: 'Highline Ballroom',
  appIcon: 'http://cdn.mblrd.com/i/10-10/ca/aHR0cDovL21vYmlsZXJvYWRpZS5jb20vZmlsZXMvMS91cGxvYWRzL2NjL2NjMjE0MjQ3ZmE0ZTllZWQ1YzUzMzRiNzY5YzAxZTQ0',
  appId: '43077',
  platform: '',
  run: function(type) {
    var platform = false;
    if (navigator.userAgent.match(/iPhone|iPad|iPod|Android/i)) {
      myMoro.platform = platform = navigator.userAgent.match(/iPhone|iPad|iPod|Android/i).toString();
    }
    if (platform && platform.match(/iPhone|iPod/i) && this.iphoneUrl) {
      landingUrl = this.iphoneUrl;
    } else if (platform && platform.match(/iPad/i) && this.ipadUrl) {
      landingUrl = this.ipadUrl;
    } else if (platform && platform.match(/Android/i) && this.androidUrl) {
      landingUrl = this.androidUrl;
    } else {
      return;
    }
        if (this.cookie.get(platform) == 'viewed') {
      return;
    }
    this.cookie.set(platform);
    eval('this.display.' + type + '(landingUrl, platform)');
  },
  display: {
    confirmPopup: function(landingUrl, platform) {
      var confirmed = confirm("Download " + platform + " app?");
      if (confirmed) {
        // setTimeout is used so that back button will be available.
        setTimeout(function(){
          window.location = landingUrl;
        },1000);
      }
    },
    modalPopup: function(landingUrl, platform) {
      var newMeta = document.createElement('meta');
      newMeta.setAttribute("name","viewport");
      newMeta.setAttribute("content","width=device-width, height=device-height, initial-scale = 1.0");
      document.head.insertBefore(newMeta, document.head.childNodes[0]);

      var newStylesheet = document.createElement('link');
      var baseUrl = 'http://www.manageapps.net';
      var isSsl = 'https:' == document.location.protocol;

      if (isSsl && baseUrl.indexOf('https:') === -1) {
        baseUrl = baseUrl.replace('http', 'https');
      }

      newStylesheet.setAttribute("href", baseUrl + "/css/clientmodal.css");
      newStylesheet.setAttribute("type","text/css");
      newStylesheet.setAttribute("rel","stylesheet");
      document.body.appendChild(newStylesheet);

      var modalHtml = escape("<div id='moroOverlay' style='z-index: 999998'></div><div id='moroAppModal' style='z-index: 999999'><div tabindex='-1' id='moroAppInner' style='display: block; outline: 0px none; height: auto; width: 300px; margin: 10px auto;'><a role='moroButton' href='#' onclick='myMoro.display.modalToggle(); return false'><span class='moroClose'>close</span></a><div scrollleft='0' scrolltop='0' style='width: auto; min-height: 49px; height: auto;' id='moroDialog'><p id='moroWeNoticed'>We noticed you're on your <span id='moroPhoneType'></span></p><h2 style='width:100%;'>Would you like to download the<br><span id='moroAppName'></span></h2><div><div id='moroAppIcon'></div><a id='moroLandingUrl' class='moroButton' href='' onclick='myMoro.display.modalToggle(); return true'>Download the App</a></div></div></div></div>");
      var newElement = document.createElement('div');
      newElement.id = 'moroAppModalWrap';
      document.body.appendChild(newElement);
      document.getElementById('moroAppModalWrap').innerHTML = unescape(modalHtml);
      document.getElementById('moroLandingUrl').setAttribute("href", landingUrl); // fix #MRMOBILE-883 and #MRMOBILE-884
      document.getElementById('moroAppName').innerHTML = myMoro.appName + ' App?';
      iconImg = new Image();
      iconImg.src = myMoro.appIcon;
      document.getElementById('moroAppIcon').style.backgroundImage = 'url(' + myMoro.appIcon + ')';
      document.getElementById('moroPhoneType').textContent = myMoro.platform;

      this.modalToggle();
    },
    visitMoWeb: function(cookieContainer) {
      var mowebCount = cookieContainer.cookie.get('moWebClickCount');
      if(mowebCount){
        cookieContainer.cookie.set('lockMoWebRedirect');
      }else{
        this.cookie.unset(platform)
        cookieContainer.cookie.set('moWebClickCount');
      }
    },
    modalToggle: function() {
      var el = document.getElementById("moroAppModalWrap");
      el.style.display = (el.style.display == "block") ? "none" : "block";
    }
  },
  cookie: {
    get: function(platform) {
      var cookieName = 'moroAppDetect' + platform + myMoro.appId + '=';
      var ca = document.cookie.split(';');
      for (var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
          c = c.substring(1, c.length);
        }
        if (c.indexOf(cookieName) == 0) {
          return c.substring(cookieName.length, c.length);
        }
      }
      return null;
    },
    set: function(platform) {
      var date = new Date();
      date.setTime(date.getTime() + (5*365*24*60*60*1000));

      document.cookie = "moroAppDetect" + platform + myMoro.appId + "=viewed; expires=" + date.toGMTString() + "; path=/";
    },
    unset: function(platform) {
      var date = new Date();
      date.setTime(date.getTime() - (122323604800));
      document.cookie = "moroAppDetect" + platform + myMoro.appId + "=; expires=" + date.toGMTString() + "; path=/";
    }
  }
};

// checking DOM readiness (http://www.javascriptkit.com/dhtmltutors/domready.shtml)
var alreadyrunflag = 0 //flag to indicate whether target function has already been run
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", function(){
    alreadyrunflag = 1;
    moroLaunch();
  }, false);
} else if (document.all && !window.opera) {
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
  var contentloadtag=document.getElementById("contentloadtag");
  contentloadtag.onreadystatechange = function(){
    if (this.readyState == "complete") {
      alreadyrunflag = 1
      moroLaunch();
    }
  }
}
window.onload=function(){
  setTimeout("if (!alreadyrunflag) moroLaunch()", 0);
}

function moroLaunch() {
  myMoro.run('modalPopup');
}
