

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 && !incoming.content.hasClassName('sneaky')) {
			// 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)';
				properties.pageName = ' - '+ 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.replace(/\s*\([A-Z][A-Z]\)/g,'');
					properties.prop4 = incoming.movieLink.href;
				}

                if (this.delegate && typeof(this.delegate.sectionDidChange) == 'function') {
                    properties = this.delegate.sectionDidChange(this, view, incoming, id, properties);
                }

				if (this.type == 'click') {
					properties.prop3 = properties.pageName.replace(/\s*\([A-Z][A-Z]\)/g,'');
					//properties.pageName = AC.Tracking.pageName() + ' (US)';
					properties.pageName = ' (US)';
					//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 = AC.Tracking.pageName() + ' - '+ id +' (US)';
			properties.pageName = ' - '+ id +' (US)';
		
		
			if (section.movieLink && section.movieLink.href) {
				properties.pageName = 'V@E: '+ properties.pageName;
				properties.prop13 = properties.pageName.replace(/\s*\([A-Z][A-Z]\)/g,'');
			}
			
	        if (this.delegate && typeof(this.delegate.movieDidEnd) == 'function') {
                properties = this.delegate.movieDidEnd(this, section, id, properties);
            }

			//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 = AC.Tracking.pageName() + ' - '+ id +' (US)';
			properties.pageName = ' - '+ 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.replace(/\s*\([A-Z][A-Z]\)/g,'');
				properties.prop4 = section.movieLink.href;
			}
	
            if (this.delegate && typeof(this.delegate.movieDidReplay) == 'function') {
                properties = this.delegate.movieDidReplay(this, section, id, properties);
            }
	
			if (this.type == 'click') {
				properties.prop3 = properties.pageName;
				//properties.pageName = AC.Tracking.pageName() + ' (US)';
				properties.pageName = ' (US)';
				//AC.Tracking.trackClick(properties, section, 'o', properties.pageName);
			} else {
				//AC.Tracking.trackPage(properties);
			}
		}
	}
	

});	
