/*jslint indent: 2, maxlen: 80, nomen: true */
/*global document: true, define: true, $: true */
define([
  'app'
],
  function (App) {
    "use strict";
    // this module provides generic methods usable across all gadgets/modules
    var that = {};

    // Javascript closest()
    that.closest = function (elem, selector) {
      var matchesSelector = elem.matches ||
          elem.webkitMatchesSelector ||
          elem.mozMatchesSelector ||
          elem.msMatchesSelector,
        fallback;

      if (matchesSelector) {
        while (elem) {
          if (matchesSelector.bind(elem)(selector)) {
            return elem;
          }
          elem = elem.parentNode;
        }
        return false;
      }
      // opera...
      fallback = $(elem).closest(selector);
      if (fallback.length > 0) {
        return fallback;
      }
      return false;

    };
    // object size
    that.objectSize = function (obj) {
      var size = 0, key;

      for (key in obj) {
        if (obj.hasOwnProperty(key)) {
          size += 1;
        }
      }
      return size;
    };

    App.util = that;

    return undefined;
  }
  );
