var AutoRefills = Class.create({
	
	initialize: function() {
		var id = jsonrpc.fetch( 'refills.information', 
			[],
			{ 
				onSuccess: this._load_data.bind(this),
				onFailure: function () { console.log( 'Refills JSON failed to load ' ) }
			}
		);
		this.pending = $H();
		this.inCartPending = $H();
		this.userConfirmed = false;
		return id;
	},
	_load_data: function (t) {
		var o = t.responseText.evalJSON()[0];
		this.confirmed = o.result;
		console.log('refills loaded');
		document.fire("refills:loaded");
	},
	add: function (SKU_ID, FREQUENCY, success, failure) {
		success = success || function () {};
		failure = failure || function () {};
		var id = jsonrpc.fetch( 'refills.add', 
			[{
				SKU_ID: SKU_ID,
				FREQUENCY: FREQUENCY
			}],
			{ 
				onSuccess: success,
				onFailure: failure
			}
		);
		return id;		
	},
	remove: function (SKU_ID, success, failure) {
		// either SKU_ID or Refill ID work
		success = success || function () {};
		failure = failure || function () {};
		var id = jsonrpc.fetch( 'refill.remove', 
			[{
				SKU_ID: SKU_ID
			}],
			{ 
				onSuccess: success,
				onFailure: failure
			}
		);
		return id;		
	},
	update_frequency: function (SKU_ID, FREQUENCY, success, failure) {
		// either SKU_ID or Refill ID work
		success = success || function () {};
		failure = failure || function () {};
		var id = jsonrpc.fetch( 'refill.update_frequency', 
			[{
				SKU_ID: SKU_ID,
				FREQUENCY: FREQUENCY
			}],
			{ 
				onSuccess: success,
				onFailure: failure
			}
		);
		return id;		
	},
	update_next_shipment: function (SKU_ID, DATE, success, failure) {
		// either SKU_ID or Refill ID work
		success = success || function () {};
		failure = failure || function () {};
		var id = jsonrpc.fetch( 'refill.update_next_shipment', 
			[{
				SKU_ID: SKU_ID,
				DATE: DATE
			}],
			{ 
				onSuccess: success,
				onFailure: failure
			}
		);
		return id;		
	},	
	set_pending: function (SKU_ID, FREQUENCY, success, failure) {
		success = success || function () {};
		failure = failure || function () {};
		
		// Save this info here so we have it for reference
		this.pending.set(SKU_ID,FREQUENCY);
		this.userConfirmed = false;
		
		// Now go tell the server
		var id = jsonrpc.fetch( 'refills.set_pending', 
			[{
				SKU_ID: SKU_ID,
				FREQUENCY: FREQUENCY
			}],
			{ 
				onSuccess: success,
				onFailure: failure
			}
		);
		return id;		
	},
	clear_pending: function (success, failure) {
		success = success || function () {};
		failure = failure || function () {};
		var id = jsonrpc.fetch( 'refills.clear_pending', 
			[],
			{ 
				onSuccess: success,
				onFailure: failure
			}
		);
		return id;		
	},
	get_pending: function () {
		this.pending = new Hash();
		var id = jsonrpc.fetch( 'refills.get_pending', 
			[],
			{ 
				onSuccess: this._load_pending.bind(this),
				onFailure: function () { console.log( 'Refills JSON failed to load pending' ) }
			}
		);
		return id;		
	},
	_load_pending: function (t) {
		var o = t.responseText.evalJSON()[0];
		this.pending = $H(o.result);
		document.fire("refills:pending_loaded");
	}		
});