jQuery(document).ready(function() {
	app.facebook.init();
	app.typekit.init();
	app.bugherd.init();
	app.ga.init();
});

var app = {
	append_async_script: function(src, onload) 
	{
		var scr = document.createElement('script');
		scr.src = src;
		scr.type = 'text/javascript';
		scr.async = true;
		if (jQuery.isFunction(onload))
		{
			scr.onload = scr.onreadystatechange = onload;
		}
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(scr, s);
	}
};

$.extend(true, app, { facebook: {
	
	init: function() {
		// first, faceblerks.
		if (app.facebook.enabled)
		{
			window.fbAsyncInit = app.facebook.async_init;
			var url = document.location.protocol + '//connect.facebook.net/en_GB/all.js';
			app.append_async_script(url);
		}
	},
	
	async_init: function() {
		
		FB.init({
			appId: app.facebook.app_id, 
			status: true, 
			cookie: true, 
			xfbml: true,
			oauth : true
		});

		$('.js_fb_login').click(function(e) {
			e.preventDefault();
		// hokay now enable clicks. 
			FB.login(function(response) {
				if (response.authResponse) {

					if ((response.status == "connected"))
					{
						// all good, lets redirect. Give the server the token.
						window.location = app.facebook.reload_url + '?accessToken=' + response.authResponse.accessToken;
					} else {
						// user is logged in, but did not grant any permissions
					}
				} else {
					// user is not logged in
				}
			}, {scope:app.facebook.perms});
		});

		$('.js_fb_logout').click(function() {
			FB.logout(function(response) {
			  // user is now logged out
			});
		});

		if ($('.js_fb_profile').length > 0)
		{
			FB.api('/me&access_token=' + app.facebook.access_token, function(user) {
				if(user != null) {
					
					$('.js_fb_profile').each(function(i, el) {
						user_id = ($(el).attr('rel') && ($(el).attr('rel').length > 0)) ? $(el).attr('rel') : user.id
						$(el).attr('src', 'http://graph.facebook.com/' + user_id + '/picture?type=large');
					});
				}
			});
		}

		// when the user hits facebook logout, then we should logout too?
		FB.Event.subscribe('auth.logout')
		{
			// app.facebook.logout_url;
		}
	}
}});

$.extend(true, app, { typekit: {
	init: function() {
		if (app.typekit.enabled)
		{
		  TypekitConfig = {
				kitId: app.typekit.id
		  };
			var url = ('https:' == document.location.protocol ? 'https:' : 'http:') + '//use.typekit.com/' + TypekitConfig.kitId + '.js';

			app.append_async_script(url, function() {
				var rs = this.readyState;
				if (rs && rs != 'complete' && rs != 'loaded') return;
				try { Typekit.load(TypekitConfig); } catch (e) {}
			});
		}
	}
}});

$.extend(true, app, { ga: {
	init: function() {
		if (app.ga.enabled)
		{
			if (typeof(_gaq) == 'undefined')
			{
				_gaq = [];
			}
			_gaq.push(['_setAccount', app.ga.id]);
			if (app.ga.domain)
			{
			  _gaq.push(['_setDomainName', '.' + app.ga.domain]);
			}
			_gaq.push(['_trackPageview']);

			var url = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			app.append_async_script(url);    
		}
	}
}});

$.extend(true, app, { bugherd: {
	init: function() {
		if (app.bugherd.enabled)
		{
			window._bugHerdAPIKey = app.bugherd.id;

			var url = ('https:' == document.location.protocol ? 'https:' : 'http:') + '//www.bugherd.com/bugherd.js?s=' + (new Date()).getTime();

			app.append_async_script(url, function() {
				var rs = this.readyState;
				if (rs && rs != 'complete' && rs != 'loaded') return;
				try { Typekit.load(TypekitConfig); } catch (e) {}
			});
		}
	}
}});
