var loadedCSS = new Array()
var loadedJS = new Array();

$.extend({
  /**
   * Подгрузка файла CSS 
   * @param {String} filename
   * @param {Boolean} cache
   */
  loadCSS: function(filename, cache)
  {
    if (jQuery.inArray(filename, loadedCSS) == -1) {
      cache = (cache == undefined) ? true : !!cache;
      
      if (!cache) {
        var ts = +new Date;
        // try replacing _= if it is there
        var ret = filename.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
        // if nothing was replaced, add timestamp to the end
        filename = ret + ((ret == filename) ? (filename.match(/\?/) ? "&" : "?") + "_=" + ts : "");
      } else {
        loadedCSS = loadedCSS.concat([filename]);
      }
      
      $('head').append('<link href="' + filename + '" rel="stylesheet" media="all" />');
    }
  },
  
  /**
   * Подгрузка файла JavaScript
   * @param {hash} options
   * filename - название файла
   * async - асинхронная загрузка
   * cache - кэширование
   * @return {Boolean}
   */
  loadJS: function(options)
  {
    try {
      if (!options.filename || options == undefined)
        throw new Error('Не указано название файла для загрузки');
    } catch(e) {
      alert(e.message);
      return false;
    }
    
    var filename = options.filename;
    var async = (options.async == undefined) ? true : !!options.async;
    var cache = (options.cache == undefined) ? true : !!options.cache;
    
    if (jQuery.inArray(filename, loadedJS) == -1) {
      if (cache) {
        loadedJS = loadedJS.concat([filename]);
      }
      
      $.ajax({
        type: "GET",
        url: filename,
        cache: cache,
        async: async,
        dataType: "script"
      });
    }
  }
});

$.fn.extend({
  dom: function () {
    return this.get(0);
  }
});
