(function() {
  var app;

  app = {
    firstLinePos: 0,
    lastScrollTop: window.pageYOffset,
    cellSize: 0,
    browseRScrollWidth: 0,
    scrollSpeed: 0,
    newFontSize: 0,
    grid: {
      row: [1, 2, 3],
      col: [6, 10, 12, 14, 16, 18, 20, 22, 24]
    },
    content: document.getElementById('content'),
    menuTopRow: document.getElementById('menuTopRow'),
    spacerRow: document.getElementById('spacerRow'),
    leftMenu: document.getElementById('leftMenu'),
    contentWrapper: document.getElementById('content_wrapper'),
    getScrollBarWidth: function() {
      var inner, outer, w1, w2;
      inner = document.createElement('p');
      inner.style.width = "100%";
      inner.style.height = "200px";
      outer = document.createElement('div');
      outer.style.position = "absolute";
      outer.style.top = "0px";
      outer.style.left = "0px";
      outer.style.visibility = "hidden";
      outer.style.width = "200px";
      outer.style.height = "150px";
      outer.style.overflow = "hidden";
      outer.appendChild(inner);
      document.body.appendChild(outer);
      w1 = inner.offsetWidth;
      outer.style.overflow = 'scroll';
      w2 = inner.offsetWidth;
      if (w1 === w2) {
        w2 = outer.clientWidth;
      }
      document.body.removeChild(outer);
      this.browseRScrollWidth = w1 - w2;

    },
    calcFontSize: function() {
      var maxF, maxW, minF, minW;
      maxW = 1217;
      minW = 1017;
      maxF = 14;
      minF = 11.5;
      app.newFontSize = maxF - ((maxF - minF) / (maxW - minW) * (maxW - window.innerWidth));
      if (app.newFontSize < minF) {
        app.newFontSize = minF;
      }
      if (app.newFontSize > maxF) {
        app.newFontSize = maxF;
      }
      document.body.style.fontSize = app.newFontSize + 'px';
    },
    resize: function() {
      app.calcFontSize();
      app.cellSize = app.content.clientWidth / 14;
      //app.dotsPos();
      app.dotsPos2();
    },
    setupTopMenuDots: function(addr) {
      var a, i, j, k, leftCorr2, len, longVertLines, p1, p2, r, redDots, vertLines;
      addr = addr.split('/')[addr.split('/').length - 1].split('.')[0];
      redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 12, 14, 16, 18, 20, 22, 24]];
      longVertLines = [];
      if (addr === 'aeronet' || addr === 'autonet' || addr === 'energynet' || addr === 'finnet' || addr === 'foodnet' || addr === 'healthnet' || addr === 'marinet' || addr === 'neuronet' || addr === 'safenet' || addr === 'technet' || addr === 'technology' || addr === 'circleMove' || addr === 'ipnet' || addr === 'talent' || addr === 'services') {
        redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 12, 14, 15, 16, 18, 19, 20, 22, 24]];
        longVertLines = [14, 15, 19, 20];
      } else {
        switch (addr) {
          case 'matrix':
            longVertLines = [16, 18];
            break;
          case 'media':
            console.log('');
            break;
          case 'principles':
            longVertLines = [12, 18];
            break;
          case 'participants':
            redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 12, 14, 16, 18, 19, 20, 22, 24]];
            longVertLines = [14, 19];
            break;
          case 'roadmaps':
            longVertLines = [14];
            break;
          case 'ntiandworld':
            redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 12, 14, 15, 16, 18, 19, 20, 22, 24]];
            longVertLines = [14, 15, 19, 20];
            break;
          case 'QandA':
            longVertLines = [16, 18];
            break;
          case 'events':
            redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 12, 14, 15, 16, 18, 19, 20, 22, 24]];
            longVertLines = [14, 15, 19, 20];
            break;
          case 'documents':
            console.log('');
            break;
          case 'dictionary':
            redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 12, 14, 15, 16, 18, 19, 20, 22, 24]];
            longVertLines = [14, 15, 19, 20];
            break;
          case 'news':
            console.log('');
            break;
          case 'contacts':
            longVertLines = [16, 18];
            break;
          case 'index':
            longVertLines = [11, 16, 18, 23];
            redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 11, 12, 14, 16, 18, 20, 22, 23, 24]];
            break;
          default:
            longVertLines = [11, 16, 18, 23];
            redDots = [[], [6, 10, 12, 14, 16, 18, 20, 22, 24], [6, 10, 11, 12, 14, 16, 18, 20, 22, 23, 24]];
        }
      }
      vertLines = [6, 10, 12, 14, 16, 18, 20, 22, 24];
      leftCorr2 = 14 / (app.browseRScrollWidth + 5);
      for (i in vertLines) {
        r = vertLines[i];
        if (r === 6 || r === 10 || r === 24) {
          $('#topDots').append("<div class='vertLineTopLong' style='top: 0px; left: " + ((r * (64 / 14)) - leftCorr2) + "em'></div>");
        } else {
          $('#topDots').append("<div class='vertLineTop' style='top: 0px; left: " + ((r * (64 / 14)) - leftCorr2) + "em'></div>");
        }
      }
      for (i in redDots) {
        p1 = redDots[i];
        for (j in p1) {
          p2 = p1[j];
          $('#topDots').append("<div class='crossDot' style='top: " + (i * 4.5714) + "em; left: " + ((p2 * (64 / 14)) - leftCorr2) + "em;'></div>");
        }
      }
      for (a = k = 0, len = longVertLines.length; k < len; a = ++k) {
        r = longVertLines[a];
        $('#linesContainer').append("<div class='vertLineTopLong' style='top: 0px; left: " + ((r * (64 / 14)) - leftCorr2) + "em'></div>");
      }
      app.resize();
    },
    dotsPos: function() {
      var left;
      if (window.innerWidth > (1000 + app.browseRScrollWidth)) {
        left = (window.innerWidth / 2) - (app.cellSize * 30 / 2);
      } else {
        left = 500 - (app.cellSize * 30 / 2) + (app.browseRScrollWidth / 2);
      }
      $('#topDots').css({
        'left': left
      });
      $('#linesContainer').css({
        'left': left
      });
    },
    dotsPos2: function(){
      leftCorr2 = 14 / (app.browseRScrollWidth + 5);
      firstLinePos = ((6 * (64 / 14)) - leftCorr2) * app.newFontSize;
      leftest = $('#ntiLogo').offset().left;

      console.log(leftest); 

      $('#topDots').css({
        'left': leftest - firstLinePos
      });
      $('#linesContainer').css({
        'left': leftest - firstLinePos
      });
    },
    detectDirection: function() {
      var direction, st;
      st = window.pageYOffset;
      app.scrollSpeed = app.lastScrollTop - st;
      if (st > app.lastScrollTop) {
        direction = -1;
      } else {
        direction = 1;
      }
      app.lastScrollTop = st;
      return direction;
    },
    scroll: function(e) {
      var bottomOffset, dir, menu, offset;
      menu = $('#menuLeftColumn');
      offset = menu.offset().top - $('body').scrollTop();
      bottomOffset = offset + menu.height();
      dir = app.detectDirection();
      if (dir === 1) {
        if (offset < 200) {
          menu.css({
            'top': parseInt(menu.css('top')) + app.scrollSpeed
          });
        } else {
          menu.css({
            'top': 200
          });
        }
      }
      if (dir === -1) {
        if (bottomOffset > window.innerHeight) {
          menu.css({
            'top': parseInt(menu.css('top')) + app.scrollSpeed
          });
        }
      }
    },
    showLogo: function() {
      if ($('#ntiLogo').hasClass('ntiLogoShow')) {
        $('#ntiLogo').removeClass('ntiLogoShow');
        $('#ntiLogoImage').animate({
          'opacity': '0'
        }, 500);
        $('#ntiLogoText1').animate({
          'opacity': '1'
        }, 500);
        $('#ntiLogoText2').animate({
          'opacity': '1'
        }, 500);
      } else {
        $('#ntiLogo').addClass('ntiLogoShow');
        $('#ntiLogoImage').animate({
          'opacity': '1'
        }, 500);
        $('#ntiLogoText1').animate({
          'opacity': '0'
        }, 500);
        $('#ntiLogoText2').animate({
          'opacity': '0'
        }, 500);
      }
    },
    showSearch: function() {
      var searchWindow;
      searchWindow = $('#searchWindow');
      if (searchWindow.hasClass('openedSearch')) {
        searchWindow.removeClass('openedSearch');
        searchWindow.animate({
          'opacity': 0
        }, 300, function() {
          searchWindow.css({
            'display': 'none'
          });
        });
      } else {
        searchWindow.addClass('openedSearch');
        searchWindow.css({
          'display': 'block'
        });
        searchWindow.animate({
          'opacity': 1
        }, 300);
        $('#searchWindow > input').focus();
      }
    }
  };

  setInterval(app.showLogo, 5000);

  app.getScrollBarWidth();

  app.resize();

  app.setupTopMenuDots(location.href);


  window.addEventListener('resize', app.resize);

  window.addEventListener('scroll', app.scroll);

  $('#mainSearch').on('click', app.showSearch);

  $('#closeSearch').on('click', app.showSearch);

}).call(this);
