// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// cookie.js
//
// copyright (c) 2006-2009 drow <drow@bin.sh>
// all rights reserved
//
// based in part on code by Bill Dortch <bdortch@hidaho.com>

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// cookie jar

var cookie_jar;

var default_domain = '.bin.sh';
var default_path = '/';
var forthwith = 'Thu, 01-Jan-70 00:00:01 GMT';
var never = cookie_date(2037,1,1,0,0,0);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// get date, fix for macs

function cookie_date (y,m,d,h,n,s) {
  var then = new Date(y,m,d,h,n,s);
  return fix_time(then);
}
function cookie_days (d) {
  var then = new Date();
      then.setTime(then.getTime() + (d * 86400 * 1000));
  return fix_time(then);
}
function fix_time (then) {
  var base = new Date(0);
  var skew = base.getTime();

  if (skew > 0) {
    then.setTime(then.getTime() - skew);
  }
  return then;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// get cookies

function get_cookies () {
  var set = document.cookie.split('; ');
  var cookie = new Array();

  for (i = 0; i < set.length; i++) {
    var list = set[i].split('=');
    cookie[list[0]] = unescape(list[1]);
  }
  return cookie;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// get cookie

function get_cookie (name) {
  if (! cookie_jar) {
    cookie_jar = get_cookies();
  }
  return cookie_jar[name];
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// set cookie

function set_cookie (name,value,expires,domain,path,secure) {
  var list = [ name + '=' + escape(value) ];
    if (expires) list.push(set_expires(expires));
    if (domain) list.push(set_domain(domain));
    if (path) list.push(set_path(path));
    if (secure) list.push('secure');
  document.cookie = list.join('; ');
}
function set_expires (expires) {
  if (expires == 'never') expires = never;
  return 'expires=' + expires.toGMTString();
}
function set_domain (domain) {
  if (domain == 'default') domain = default_domain;
  return 'domain=' + domain;
}
function set_path (path) {
  if (path == 'default') path = default_path;
  return 'path=' + path;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// delete cookie

function delete_cookie (name,domain,path) {
  if (get_cookie(name)) {
    var list = [ name + '=', 'expires=' + forthwith ];
      if (domain) list.push(set_domain(domain));
      if (path) list.push(set_path(path));
    document.cookie = list.join('; ');
  }
  return;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

