(function($, win, undefined) {

  'use strict';

  function init() {
    var doc = document;
    var lang = doc.documentElement.lang;
    var pageModule = doc.body.getAttribute('data-module');
    var resizeTimer = -1;

    var orbit = {

      helpers: {
        // Cross-browser class manipulation
        addClass: function(el, className) {
          if (el.classList) {
            el.classList.add(className);
          } else {
            el.className += ' ' + className;
          }
        },
        hasClass: function(el, cls) {
          return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
        },
        removeEmpty: function(els, parent) {
          var $els = $(els);
          $.each($els, function(i, val) {
            if ($els.eq(i).text().trim() === '') {
              // check if there's parent el
              if (typeof parent === 'string') {
                $els
                  .closest($(parent))
                  .addClass('hide');
              } else {
                $els.eq(i).addClass('hide');
              }
            }
          });
        }
      },

      plugins: {
        // RWD image resize script
        bullEye: function() {
          $('.bullseye').bullseye({
            fadeEffect: false
          });
        }
      },

      nav: {
        // Add class name to the menu item when its child items are more than 8 items
        addMegaDropdownClass: function(els, len, className) {
          var els = doc.querySelectorAll(els),
            elsLen = els.length,
            i = -1;

          if (elsLen > 1) {
            for (i = 0; i < elsLen; i++) {
              if (els[i].children.length > len) {
                this.helpers.addClass(els[i].parentNode, className || 'mega-dropdown');
              }
            }
          }
        },
        // set up mobile dropdown
        setDropdown: function() {
          var $caret1 = $('<i class="dropdown-toggle-icon level-1 fa fa-chevron-down"></i>');
          var $caret2 = $('<i class="dropdown-toggle-icon level-2 fa fa-chevron-down"></i>');
          var $li = null;
          var $this = null;
          var cls = 'active';

          // add class and caret on element that has dropdown
          $('.nav-level-1')
            .parent('li')
            .addClass('has-dropdown level-1');

          // check if the DOM exists
          if ($('.has-dropdown.level-1 > .dropdown-toggle-icon').length < 1) {
            $caret1.appendTo('.has-dropdown.level-1');

            $('.nav-level-2')
              .parent('li')
              .addClass('has-dropdown level-2');
            $caret2.appendTo('.has-dropdown.level-2');
          }

          // same click event for first and second level dropdown
          $('.dropdown-toggle-icon.level-1, .dropdown-toggle-icon.level-2').on('click', function() {
            $this = $(this);
            $li = $this.parent('li');

            // toggle els class
            if ($li.hasClass(cls)) {
              $li.removeClass(cls);
            } else {
              $li.siblings('li').removeClass(cls)
              $li.addClass(cls);
            }
          });
        },
        // remove mobile dropdown
        removeDropdown: function() {
          $('.dropdown-toggle-icon').remove();
        }
      },
      // Modules
      announcement: {
        // Announcement text truncation
        truncateAnnouncement: function(els, maxLen) {
          var els = doc.querySelectorAll(els),
            newTitle = '';
          i = -1,
            elsLen = els.length;

          for (i = 0; i < elsLen; i++) {
            if (els[i].firstChild !== null) {
              if (els[i].firstChild.length > maxLen) {
                newTitle = els[i].firstChild.textContent;
                els[i].textContent = newTitle.substring(0, maxLen) + '...';
              }
            }
          }
        },

        // Print page
        printPage: function(el) {
          var $el = $(el);

          $el.on('click', function(e) {
            e.preventDefault();
            window.print();
          });

          if (lang === 'en') {
            $el
              .find('.print-txt')
              .text('Print this page');
          }
        }
      },

      member: {
        equalHeight: function() {
          var bigbrother = -1;
          $('.i-member-item').each(function() {
            bigbrother = bigbrother > $('.i-member-item').height() ? bigbrother : $('.i-member-item').height();
          });

          $('.i-member-item').each(function() {
            $('.i-member-item').height(bigbrother);
          });
        },

        // Remove table row, list item if it only has sigle title or value not in pair
        removeEmptyRow: function() {
          // Remove index page empty item
          $('.i-member-profile-item .i-member-value').each(function() {
            if ($(this).text().trim() === '' || $(this).text().trim() === ':') {
              $(this).parent().addClass('hide');
            }
          });

          // Remove empty table cell on show page
          $('.show-member .member-data th, .show-member .member-data td').each(function() {
            if ($(this).text().trim() === '') {
              $(this).parent('tr').addClass('hide');
            }
          });
        },
      },
      archives: {
        removeEmptyTitle: function(el) {
          var $el = $(el);
          var $els = $el.children();

          $.each($els, function(i, val) {
            if ($els.eq(i).text().trim() === '') {
              $els.eq(i).addClass('hide');
            }
          });

          $.each($el, function(i, val) {
            if ($el.eq(i).children('.hide').length >= 2) {
              $el.eq(i).addClass('hide');
            }
          });
        }
      },

      // Add link and cursor class name on element that has data-link attribute
      addLinkOnADBanner: function(els) {
        $.each(els, function() {
          if ($(this).data('link') !== '' && !$(this).hasClass('youtube')) {
            $(this).on('click', function() {
              var target = $(this).data('target'),
                link = $(this).data('link');
              if (target === '_blank') {
                window.open(link, target);
              } else {
                window.location.href = link;
              }
            }).addClass('cursor');
          }
        });
      },

      // Sitemenu dropdown
      sitemenuDropdown: function(els) {
        var els = doc.querySelectorAll('.sitemenu-list.level-2'),
          len = els.length,
          i = -1;
        for (i = 0; i < len; i++) {
          if (els[i].children.length) {
            var caret = doc.createElement('span');
            caret.className = 'sitemenu-dropdown-toggle fa fa-caret-down';
            caret.setAttribute('data-toggle', 'dropdown');

            els[i].parentNode.insertBefore(caret, els[i]);
            this.helpers.addClass(els[i], 'dropdown-menu');
          }
        }
      },

      // Go back top button
      goBackTop: function(txt, speed) {
        var top = document.createElement('div');
        top.className = 'go-back-top no-print';
        top.textContent = txt;
        doc.body.appendChild(top);

        $(window).scroll(function() {
          if ($(this).scrollTop() != 0) {
            $('.go-back-top').fadeIn();
          } else {
            $('.go-back-top').fadeOut();
          }
        });

        $('.go-back-top').click(function() {
          $('body, html').animate({
            scrollTop: 0
          }, speed);
          return false;
        });
      }
    };

    // add to window object
    win.ORBITFRONT = orbit;

    // Specific functions that will be running on homepage
    switch (pageModule) {
      case 'home':
        break;
      case 'announcement':
        orbit.announcement.printPage('.print-button');
        break;
      case 'member':
        orbit.member.removeEmptyRow();
        break;
      case 'archive':
        orbit.archives.removeEmptyTitle('.i-archive__category-item');
      default:
        break;
    }
    // Functions that will be running on every page
    orbit.sitemenuDropdown();
    orbit.goBackTop('top', 800);
    orbit.plugins.bullEye();

    if ($(window).width() < 768) {
      orbit.nav.setDropdown();
    }

    // Responsive specific
    $(window).resize(function() {
      if ($(window).width() < 768) {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(orbit.nav.setDropdown, 500);
      } else {
        resizeTimer = setTimeout(orbit.nav.removeDropdown, 500)
      }
    });
  }

  // Run the init function when DOM is ready
  $(document).ready(function() {
    var leftC = $('.layout-content-box.left-column');
    var rightC = $('.layout-content-box.right-column');
    if( $('.layout-content-box.left-column').children().is(':empty') ) {
      leftC.removeClass('col-sm-3');
      rightC.removeClass('col-sm-9');
      rightC.addClass('col-sm-12');
    }
    $('.i-member-value').each(function(){ $(this).find('br').remove(); })
    $('.member-data-value-positions').each(function(){ $(this).find('br').remove(); })
    $('.member-data-value-expertise').each(function(){ $(this).find('br').remove(); })
    init();
    $('.navbar-toggle').click(function(){
      $('.navbar-expand-lg').toggleClass('width-controler')
    })
    if( $('div[data-pp="1000"]').length != 0 && $('div[data-pp="1000"]').html().trim() !== ''){
      $('.black-screen').addClass('active');
    }
    $('.close-btn').click(function() {
      $('.black-screen').removeClass('active');
    })
  });

}(jQuery, window));
$(document).ready(function(){
    if(window.location.search.indexOf('editmode=on') == -1){
       $(".nav-tabs").each(function(i,v){
          $(v).nextAll("*").not("script").addClass("tab-pane fade");
            $(v).nextAll("*").not("script").eq(0).addClass("active in");
            var tab_content = $("<div class=\"tab-content\"></div>")
            $(v).nextAll("*").not("script").appendTo(tab_content);
            $(v).after(tab_content);
           $(v).find("li").off('click').click(function(){
               $(this).parent().nextAll(".tab-content").eq(0).find(".tab-pane").removeClass("active in");
               $(this).parent().nextAll(".tab-content").eq(0).find(".tab-pane").eq($(this).index()).addClass("active in");
                $(this).addClass('active');
                $(this).siblings('li').removeClass('active');
            })
        })
    }
  })