;(function(){

// Don't cache _gaq array as it'll be replaced with
// another object on GA load.
if (!window._gaq)
	window._gaq = []

var Me =
{
	setAccount: function (a) { this.account = a },
	
	load: function ()
	{
		if (this.loaded)
			return
		this.loaded = true
		
		// must be called before any other event tracking
		window._gaq.push(['_setAccount', this.account])
		window._gaq.push(['_trackPageview'])
		
		// untouched inclusion snippet
		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);
	},
	
	push: function (a)
	{
		this.load()
		window._gaq.push(a)
	},
	
	trackPageview: function () { this.load() }
}

Me.className = 'GoogleAnalytics'
self[Me.className] = Me

})();

GoogleAnalytics.setAccount('UA-1635720-11')

;(function(){

function log (str) { try { console.log(Me.className + ': ' + str) } catch (ex) {} }

// As far as Tracker could be used as error reporter
// wrap everything in try/catch blocks to avoid
// infinite reporting on error in Tracker itself.

var Me =
{
	event: function (category, action, label, value)
	{
		try // to track an event
		{
			// prepare types
			category = String(category)
			action = String(action)
			label = String(label)
			value = +value || 0
			
			log(category + '-' + action + ': ' + label + ' (' + value + ')')
			GoogleAnalytics.push(['_trackEvent', category, action, label, value])
			return true
		}
		catch (ex)
		{
			// to warn the developer
			log('could not report an event')
		}
	},
	
	path: function (path)
	{
		try // to track an event
		{
			// prepare types
			path = String(path)
			
			log('path' + ': ' + path)
			GoogleAnalytics.push(['_trackPageview', path])
			return true
		}
		catch (ex)
		{
			// to warn the developer
			log('could not report a path')
		}
	}
}

Me.className = 'Tracker'
self[Me.className] = Me

})();

// at this stage no fixes or wrappers are loaded from any lib
// except for Tracker object that hmm… tracks :)
;(function(){

function log (str) { try { console.log('Oops: ' + str) } catch (ex) {} }

var Me =
{
	enabled: false,
	masking: true,
	total: 0,
	
	handler: function (message, uri, line)
	{
		try
		{
			// forward masking mode with return
			return Me.onerror.apply(Me, arguments)
		}
		catch (ex) { log('error on reporting') }
	},
	
	onerror: function (message, uri, line)
	{
		if (message && message.target)
		{
			uri = message.target.src
			message = 'Error loading script'
			line = 1
		}
		
		// cutting out current page uri prefix
		uri = String(uri).replace(location.protocol + '//' + location.hostname, '')
		
		this.report('error', message + ' at ' + uri + ':' + line)
		
		// prevent error message from appearing in the browser console
		return this.masking
	},
	
	report: function (type, message)
	{
		try // to fully describe an error
		{
			Tracker.event('Oops', type, message, this.total++)
		}
		catch (ex)
		{
			log('could not report an error')
		}
		
		return true
	},
	
	maybeEnable: function ()
	{
		if (!/Oops=disabled/.test(document.cookie))
			this.enable()
	},
	
	enable: function ()
	{
		if (this.enabled)
			return
		this.enabled = true
		
		window.onerror = this.handler
		log('error catching enabled')
	},
	
	disable: function ()
	{
		if (!this.enabled)
			return
		this.enabled = false
		
		window.onerror = null
		log('error catching disabled')
	},
	
	log: function (message)
	{
		Tracker.event('Oops', 'log', message)
	},
	
	error: function (message)
	{
		Tracker.event('Oops', 'error', message)
	},
	
	time: function (message, time)
	{
		Tracker.event('Oops', 'time', message, time)
	}
}

Me.className = 'Oops'
self[Me.className] = Me

})();
Oops.maybeEnable()

