(function(doc,navigator){

  if( !doc.querySelector ){ return; }

  window.app_published = {};

  var doc_head = doc.head || doc.getElementsByTagName('head')[0],
      url = "http://appsmakerstore.com/appm/iqatjxvq2ty8jf",
      is_showing_app =  false,
      app_color = "50d6fc",
      app_main_color = "50d6fc",
      app_color_brightness = "light",
      app_text_color = "#205665",
      app_main_text_color = "#205665",
      app_name = "Гостиница Астра",
        app_icon = "https://az668752.vo.msecnd.net/images/apps-appicon/000/404/153/404153-ipad_small.png?1481880352",
      published_data = {"android":{"is_published":true,"market_link":"https://play.google.com/store/apps/details?id=com.appsmakerstore.appAstra"}},
      number_of_times_to_dismiss = 1,
      storage_prefix = "mobile_app_banner_iqatjxvq2ty8jf_";

  var close_image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAUCAIAAADtKeFkAAACI0lEQVR4AX2T6XLaQBCEef/XSRwOm0M3uoUObCQBxsQJLmKrcKIrBeX0eksTQine0h+W+Xpnens7b+8rjJJPXUGdenlRvv1/PWeH0URH5XL1wHcY70cxtq664peeNDVmRZsEh4eCgbLPPVa5Wm8Zv0jvAbOtgdy7Ua6HmmEHXKIVvupL3WulP1RR+fB114mTe8C9gTwYaSPRnMjWWDItNyrKqhUe3KgYQZAsVG4fd508LzTdgxgwRfPQP34Kim17cy6R/QuPBUNUHdQk6eZ4PLL587w0nVDSXN30DSfU7QAqguLAl2+7vay6aJBgWXV0c5aut6fTifvHFgb2g4VuBRBC81xCUhy0ClMwHQYmeNXADd9IBPME5oE33choJIANxzqsIfjYwMSTRAUJHA7eaiQUzQUJawDj2gm+5EkCWeISvAvd8uELRqOTP+IPh5+WHarGjLqAI9wU3HRd//6Iz7JXUWWesRsyZ8Y7dv4t4lVV1+38S/Y6Ec1uX+rzLOgetUAfxonmaVnVlzxOHoksJIwfcd5t5fHGXP+uLCviOWxSwpA2UbPR/2rz+CsvMDbxMAI3gumc2S2X6BBMCZMUG/GMlyyeqEDd7d0SJJegXNgek8D7WXOY4nkOY3GJqIkWSYiyw95futzgMSOksB0w/riAKZ14DucSuOPvTz/Y/LfxejieoiXAaQvcLvG0f/nrP4KJhCYpwe0rxxsJmcT+OeM7fwAKr0kjJDMGKgAAAABJRU5ErkJggg==';


  // fix viewport
  var meta = doc.querySelector( "meta[name=viewport]" ),
      initialContent = "",
      disabledZoom = "width=device-width, initial-scale=1.0";

  if( meta ){
    initialContent = meta.getAttribute("content");
  }else{
    meta = doc.createElement("meta");
    meta.setAttribute("name", "viewport");
    meta.setAttribute("content", "");
    doc_head.appendChild(meta);
  }

  function restoreZoom(){
    meta.setAttribute( "content", initialContent );
  }
  function fixZoom(){
    meta.setAttribute( "content", disabledZoom );
  }


  // Dismiss counter
  function rememberDismissal() {
    if (window.localStorage) {
      try {
        var key = storage_prefix + "dismissed";
        var value = Number(window.localStorage[key]) || 0;
        window.localStorage[key] = String(value + 1);
      } catch (ex) {
      }
    }

  }
  function hasBeenDismissedTooManyTimes() {
    if (!window.localStorage) {
      return false;
    }
    try {
      var key = storage_prefix + "dismissed";
      var value = Number(window.localStorage[key]) || 0;
      return value >= number_of_times_to_dismiss;
    } catch (ex) {
      return true;
    }
  }


  // user agent
  function isMobile() {
    return navigator.userAgent.match(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i);
  }
  function isItunes() {
    return navigator.userAgent.match(/ip(hone|od)|ipad/i);
  }
  function isAndroid() {
    return navigator.userAgent.match(/android/i);
  }
  function isBlackBerry() {
    return navigator.userAgent.match(/blackberry\s[0-9]/i);
  }
  function isWindowsPhone() {
    return navigator.userAgent.match(/windows phone/i);
  }



  var app_link_innerHTML = "Проверьте мобильную версию",
      app_link_href = url,
      popup_type = "web";

  if( published_data && published_data.itunes && published_data.itunes.is_published && published_data.itunes.market_link && isItunes() ){

    window.app_published.itunes = true;

    app_link_href = published_data.itunes.market_link;
    app_link_innerHTML = "Установить приложение";
    popup_type = "itunes";

  }else if( published_data && published_data.android && published_data.android.is_published && published_data.android.market_link && isAndroid() ){

    window.app_published.android = true;

    app_link_href = published_data.android.market_link;
    app_link_innerHTML = "Установить приложение";
    popup_type = "android";

  }else if( published_data && published_data.blackberry && published_data.blackberry.market_link && isBlackBerry() ){

    window.app_published.blackberry = true;

    app_link_href = published_data.blackberry.market_link;
    app_link_innerHTML = "Установить приложение";
    popup_type = "blackberry";

  }else if( published_data && published_data.windows && published_data.windows.market_link && isWindowsPhone() ){

    window.app_published.windows = true;

    app_link_href = published_data.windows.market_link;
    app_link_innerHTML = "Установить приложение";
    popup_type = "windows";

  }


  if( !hasBeenDismissedTooManyTimes() && isMobile() ){

    if( (is_showing_app && popup_type=="web") ||
        (is_showing_app && !!window.AMSdetect && !!window.AMSdetect.iOS_uiwebview) ||
        (is_showing_app && !!window.AMSdetect && !!window.AMSdetect.android_uiwebview)||
        (is_showing_app && isBlackBerry()) ||
        (is_showing_app && isWindowsPhone())  ){
      return false;
    }

    fixZoom();

    window.addEventListener('load', function() {

      var style = document.createElement("style");
      style.appendChild(document.createTextNode(""));
      doc_head.appendChild(style);
      var style_sheet = style.sheet;


      var bubble = doc.createElement('div');
      bubble.setAttribute('id','apps-bubble');

      style_sheet.insertRule("#apps-bubble{"+
        "font-size: 16px;"+
        "position: fixed;"+
        "z-index: 10000;"+
        "top: -200px;"+
        "right: 0;"+
        "left: 0;"+
        "width: 100%;"+
        "-webkit-transition: all 500ms;"+
        "transition: all 500ms;"+
        "opacity: 0;"+
        "border-bottom: 1px solid #d8d8d8;"+
        "background: #fff;}", 0);


      var bubble_inner = doc.createElement('div');
      bubble_inner.setAttribute('id','apps-bubble-inner');
      bubble.appendChild(bubble_inner);

      style_sheet.insertRule("#apps-bubble-inner{"+
        "min-height: 57px;"+
        "padding: 16px 16px 16px 89px;}", 1);


      var icon = document.createElement('div');
      icon.setAttribute('id','apps-bubble-icon');
      icon.style.backgroundImage = 'url(' + app_icon + ')';
      bubble_inner.appendChild(icon);

      style_sheet.insertRule("#apps-bubble-icon{"+
        "position: absolute;"+
        "top: 16px;"+
        "left: 16px;"+
        "width: 57px;"+
        "height: 57px;"+
        "margin-right: 16px;"+
        "border: 1px solid #d8d8d8;"+
        "-webkit-border-radius: 10px;"+
        "border-radius: 10px;"+
        "background-size: 57px 57px;}", 2);


      var name = document.createElement('div');
      name.setAttribute('id','apps-bubble-name');
      name.innerHTML = app_name;
      bubble_inner.appendChild(name);

      style_sheet.insertRule("#apps-bubble-name{"+
        "font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif;"+
        "font-weight: bold;"+
        "margin: 0 20px .5em 0;"+
        "color: #4d4d4d;}", 3);


      var buttons = document.createElement('div');
      buttons.setAttribute('id','apps-bubble-buttons');
      bubble_inner.appendChild(buttons);

      style_sheet.insertRule("#apps-bubble-buttons{text-align: right;}", 4);


      var app_link = doc.createElement("a");
      app_link.innerHTML = app_link_innerHTML;
      app_link.setAttribute("href", app_link_href);
      app_link.setAttribute("id","apps-bubble-button");
      buttons.appendChild(app_link);

      style_sheet.insertRule("#apps-bubble-button{"+
        "font-size: .9em;"+
        "display: inline-block;"+
        "padding: .4em .7em;"+
        "text-align: center;"+
        "white-space: nowrap;"+
        "text-decoration: none;"+
        "color: "+app_text_color+";"+
        "border: 1px solid "+app_text_color+";"+
        "-webkit-border-radius: 2px;"+
        "border-radius: 2px;"+
        "background: #"+app_color+";}", 5);

      var close = doc.createElement('span');
      close.setAttribute('id','apps-bubble-close');
      close.onclick = function() {
        bubble.style.opacity = '0';
        bubble.style.top = '-200px';

        rememberDismissal();
        restoreZoom();
      };


      style_sheet.insertRule("#apps-bubble-close{"+
        "position: absolute;"+
        "top: 0;"+
        "right: 0;"+
        "display: block;"+
        "width: 16px;"+
        "height: 16px;"+
        "border: 10px solid transparent;"+
        "background: url(" + close_image + ") no-repeat 50% 50%;"+
        "background-size: 16px 16px;}", 6);

      bubble_inner.appendChild(close);


      doc.body.appendChild(bubble);


      window.setTimeout(function() {
        bubble.style.opacity = '1';
        bubble.style.top = '0px';
      }, 1000);


    }, false);


  }

})(window.document, window.navigator);
