project.player.Actions = new Class({
    Implements: project.player.Logging,

    log_prefix: 'Actions',

    proxy: null,
    visible: false,
    clip_id: null,
    clip_title: null,
    contentset_id: null,
    contentset_title: null,
    track_data: null,
    elements: {
        favor: null,
        comment: null,
        embed: null,
        tellafriend: null,
        url: null,
        facebook: null,
        rssfeed: null,
        abuse: null,
        showonmap: null
    },
    comments_disabled: false,
    embed_disabled: false,

    // Kategorien wo die Aktionsleiste automstisch deaktiviert ist
    actions_blocked_categories: [
        'Filme',
        'Werbung',
        'Unternehmen',
        'Presse',
        'Sonderwerbeformen'
    ],

    // Kategorien wo die Kommentarfunktion deaktivert ist
    comment_blocked_categories: [
        'Filme',
        'Moderatoren',
        'Werbung',
        'Unternehmen',
        'Presse',
        'Sonderwerbeformen'
    ],

    request_url: {
        clip_add_favorite: 'community/clip/addfavorite/CLIP_ID',
        user_add_favorite: 'community/profile/favorit/USER_ID'
    },


    initialize: function(options)
    {
        this.proxy = options.proxy;
        this.visible = options.visible;
        this.visible = options.visible;
        this.comments_disabled = options.comments_disabled;
        this.embed_disabled = options.embed_disabled;

        this.render();
    },

    update: function(clip)
    {
        this.clip_id = clip.id;
        this.clip_title = clip.title;
        this.contentset_id = clip.contentset_id;
        this.contentset_title = clip.contentset_title;

        this.track_data = {
            'name': clip.keyValueName,
            'staffel': clip.keyValueSeason,
            'folge': clip.keyValueEpisode,
            'art': clip.keyValueType,
            'laenge': clip.keyValueLength,
            'programid': clip.zoneCategoryName,
            'action': 'Aktionsleiste'
        };

        /*
         * Embed Funktion auf Clip Ebene blocken
         */
        if(clip.embedblocked || this.embed_disabled)
        {
            this.disableEmbed();
            this.disableFacebook();
        }
        else
        {
            this.enableEmbed();
            this.enableFacebook();
        }

        /*
         * Kommentar Funktion für bestimmte Kategorien deaktiviert
         */
        if(this.comment_blocked_categories.indexOf(clip.category) >= 0 || this.comments_disabled)
        {
            this.disableComments();
        }
        else
        {
            this.enableComments();
        }

        /*
         * Aktionsleiste ein/ausblenden - default: deaktiviert
         */
        this.visible = (this.actions_blocked_categories.indexOf(clip.category) >= 0) ? false : this.visible;
        if(this.visible)
        {
            this.showActions();
        }
        else
        {
            this.hideActions();
        }
    },

    track: function()
    {
        this.proxy.tracking.obj.trackAll(this.track_data);
    },

    render: function()
    {
        var that = this;

        that.elements.favor = new Element('button', {
            'class': 'favor',
            'title': 'Video favorisieren'
        }).addEvent('click', function() {
            that.executeSideBarFavVideo(that.clip_id);
            that.track();
        });

        that.elements.comment = new Element('button', {
            'class': 'comment',
            'title': 'Video kommentieren'
        }).addEvent('click', function() {
            if(!this.hasClass('disabled'))
            {
                that.executeSideBarCommentVideo();
                that.track();
            }
        });

        if(that.comments_disabled)
        {
            that.disableComments();
        }

        that.elements.embed = new Element('button', {
            'class': 'embed',
            'title': 'Embed-Code für Video'
        }).addEvent('click', function() {
            if(!this.hasClass('disabled'))
            {
                executeSideBarEmbedVideo(that.clip_id, that.contentset_id, that.clip_title);
                that.track();
            }
        });

        that.elements.tellafriend = new Element('button', {
            'class': 'tellafriend',
            'title': 'tell-a-friend'
        }).addEvent('click', function() {
            executeSideBarSendVideo(that.clip_id, that.contentset_id, that.clip_title);
            that.track();
        });

        that.elements.url = new Element('button', {
            'class': 'url',
            'title': 'Video URL'
        }).addEvent('click', function() {
            executeSideBarLinkVideo(that.clip_id, that.contentset_id, that.clip_title);
            that.track();
        });

        that.elements.facebook = new Element('button', {
            'class': 'facebook',
            'title': 'Video auf Facebook teilen'
        }).addEvent('click', function() {
            if(!this.hasClass('disabled'))
            {
                executeSideBarFacebookShare(that.clip_id, that.contentset_id, that.clip_title);
                that.track();
            }
        });

        that.elements.rssfeed = new Element('button', {
            'class': 'rssfeed',
            'title': 'RSS Feed'
        }).addEvent('click', function() {
            executeSideBarRssVideo(that.clip_id, that.contentset_id, that.clip_title);
            that.track();
        });

        that.elements.abuse = new Element('button', {
            'class': 'abuse',
            'title': 'Video melden'
        }).addEvent('click', function() {
            executeSideBarReportVideo(that.clip_id, that.contentset_id);
            that.track();
        });

        that.elements.showonmap = new Element('button', {
            'class': 'showonmap',
            'title': 'Video auf Karte anzeigen'
        }).addEvent('click', function() {
            project.navigateTo( 'categories/community?mark_id=' + that.clip_id);
            that.track();
        });

        if(that.embed_disabled)
        {
            that.disableEmbed();
            that.disableFacebook();
        }

        $('player_actions_list').adopt([
            that.elements.favor,
            that.elements.comment,
            that.elements.embed,
            that.elements.tellafriend,
            that.elements.url,
            that.elements.facebook,
            that.elements.rssfeed,
            that.elements.abuse,
            that.elements.showonmap
        ]);
    },

    showActions: function()
    {
        $('player_actions').removeClass('disabled');
        this.proxy.header.obj.favorites.show();
    },

    hideActions: function()
    {
        $('player_actions').addClass('disabled');
        this.proxy.header.obj.favorites.hide();
    },

    disableComments: function()
    {
        this.elements.comment.addClass('disabled');
        if($('comments_floatbox'))
        {
            $('comments_floatbox').hide();
        }
    },

    enableComments: function()
    {
        this.elements.comment.removeClass('disabled');
        if($('comments_floatbox'))
        {
            $('comments_floatbox').show();
        }
    },

    disableEmbed: function()
    {
        this.elements.embed.addClass('disabled');
    },

    enableEmbed: function()
    {
        this.elements.embed.removeClass('disabled');
    },

    disableFacebook: function()
    {
        this.elements.facebook.addClass('disabled');
    },

    enableFacebook: function()
    {
        this.elements.facebook.removeClass('disabled');
    },

    
    /*
     * Actions: ATV/app/modules/Contentset/templates/IndexSuccess.php
     */

    executeSideBarFavVideo: function(id)
    {
        if( ATV.isAuthenticated() )
        {
            var url = project.getBaseUrl() + this.request_url.clip_add_favorite.replace(/CLIP_ID/g, id);

            atvConfirm('Als Favorit hinzufügen?', 'Favorit', false,
                function () {
                   var jsonRequest = new Request.JSON({
                        url: url,
                        onComplete: function(retval){
                            if (retval.status)
                            {
                                var fav = parseInt($('header_favorized').get('text'))+1;
                                $('header_favorized').set('html', fav);
                                atvAlert('Dieses Video wurde zu deinen Favoriten hinzugefügt!','Favorit', false, false, 2000);
                            }
                            else
                            {
                                atvAlert('Dieses Video hast du bereits gefaved!','Favorit', false, false, 2000);
                            }
                        }
                    }).post({});
                }
            );
        }
        else
        {
            atvAlert('Für diese Aktion musst du registriert und eingeloggt sein.','Bitte einloggen');
        }
    },


    executeSideBarCommentVideo: function()
    {
        if( ATV.isAuthenticated() )
        {
            commentsReplyBox.toggle();
        }
        else
        {
            atvAlert('Bitte logge Dich ein, um Videos zu kommentieren','Bitte Einloggen');
        }
    }

});

