project.player.FlashPlayer = new Class({
    Implements: [Options, project.player.Logging],

    log_prefix: 'FlashPlayer',

    flashVars: {
        config: 'static/flash/proxy_player/main/conf/config.xml',
        logLevel: 'LEVEL_DEBUG', // Live: NONE
        environment: 'development', // Live: production
        baseHref: 'http://tvnext.tv',
        locale: '../../../static/flash/proxy_player/main/locale/de_DE'
    },

    params: {
        allowfullscreen: true,
        allowscriptaccess: 'always',
        wmode: 'transparent'
    },

    attributes: {
        id: 'proxyPlayer',
        name:'proxyPlayer'
    },

    adreload_allowed: false,

    proxy: null,
    dom_element: null,
    last_clip: null,

    initialize: function(obj)
    {
        this.logger('initialize');
        this.dom_element = $('proxyPlayer');
        this.proxy = obj.proxy;
        this.renderFlash();
    },

    executeCall: function(call, infoObject)
    {
        var info = JSON.decode(infoObject);

        this.logger('call: ' + call);
        this.logger('info: ' + info.infoId);

        switch (call)
        {
            case 'gemiusTracking:LOG_EVENT':

                this.testing_logger(info.message, 'gemius');

            break;

            case 'PlayerNotification':
                if ('PlayerReady' == info.infoId)
                {
                    this.changeClip(this.proxy.player.active_clip);
                }

                if('PlaylistFinished' == info.infoId)
                {
                    this.proxy.layer.obj.show();
                }

                if('PlayerMuteChanged' == info.infoId)
                {
                    this.proxy.advertising.obj.adReload(info.infoId);
                }

                if('companionAds:ReloadAds' == info.infoId)
                {
                    this.adreload_allowed = true;
                    this.proxy.advertising.obj.adReload(info.infoId);
                }

                if('PlayerFullscreenChanged' == info.infoId)
                {
                    this.proxy.setFullscreenState(info.message);
                }

                break;

            case 'AdNotification':

                if(info.infoId == 'hideAd:PauseAd')
                {
                    this.proxy.advertising.obj.hidePauseAd();
                    this.logger('hideAd:PauseAd');
                }

                if(info.infoId == 'loadAd:PauseAd')
                {
                    this.proxy.advertising.obj.showPauseAd();
                    this.logger('loadAd:PauseAd');
                }
                
                if ('AdLoaded' == info.infoId)
                {
                    this.logger('execute call: ' + call + ' infoObject: ' + infoObject);
                }

                if ('instream:AdStarted' == info.infoId)
                {
                    this.proxy.advertising.obj.instreamAdStarted(info.message);
                }

                if ('instream:AdFinished' == info.infoId)
                {
                    this.proxy.advertising.obj.instreamAdFinished(info.message);
                    this.adreload_allowed = true;
                }

                this.proxy.advertising.obj.checkAdType(info);


                this.logger(info);
                this.testing_logger(info.message, 'advertising');

                break;

            case 'TrackingNotification':

                if("callGoogleAnalytics" == info.infoId)
                {
                    this.proxy.tracking.obj.googleAnalyticsPageTrack(info);
                }

                if("callOewaAnalytics" == info.infoId)
                {
                    this.proxy.tracking.obj.oewaAnalyticsPageTrack(info);

                    if('Play' == info.action || 'Pause' == info.action || 'Seek' == info.action || 'Clipteil' == info.action)
                    {
                        this.proxy.advertising.obj.adReload(info.action);
                    }
                }

                break;

            case 'PLAYER_ALERT':
                this.logger('player alert: ' + info.message);
                break;

            default:
                this.logger("command not known: " + call + " \n infoObject: " + infoObject);
                break;
        }
    },

    renderFlash: function()
    {
        swfobject.embedSWF("static/flash/proxy_player/Player.swf", "proxy_player", "528", "360", "9.0.115", "js/swfobject/expressInstall.swf", this.flashVars, this.params, this.attributes);

        /*
         * show no flash alternative content
         */
        if($('proxy_player'))
        {
            $('proxy_player').show();
        }
    },



    /*
    {
        "clip": {
            "autoPlay": false,
            "playerAdvertising": false,
            "previewImageUrl": "http://stage-frontend.atv.invaliddomain.de/binaries/asset/tvnext_clip/353277/player_image",
            "geoBlocked": false,
            "zoneCategoryName": "ONLINE",
            "keyValueType": "V1",
            "keyValueLength": "Laenge2",
            "zoneName": "",
            "wrongFormat": true,
            "clipParts": [
                {
                    "id": 1538579,
                    "url": "http://atv.at/binaries/asset/tvnext_clip/1464740/video",
                    "duration": 411
                },
                {
                    "id": 1538582,
                    "url": "http://atv.at/binaries/asset/video_file/1538581/video",
                    "duration": 436
                }
            ]
        }
    }
     */
    changeClip: function(clip, autoplay)
    {
        var that = this;
        this.logger('changeClip');

        this.proxy.advertisingReset();
        this.adreload_allowed = false;

        var geo_blocked = (project.environment == 'production') ? clip.geoblocked : false;

        var json = {
            "clip": {
                "autoPlay": autoplay || that.proxy.player.autoplay,
                "playerAdvertising": this.proxy.player.advertising,
                "previewImageUrl": clip.image_url.getAtvUrl(),
                "geoBlocked": geo_blocked,

                "clipId": clip.id,
                "title": clip.title,
                "subtitle": clip.subtitle,
                "contentSet": clip.contentset_id,
                "category": clip.category,

                "clipName": clip.keyValueName,
                "clipSeason": clip.keyValueSeason,
                "clipEpisode": clip.keyValueEpisode,
                "zoneCategoryName": clip.zoneCategoryName,
                "keyValueType": clip.keyValueType,
                "keyValueLength": clip.keyValueLength,
                "zoneName": clip.zoneName,

                "gemiusTreeId": clip.gemiusTreeId,
                "gemiusMaterialId": clip.gemiusMaterialId,

                "wrongFormat": true === clip.wrong_format,
                "clipParts": []
            }
        };

        if(clip.parts)
        {
            clip.parts.each(function(part){
                json.clip.clipParts.push({
                    'duration': part.duration,
                    'id': part.id,
                    'url': part.url.getAtvUrl()
                });

                that.logger( 'clip part: ' + part.url.getAtvUrl() );
            });
        }

        that.logger(json);
        
        var params = JSON.encode(json);


        //params = params.replace(/\%/g,"%1").replace(/\\\\/g,"%2").replace(/\\/g, "%3").replace(/&/g, "%4").replace(/"/g, "%5");

        this.last_clip = params;

        that.logger('clip params: ' + params);

        document.getElementById('proxyPlayer').executeCommand('ChangeClip', params);
    },


    changeClipTesting: function(params)
    {
        this.proxy.advertisingReset();
        document.getElementById('proxyPlayer').executeCommand('ChangeClip', params);
    },


    playClip: function()
    {
        document.getElementById('proxyPlayer').executeCommand('PlayClip');
    },

    pauseClip: function()
    {
        document.getElementById('proxyPlayer').executeCommand('PauseClip');
    },

    repeatClip: function()
    {
        this.logger('repeatClip');
        this.proxy.layer.obj.hide();
        this.playClip();
    }
});



function executeCall(call, infoObject)
{
    PlayerProxy.player.obj.executeCall(call, infoObject);
}

function iab_videopause()
{
    PlayerProxy.player.obj.pauseClip();
}

function iab_videoplay()
{
    PlayerProxy.player.obj.playClip();
}

function Overlay_Hide()
{
    PlayerProxy.advertising.obj.hide();
    PlayerProxy.advertising.obj.setSkinbrandingVisible(false);
}

function iab_click()
{
    //window.open(url);
}

function iab_close()
{
    PlayerProxy.advertising.obj.hide();

}


