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

    count: 0,
    type: '',
    isReplay: false,
    ccTime: 0,
    mediaType: '',
    geoCode: '',
    movieType: '',

    initialize: function(type, options) {
        this.type = type; // 'page' or 'click', defaults to 'page'
        this.options = options || {};
        this.qtEventSource = document.getElementsByTagName('body')[0];
        
        var pathName = window.location.pathname;
        var hostName = window.location.hostname;
        if (hostName.match(/apple.com.cn/)){
            this.geoCode = " (CN)";
        }else if (!pathName.match(/^\/(ws|pr|g5|go|ta|wm)\//)) {
            if (pathName.match(/^\/(\w{2}|befr|benl|chfr|chde|asia|lae)(?=\/)/)){
                pathName = pathName.split(/\//);
                this.geoCode = " (" + pathName[1].toUpperCase() + ")";
            }
        }
        if (this.geoCode==''){this.geoCode=" (JP)";}

        this.listenForEvent(AC.ViewMaster, 'ViewMasterDidShowNotification', false, this.sectionDidChange);
        this.listenForEvent(document.event, 'replayMovie', false, this.movieDidReplay.bind(this));
        this.listenForEvent(document.event, 'didFinishMovie', false, this.movieDidEnd);
        Event.observe(this.qtEventSource, 'QuickTime:didStartJogging', this.didStartJogging.bind(this));
        Event.observe(this.qtEventSource, 'QuickTime:didStopJogging', this.didStopJogging.bind(this));
        Event.observe(this.qtEventSource, 'QuickTime:begin', this.didBegin.bind(this));
        Event.observe(this.qtEventSource, 'QuickTime:end', this.didEnd.bind(this));
        Event.observe(this.qtEventSource, 'QuickTime:start', this.didStart.bind(this));
        Event.observe(this.qtEventSource, 'QuickTime:stop', this.didStop.bind(this));
        Event.observe(this.qtEventSource, 'QuickTime:noCompatibleQTAvailable', this.noCompatibleQTAvailable);
        Event.observe(this.qtEventSource, 'QuickTime:didSetClosedCaptions', this.didSetClosedCaptions.bind(this));
    },

    setDelegate: function(delegate) {
        this.delegate = delegate;
    },

    pageName: function(section) {
        this._id = '';

        if (section) {
            this._id = this.trackingNameForSection(section);
        } else if (this.viewMaster.currentSection) {
            this._id = this.trackingNameForSection(this.viewMaster.currentSection);
        }

        this._pageName = AC.Tracking.pageName() + ' - '+ this._id;
        if (typeof this._pageName === "string") {
            this._pageName = this._pageName.replace(/[\'\’\"]/g, '');
        }

    },

    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;
    },

	isSnowLeopardControllerAvailable: function() {
		return (typeof(Media)!="undefined");
	},
	
    didBegin: function(evt) {
        if(this.mediaType!='audio'){
            var controller = evt.memo.controller;
            this._pageName = this._pageName.toLowerCase();
            this.movieType = this.isSnowLeopardControllerAvailable() ? controller.movieType() : false;
            
            try {
				this._timeScale = (this.isSnowLeopardControllerAvailable()) ? controller.timeScale() : controller.GetTimeScale();
	            var timeDuration = (this.isSnowLeopardControllerAvailable()) ? controller.duration() : controller.GetDuration(),
                	time = (this.movieType) ? Math.floor(timeDuration) : Math.floor(timeDuration/this._timeScale),
                properties = {},
                preFix = '';
            } catch(e) {}
            if (this.isReplay) {
                preFix = 'V@R: ';
            } else {
                preFix = 'V@S: ';
            }

            properties.pageName = preFix + this._pageName;

            if (typeof this.type === 'undefined') {
                properties.prop13 = properties.pageName;
                properties.prop4 = (this.isSnowLeopardControllerAvailable()) ? controller.src() : controller.GetURL();
                AC.Tracking.trackPage(properties);
                //empty variables out so they are not picked up on the play request
                properties.prop13=properties.prop3=properties.prop4 = '';
            } else {
                s.prop13 = preFix + this._pageName;
                s.prop4 = (this.isSnowLeopardControllerAvailable()) ? controller.src() : controller.GetURL();
                s.eVar16 = s.prop16 = "Video Plays"
                s.events = "event2"
                s.Media.trackVars="events,prop13,prop4,prop16,eVar16"
                s.Media.trackEvents="event2"
            }

            if (this.delegate && typeof this.delegate.QTdidBegin == 'function') {
                properties = this.delegate.QTdidBegin(this, properties);
                var MediatrackVars = '';
                for (var key in properties) {
                    if(key != 'pageName'){
                        MediatrackVars += ','+ key;
                        s[key]=properties[key];
                    }
                }
                s.Media.trackVars += MediatrackVars;
            }
            
            var playerType = (this.movieType) ? this.movieType : 'QuickTime';
            s.Media.open(this._pageName, time, playerType);
            s.Media.play(this._pageName, '0');
            //empty variables out so they are not picked up on subsequent requests
            s.prop13=s.prop4=s.prop16=s.eVar16=s.events="";
            //set media Type to Video to elimiate additional swapview at end of video
            this.mediaType="video";
            
        }
    },

    didEnd: function(evt) {
        if(this.mediaType!='audio'){
            try {
                var controller = evt.memo.controller,
					currentTime;
					
				currentTime = (this.isSnowLeopardControllerAvailable()) ? controller.time() : controller.GetTime();
				time = (this.movieType) ? Math.floor(currentTime)-1 : Math.floor(currentTime/this._timeScale-1);
            } catch(e) {}
            this._pageName
            s.Media.monitor = function (s,media) {
                if(media.event=="CLOSE") {
                    if(media.percent>="99"){
                        s.Media.trackVars="prop13,prop16,eVar16";
                        s.prop13="V@E: " + media.name;
                        s.eVar16=s.prop16="Video Ends";
                        s.Media.track(media.name);
                        //empty variables out so they are not picked up on subsequent requests
                        s.prop13=s.prop16=s.eVar16="";
                    }
                }
            }
            s.Media.stop(this._pageName,time);
            s.Media.close(this._pageName);
        }
    },

    didStartJogging: function(evt) {
        if(this.mediaType!='audio'){
            try {
                var controller = evt.memo.controller,
					currentTime = (this.isSnowLeopardControllerAvailable()) ? controller.time() : controller.GetTime(),
                	time = (this.movieType) ? Math.floor(currentTime)-1 : Math.floor(currentTime/this._timeScale-1);
            } catch(e) {}

            if (time==-1) { time=0; }
            s.Media.stop(this._pageName, time);
        }
    },

    didStopJogging: function(evt) {
        if(this.mediaType!='audio'){
            try {
                var controller = evt.memo.controller,
                currentTime = (this.isSnowLeopardControllerAvailable()) ? controller.time() : controller.GetTime(),
                time = (this.movieType) ? Math.floor(currentTime)-1 : Math.floor(currentTime/this._timeScale-1);
            } catch(e) {}
            if (time==-1) { time=0; }
            s.Media.play(this._pageName, time);
        }
    },

    didStart: function(evt) {
        if(this.mediaType!='audio'){
            try {
                var controller = evt.memo.controller,
                currentTime = (this.isSnowLeopardControllerAvailable()) ? controller.time() : controller.GetTime(),
                time = (this.movieType) ? Math.floor(currentTime)-1 : Math.floor(currentTime/this._timeScale-1);
            } catch(e) {}
            if (time==-1) { time=0; }
            s.Media.play(this._pageName, time);
        }
    },

    didStop: function(evt) {
        if(this.mediaType!='audio'){
            try {
                var controller = evt.memo.controller,
                currentTime = (this.isSnowLeopardControllerAvailable()) ? controller.time() : controller.GetTime(),
                time = (this.movieType) ? Math.floor(currentTime)-1 : Math.floor(currentTime/this._timeScale-1);
            } catch(e) {}
            if (time==-1) { time=0; }
            s.Media.stop(this._pageName, time);
        }
    },

    noCompatibleQTAvailable: function(evt) {
        var properties = {};
        properties.prop6 = 'no QT: ' + AC.Tracking.pageName();
        AC.Tracking.trackClick(properties, name, 'o', properties.prop6);
    },

    didSetClosedCaptions: function(evt) {
        var controller = evt.memo.controller,
        timeDuration = this.isSnowLeopardControllerAvailable() ? controller.duration() : controller.GetDuration(),
        enabled = evt.memo.enabled;
        currentTime = this.isSnowLeopardControllerAvailable() ? controller.time() : controller.GetTime(),
        time = (this.movieType) ? Math.floor(currentTime)-1 : Math.floor(currentTime/this._timeScale-1);
        if (enabled) {
            this.ccTime = time
        } else {
            var timeBucket,
            timePercent;
            this.ccTime = time - this.ccTime;
            timeDuration = this.isSnowLeopardControllerAvailable() ? timeDuration : timeDuration/this._timeScale
            timePercent = Math.round((this.ccTime/timeDuration)*100);
            if (timePercent > 0 && timePercent < 11) {
                timeBucket = '<11';
            } else if (timePercent > 10 && !timePercent < 51) {
                timeBucket='>10<51';
            } else if (timePercent > 50 && !timePercent < 91) {
                timeBucket='>50<91';
            } else if (timePercent > 90) {
                timeBucket='>90';
            } else {
                timePercent=null;
            }

            if (timePercent!=null) {
                var properties = {};
                properties.pageName = AC.Tracking.pageName() + this.geoCode;
                properties.prop3 = 'cc@o: ' + timeBucket + ' - ' + this._pageName;
                AC.Tracking.trackClick(properties, this, 'o', properties.prop3);
            }
        }
    },

    sectionDidChange: function(evt) {
        this.viewMaster = 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 = {};

            this.pageName(incoming);
            if (this._id) {
                properties.pageName = this._pageName + this.geoCode;
				
                // special if our section is a movie or audio
                if (incoming.movieLink && incoming.movieLink.href) {
                    if (incoming.mediaType().match(/audio\//)){
                        this.mediaType = 'audio';
                        properties.pageName = 'A@S: '+ properties.pageName;
                    } else if (incoming.mediaType().match(/video\//)){
                    	if(!this._id=='360'||!this._id=='vr'){
							this.mediaType = 'video';
							return false;
						}
						this.mediaType = 'video';
						return false;
                    }

                    properties.prop13 = properties.pageName.replace(/\s*\((\w{2}|befr|benl|chfr|chde|asia|lae)\)/g,'');
                    properties.prop4 = incoming.movieLink.href;
                }

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

                if (this.type == 'click') {
                    properties.prop3 = properties.pageName.replace(/\s*\((\w{2}|befr|benl|chfr|chde|asia|lae)\)/g,'');
                    properties.pageName = AC.Tracking.pageName() + this.geoCode;
                    AC.Tracking.trackClick(properties, this.viewMaster, 'o', properties.prop3);
                } 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 + this.geoCode;
		
			
            // special if our section is a movie or audio
            if (section.movieLink && section.movieLink.href) {
                if (this.mediaType == 'audio'){
                    properties.pageName = 'A@E: '+ properties.pageName;
                } else if (this.mediaType == 'video'){
                    return false;
                }
                properties.prop13 = properties.pageName.replace(/\s*\((\w{2}|befr|benl|chfr|chde|asia|lae)\)/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 + this.geoCode;

            // special if our section is a movie or audio
            if (section.movieLink && section.movieLink.href) {
                if (this.mediaType == 'audio'){
                    properties.pageName = 'A@R: '+ properties.pageName;
                } else if (this.mediaType == 'video'){
                    this.isReplay = true;
                    return false;
                }
                properties.prop13 = properties.pageName.replace(/\s*\((\w{2}|befr|benl|chfr|chde|asia|lae)\)/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.replace(/\s*\((\w{2}|befr|benl|chfr|chde|asia|lae)\)/g,'');
                properties.pageName = AC.Tracking.pageName() + this.geoCode;
                AC.Tracking.trackClick(properties, section, 'o', properties.prop3);
            } else {
                AC.Tracking.trackPage(properties);
            }
        }
    }

});
