var Utils = {
  file_ext: function(name) {
    return name.split('.').pop().toLowerCase();
  },

  base_name: function(fullname) {
    return fullname.replace(/.*[\/\\]/, '');
  },

  only_name: function(name) {
    n = name.replace(/.*[\/\\]/,'');
    n = n.replace(/\.[^.]*$/, '');
    return n;
  },

  get_cookie: function(cookie_name) {
    var results = document.cookie.match('(^|;) ?'+cookie_name+'=([^;]*)(;|$)');

    if (results) { return (unescape(results[2])) }
    else { return null }
  },

  keys_count: function (obj) {
    var counter = 0;
    $.each(obj, function() { counter++ });
    return counter;
  },

  htmlize: function(text) {
  if (!text) { return '' }

  return text.replace(/&/g, '&amp')
             .replace(/</g, '&lt;')
             .replace(/>/g, '&gt;')
             .replace(/"/g, '&quot;');
  },

  utf8: {

    // public method for url encoding
    encode : function (string) {
      string = string.replace(/\r\n/g,"\n");
      var utftext = "";

      for (var n = 0; n < string.length; n++) { 
        var c = string.charCodeAt(n);

        if (c < 128) {
          utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
          utftext += String.fromCharCode((c >> 6) | 192);
          utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
          utftext += String.fromCharCode((c >> 12) | 224);
          utftext += String.fromCharCode(((c >> 6) & 63) | 128);
          utftext += String.fromCharCode((c & 63) | 128);
        }
      }

      return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
      var string = "";
      var i = 0;
      var c = c1 = c2 = 0;

      while ( i < utftext.length ) {
        c = utftext.charCodeAt(i);

        if (c < 128) {
          string += String.fromCharCode(c);
          i++;
        }
        else if((c > 191) && (c < 224)) {
          c2 = utftext.charCodeAt(i+1);
          string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
          i += 2;
        }
        else {
          c2 = utftext.charCodeAt(i+1);
          c3 = utftext.charCodeAt(i+2);
          string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
          i += 3;
        }
      }

      return string;
    }
  },

  translit: function(text) {
    text = text.replace(/[Аа]/g,'a');
    text = text.replace(/[Бб]/g,'b');
    text = text.replace(/[Вв]/g,'v');
    text = text.replace(/[Гг]/g,'g');
    text = text.replace(/[Дд]/g,'d');
    text = text.replace(/[Ее]/g,'e');
    text = text.replace(/[Ёё]/g,'e');
    text = text.replace(/[Жж]/g,'zh');
    text = text.replace(/[Зз]/g,'z');
    text = text.replace(/[Ии]/g,'i');
    text = text.replace(/[Йй]/g,'i');
    text = text.replace(/[Кк]/g,'k');
    text = text.replace(/[Лл]/g,'l');
    text = text.replace(/[Мм]/g,'m');
    text = text.replace(/[Нн]/g,'n');
    text = text.replace(/[Оо]/g,'o');
    text = text.replace(/[Пп]/g,'p');
    text = text.replace(/[Рр]/g,'r');
    text = text.replace(/[Сс]/g,'s');
    text = text.replace(/[Тт]/g,'t');
    text = text.replace(/[Уу]/g,'u');
    text = text.replace(/[Фф]/g,'f');
    text = text.replace(/[Хх]/g,'h');
    text = text.replace(/[Цц]/g,'ts');
    text = text.replace(/[Чч]/g,'ch');
    text = text.replace(/[Шш]/g,'sh');
    text = text.replace(/[Щщ]/g,'sh');
    text = text.replace(/[Ъъ]/g,'j');
    text = text.replace(/[Ыы]/g,'y');
    text = text.replace(/[Ьь]/g,'j');
    text = text.replace(/[Ээ]/g,'ie');
    text = text.replace(/[Юю]/g,'iu');
    text = text.replace(/[Яя]/g,'ya');
    return text;
  },

  browser: function() {
    var ua = navigator.userAgent.toLowerCase();

    // Internet Explorer
    if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
      return "msie";
    };

    // Opera
    if (ua.indexOf("opera") != -1) {
      return "opera";
    };

    // Gecko = Mozilla + Firefox + Netscape + chrome
    if (ua.indexOf("gecko") != -1) {
      if (/chrome/.test(ua)) { return "chrome" };
      return "gecko";
    };

    // Safari
    if (ua.indexOf("safari") != -1) {
      return "safari";
    };

    // Konqueror
    if (ua.indexOf("konqueror") != -1) {
      return "konqueror";
    }

    return "unknown";
  }
};

