YAHOO.namespace("sputnik");
YAHOO.namespace("sputnik.player");
YAHOO.sputnik.player.track = function(name, duration, category, series, playerName, isBroadcast, startOffset) {
    //note: this class requires that the omniture page tracking has been loaded first and that the omniture object is named "s" (default)

    /* construction */
    var debug = false;//turn on/off debugging (alerts)
    var report = true;//turn on/off actual reporting
    var stoppedAt = null;
    var complete = false;
    var started = false;

    //do not report anything if this is a broadcast
    if (isBroadcast && report) report = false;

    //setup
    if (report) {
        var wait = 1500000;//keep alive omniture client session every 25 min (1000*60*25)
        setTimeout('s.trackLink()', wait);
    }

    /*private methods */
	var isDefined = function(variable) {
	    return (typeof(variable) == "undefined")?  false: true;
	}

    var trackEvent = function(event) {
        s.Media.trackVars = "prop13,eVar10,prop6,eVar8,prop12,eVar9,events";
        s.Media.trackEvents = event;
        
        s.prop13 = name;
        s.eVar10 = name;
        if (category) {
            s.prop6 = category;
            s.eVar8 = category;
        }
        if (series) {
            s.prop12 = series;
            s.eVar9 = series;
        }
        s.events = event;
        
        s.Media.track(name);//sends current state to omniture
    }

    /* public methods (interface) */
    //call when video is loaded and starts playing (from the beginning)
    this.start = function() {
        if (report && !complete && !started) {//if already complete or already started
            //video start            
            s.Media.open(name, duration, playerName);

            s.Media.play(name, startOffset ? startOffset : 0);//use startOffset if specified
                        
            trackEvent('event4');//start (must call open + play before we can use s.Media.track p14)
        	started = true;
			if (debug) alert('video tracking started');
        }
    }

    //call when video finished (at the end only)
    //if offset is omitted duration will be used (the end)
    this.complete = function() {
		if (report && !complete && started) {
        	this.stop(duration);
        	complete = true;
			if (debug) alert('video complete');
		}
    }

    //movie paused at specified offset (required)
    this.pause = function(offset) {
		if (report && isDefined(offset) && !complete && started && !isBroadcast && stoppedAt === null) {
			//video pause
            //trackEvent('event5');//pause
            s.Media.stop(name,offset);
			stoppedAt = offset;
			if (debug) alert('video paused at '+offset);
		}
    }

    //resume after pause
    this.resume = function() {
		if (report && stoppedAt !== null && !complete && started && !isBroadcast) {
			//video resume (from pause)
			s.Media.play(name, stoppedAt);
			if (debug) alert('video resumed at '+stoppedAt);
			stoppedAt = null;
		}
    }

    this.scrub = function(from, to) {
		if (report && isDefined(from) && isDefined(to)
				&& !complete && started && !isBroadcast) {
            if (stoppedAt === null) {
                //first stop it (without closing) (we have no custom omniture event for scrubbing)
                s.Media.stop(name, from);

                //then start at the new position
                s.Media.play(name, to);
				if (debug) alert('video scrubbed from '+from+' to '+to);
            } else {//scubbing while paused!
                stoppedAt = to;
				if (debug) alert('video scrubbed while paused to '+to);
			}
		}
    }

    //when user stops the video, offset required
    this.stop = function(offset) {
		if (report && isDefined(offset) && !complete && started) {
			//stop video at (specified offset)
			s.Media.stop(name, offset);
            s.Media.close(name);
			if (debug) alert('video stopped at '+offset);
		} else if (debug && !isDefined(offset))
			alert('offset was undefined for video stop: ignored');
    }
}
