File:KartKingdomPlugin.js

/**
 * @module Kart Kingdom
 * @namespace springroll
 * @requires Core
 */
(function(undefined)
{
	// Include classes
	var ApplicationPlugin = include('springroll.ApplicationPlugin');
	var KartKingdom = include('springroll.pbskids.KartKingdom');

	/**
	 * @class Application
	 */
	var plugin = new ApplicationPlugin();

	// Setup plugin before any display is setup
	plugin.setup = function()
	{
		this.once('configLoaded', function(config)
		{
			if (!config.kartkingdom)
			{
				if (DEBUG)
				{
					throw "Missing a kartkingdom config object. Please create a kartkingdom.json file in your config directory.";
				}
				else
				{
					throw "Missing config.kartkingdom.";
				}
			}
			else if (!config.kartkingdom.gameID)
			{
				if (DEBUG)
				{
					throw "Missing a guid for the gameID param on the kartkingdom config object. Please define, { 'gameID': guid }, in your kartkingdom.json file.";
				}
				else
				{
					throw 'GUID for the Minigame was not provided.';
				}
			}

			/**
			 * The instance of the wrapper for talking to the Kart Kingdom Minigame API.
			 * @property {springroll.pbskids.KartKingdom} kartkingdom
			 */
			this.kartkingdom = new KartKingdom(config.kartkingdom);
		});
	};

	// Preload the plugin
	plugin.preload = function(done)
	{
		if (!this.container)
		{
			if (DEBUG)
			{
				throw "Container Client module is required to use the Kart Kingdom API";
			}
			else
			{
				throw "Missing container-client";
			}
		}
		this.kartkingdom.init(this.container, done);
	};

	// Clean-up when the application is destroyed
	plugin.teardown = function()
	{
		if (this.kartkingdom)
		{
			this.kartkingdom.destroy();
			this.kartkingdom = null;
		}
	};

}());