var xrx_hbx_proxy = function() {
	var hbx_mlc;
	var hbx_pn;
	var sc_pn;
	var debug = false;

	function set_pn_mlc(pn, mlc, scpn)
	{
		if ( !pn || pn == '' ) {
			if ( hbx_pn != null ) {
				pn = hbx_pn;
				if (debug) console.log("DEFAULTING PN: "+pn);
			} else if ( typeof(hbx) != 'undefined' && hbx.pn ) {
				pn = hbx.pn;
			}
		}

		if ( !mlc || mlc == '' ) {
			if ( hbx_mlc != null ) {
				mlc = hbx_mlc;
				if (debug) console.log("DEFAULTING MLC: "+mlc);
			} else if ( typeof(hbx) != 'undefined' && hbx.mlc ) {
				mlc = hbx.mlc;
			}
		}

		if ( !scpn || scpn == '' ) {
			if ( sc_pn != null ) {
				scpn = sc_pn;
				if (debug) console.log("DEFAULTING SCPN: "+scpn);
			} else if ( typeof(s) != 'undefined' && s.pageName ) {
				scpn = s.pageName;
			} else if ( mlc !== 'undefined' && pn !== 'undefined' ) {
				scpn = mlc+'/'+pn;
			}
		}

		return [pn, mlc, scpn];
	}

	function track_exit_download(url, pn, mlc, scpn) {
		var pn_mlc_scpn = set_pn_mlc(pn, mlc, scpn);
		pageView(pn_mlc_scpn[0], pn_mlc_scpn[1], pn_mlc_scpn[2], false);
		eventTrack(url, pn_mlc_scpn[0], pn_mlc_scpn[1]);
	}

	function pageView(pn, mlc, scpn, suppress_sc) {
		var pn_mlc_scpn = set_pn_mlc(pn, mlc, scpn);
		pn   = pn_mlc_scpn[0];
		mlc  = pn_mlc_scpn[1];
		scpn = pn_mlc_scpn[2];
		
		// Save the following values to restore
		page_pn   = hbx.pn;
		page_mlc  = hbx.mlc;
		page_scpn = s.pagename;
		
		if (typeof _hbPageView == 'function' ) {
			_hbPageView(pn, mlc);
			_hbSet('vcon', page_mlc);
			_hbSet('n', page_pn);
			if (debug) console.log("PAGE VIEW: pn("+pn+"), mlc("+mlc+")");
		} else if (debug) {
			console.log("Could not find reporting functions: _hbPageView");
		}

		if (s != 'undefined' && typeof s.t == 'function' && scpn != '__dont_send__' ) {
			s.pageName = scpn;
			var re_mlc = /channelintelligence|research-read_review|research-write_review/i;
			if (re_mlc.test(mlc)) {
				s.prop2 = pn;
			}
			if (suppress_sc == true) { 
				// This is just a placeholder in case we want the native 
				// SiteCatalyst click handler for downloads and exit 
				// links to record the pageview.  Otherwise we want to
				// go ahead and record the pageview since there isn't a 
				// a native click handler.
			} else {
				s.t();
			}
			if (debug) console.log("PAGE VIEW: scpn("+scpn+")");
		} else if (debug) {
			console.log("Could not find reporting functions: s.t");
		}

		// Restore the following values
		hbx.pn     = page_pn;
		hbx.mlc    = page_mlc;
		s.pagename = page_scpn;
		sc_pn      = page_scpn;
	}

	function eventTrack(url, pn, mlc) {
		if(typeof report_campaign_trax == 'function') {
			var hbx_vars    = {mlc: mlc, page: pn};
			var anchor_data = {href: url};
			report_campaign_trax(hbx_vars, anchor_data, {}); 

			if (debug) console.log("CAMPAIGN TRAX: pn("+pn+"), mlc("+mlc+"), url("+url+")");
		} else if (debug) {
			console.log("Could not find reporting functions: report_campaign_trax");
		}
	}

	return {
		debug: function(flag) {
			if (flag != null) debug = flag;
			return debug;
		},
		xrxCmpTrax: eventTrack,
		xrxLid: function (linkId, suppress_hbx) {
			try {
				if(typeof _hbLink == 'function' && suppress_hbx != true) {
					// if the suppress_hbx flag is true, that means that HBX's native
					// click handler has already recorded the click and we just need
					// a call to SiteCatalyst.
					_hbLink(linkId);
				} else if (debug) {
					console.log("Could not find reporting functions: _hbLink");
				}
				if (s != 'undefined' && typeof s.tl == 'function' ) {
					if (sc_pn != null) s.pagename = sc_pn;
					s.linkTrackVars = 'prop4';
					s.prop4 = linkId;
					s.tl(this,'o',linkId);
				} else if (debug) {
					console.log("Could not find reporting functions: s.tl");
				}
			} catch (err) {
				console.log("ERROR:" + err.message);
			}
		},
		xrxLink: function(lid, pn, mlc, scpn) {
			try {
				var pn_mlc_scpn = set_pn_mlc(pn, mlc, scpn);
				pn   = pn_mlc_scpn[0];
				mlc  = pn_mlc_scpn[1];
				scpn = pn_mlc_scpn[2];
				
				if(typeof _hbSet == 'function' && typeof _hbLink == 'function') {
					_hbSet('vcon', mlc);
					_hbSet('n', pn);
					_hbLink(lid); // assume that if we have _hbSet, we have _hbLink
					
					if (debug) console.log("LINK CLICK: pn("+pn+"), mlc("+mlc+"), lid("+lid+")");
				} else if (debug) {
					console.log("Could not find reporting functions: _hbSet || _hbLink");
				}
		
				if (s != 'undefined' && typeof s.tl == 'function' ) {
					if (sc_pn != null) s.pagename = sc_pn;
					s.linkTrackVars = 'prop4';
					//s.prop4 = linkId;
					s.tl(this,'o',lid);
					if (debug) console.log("LINK CLICK: scpn("+scpn+")");
				} else if (debug) {
					console.log("Could not find reporting functions: s.t");
				}
			} catch (err) {
				console.log("ERROR:" + err.message);
			}
		},
		xrxPageView: pageView,
		xrxExit: track_exit_download,
		xrxDownload: track_exit_download,
		xrxAtlasTag: function(url) {
			if (debug) console.log("ATLAS: "+url);
			var img = new Image();
			img.src = 'http://switch.atdmt.com/action/' + url;
		},
		xrxMediaPlay: function(fileName, currPos, endPos, flashVersion) {
			if (debug) console.log("PLAY: filename("+fileName+"), currPos("+currPos+"), endPos("+endPos+"), flashVersion("+flashVersion+")");
			
			if(typeof _hbSet == 'function' && typeof _hbSend == 'function') {
				_hbSet('m.f',  fileName);
				_hbSet('m.cp', currPos);
				_hbSet('m.ep', endPos);
				_hbSet('m.s',  'play');
				_hbSet('m.cl', 'Flash Player');
				_hbSet('m.cv', flashVersion);
				_hbSet('m.tt', 'e');
				_hbSend();
			} else if (debug) {
				console.log("Could not find reporting functions: _hbSet || _hbSend");
			}
		},
		xrxMediaPause: function(fileName, currPos, endPos, flashVersion) {
			if (debug) console.log("PAUSE: filename("+fileName+"), currPos("+currPos+"), endPos("+endPos+"), flashVersion("+flashVersion+")");
			
			if(typeof _hbSet == 'function' && typeof _hbSend == 'function') {
				_hbSet('m.f',  fileName);
				_hbSet('m.cp', currPos);
				_hbSet('m.ep', endPos);
				_hbSet('m.s',  'pause');
				_hbSet('m.cl', 'Flash Player');
				_hbSet('m.cv', flashVersion);
				_hbSet('m.tt', 'e');
				_hbSend();
			} else if (debug) {
				console.log("Could not find reporting functions: _hbSet || _hbSend");
			}
		},
		xrxMediaStop: function(fileName, currPos, endPos, flashVersion) {
			if (debug) console.log("STOP: filename("+fileName+"), currPos("+currPos+"), endPos("+endPos+"), flashVersion("+flashVersion+")");
			
			if(typeof _hbSet == 'function' && typeof _hbSend == 'function') {
				_hbSet('m.f',  fileName);
				_hbSet('m.cp', currPos);
				_hbSet('m.ep', endPos);
				_hbSet('m.s',  'stop');
				_hbSet('m.cl', 'Flash Player');
				_hbSet('m.cv', flashVersion);
				_hbSet('m.tt', 'e');
				_hbSend();
			} else if (debug) {
				console.log("Could not find reporting functions: _hbSet || _hbSend");
			}
		},
		xrxMediaPlayProgress: function(fileName, currPos, endPos, flashVersion) {
			if (debug) console.log("PROGRESS: filename("+fileName+"), currPos("+currPos+"), endPos("+endPos+"), flashVersion("+flashVersion+")");
			
			if(typeof _hbSet == 'function' && typeof _hbSend == 'function') {
				_hbSet('m.f',  fileName);
				_hbSet('m.cp', currPos);
				_hbSet('m.ep', endPos);
				_hbSet('m.s',  'playp');
				_hbSet('m.cl', 'Flash Player');
				_hbSet('m.cv', flashVersion);
				_hbSet('m.tt', 'e');
				_hbSend();
			} else if (debug) {
				console.log("Could not find reporting functions: _hbSet || _hbSend");
			}
		}
	};
} ();

