var CartListener = new CartJS();

//class to call AJAX PHP account functions, such as login, logout, register, lostpass
function CartJS() {
	this.refreshList = new Array();
	this.refreshId = null;
}



//---------------------------ADD CART ITEM
//add cart item
CartJS.prototype.add = function(args) {
	this.returnTarget = isset(args['target']) ? args['target'] : null;
	this.returnFunc = isset(args['func']) ? args['func'] : null;
	delete(args['target']);
	delete(args['func']);
	
	//alert('add to cart ' + itemId + ', ' + quantity);
	this.refreshId = args['id'];
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'add',
					args:			args, 
					return_target:	this, 
					return_func:	'changeReturn'});
					/**/
}

//---------------------------REMOVE CART ITEM
//add cart item
CartJS.prototype.removeAccount = function(args) {
	this.returnTarget = isset(args['target']) ? args['target'] : null;
	this.returnFunc = isset(args['func']) ? args['func'] : null;
	delete(args['target']);
	delete(args['func']);
	
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'removeAccount',
					args:			args, 
					return_target:	this, 
					return_func:	'changeReturn'});
}



//---------------------------UPDATE QUANTITY
//update cart quantity
CartJS.prototype.updateQuantity = function(args) {
	this.returnTarget = isset(args['target']) ? args['target'] : null;
	this.returnFunc = isset(args['func']) ? args['func'] : null;
	delete(args['target']);
	delete(args['func']);
	
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'updateQuantity',
					args:			args, 
					return_target:	this, 
					return_func:	'changeReturn'});
}



//---------------------------UPDATE QUANTITY
//update cart quantity
CartJS.prototype.findAccount = function(args) {
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'findAccount',
					args:			null, 
					return_target:	args['target'], 
					return_func:	args['func']});
}

//clear cart
CartJS.prototype.clearAccount = function(args) {
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'clearAccount',
					args:			null, 
					return_target:	args['target'], 
					return_func:	args['func']});
}





//sends the cart order
CartJS.prototype.sendOrder = function(args) {
	var returnTarget = isset(args['target']) ? args['target'] : null;
	var returnFunc = isset(args['func']) ? args['func'] : null;
	delete(args['target']);
	delete(args['func']);
	
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'sendOrder',
					args:			args, 
					return_target:	returnTarget, 
					return_func:	returnFunc});
}


//---------------------------CALLED AFTER ADD OR REMOVE
//finishes above
CartJS.prototype.changeReturn = function(config) {
	if (config) {
		//alert('Changed ' + config['action'] + ', ' + config['success']);
		if (this.refreshList.length > 0) {
			this.refresh();
		}
	} else {
		//alert('cart error');
	}
	
	if (this.returnFunc != null) {
		callFunc(this.returnTarget, this.returnFunc, config);
	}
}


//---------------------------UPDATES
//add update function to cart (so when cart updates it gets called)
CartJS.prototype.addUpdate = function(target, func) {
	this.refreshList.push({target:target, func:func});
	this.refresh();
}


//gets cart data ready for update
CartJS.prototype.refresh = function() {
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'createSidebar',
					args:			{id:this.refreshId}, 
					return_target:	this, 
					return_func:	'refreshReturn'});
}

//call all attached update functions
CartJS.prototype.refreshReturn = function(config) {
	for (var i=0; i<this.refreshList.length; i++) {
		var target =	this.refreshList[i]['target'];
		var func =		this.refreshList[i]['func'];
		
		callFunc(target, func, config);
	}
	
}
/*
function CartJS(DOC_MODULE_PATH, refresh_func) {
	this.refresh_func = refresh_func;
	// look at replacing DOC_MODULE_PATH with DOC_MODULE_PATH
}


//refresh (calls no function but updates the values)
CartJS.prototype.refresh = function() {
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'index',
					args:			null, 
					return_target:	this, 
					return_func:	'refresh_func'});
	
	
}

//add an item
CartJS.prototype.add = function(cart_id, qty, opts) {
	var args = {id:cart_id, qty:qty, opts:opts};
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'add',
					args:			args, 
					return_target:	this, 
					return_func:	'refresh_func'});
	
}

//remove an item
CartJS.prototype.remove = function(cart_id) {
	var args = {id:cart_id};
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'remove',
					args:			args, 
					return_target:	this, 
					return_func:	'refresh_func'});
}


//edit an array of items
CartJS.prototype.edit = function(item_array) {
	var cart_item;
	var args = new Array();
	
	for (var i=0; i<item_array.length; i++) {
		cart_item = item_array[i];
		if (i > 0) {args += ',';}
		args.push({id:cart_item.id, qty:cart_item.qty, opts:cart_item.opts});
	}
	
	ajaxRequest({	module:			'cart',
					target:			'Cart',
					func:			'update',
					args:			args, 
					return_target:	this, 
					return_func:	'refresh_func'});
}
/**/