// written by Jason Sebring (http://www.jasonsebring.com) on February 24th, 2009, reuse at own peril
var cookieCart = {};
cookieCart.items = [];
cookieCart.daysToExpire = 20;
cookieCart.cookieName = 'cookiecart';
cookieCart.alreadyInitialized = false;
cookieCart.clearCookie = function() {
	this.items = [];
	expireDate = new Date(); expireDate.setTime(expireDate.getTime() - (1000 * 60 * 60 * 24 * this.daysToExpire));
	document.cookie = this.cookieName + '=;path=/;expires=' + expireDate.toGMTString();
};
cookieCart.populateFromCookie = function() {
	if (!document.cookie || document.cookie.length < 1) return;
	var s = document.cookie.search('(\\s*|\\s*;\\s)' + this.cookieName + '=');
	if (s == -1) return;
	var sub = document.cookie.substring(s);
	s = document.cookie.indexOf(this.cookieName + '=');
	if (s == -1) return;
	s += this.cookieName.length+1;
	var e = document.cookie.indexOf(';',s)
	if (e == -1) e = document.cookie.length;
	var v = document.cookie.substring(s,e);
	pairs = v.split('&');
	this.items = [];
	for (var i = 0; i < pairs.length; i++) {
		pair = pairs[i].split('=');
		if (pair.length == 2) {
			this.items[this.items.length] = {};
			this.items[this.items.length-1].key = unescape(pair[0]);
			this.items[this.items.length-1].val = unescape(pair[1]);
		}
	}
};
cookieCart.repopulateCookie = function() {
	var cookieValue = '';
	for (var i = 0; i < this.items.length; i++) {
		cookieValue += '&' +  escape(this.items[i].key) + '=' + escape(this.items[i].val);
	}
	cookieValue = cookieValue.substr(1);
	var expireDate = new Date(); expireDate.setDate(expireDate.getDate() + this.daysToExpire);
	document.cookie = this.cookieName + '=' + cookieValue + ';path=/;expires=' + expireDate.toGMTString();
};
cookieCart.getItemIndex = function(name) {
	for (var i = 0; i < cookieCart.items.length; i++) {
		if (this.items[i].key == name)
			return i;
	}
	this.items[this.items.length] = {};
	this.items[this.items.length-1].key = name;
	this.items[this.items.length-1].val = 0;
	return this.items.length - 1;
}
cookieCart.addQuantity = function(name,quantity) {
	var i = this.getItemIndex(name);
	this.items[i].val = parseInt(this.items[i].val) + parseInt(quantity);
	this.repopulateCookie();
};
cookieCart.set = function(name,quantity) {
	var i = this.getItemIndex(name);
	this.items[i].val = parseInt(quantity);
	this.repopulateCookie();
};
cookieCart.add = function(name) {
	var i = this.getItemIndex(name);
	this.items[i].val = parseInt(this.items[i].val) + 1;
	this.repopulateCookie();
};
cookieCart.subtract = function(name) {
	var i = this.getItemIndex(name);
	if (parseInt(this.items[i].val) > 0)
		this.items[i].val = parseInt(this.items[i].val) - 1;
	this.repopulateCookie();
};
cookieCart.remove = function(name) {
	var i = this.getItemIndex(name);
	this.items.splice(i,1);
	this.repopulateCookie();
};
cookieCart.itemCount = function() {
	var count = 0;
	for (var i = 0; i < this.items.length; i++) {
		count += parseInt(this.items[i].val);
	}
	return count;
};
cookieCart.init = function() {
	if (!this.alreadyInitialized)
		this.populateFromCookie();
	this.alreadyInitialized = true;
};
