﻿var iPaper = function () {

	// EventDispatcher
	function eventDispatcher() {
		this.listeners = {};
	};

	eventDispatcher.prototype.dispatchEvent = function (event, data) {
		var result = {};

		if (this.listeners[event] != null)
			for (index in this.listeners[event])
				$.extend(result, this.listeners[event][index](data));

		return result;
	};

	eventDispatcher.prototype.addEventListener = function (event, callback) {
		if (this.listeners[event] == null)
			this.listeners[event] = new Array();

		this.listeners[event].push(callback);
	};

	// Notify parents of API readyness
	$(function () {
		try {
			window.parent.iPaperInit({
				// Publics
				Shop: shopCatalog
			});
		}
		catch (e) { }
	});

	// Privates
	var shopCatalog = new eventDispatcher();
	var shopFacade = new eventDispatcher();
	shopCatalog.addItem = function (item) { shopFacade.dispatchEvent("addItem", item); }

	// Internals
	return {
		ShopCatalog: shopCatalog,
		ShopCatelog: shopCatalog,
		ShopFacade: shopFacade
	}

} ();
