/**
 * Create flowplayer instance for topnavigation videos
 *
 * @param {Object} options
 * @author Juri Leino, EQEEE
 * @company spot-media consulting GmbH
 */
function SpotLightVideo (options)
{
    //abort initialization on iPad
    if (!window.hasFlash) return;
    this.file               = options.flvPath;
    this.parentId           = 'spotlight' + options.parentId;
    this.dcsuri             = options.DCSdcsuri || window.location;
    this.WT                 = {
            cusVideo : "Animation - " + (options.WTcusAnimation.name || this.parentId),
            cusVideoCategory : options.WTcusAnimation.category
    };
    this.videoAlreadyPlayed = -1;
    this.player             = null;
    this.loading            = false;
    this.to                 = 0;
    this.previewImage       = $(this.parentId).getElementsByTagName('img')[0].src;
    debug(this.previewImage);
    this._tag               = (typeof _tag != 'undefined') ? _tag : new WebTrends();
    // init flowplayer instance
    this.playerInit();
}

/**
 * video status tracking
 * @param  {String}    status
 * @param  {Number}    percentage
 * @global {WebTrends} _tag
 * @deprecated
 */
SpotLightVideo.prototype.trackStatus = function (status, percentage) {
    var currentStatus = status || '';
    if ( percentage <= this.videoAlreadyPlayed ) return; // prevent double tracking - die quietly
    this.videoAlreadyPlayed = percentage;
    with (this._tag) {
        DCS.dcsuri = this.DCSdcsuri;
        WT.cusVideo           = "Animation - " + this.WT.name;
        WT.cusVideoStatus     = currentStatus;
        WT.cusVideoCategory   = this.WT.category;
        WT.cusVideoPercentage = percentage.toString();
        dcsCollect();
        //reset custom properties
        WT.cusVideo = '';
        WT.cusVideoStatus = '';
        WT.cusVideoCategory = '';
        WT.cusVideoPercentage = '';
    }
};

/**
 * FlowPlayer initialization
 */
SpotLightVideo.prototype.playerInit = function() {
    var self = this;
    this.player = FlowplayerFactory({
        id: self.parentId,
        options: {
            playlist: [
                // this first PNG clip works as a splash image
                {
                    url: self.previewImage, 
                    scaling: 'orig'
                },
                   // second clip is a video. when autoPlay is set to false the splash screen will be shown
                {
                    autoPlay: false, 
                    autoBuffering: true,
                    url: self.file,
                    scaling: 'scale',
                    // scaling: 'fit',
                    onStart: flowt.getStartHandler(self),
                    onPause: function() {
                        //do not allow pause
                       this.getPlugin('play').hide();
                        return false;
                    },
                    onBeforeFinish: function() {
                        flowt.status(self, 'finished', 100);
                        this.getPlugin('play').hide();
                        // show preview image again
                        this.play(0);
                        // disable finish event from getting fired
                        return false;
                    }
                }
            ],
            onLoad: function() {
                self.loading = false;
                this.play(1);
                //alert('loaded');
            },
            canvas: { backgroundColor: "#000000" },
            plugins: { controls: null }
        }
    });
    this.addEvents();
};

/*
 * @event mouseout
 */
SpotLightVideo.prototype.out = function () {
//    debug(this.player.getState());
    if (this.player && this.player.getState() > 0) {
        this.player.play(0);
    }
};
/*
 * @event mouseover
 */
SpotLightVideo.prototype.over = function () {
    //var self =  this;
    if (this.player && this.player.getState() == 5) {
        this.player.play(1);
    }
    else if (this.loading) {
        // do nothing while loading
        return;
    }
    else {
        // initialize player
        this.player.play();
        this.loading = true;
    }
};
SpotLightVideo.prototype.addEvents = function () {
    //register eventlisteners on 
    //debug(this.parentId + ' register events');
    $(this.parentId)
        .observe('mouseover', this.over.bind(this))
        .observe('mouseout', this.out.bind(this));
    //debug(this.parentId + ' register complete');
};

/**
 * add eventlistener to all players
$f("*").each(function() {
    // this- variable is a pointer to a Player instance in current iteration
    this.onLoad(function() {
        // now each player on the page does this when it's loaded
        alert("player loaded: " + this.id());
    });
});
 */


