function op_bridgeCrossed(json) {
	for (var cookie in json) {
		if (cookie) {
			document.cookie = cookie + '=' + json[cookie];
		}
	}
}

function googleAnalytics(realm, gaAccNum) {
	var addScript, isLoaded, tracker, trackOrder, realmAndCookie,
	attempts = 0,
	protocol = document.location.protocol;
	
	addScript = function (src) {
		var script = document.createElement('script');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', src);
		document.getElementsByTagName('head')[0].appendChild(script);
	};
	
	trackOrder = function () {
		var i, len, catId;
		if (typeof(orderSubTotal) !== 'undefined') {
			tracker._addTrans(orderNum, '', orderSubTotal, orderTax, orderShipping, '', '', '');
			len = codes.length;
			for (i = 0; i < len; i++) {
				catId = codes[i].split('-')[1];
				tracker._addItem(orderNum, codes[i], items[i], catId, price[i], qtys[i]);
			}
			tracker._trackTrans();
		}
	};
	
	isLoaded = function () {
		return function () {
			if (typeof(_gat) !== 'undefined') {
				tracker = _gat._getTracker(gaAccNum);
				tracker._setDomainName('none');
				tracker._setAllowLinker(true);
				tracker._trackPageview();
				trackOrder();
				realmAndCookie = tracker._getLinkerUrl(realm);
				addScript(protocol + '//tracker.opticsplanet.com/bridge.php?_rand=' + Math.random() + '&_realm=' + realmAndCookie.replace(/\?/, '&'));
			} else {
				if (attempts < 100) {
					attempts++;
					window.setTimeout(isLoaded(), 100);
				}
			}
		};
	};
	
	// init
	if (!document.referrer.match(document.domain)) {
		addScript(protocol + '//tracker.opticsplanet.com/bridge.php?_rand=' + Math.random() + '&_realm=' + realm);
	}
	addScript(('https:' === protocol ? 'https://ssl.' : 'http://www.') + 'google-analytics.com/ga.js');
	window.setTimeout(isLoaded(), 100);
}
