var sortingdropdown = {
  initialize: function (elements, sortcolinput, sortdirinput, postbacktrigger) {
    if (elements.length) {
      for (var i = 0; i < elements.length; i++) {
        var el = elements[i];
        if (el.button && el.dropdown) {
          sortingdropdown.id = el.button.attr('id');
          el.dropdown.mouseover(function (eventObject) {
            if (sortingdropdown.timer) {
              clearTimeout(sortingdropdown.timer);
            }
          }).mouseout(function (eventObject) {
            if (!$(this).hasClass('dn')) {
              sortingdropdown.timer = setTimeout(function () {
                sortingdropdown.toggle(sortingdropdown.currentSelected);
                sortingdropdown.timer = null;
              }, sortingdropdown.timeout);
            }
          });
          /*
          el.button.mouseover(function (eventObject) {
          if (sortingdropdown.timer) {
          clearTimeout(sortingdropdown.timer);
          }
          }).mouseout(function (eventObject) {
          if (!$(this).hasClass('dn')) {
          sortingdropdown.timer = setTimeout(function () {
          sortingdropdown.toggle(sortingdropdown.currentSelected);
          sortingdropdown.timer = null;
          }, sortingdropdown.timeout);
          }
          });
          */
          el.button.click(function () {
            var e = typeof (event) != 'undefined' ? event : null;
            this.blur();
            sortingdropdown.toggle();
            if (sortingdropdown.focusInput)
              $(sortingdropdown.focusInput).focus();
            if (e && e.preventDefault) {
              e.preventDefault();
            }
            return false;
          });
          sortingdropdown.pairs[sortingdropdown.id] = el;
        }
      }
    }
    sortingdropdown.sortcolInput = sortcolinput;
    sortingdropdown.sortdirInput = sortdirinput;
    sortingdropdown.postbackTrigger = postbacktrigger;
  },
  toggle: function () {
    var pair = sortingdropdown.pairs[sortingdropdown.id];
    if (pair) {
      var button = pair.button;
      var dropdown = pair.dropdown;
      if (dropdown.hasClass('dn')) {
        dropdown.removeClass('dn');
        button.addClass('selected');
        button.parent().addClass('top');
        $('#currentSortAdditional').removeClass('dn');
        sortingdropdown.currentSelected = button;
      }
      else {
        dropdown.addClass('dn');
        button.removeClass('selected');
        button.parent().removeClass('top');
        $('#currentSortAdditional').addClass('dn');
        sortingdropdown.currentSelected = null;
      }
    }
  },
  itemclicked: function (col, dir) {
    sortingdropdown.sortcolInput.val(col);
    sortingdropdown.sortdirInput.val(dir);
    sortingdropdown.postbackTrigger.click();
    sortingdropdown.toggle();
    $('#sortButtonLoading').removeClass('dn');
  },
  pairs: {},
  timer: null,
  currentSelected: null,
  timeout: 500
};

