// This file requires these libraries:
//
//   cookiemanager.js
//   prototype.js (required by cookiemanager.js)
//

function Cart(cart_name) {
    this.cart_name = cart_name;

    this.cookiemanager = new CookieManager();

    // 有効期限を 14 日 (= 2 週間) に設定する
    this.cookiemanager.cookieShelfLife = 14;

    var val = this.cookiemanager.getCookie(cart_name);
	this.data = {}
    if ((val != null) && (val != "")) {
        tmp = val.split(this.SEPARATOR1);
		if (typeof(tmp)=='string') {
			tmp2 = tmp.split(this.SEPARATOR2);
			this.data[tmp2[0]] = unescape(tmp2[1]);
		}
		else {
			for (i=0; i<tmp.length; i++) {
				tmp2 = tmp[i].split(this.SEPARATOR2);
				this.data[tmp2[0]] = unescape(tmp2[1]);
			}
		}
    }

    this._itemhash = null;
}

Cart.prototype = {

    SEPARATOR1: ",",
    SEPARATOR2: ":",

    contains: function(item) {
        this._update_itemhash();
        return this._itemhash[item];
    },

    count: function() {
		c = 0;
		for (var i in this.data) {
			c++;
		}
        return c;
    },

    add: function(items, names) {
        this._update_itemhash();

        var n = 0;
        for (var i = 0; i < items.length; i++) {
            if (! this._itemhash[items[i]]) {
                this.data[items[i]] = names[i];
                this._itemhash[items[i]] = names[i];
                n++;
            }
        }
		/*
		tmp = '';
		for (var i in this.data) {
			tmp += i + this.SEPARATOR2 + this.data[i];
		}
		alert(tmp);
		*/

        return n;
    },

    del: function(items) {
		for (var i in this.data) {
			if (i==items) {
				delete this.data[i];
			}
		}
		/*
        this.data = this.data.reject(function(x) {
                return items.find(function(y) {
                        return x == y;
                    });
            });
		*/
        this._itemhash = null;
    },

    clear: function() {
        this.cookiemanager.clearCookie(this.cart_name);
        this.data = {}
        this._itemhash = null;
    },

    save: function() {
		tmp = [];
		for (var i in this.data) {
			tmp.push(i + this.SEPARATOR2 + escape(this.data[i]));
		}
		tmp = tmp.join(this.SEPARATOR1);
        this.cookiemanager.setCookie(this.cart_name, tmp);
    },

	get_names: function() {
		names = [];
		for (var i in this.data) {
			names.push(this.data[i]);
		}
		return names;
	},

    _update_itemhash: function() {
        if (this._itemhash == null) {
            this._itemhash = {};
            for (var i in this.data) {
                this._itemhash[i] = this.data[i];
            }
			/*
            for (var i = 0; i < this.data.length; i++) {
                this._itemhash[this.data[i]] = 1;
            }
			*/
        }
    }
};

function update_cart(cookie_name) {
  if (typeof(cart)=='undefined') {
    cart = new Cart(cookie_name);
  }

  var cart_num = cart.count();

  if ($('cart_empty')) {
  if (cart_num==0) {
    $('cart_empty').style.display = '';
    $('cart_now').style.display = 'none';
    $('cart_list').style.display = 'none';
  }
  else {
    $('cart_empty').style.display = 'none';
    $('cart_now').style.display = '';
    $('cart_list').style.display = '';

    $('cart_num').innerHTML = cart_num;

    names = cart.get_names();
    len = names.length;
    html = '';
    for(i=len-1; i>=0; i--) {
      html += '<li>' + names[i] + '</li>';
    }
    $('cart_list').innerHTML = html;
  }
  }
}

