// Copyright (c) 2016 NetEase Youdao, All rights reserved.

$(function() {

$("body").swipe( {
  swipe: function(event, direction, distance, duration, fingerCount, fingerData, currentDirection) {
    if (!direction || !currentDirection || !distance) {
      return false;
    }
    if (fingerCount == 2 && $('#queryinput').val()) {
      if ((direction == 'left') && !$('#dict').hasClass('open')) {
        $('#toggleNav')[0].click();
      }

      if ((direction == 'right') && $('#dict').hasClass('open')) {
        $('#toggleNav')[0].click();
      }
    }

  if (fingerCount == 2) {
    if ($('#translateWnd').hasClass('layout-vertical')) {
      if ((direction == 'left') && !$('#trans').hasClass('open')) {
        $('#toggleFanyiNav')[0].click();
      }
      if ((direction == 'right') && $('#trans').hasClass('open')) {
        $('#toggleFanyiNav')[0].click();
      }
    }

    if ($('#translateWnd').hasClass('layout-horizontal')) {
      if ((direction == 'up') && !$('#trans').hasClass('open')) {
        $('#toggleFanyiNav')[0].click();
      }
      if ((direction == 'down') && $('#trans').hasClass('open')) {
        $('#toggleFanyiNav')[0].click();
      }
    }
  }

    if (fingerCount > 2 && $('#queryinput').val()) {
      if ((direction == 'up') && $('#queryinput').val() != '') {
        deskdict.inputChange('');
      }
    }

  if (fingerCount > 2) {
    var tabs = $(event.target).parents('body').find('#control li.js-type');
    for (var j = 0; j < tabs.length; j++) {
      if ($(tabs[j]).hasClass('current')) {
        if ((direction == 'right') && j != 0) {
          $(tabs[j - 1]).mousedown();
          return;
        }
        if ((direction == 'left') && j != tabs.length - 1) {
          $(tabs[j + 1]).mousedown();
          return;
        }
      }
    }
  }

  if (fingerCount == 1) {
    if (event.target == $('#pic_dict>.selected-container>.selected_img>img')[0]) {
      if (direction == 'left' || direction == 'down') {
        $(event.target).mouseenter();
        return;
      }
    }
  }

  if (fingerCount > 1 && $('#ads').length > 0) {
    if (direction == 'down') {
      $('a.js-close-ads').click();
    }
  }

    if (fingerCount == 1) {
      var inner_tabs = $(event.target).parents('article').find('a.js-ctlog.tab-menu:not(.js-empty)');
      if (inner_tabs && inner_tabs.length > 1) {
        for (var i = 0; i < inner_tabs.length; i++) {
          if ($(inner_tabs[i]).hasClass('current')) {
            if ((direction == 'right') && i != 0) {
              $(inner_tabs[i - 1]).click();
              return;
            }
            if ((direction == 'left') && i != inner_tabs.length - 1) {
              $(inner_tabs[i + 1]).click();
              return;
            }
          }
        }
      }
    }
  },

  tap: function (event, target) {
    var dict_img = $('#pic_dict>.selected-container>.selected_img>img');
    if (target != dict_img[0]) {
      dict_img.mouseleave();
    }

     if (target.tagName == 'A') {
      event.preventDefault();
      $(target).parents('nav').find('a.tab-menu').removeClass('current');
      $(target).mouseenter();
      $(target).click();
    }
    
   if ($(target).parents('#mainMenu').attr('id') == 'mainMenu') {
      event.preventDefault();
      $(target).mouseover();
      if (!$(target).hasClass('has-submenu')) {
        $(target).click();
      }
      return false;
    }

    if ($(target).parents('footer>.menu').length > 0 || $(target).parents('footer>.set-word').length > 0) {
      event.preventDefault();
      $(target).click();
      return false;
    }	

    if ($(target).parents('.everyDayEnglish').length > 0 && target.tagName.toLowerCase() == 'a') {
      event.preventDefault();
      $(target).click();
      return false;
    }

    if ($(target).parents('#spoken').length > 0 && target.tagName.toLowerCase() == 'a') {
      event.preventDefault();
      $(target).click();
      return false;
    }
    
    if ((target.tagName == 'SPAN' || target.tagName == 'A') && $(target).hasClass('button')) {
      event.preventDefault();
      $(target).click();
      if (target.tagName == 'A' && target.href) {
        window.open(target.href);
      }
      return false;
    }
    
    if (target.tagName == 'DIV' && target.contentEditable) {
      event.preventDefault();
      $(target).focus();
      return false;
    }
    
    if ($(target).parents('.water-card')) {
      event.preventDefault();
      $(target).click();
      return false;
    }
  },

  swipeStatus: function (event, phase, direction, distance, duration, fingerCount, fingerData, currentDirection) {
    if (phase == 'end' && distance > 300 && fingerCount == 1) {
      if ($('ul.pager.pull-left>li.pre').eq(0).hasClass('active')) {
        if (direction == 'right') {
          $('ul.pager.pull-left>li.pre>a').eq(0).click();
        }
      }
      if ($('ul.pager.pull-left>li.next').eq(0).hasClass('active')) {
        if (direction == 'left') {
          $('ul.pager.pull-left>li.next>a').eq(0).click();
        }
      }
    }
  },

  fingers: 'all',
  threshold: 50,
  maxTimeThreshold: 2000,
  allowPageScroll: 'vertical',
  excludedElements: ".noSwipe, #toggleNav, header, .combobox, #ads, a:not(.js-ctlog):not(.in-menu) input, button, select, textarea, .js-switch, #toggleFanyiNav, .everyDayEnglish a, #spoken a"
});
  
});
