Ext.onReady(function(){
	Appmaker = function() {
		return {
			init: function() {
				var qs = Ext.urlDecode(location.search.substring(1));
				if (qs.auth && qs.uuid) {
					Ext.Ajax.request({
						url: 'ajax/factory/action/session/Factory.html',
						params: {
							'tx_lalappfactory_pi2[uuid]': qs.uuid
						},
						success: function(response, opts) {
							if (response.responseText != '') {
								responseArray = response.responseText.split('$$');
								Appmaker.setCookie('fe_typo_user', responseArray[0], '/', window.location.hostname, false);
								if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
									document.location.href = 'mobile/thanks.html?app=' + responseArray[1];
								} else {
									document.location.href = 'build-an-app.html';
								}
							} else {
								console.log('not logged in!');
							}
						},
						failure: function(response, opts) {
							console.log('server-side failure with status code ' + response.status + ' on App setup ');
						}
					});
				}
			},
			getStarted: function() {
				if (Ext.isIE || Ext.isSafari2) {
					document.location.href = 'fb/browsercheck.html';
				} else {
					var qs = Ext.urlDecode(location.search.substring(1));
					if (!qs.auth && !qs.uuid) {
						document.location.href = 'snconnect/connect.php?connect=1&cont=authorize';
					} 
				}
			},
			setCookie: function(name, value, path, domain, secure) {
				var parts = domain.toString().split('.'.toString());
				domain = parts[1] + '.' + parts[2];

				var cookieText = escape(name) + '=' + escape(value.replace (/^\s+/, '').replace (/\s+$/, ''));
					cookieText += (path ? '; PATH=' + path : '');
				document.cookie = cookieText;
			},
			getInstaller: function() {
				var qs = Ext.urlDecode(location.search.substring(1));
				if (qs.app) {
					document.location.href = 'ajax/factory/action/instant/Content/app/' + qs.app + '.html';
				}
			}
		};
	}();
	Appmaker.init();
});
