

AC.ViewMaster.Tracker = Class.create();
Object.extend(AC.ViewMaster.Tracker.prototype, Event.Listener);
Object.extend(AC.ViewMaster.Tracker.prototype, {

	count: 0,
	type: '',
	
	initialize: function(type, options) {
		this.type = type; // 'page' or 'click', defaults to 'page'
		this.options = options || {};
		
		this.listenForEvent(AC.ViewMaster, 'ViewMasterDidShowNotification', false, this.sectionDidChange);
		this.listenForEvent(document.event, 'didFinishMovie', false, this.movieDidEnd);
		this.listenForEvent(document.event, 'replayMovie', false, this.movieDidReplay);
	},
	
	setDelegate: function(delegate) {
		this.delegate = delegate;
	},
	
	trackingNameForSection: function(section) {
		var id = section.id.replace('MASKED-', '');

		// complex resetting of the id
		// in a delegate so we can reset it whenever we want
		if (this.delegate && typeof(this.delegate.trackingNameForSection) == 'function') {
			id = this.delegate.trackingNameForSection(this, id, section);
		}
		
		return id;
	},
	
	sectionDidChange: function(evt) {
		var view = evt.event_data.data.sender;
		var incoming = evt.event_data.data.incomingView;

		if (incoming) {
			// default if we don't have a delegate tracking function
			var properties = {};
			var id = this.trackingNameForSection(incoming);

			if (id) {
				properties.pageName = document.title.toString().replace('Apple - ', '') +' - '+ id +' (US)';

				// special if our section is a movie
				if (incoming.movieLink && incoming.movieLink.href) {
					properties.pageName = 'V@S: '+ properties.pageName;
					properties.prop13 = properties.pageName;
					properties.prop4 = incoming.movieLink.href;
				}

				if (this.type == 'click') {
					properties.prop3 = properties.pageName;
					AC.Tracking.trackClick(properties, view, 'o', properties.pageName);
				} else {
					AC.Tracking.trackPage(properties);
				}

				this.count++;
			}
		}
	},
	
	movieDidEnd: function(evt) {
		var section = evt.event_data.data;

		var properties = {};
		var id = this.trackingNameForSection(section);

		if (id) {
			properties.pageName = document.title.toString().replace('Apple - ', '') +' - '+ id +' (US)';
		
			if (section.movieLink && section.movieLink.href) {
				properties.pageName = 'V@E: '+ properties.pageName;
				properties.prop13 = properties.pageName;
			}

			AC.Tracking.trackClick(properties, section, 'o', properties.pageName);
		}
	},
	
	movieDidReplay: function(evt) {
		var section = evt.event_data.data;

		var properties = {};
		var id = this.trackingNameForSection(section);
		
		if (id) {
			properties.pageName = document.title.toString().replace('Apple - ', '') +' - '+ id +' (US)';
		
			// special if our section is a movie
			if (section.movieLink && section.movieLink.href) {
				properties.pageName = 'V@R: '+ properties.pageName;
				properties.prop13 = properties.pageName;
				properties.prop4 = section.movieLink.href;
			}
		
			if (this.type == 'click') {
				properties.prop3 = properties.pageName;
				AC.Tracking.trackClick(properties, section, 'o', properties.pageName);
			} else {
				AC.Tracking.trackPage(properties);
			}
		}
	}
	

});	
