/**
 * glue Flowplayer to WebTrends tracking
 * @author Juri Leino, spot media consulting GmbH
 */
var flowt = {
    /**
     * @global _tag
     * @var {WebTrends} internal (or global) Webtrends instance
     */
    _tag: null, //(typeof _tag != 'undefined') ? _tag : new WebTrends(),
    /**
     * start handler factory
     * @returns {function}
     */
    getStartHandler: function (wrapper) {
        var self = this;
        debug("getStartHandler: " , self, wrapper);
        return function( clip ) {
            var quarter = Math.round( clip.duration * 250 ), // 1000 / 4
                cuepoints = [quarter, 2*quarter, 3*quarter];
            debug('starthandler called for ', wrapper);
            //register callback for computed cuepoints
            this.onCuepoint( cuepoints, function(clip, tc) {
                //equals:       100     tc   25
                //         -------- * ---- * --
                //         duration   1000   25
                var percentage = Math.round( tc / ( clip.duration * 250 ) ) * 25;
                self.status(wrapper, 'playing', percentage );
            });
            
            self.status(wrapper, 'started',0);
            return true;
        };
    },
    /**
     * @param wrapper {Object}
     * @param status {String}
     * @param percentage {Number}
     * @returns {void}
     */
    status: function (wrapper, status, percentage) {
        var currentStatus = status || '';
        if ( percentage <= wrapper.videoAlreadyPlayed ) return; // prevent double tracking - die quietly
        wrapper.videoAlreadyPlayed = percentage;
        wrapper.WT.cusVideoStatus = currentStatus;
        wrapper.WT.cusVideoPercentage = percentage.toString();
        
        this._track(wrapper.WT,wrapper.DCSdcsuri);
    },
    /**
     * populate request with custom properties
     * @param {Object} cusProps - custom Properties
     * @param {String} uri - DCS.dcsuri override
     */
    _track: function(cusProps, uri) {
        with (this._tag) {
            //general video information
            if (uri) DCS.dcsuri = uri;
            for (var cusProp in cusProps) {
                if (cusProps.hasOwnProperty(cusProp))
                    WT[cusProp] = cusProps[cusProp];
            }
            //fire
            dcsCollect();
            //reset custom properties
            for (var cusProp in cusProps) {
                if (cusProps.hasOwnProperty(cusProp))
                    WT[cusProp] = '';
            }
        }
    }
};
flowt._tag = (typeof _tag != 'undefined') ? _tag : new WebTrends();

