(function($)
{
  /**
   * Testing variables type.
   *
   * @author Michal Kandr
   **/
  $.extend({
    isBoolean: function(variable)
    {
      return typeof variable == 'boolean';
    },
    
    isBool: function(variable)
    {
      return $.isBoolean(variable);
    },

    isNumber: function(variable)
    {
      return typeof variable == 'number';
    },

    isString: function(variable)
    {
      return typeof variable == 'string';
    },

    isObject: function(variable)
    {
      return typeof variable == 'object';
    },

    isUndefined: function(variable)
    {
      return typeof variable == 'undefined';
    },

    isSet: function(variable)
    {
      return ! $.isUndefined(variable) && variable != null;
    },

    isEmpty: function(variable)
    {
      return ! $.isSet(variable) || ! variable;
    },
    
    isJQuery: function(variable)
    {
      return $.isSet(variable) && $.isSet(variable.jquery);
    },

    /**
     * Escape string for usage in regular expression.
     * @param string str
     * @return string
     */  
    regExpEscape: function(str)
    {
      var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\
      return str.replace(specials, "\\$&");
    },
    
    htmlEntityEncode: function(str)
    {
      var chars = [
        ['&', '&amp;'],
        ['>', '&gt;'],
        ['<', '&lt;'],
        ['"', '&quot;'],
        ['\'', '&#39;']
      ];
      if( ! str)
      {
        return str;
      }
      for(var i=0; i<chars.length; ++ i)
      {
        str = str.replace(chars[i][0], chars[i][1]);
      }
      return str;
    },

    arrayRemove : function(array, index){
      array.splice(index, 1);
    }
  });

  /**
   * Get elements maximal z-index.
   * Maximal z-index is maximum of z-indexes of element and it's parents
   * @param object elem
   * @return integer
   **/
  $.fn.elemZIndex = function (elem){
    var zIndex = parseInt($(this).css('z-index'));
    zIndex = isNaN(zIndex) ? 0 : zIndex;

    $(this).parents().each(function(){
      var parentZIndex = parseInt($(this).css('z-index'));
      if( ! isNaN(parentZIndex) && parentZIndex > zIndex) {
        zIndex = parentZIndex;
      }
    });
    
    return zIndex;
  }
})(jQuery);
