
if (typeof Blog === 'undefined') {
	var Blog = {
		'debug':function() { return false; }
	}
}

Blog.getVersion = function() {
	return '20111006.150100';
}

Blog.log = function() {
	if (!console) return;			// Don't do anything if browser doesn't support a console

	console.log(arguments[0]);		// TODO Change this so log either takes a string or an object to differentiate between severities
}

Blog.Ajax = function() {
	// Private
	var that = this;

	// Public
	return {
		'send':function(o) {
			var c = o || {};
			var isGET = typeof o == 'string' ? true : false;
			var method = (isGET ? 'GET' : c.method || 'GET').toUpperCase();
			var url = isGET ? o : c.url || '';
			var failure = o.failure || null;
			var success = o.success || null;

			var xhr = new XMLHttpRequest();
			xhr.onreadystatechange = function() {
				if (this.readyState == 4 && this.status == 200) {
					if (this.responseText != null) {
						Blog.log('responseText: ' + this.responseText);
					} else {
						Blog.log('responseText n/a');
					}
					if (null != success) success(this);
//				} else if (this.readyState == 4 && this.status != 200) {
				} else {
					Blog.log('readyState: ' + this.readyState + ', status: ' + this.status);
					if (null != failure) failure(this);
				}
			}
			xhr.open(method, url);
			xhr.send();
		}
	}
}();

Blog.API = function() {
	// Private
	var that = Blog;

	// Public
	return this;
}();

Blog.API.GeoLocation = function() {
	// Private
	var that = Blog.API;

	// Public
	return {
		'init':function() {
			Blog.log('Blog.API.GeoLocation.init(): Begin');

			if (!navigator.geolocation) return;

			navigator.geolocation.getCurrentPosition(
				function(position) {
					Blog.log('Blog.API.GeoLocation.init(): Latitude = ' + position.coords.latitude + ', Longitude = ' + position.coords.longitude);
				},
				function(error) {
					Blog.log('Error: ' + error.code);
					switch(error.code) {
						case error.PERMISSION_DENIED: Blog.log('User did not share geolocation');
						break;
						case error.POSITION_UNAVAILABLE: Blog.log('Could not detect current position');
						break;
						case error.TIMEOUT: Blog.log('Retrieving position timeout');
						break;
						default: Blog.log('Unknown error');
						break;
					}
				}
			);

			Blog.log('Blog.API.GeoLocation.init(): End');
		}
	}
}();

Blog.Tracking = function() {
	// Private
	var that = Blog;

	// Public
	return this;
}();

Blog.Tracking.GoogleAnalytics = function() {
	// Private
	var that = Blog;

	// Public
	return {
		'init':function() {
			Blog.log('Blog.Tracking.GoogleAnalytics.init(): Begin');
	
			var gaAccount = 'UA-26172529-1';
			var gaDomainName = '.johnfoldager.com';

			var head = document.getElementsByTagName('head')[0];
			var script = document.createElement('script');
			var source = document.createTextNode(
				'var _gaq = _gaq || [];' +
				'_gaq.push(["_setAccount", "' + gaAccount + '"]);' +
				'_gaq.push(["_setDomainName", "' + gaDomainName + '"]);' +
				'_gaq.push(["_trackPageview"]);' +
				'(function() {' +
				'var ga = document.createElement("script");' +
				'ga.type = "text/javascript";' +
				'ga.async = true;' +
				'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";' +
				'var s = document.getElementsByTagName("script")[0];' +
				's.parentNode.insertBefore(ga, s);' +
				'})();'
			);
			script.appendChild(source);
			head.appendChild(script);
	
			Blog.log('Blog.Tracking.GoogleAnalytics.init(): End');
		}
	}
}();

Blog.Social = function() {
	// Private
	var that = Blog;

	// Public
	return this;
}();

Blog.Social.Twitter = function() {
	// Private
	var that = Blog.Social;

	// Public
	return {
		'init':function() {
			Blog.log('Blog.Twitter.init(): Begin');

			var head = document.getElementsByTagName('head')[0];
			var script = document.createElement('script');
			script.setAttribute('src', '//platform.twitter.com/widgets.js');
			head.appendChild(script);

			Blog.log('Blog.Twitter.init(): End');
		}
	}
}();

Blog.Social.GooglePlus = function() {
	// Private
	var that = Blog.Social;

	// Public
	return {
		'init':function() {
			Blog.log('Blog.GooglePlus.init(): Begin');

			var div = document.querySelector('.blog-social-googleplus');

//			var head = document.getElementsByTagName('head')[0];
//			var script = document.createElement('script');
//			script.setAttribute('src', '//connect.facebook.net/en_US/all.js#xfbml=1');
//			head.appendChild(script);

			Blog.log('Blog.GooglePlus.init(): End');
		}
	}
}();

Blog.Social.Facebook = function() {
	// Private
	var that = Blog.Social;

	// Public
	return {
		'init':function() {
			Blog.log('Blog.Facebook.init(): Begin');

			if (document.getElementById('facebook-jssdk')) return;		// Do not load Facebook JavaScript API more than once

			var head = document.getElementsByTagName('head')[0];
			var script = document.createElement('script');
			script.setAttribute('id', 'facebook-jssdk');
			script.setAttribute('src', '//connect.facebook.net/en_US/all.js#xfbml=1');
			head.appendChild(script);

			Blog.log('Blog.Facebook.init(): End');
		}
	}
}();

(function(){
	window.addEventListener('load', Blog.Social.Twitter.init, false);
	window.addEventListener('load', Blog.Social.GooglePlus.init, false);
	window.addEventListener('load', Blog.Social.Facebook.init, false);
/*
	window.addEventListener('load', Blog.API.GeoLocation.init, false)
	window.addEventListener('load', Blog.Tracking.GoogleAnalytics.init, false)

	window.addEventListener('load', function() {
		Blog.Ajax.send('http://johnfoldager.com/blog/cache.manifest');
	}, false);
	window.addEventListener('load', function() {
		Blog.Ajax.send('http://johnfoldager.com/blogx');
	}, false);
*/
	window.addEventListener('load', function() {
		Blog.Ajax.send({
			'method':'GET',
			'url':'http://johnfoldager.com/robots.txt',
			'success':function() {
				alert('SUCCESS:\nreadyState: ' + this.readyState + '\nStatus: ' + this.status + '\nresponseText: ' + this.responseText);
			},
			'failure':function() {
				alert('FAILURE:\nreadyState: ' + this.readyState + '\nStatus: ' + this.status);
			}
		});
	}, false);

})();

