var Clay = Clay || {};
// GameMaker related data is stored here... (a lot of callback stuff)
Clay['gamemaker'] = Clay['gamemaker'] || {};

/**
 * 
 * PAYMENT FUNCTIONS
 *  
 */

Clay['gamemaker']['itemAdded'] = {};
function clay_payment_item_add( itemID )
{
	Clay['gamemaker']['itemAdded'] = {};
	ClayPay['addItem']( { id: itemID }, function( response ) {
		if( response.success )
			Clay['gamemaker']['itemAdded'][ itemID ] = response;
	} );
}

function clay_payment_item_added_ready( itemID )
{
	return clay_check_ready( Clay['gamemaker']['itemAdded'][ itemID ] );
}

Clay['gamemaker']['itemRemoved'] = {};
function clay_payment_item_remove( itemID )
{
	Clay['gamemaker']['itemRemoved'] = {};
	ClayPay['removeItem']( { id: itemID }, function( response ) {
		if( response.success )
			Clay['gamemaker']['itemRemoved'][ itemID ] = response;
	} );
}

function clay_payment_item_removed_ready( itemID )
{
	return clay_check_ready( Clay['gamemaker']['itemRemoved'][ itemID ] );
}

Clay['gamemaker']['checkedOut'] = false;
function clay_payment_checkout()
{
	Clay['gamemaker']['checkedOut'] = false;
	ClayPay['checkout']( function( response ) {
		if( response.success )
			Clay['gamemaker']['checkedOut'] = response;
	} );
}

function clay_payment_game_purchase()
{
	Clay['gamemaker']['purchaseGame'] = false;
	Clay['Player']['purchaseGame']( function( response ) {
		if( response.success )
			Clay['gamemaker']['checkedOut'] = response;
	} );
}
var clay_game_purchased = clay_payment_checked_out_fail;

function clay_payment_checked_out_success()
{
	return clay_check_ready( Clay['gamemaker']['checkedOut'], ( Clay['gamemaker']['checkedOut'] && Clay['gamemaker']['checkedOut']['success'] ) );
}
// Alias
var clay_payment_game_purchased_success = clay_payment_checked_out_success;

function clay_payment_checked_out_fail()
{
	return clay_check_ready( Clay['gamemaker']['checkedOut'], ( Clay['gamemaker']['checkedOut'] && ! Clay['gamemaker']['checkedOut']['success'] ) );
}
// Alias
var clay_payment_game_purchased_fail = clay_payment_checked_out_fail;

function clay_payment_checked_out_count()
{
	if( clay_payment_checked_out_success() )
		return Clay['gamemaker']['checkedOut']['itemIds']['length'];
}

function clay_payment_checked_out_item_id_at( index )
{
	if( clay_payment_checked_out_success() )
		return Clay['gamemaker']['checkedOut']['itemIds'][ index ];
}
