(function() {
	var App = {
		base_url: "/",

		init: function() {
			App.FBWidget.init();

			App.FBApp.init();

			App.load_flash("media-main", "media/Avon25plus.swf", { base_url: App.base_url });
		},

		load_flash: function(where, what_flash, flashvars) {
			swfobject.embedSWF(
				App.base_url + what_flash,
				where, "100%", "100%", "10.0.0",
				App.base_url +"media/expressInstall.swf",
				{},
				{ menu: "false", scale: "noScale", allowFullscreen: "true", allowScriptAccess: "always", bgcolor: "#000000", flashvars: flashvars ? PHPJS.http_build_query(flashvars) : '', wmode: "transparent" },
				{}
			);
		},

		// fb widget
		FBWidget: {
			init: function() {
				App.FBWidget.hide();
				$("#fb-close").click(function() {
					App.FBWidget.hide();
					document.getElementById("media-main").removeWhiteScreen();
					return false;
				});
			},

			show: function() {
				$("#fb-popup").show();
			},

			hide: function() {
				$("#fb-popup").hide();
			}
		},

		// fb app
		FBApp: {
			id: "131168153568583",

			current_user: null,

			init: function() {
				FB.init({ appId : App.FBApp.id, status: true, cookie: true, xfbml : false });
			},

			login: function() {
				if (App.FBApp.current_user) {
					// call media method
					document.getElementById("media-main").SendFBData(JSON.stringify(App.FBApp.current_user));
				}
				else {
					FB.login(function(fb_response) {
						if (fb_response.session) {
							// user is logged in
							FB.api("/me", function(fb_response) {
								var me = fb_response;
								FB.api("/me/friends", function(fb_response) {
									var friend_list = fb_response.data;
									App.FBApp.current_user = {
										id: me.id,
										name: me.name,
										first_name: me.first_name,
										last_name: me.last_name,
										friend_list: friend_list
									};

									// call media method
									document.getElementById("media-main").SendFBData(JSON.stringify(App.FBApp.current_user));
								});
							});
						}
						else {
							// user is not logged in or did not approve this app or whatever
							// call media method
							document.getElementById("media-main").RemoveBlackScreen();
						}
					}, {perms: 'read_friendlists, publish_stream'});
				}
			}
		}
	};

	var PHPJS = {
		// http://phpjs.org/functions/http_build_query
		http_build_query: function(formdata, numeric_prefix, arg_separator) {
			var value, key, tmp = [];

			var _http_build_query_helper = function(key, val, arg_separator) {
				var k, tmp = [];
				if (val === true) {
					val = "1";
				}
				else if (val === false) {
					val = "0";
				}
				if (val !== null && typeof(val) === "object") {
					for (k in val) {
						if (val[k] !== null) {
							tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));
						}
					}
					return tmp.join(arg_separator);
				}
				else if (typeof(val) !== "function") {
					return PHPJS.urlencode(key) + "=" + PHPJS.urlencode(val);
				}
				else {
					throw new Error('There was an error processing for http_build_query().');
				}
			};

			if (!arg_separator) {
				arg_separator = "&";
			}
			for (key in formdata) {
				value = formdata[key];
				if (numeric_prefix && !isNaN(key)) {
					key = String(numeric_prefix) + key;
				}
				tmp.push(_http_build_query_helper(key, value, arg_separator));
			}

			return tmp.join(arg_separator);
		},

		// http://phpjs.org/functions/urlencode
		urlencode: function(str) {
			str = (str+'').toString();
			// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
			// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
			return encodeURIComponent(str)
				.replace(/!/g, '%21')
				.replace(/'/g, '%27')
				.replace(/\(/g, '%28')
				.replace(/\)/g, '%29')
				.replace(/\*/g, '%2A')
				.replace(/%20/g, '+');
		}
	};

	window.App = App;
	window.PHPJS = PHPJS;
})();

