/* 
 * Cart
 *--------------------------------------------------------------------------*/

var Cart = function(list, total, g) {
	this.initialize(list, total, g);
};
Cart.prototype = {
	list: null,
	total: null,
	g: null, g_top: 0,
	res: null,
	initialize: function(list, total, g) {
		this.list  = xGetElementById(list);
		this.total = xGetElementById(total);
		this.g     = xGetElementById(g);
		var det = xGetElementById('detail_main');
		this.g_top = xHeight(det) + 120;
		xTop(this.g, this.g_top);
	},
	onSubmit: function(f) {
		if (this.list  == null) return true;
		if (this.total == null) return true;
		if (this.g     == null) return true;
		var form = 'ADD_GOODS=1&hinban=' + f.hinban.value
		 + '&color=' + f.color.value
		 + '&amount=' + f.amount.value;
		var opt = {
		  method: 'post'
		, parameters: form
		, onSuccess: this.setResult.bind(this)
		};
		new Ajax.Request('cart.cgi', opt);
		return false;
	},
	setResult: function(q) {
		var txt = decodeURIComponent(q.responseText);
		this.res = null;
		eval("try { this.res = txt.parseJSON(); } catch (e) { }");
		if (this.res == null) return;
		if (this.res.err) alert(this.res.err);
		
		if (this.res.get == 1) {
			xLeft(this.g, 670);
			xTop(this.g,  this.g_top);
			xDisplay(this.g, 'block');
			var y = 0;
			if (this.res.idx) {
				if (!isNaN(this.res.idx)) y = this.res.idx * 20;
			}
			xAnimateXY(this.g, 717, 200 + y, 500, this.setCart.bind(this));
		} else {
			if (this.res.list       != null) xInnerHtml(this.list, this.res.list);
			if (this.res.cart_total != null) xInnerHtml(this.total, this.res.cart_total);
		}
	},
	setCart: function() {
		if (this.res == null) return;
		xDisplay(this.g, 'none');
		if (this.res.list       != null) xInnerHtml(this.list, this.res.list);
		if (this.res.cart_total != null) xInnerHtml(this.total, this.res.cart_total);
	},
	loadCart: function() {
		var opt = {
		  method: 'get'
		, onComplete: this.setResult.bind(this)
		};
		new Ajax.Request('cart.cgi', opt);
		return false;
	}
};

var cart = null;
function init_cart() {
	cart = new Cart('cart_list', 'cart_total', 'get_icon');
}
xAddEventListener(window, 'load', init_cart, true);

