Array.prototype.first = function() {
  return this[ 0 ]
}

Array.prototype.last = function() {
  return this.reverse().first()
}

Array.prototype.unique = function() {
  var a = [], l = this.length

  for ( var i = 0; i < l; i++ ) {
    for ( var j = i + 1; j < l; j++ ) {
      if ( this[ i ] === this[ j ] ) j = ++i
    }
    a.push( this[ i ] )
  }

  return a
}

Array.prototype.u_hash = function() {
  var a = {}

  for ( var i = 0; i < this.length; i++ ) {
    a[ this[ i ] ] = a[ this [ i ] ] ? ++a[ this [ i ] ] : 1
  }

  return a
}

Array.prototype.u_count = function( elem ) {
  var count = 0

  for ( var i = 0; i < this.length; i++ ) {
    if ( this[ i ] === elem ) count++
  }

  return count
}

var Cache = window.Cache = { temp: {} }

Array.prototype.store = function( key, persist ) {
  if ( persist && 'localStorage' in window) {
    try {
      if ( this === null )
        localStorage.removeItem( key )
      else
        localStorage.setItem( key, JSON.stringify( this ) )
    } catch ( e ) {
      localStorage.clear()
      localStorage.setItem( key, JSON.stringify( this ) )
    }
  }

  Cache.temp[ key ] = this

  return this
}

Array.prototype.get = function( key ) {
  this.length = 0

  var val = Cache.temp[ key ]

  if ( !'localStorage' in window || typeof( val ) !== 'undefined' )
    return this.concat( val )

  val = localStorage.getItem( key )

  if ( val === null || typeof( val ) === 'undefined' )
    return this

  val = JSON.parse( val )
  Cache.temp[ key ] = val

  return this.concat( val )
}

String.prototype.capitalize = function() {
  return this.replace( /\w+/g, function( a ) {
    return a.charAt( 0 ).toUpperCase() + a.substr( 1 ).toLowerCase()
  })
}
