window.utils = {};
(function(ns) {

var webglcontexts = ['webgl','moz-webgl','webkit-3d','experimental-webgl','webkit-3d'];
var websocketimpls = ['WebSocket','MozWebSocket'];
var requestanimationnames = ['requestAnimationFrame','webkitRequestAnimationFrame','mozRequestAnimationFrame','oRequestAnimationFrame','msRequestAnimationFrame'];

function detectWebGL()
{
	try
	{
		var x = document.createElement('canvas');
		if (x && typeof(x.getContext) === 'function')
			for (var i = 0, contexts = webglcontexts; i != contexts.length; ++i)
				if (x.getContext(contexts[i]))
					return true;
	}
	catch (e)
	{
	}
	return false;
}

function detectCanvas()
{
	try
	{
		return typeof(document.createElement('canvas').getContext) === 'function';
	}
	catch (e)
	{
		return false;
	}
}

function detectAnimationFrame()
{
	for (var i = 0; i != requestanimationnames.length; ++i)
	{
		var implname = requestanimationnames[i];

		if (!!window[implname])
		{
			window.requestAnimationFrame = window[implname];
			return true;
		}
	}

	return false;
}

function detectMedia(type)
{
	try
	{
		return typeof(document.createElement(type).canPlayType) === 'function';
	}
	catch (e)
	{
		return false;
	}
}

function detectLocalStorage()
{
	return !!window.localStorage;
}

function detectFileSystem()
{
	return !!window.requestFileSystem;
}

function detectOfflineApp()
{
	return !!window.applicationCache;
}

function detectStandalone()
{
	return !!window.navigator.standalone;
}

function detectWebSocket()
{
	for (var i = 0; i != websocketimpls.length; ++i)
	{
		var implname = websocketimpls[i];

		if (implname in window)
		{
			window.WebSocket = window[implname];
			return true;
		}
	}

	return false;
}

function detectAppMozilla()
{
	return !!navigator.apps;
}

function detectAppChrome()
{
	return !!window.chrome && !!window.chrome.app;
}

function detectAppIE()
{
	return !!window.external && typeof(window.external.msIsSiteMode) == 'unknown'; /* TODO: find a better way, this is fishy */
}

function detectProtocolHandler()
{
	return !!navigator.registerProtocolHandler;
}

ns.webglcontexts = webglcontexts;
ns.standalone = detectStandalone();
ns.supports =
{
	multimedia:
	{
		canvas: detectCanvas(),
		video: detectMedia('video'),
		audio: detectMedia('audio'),
		webgl: detectWebGL(),
		websocket: detectWebSocket(),
		animationframe: detectAnimationFrame()
	},
	storage:
	{
		localstorage: detectLocalStorage(),
		filesystem: detectFileSystem(),
		offlineapp: detectOfflineApp()
	},
	app:
	{
		mozilla: detectAppMozilla(),
		chrome: detectAppChrome(),
		ie: detectAppIE(),
		protocolhandler: detectProtocolHandler()
	}
};

})(utils);

