+function ($) {

  function fixmainmenu() {
    var menuWidth = 0;
    var containerMenu = $('.main-navigation .container').width();

    $('.main-navigation .navbar-nav > ul > li').each(function(){
      var menuItemWidth = $(this).outerWidth();
      menuWidth += menuItemWidth + 1;
    });
    if (menuWidth > containerMenu) {
      $('body').css({'overflow-x':'hidden'});
      $('.main-navigation .navbar-nav > ul').addClass('longMenu');
      $('.main-navigation .navbar-nav > ul').after('<span class="control next"><i class="icon-chevron-right"></i></span><span class="control back"><i class="icon-chevron-left"></i></span>');
      $('.main-navigation .navbar-nav').css({'position' : 'absolute', 'clip' : 'rect(0,'+ containerMenu + 'px,1000px,0)', 'width': containerMenu});

      var owiw = menuWidth - containerMenu + 100;
      var mainNav = $('.main-navigation .navbar-nav > ul');
      var onNav = false;
      $('.main-navigation .next').on('mouseover touchstart',function(event){
        event.preventDefault();
        onNav = true;
        var moveMenu = setInterval(function(){
          var left = mainNav.css('left');
          left = left.indexOf('px') > -1 ? parseInt( left.replace( 'px','') ) : 0;

          if( Math.abs( left ) <= owiw && onNav === true  ){
            if( left == 0 ) {
              mainNav.css({left : "-10px"});
            } else {
              mainNav.css({left : "-=10"});
            }
          } else {
            clearInterval( moveMenu );
          }
        },50);
      }).on('mouseout touchend touchcancel',function(event){
        event.preventDefault();
        onNav = false;
      });

      $('.main-navigation .back').on('mouseover touchstart',function(event){
        event.preventDefault();
        onNav = true;
        var moveMenu = setInterval(function(){
            var left = mainNav.css('left');
            left = left.indexOf('px') > -1 ? parseInt( left.replace( 'px','') ) : 0;

            if( left < 0 && onNav === true  ){
                $('.main-navigation .navbar-nav > ul').css({left : "+=10"});
            } else {
                clearInterval( moveMenu );
            }
        },50);
      }).on('mouseout touchend touchcancel',function(event){
        event.preventDefault();
        onNav = false;
      });
    };
  }

  function secondaryLayout(){
    //Secondary Masonry viewport 
    var viewportWidth = $(window).width();
    var secondary = $('#secondary .secondary-inner');
    var widgets = secondary.find('.widget');
    if( viewportWidth >= 768 && viewportWidth < 1024 ) {
      var leftColumn = 0, rightColumn = 0;
      widgets.each(function(index, el) {
        if( leftColumn <= rightColumn ) {
          $(el).css({ 'float': 'left', 'clear' : 'left' });
          leftColumn += $(el).outerHeight();
        } else { 
          $(el).css({ 'float': 'right', 'clear' : 'right' });
          rightColumn += $(el).outerHeight();
        }
      });
    } else {
      widgets.each(function(index, el) {
        $(el).css({ 'float': 'none', 'clear' : 'both' });
      });
    }
  }

  function responsiveIframe() {
     // Fix responsive iframe
    $('iframe').each(function(){
      var iw = $(this).width();
      var ih = $(this).height();
      var ip = $(this).parent().width();
      var ipw = ip/iw;
      var ipwh = Math.round(ih*ipw);
      $(this).css({
        'width': ip,
        'height' : ipwh,
      });
    });
  }

  function hasParent(el, id) {
      if (el) {
          do {
              if (el.id === id) {
                  return true;
              }
              if (el.nodeType === 9) {
                  break;
              }
          }
          while((el = el.parentNode));
      }
      return false;
  };

  $(document).ready(function($) {
    // Fix long menu
    // fixmainmenu();

    //Secondary Sidebar
    secondaryLayout();

    $('#secondary a[data-toggle="tab"]').on('shown.bs.tab', function(){
      secondaryLayout();
    });

    $('.dw_accordion').on('shown.bs.collapse hidden.bs.collapse', function(){
      secondaryLayout();
    });

    // Responsive Iframe
    responsiveIframe();

    // Fix menu on monitor touch
    if( 'ontouchstart' in document.documentElement ) {
      var clickable = null;
      var menu = $('.navbar-nav ul').attr('id');
      $('.navbar-nav ul .menu-item').each(function(){
        var $this = $(this);

        if( $this.find('ul.sub-menu').length > 0 ) {
          $this.find('a:first').unbind('click').bind('touchstart',function(event){
            if( clickable != this ) {
              clickable = this;
              event.preventDefault();
              return false;
            } else {
                clickable = null;
            }
          });
        }
      });
    }

    // Add arrow to submenu
    $('.main-navigation .sub-menu').before( '<i class="sub-menu-collapse icon-caret-down"></i>');
    $('.sub-menu-collapse').click(function(){
      $(this).toggleClass('active');
    });

    // Back to top
    $('.back-top').click(function () {
      $('body,html').animate({
        scrollTop: 0
      }, 500);
      return false;
    });

    // Main content actions
    $('.main-content-actions a').click(function () {
      $(this).html('<i class="icon-spinner icon-spin"></i>');
      $.cookie('dw_gamez_main_content_layout', $(this).attr('title'));
      document.location.reload();
      return false;
    });

    // Fix 404 page
    $('[class*="error404"]').parent('html').css('height','100%');
    
  });

  $(window).resize(function($){
    // Fix long menu
    // fixmainmenu();

    //Secondary Sidebar
    secondaryLayout();

    // Responsive Iframe
    responsiveIframe();
  });
}(jQuery);
