/*
 * File:					cookie.js
 * Title:					Cook(ie) Class
 * Description:		Contains cookie functionality
 * Author:				Kevin Huisman, Florentine Design (www.florentinedesign.com)
 */


 /*	BROWSE CLASS
--------------------------------------------------------->>
--------------------------------------------------------->>
 */

var Cook = {
  /*
	 * @class params
	 *    _path :: cookie path
	 *    _domain :: cookie domain
	 *    _day  :: number of milliseconds in a day
	 *    _hour :: number of milliseconds in an hour
	 *    _min  :: number of milliseconds in a minute
	 *    _sec  :: number of milliseconds in a second
	 */
	_path   : '/',
	_domain : null,
  _day    : 86400000,
  _hour   : 3600000,
  _min    : 60000,
  _sec    : 1000,
  _cookies : {},

	/*
	 * set :: sets a cookie
	 * @param _in :: object containing name, value & time constraints for cookie
	 */
	set : function(_in) {
	  try {
      var cstr = _in['name'] + '=' + _in['value'] + ';' ;
          cstr += this.getExpire(_in['time']) ;
          cstr += ' path=' + this._path + ';' ;
          cstr += (this._domain) ? ' domain=' + this._domain + ';' : '' ;

      document.cookie = cstr ;
      this._cookies[_in['name']] = _in['value'] ;
      return true ;
    } catch (e){ return false; }
  },
  del : function (_name) {
    if (_name == undefined) return false ;
    this.set({name : _name, value : '', time : { days : -3 }}) ;
    this._cookies[_name] = undefined;
  },
  get : function (_name) {
    var _val = this._cookies.find(
      function(val){
		    return (val.key == _name) ;
		  }
		);
		try {
		  return _val.value ;
		} catch (e) {
		  return false ;
		}

  },
  init : function() {
    var tmp = document.cookie.split('; ') ;
    var _cookies = {};

    $A(tmp).each(function(value) {
        var _t = value.split('=') ;
        _cookies[_t[0]] = _t[1] ;
      });
    this._cookies = $H(_cookies) ;
  },

	/*
	 * getExpire :: returns gmt formatted string for expiration part of cookie
	 * @param time :: object containing time
	 */
	getExpire : function($_time) {

	  if ($_time == undefined) return '' ;

	  var days = ($_time['days'] == undefined) ? 0 : Number(this._day * $_time['days']) ;
	  var hours = ($_time['hours'] == undefined) ? 0 : Number(this._hours * $_time['hours']) ;
	  var minutes = ($_time['minutes'] == undefined) ? 0 : Number(this._minutes * $_time['minutes']) ;
	  var seconds = ($_time['seconds'] == undefined) ? 0 : Number(this._seconds * $_time['seconds']) ;

	  var time = Number(days + hours + minutes + seconds) ;
	  if (time != 0) {
	    var _date = new Date();
	    _date.setTime(_date.getTime() + time) ;
	    return ' expires=' + _date.toGMTString() + ';' ;

	  }else{
	    return '' ;
	  }
	}
};
Cook.init();