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


function clay_init_super( gameKey, debug )
{
	Clay['gameKey'] = gameKey;
	Clay['readyFunctions'] = [];
	Clay['ready'] = function( fn ) {
	    Clay['readyFunctions']['push']( fn );
	};
	
	if( typeof debug !== 'undefined' && debug )
		Clay['options'] = { debug: true };
		
	( function() {
	    var clay = document.createElement("script");
	    clay['src'] = ( "https:" == document.location.protocol ? "https://" : "http://" ) + "clay.io/api/api.js"; 
	    var tag = document.getElementsByTagName("script")[0]; tag.parentNode.insertBefore(clay, tag);
	} )();
	
	Clay['ready']( function() {
		ClayPay = new Clay['Payment']();
	} );
}

/**
 * Loads in the Clay.io API
 * @param {String} gameKey
 */
function clay_init( gameKey )
{
	clay_init_super( gameKey );
}

/**
 * Loads in the Clay.io API with debug mode enabled
 * @param {String} gameKey
 */
function clay_init_debug( gameKey )
{
	clay_init_super( gameKey, true );
}

/**
 * Performs a simple console.log
 * @param {Object} object 
 */
function clay_log( object )
{
	Clay.log( object );
}

/**
 * Closes all open Clay.io modals/notifications
 * @param {Object} object 
 */
function clay_close_all_ui( object )
{
	Clay.UI.closeAllUI();
}

/**
 * Returns true when the API is fully loaded in 
 */
function clay_ready()
{
	return Clay['isReady'] ? true : false;
}

/**
 * Used by each ready check function to see if the given data is ready (pseudo-callback) 
 * @param {Object} data
 */
function clay_check_ready( data, secondaryCondition )
{
	if( typeof secondaryCondition === 'undefined' )
		secondaryCondition = true;
		
	if( data && secondaryCondition )
	{
		// This function has already been checked
		if( data['checked'] )
			return 'checked';
			
		// Mark the ready as checked to prevent if statements from constantly returning true...
		data['checked'] = true;
		return true;
	}
	else
		return false;	
}