(function() {

  window.proassistData = window.proassistData || {};

  function loadCss() {
    var overflowFix = '';
    var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
    if (iOS) {
      overflowFix = 'overflow: auto;';
    }
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = '@import url(\'https://fonts.googleapis.com/css?family=Roboto&subset=latin-ext\');' +
      '#proassistWidget {position: fixed; display: flex; top: 20%; left: 0; z-index: 9999999; transition: left 0.5s ease-in-out;}' +
      '.proassist-container {display: flex; width: 362px; height: 500px;}' +
      '.proassist-content {width: 322px; height: 500px; border: 2px solid #0BB1BD; background: #ECEFF4; border-left: none; ' + overflowFix + ' -webkit-overflow-scrolling:touch;}' +
      '.proassist-handle {background: #0BB1BD; width: 40px; height: 464px; padding-top: 40px; cursor: pointer;}' +
      '.proassist-handle h2 {color: #fff; text-align: center; letter-spacing: 6px; word-wrap: normal; font-weight: normal; font-family: \'Roboto\', sans-serif; -webkit-transform: rotate(90deg); -moz-transform: rotate(90deg); -ms-transform: rotate(90deg); -o-transform: rotate(90deg); filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);}' +
      '#proassistWidget.proassist--folded {left: -322px}' +
      '.proassist-content iframe {width: 100%; height: 100%; overflow: hidden;}' +
      '' +
      '';
    document.getElementsByTagName('head')[0].appendChild(style);
  }

  function loadHtml() {
    var content = '<div class="proassist-content">' +
      '<iframe src="https://system.proassist.pl/api/widget/specialist/view/specialist/ps-u8808-1h3831" width="320" height="496" frameborder="0"></iframe>' +
      '' +
      '</div>' +
      '<div class="proassist-handle" id="proassistHandle">' +
      '<h2>' + window.proassistData.title + '</h2>' +
      '</div>';
    var widget = document.createElement('div');
    widget.id = 'proassistWidget';
    widget.className = 'proassist-container proassist--folded';
    widget.innerHTML = content;
    document.getElementsByTagName('body')[0].appendChild(widget);
  }

  function domReady() {
    document.body.className += ' proassist';
    console.log('Proassist Widget Init');
    console.log(window.proassistData);
    loadCss();
    loadHtml();
    document.getElementById('proassistHandle').addEventListener('click', function() {
      document.getElementById('proassistWidget').classList.toggle('proassist--folded');
    }, false);
  }

  window.onload = function() {
    domReady();
  };

//    // Mozilla, Opera, Webkit
//    if (document.addEventListener) {
//        console.log('Mozilla, Opera, Webkit');
//        document.addEventListener("DOMContentLoaded", function () {
//            console.log('DOMContentLoaded');
//            document.removeEventListener("DOMContentLoaded", arguments.callee, false);
//            domReady();
//        }, false);
//
//    // If IE event model is used
//    } else if (document.attachEvent) {
//        console.log('IE');
//        // ensure firing before onload
//        document.attachEvent("onreadystatechange", function () {
//            if (document.readyState === "complete") {
//                document.detachEvent("onreadystatechange", arguments.callee);
//                domReady();
//            }
//        });
//    }
})();
