// requires google-analytics v2 added 3-13-2008
(function () {
	var window = this,
		document = window.document,
		gaJsHost = (('https:' === document.location.protocol.toString()) ?
			'https://ssl.' :
			'http://www.'),
		DDC = (window.DDC = (window.DDC || {})),
		unescape = window.unescape,
		isFunction = function (obj) {
			return typeof obj === 'function';
		},
		extend = function() {
			var target = arguments[0] || {},
				i = 1,
				length = arguments.length,
				deep = false,
				options,
				name,
				src,
				copy;
			
			if (typeof target === 'boolean') {
				deep = target;
				target = arguments[1] || {};
				i = 2;
			}
			
			if (typeof target !== 'object' && !isFunction(target)) {
				target = {};
			}
			
			if (length == i) {
				target = this;
				--i;
			}

			for (; i < length; i++) {
				if ((options = arguments[i]) != null) {
					for (name in options) {
						src = target[name];
						copy = options[name];
						
						if (target === copy) {
							continue;
						}
						
						if (deep && copy && typeof copy === 'object' &&
							!copy.nodeType) {
							target[name] = extend(deep,
								src || (copy.length != null ? [] : {}), copy);
						} else if (copy !== undefined) {
							target[name] = copy;
						}
					}
				}
			}
			
			return target;
		},
		console = window.console,
		log = function () {
			if (console && isFunction(console.log) && window.location.toString()
				.indexOf('_mode=debug') !== -1) {
				try {
					console.log.apply(window, arguments);
				} catch (e) {
					// never show errors due to console logging
				}
			}
		};
	
	// document.write is evil, but necessary, since GA must work in the
	// absence of jQuery
	document.write(unescape("%3Cscript type='text/javascript' src='" +
		gaJsHost + "google-analytics.com/ga.js'%3E%3C/script%3E"));
	
	DDC.GoogleAnalyticsManager = function (initial) {
		var i,
			index = {},
			trackers = [],
			get = function (id) {
				var i,
					tracker;
				
				if (index[id]) {
					for (i = 0; i < trackers.length; i++) {
						if (trackers[i].s === id) {
							tracker = trackers[i];
						}
					}
				}
				
				return tracker;
			},
			add = function (id) {
				if (id && !index[id]) {
					// create and add a new Google Analytics tracker
					trackers.push(window._gat._getTracker(id));
					// remember ID in index for quick look-ups
					index[id] = true;
				}
			},
			disable = function (id) {
				if (id in index) {
					index[id] = false;
				}
			},
			disableAll = function (id) {
				var key;
				
				for (key in index) {
					index[key] = false;
				}
			},
			enable = function (id) {
				if (id in index) {
					index[id] = true;
				}
			},
			enableAll = function () {
				var key;
				
				for (key in index) {
					index[key] = true;
				}
			},
			executeMethod = function (name, args) {
				var i;
				
				for (i = 0; i < trackers.length; i++) {
					if (trackers[i] && index[trackers[i].s] &&
						isFunction(trackers[i][name])) {
						trackers[i][name].apply(trackers[i], args);
						log('[{{ id }}] {{ name }}()'
							.replace('{{ id }}', trackers[i].s)
							.replace('{{ name }}', name), args);
					}
				}
			},
			makeHigherOrder = function (name) {
				return function () {
					executeMethod(name, arguments);
					return this;
				};
			};

		if (this !== DDC && window._gat && window._gat._getTracker) {
			extend(true, this, {
				get: function (id) {
					// returns a tracker for the ID if one exists
					// usage:  get({id: 'UA-1234567-8'})
					return get(id);
				},
				add: function (id) {
					// usage:  add({id: 'UA-1234567-8'})
					add(id);
					return this;
				},
				disable: function (id) {
					// disables tracker of given id, if indexed
					// no methods will be executed on this tracker
					disable(id);
					return this;
				},
				disableAll: function () {
					// disable all indexed trackers
					disableAll();
					return this;
				},
				enable: function (id) {
					// enables tracker of given id, if one exists
					enable(id);
					return this;
				},
				enableAll: function () {
					// enable all indexed trackers
					enableAll();
					return this;
				},
				_trackPageview: makeHigherOrder('_trackPageview'),
				_trackEvent: makeHigherOrder('_trackEvent'),
				_setAllowLinker: makeHigherOrder('_setAllowLinker'),
				_setDomainName: makeHigherOrder('_setDomainName')
			});
			
			if (initial && initial.length) {
				for (i = 0; i < initial.length; i++) {
					this.add(initial[i]);
				}
			}

			return this;
		} else {
			if (!window._gat || !window._gat._getTracker) {
				throw new Error('Google Analytics is missing.');
			} else {
				throw new Error('DDC.GoogleAnalyticsManager is a ' +
					'constructor and may not be called directly.');
			}
		}
	};

}());
