if ( Object.isUndefined(CodeCompany) ) { var CodeCompany = { }; }

//-------------------------------------------------------------------------------------------------------------

CodeCompany.PageTracker = Class.create({
	initialize: function(trackId,options) {
		this._trackId = trackId;
		this._pageTracker = null;
		this.pendingTracks = [];
		
		document.observe(this.options.eventName, this._onTrackEvent.bindAsEventListener(this));
		
		var scriptUrl = (document.location.protocol=='https:' ? this.options.httpsUrl : this.options.httpUrl);
		this.options.logFunction('Lazy-loading '+scriptUrl);
		LazyLoader.load(scriptUrl,this._setup.bind(this));
	},
	_setup: function(){ throw "You need to implement _setup()"; },
	_track: function(){ throw "You need to implement _track()"; },
	_trackPending: function()
	{
		if (this._pageTracker==null) { return; }
		var opts;
		while (opts=this.pendingTracks.shift()) {
			this._track(opts);
		}
	},
	_queueTrack: function(opts) {
		this.pendingTracks.push(opts);
		this._trackPending();
	},
	_onTrackEvent: function(event)
	{
		this.options.logFunction('Track event received');
		this._queueTrack(event.memo);
	}
});

//-------------------------------------------------------------------------------------------------------------

CodeCompany.PageTrackerGoogle = Class.create(CodeCompany.PageTracker,{
	initialize: function($super, trackId,options)
	{
		this.options = Object.extend({
			logFunction:	function(){},
			eventName:		'custom:googletrack',
			httpUrl:		'http://www.google-analytics.com/ga.js',
			httpsUrl:		'https://ssl.google-analytics.com/ga.js'
		}, options || {} );
		$super(trackId,options);
	},
	_setup: function()
	{
		if (undefined===_gat) {
			this.options.logFunction('Google tracking script did not provide _gat object.');
			return;
		}
		try {
			this._pageTracker = _gat._getTracker(this._trackId);
			this._pageTracker._initData();
			this.options.logFunction('Initialized');
		}
		catch (e) {
			this.options.logFunction('Initialization failed: '+e.message);
		}
		this._queueTrack({});
	},
	_track: function(opts)
	{
		var debugStr = 'Tracking';
		if (undefined !== opts.trackUrl) {
			debugStr += ' trackUrl='+opts.trackUrl;
		}
		this.options.logFunction(debugStr);
		try {
			this._pageTracker._trackPageview(opts.trackUrl);
		} catch(e) {
			this.options.logFunction('Tracking call to Google failed: '+e.message);
		}
	}
});

//-------------------------------------------------------------------------------------------------------------

CodeCompany.PageTrackerYahoo = Class.create(CodeCompany.PageTracker,{
	initialize: function($super,trackId,options)
	{
		this.options = Object.extend({
			logFunction:	function(){},
			eventName:				'custom:yahootrack',
			defaultDocumentGroup:	undefined,
			defaultDocumentName:	undefined,
			httpUrl:				'http://d.yimg.com/mi/eu/ywa.js',
			httpsUrl:				'https://s.yimg.com/mi/eu/ywa.js'
		}, options || {} );
		$super(trackId,options);
	},
	_setup: function()
	{
		if (undefined===YWA) {
			this.options.logFunction('Yahoo tracking script did not provide YWA object.');
			return;
		}
		try {
			this._pageTracker = YWA.getTracker(this._trackId);
			this.options.logFunction('Initialized');
		}
		catch (e) {
			this.options.logFunction('Initialization failed: '+e.message);
		}
		this._queueTrack({});
	},
	_track: function(opts)
	{
		var debugStr = 'Tracking';

		// DocumentGroup - fallback to default specified in constructor options
		if (undefined === opts.documentGroup) {
			opts.documentGroup = this.options.defaultDocumentGroup;
		}
		if (undefined !== opts.documentGroup) {
			this._pageTracker.setDocumentGroup(opts.documentGroup);
			debugStr += ' documentGroup='+opts.documentGroup;
		}
		// DocumentName - fallback to default specified in constructor options
		if (undefined === opts.documentName) {
			opts.documentName = this.options.defaultDocumentName;
		}
		if (undefined !== opts.documentName) {
			this._pageTracker.setDocumentName(opts.documentName);
			debugStr += ' documentName='+opts.documentName;
		}

		this.options.logFunction(debugStr);

		try {
			this._pageTracker.submit();
		} catch(e) {
			this.logFunction('Tracking call to Yahoo failed: '+e.message);
		}
	}
});


