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

    log_prefix: 'Html5Player',

    proxy: null,
    dom_element: null,
    video_element: null,
    video_src_element: null,
    video_js: null,
    player_preview_image: null,

    clip_parts: [],
    clip_part_index: 0,

    track_data: null,

    adreload_allowed: true,
    geoblocked: true,

    initialize: function(options)
    {
        this.logger('initialize');

        this.proxy = options.proxy;

        this.dom_element = $('proxy_player');
        this.dom_element.addClass('video-js-box tube-css')
        this.dom_element.empty();
        this.dom_element.show();
        
        this.render();

        this.changeClip(this.proxy.player.active_clip);
    },

    getCurrentTime: function()
    {
        return this.video_element.currentTime
    },

    render: function()
    {
        var that = this;

        that.player_preview_image = new Element('img', {
            'id': 'player_preview_image'
        });
        $('player_video').grab(that.player_preview_image);

        that.video_src_element = new Element('source', {
            'id': 'mp4source',
            'src': '', //http://video-js.zencoder.com/oceans-clip.mp4
            'type': 'video/mp4',
            'codecs': 'avc1.42E01E, mp4a.40.2'
        });

        that.video_element = new Element('video', {
            'id': 'video',
            'class': 'video-js',
            'width': 528,
            'height': 295,
            'controls': 'controls',
            'preload': 'auto',
            'poster': that.proxy.player.active_clip.image_url.getAtvUrl()
        }).adopt([
            that.video_src_element
        ]);

        that.dom_element.grab(this.video_element);


        that.video_js = VideoJS.setup("video");

        /*
         * Events
         */
        that.video_js.onPlay(function(){
            that.logger('play');

            that.track('Play');
            that.proxy.tracking.obj.gemiusEvent('playing', that.getCurrentTime());
        });

        that.video_js.onPause(function(){
            that.logger('pause');

            var ua = navigator.userAgent;
            var isiPhone = /iPhone OS/i.test(ua);
            if(!isiPhone)
            {
                that.video_js.showBigPlayButtons();
            }

            that.track('Pause');
            that.proxy.tracking.obj.gemiusEvent('paused', that.getCurrentTime());
        });

        that.video_js.onVolumeChange(function(){

           that.logger('volume chage');
        });

        /*
        
        that.video_js.enterFullScreen(function(){
            that.logger('enter fullscreen');
            that.proxy.setFullscreenState(true);
        });


        that.video_js.exitFullScreen(function(){
            that.logger('exit fullscreen');
            that.proxy.setFullscreenState(false);
        });

        */


        that.video_js.onEnded(function(){
            that.logger('end');

            that.video_js.hideBigPlayButtons();

            that.clip_part_index = that.clip_part_index+1;
            var next_clip_part = that.clip_parts[that.clip_part_index];
            if(undefined == next_clip_part)
            {
                $('proxy_player').hide(); // ipad fix

                that.proxy.tracking.obj.gemiusEvent('complete', that.getCurrentTime());
                that.proxy.tracking.obj.gemiusCloseStream(that.getCurrentTime());

                that.proxy.layer.obj.show();
            }
            else
            {
                that.playClipPart(next_clip_part);
            }
        });
    },

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

    playClip: function()
    {
        this.logger('play before');
        this.video_element.player.play();
        this.logger('play after');
    },

    pauseVideo: function()
    {
        this.video_element.player.pause();
    },

    stopVideo: function()
    {
        this.video_element.player.currentTime(0);
        this.video_element.player.pause();
    },

    playNextPart: function()
    {
        this.logger('playNextPart: ' + part);
        var part = this.getNextClipPart();
        this.playClipPart(part);
    },

    playPrevPart: function()
    {
        this.logger('playPrevPart: ' + part);
        var part = this.getPrevClipPart();
        this.playClipPart(part);
    },

    getNextClipPart: function()
    {
        this.logger('getNextClipPart: ' + part + ' | ' + index);
        var index = this.clip_part_index = this.clip_part_index + 1;
        var part = this.clip_parts[index];
        return part;
    },

    getPrevClipPart: function()
    {
        this.logger('getPrevClipPart: ' + part + ' | ' + index);
        var index = this.clip_part_index = this.clip_part_index - 1;
        var part = this.clip_parts[index];
        return part;
    },

    afterChangeClip: function() {
        this.logger('afterChangeClip');
        //this.pauseVideo();
    },

    beforeChangeClip: function() {
        this.logger('beforeChangeClip');
    },

    changeClip: function(clip, autoplay)
    {
        this.logger('ChangeClip');
        this.beforeChangeClip();

        this.logger(clip);

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

        // geoblocked
        this.geoblocked = clip.geoblocked;

        this.updatePoster(clip.image_url.getAtvUrl());

        this.clip_parts = clip.parts;
        this.clip_part_index = 0;

        this.logger('ChangeClip - part: ' + this.clip_part_index);

        var part = this.clip_parts[this.clip_part_index];
        if(part)
        {
            this.player_preview_image.hide();
            $('proxy_player').show();
            this.playClipPart(part);
            //this.video_element.player.pause();
        }
        else
        {
            // show preview image only
            $('proxy_player').hide();
            this.player_preview_image.show();
        }

        if(autoplay)
        {
            this.playClip();
        }

        this.afterChangeClip();
    },

    changeClipTesting: function()
    {

    },

    updatePoster: function(url) {
        this.player_preview_image.src = url;
        this.video_element.poster = url;
        $('vjs-poster').src = url;
    },

    repeatClip: function()
    {
        this.logger('repeatClip');
        this.stopVideo();

        var index = this.clip_part_index = 0;
        var part = this.clip_parts[index];
        this.playClipPart(part);
    },

    playClipPart: function(part) {

        if(this.clip_part_index > 0)
        {
            this.proxy.tracking.obj.gemiusCloseStream(this.getCurrentTime());
        }


        this.proxy.layer.obj.hide();
        var clip_part_url = part.url.getAtvUrl();

        if(this.geoblocked)
        {
            this.logger('geoblocked');
            clip_part_url = 'http://atv_iphone_geo-i.akamaihd.net/i/' + part.id + '.mp4/master.m3u8';
        }
        else
        {
            this.logger('non geoblocked');
            clip_part_url = 'http://atv_iphone-i.akamaihd.net/i/' + part.id + '.mp4/master.m3u8';
        }

        this.logger('playClipPart' + clip_part_url);

        this.video_element.player.pause();
        this.video_src_element.src = clip_part_url;


        /*
        var ua = navigator.userAgent;
        var isiUser = /iPad/i.test(ua) || /iPhone OS/i.test(ua);
        if(isiUser)
        {
            this.video_element.src = 'http://video-js.zencoder.com/oceans-clip.mp4'; //part.url.getAtvUrl();
        }
        else
        {
            this.video_element.src = part.url.getAtvUrl();
        }
        */

        this.video_element.src = clip_part_url;

        //if (theVid.canPlayType('video/ogg; codecs="theora"')) { theVid.src = vidPath + "vid.ogv"; }
        //if (theVid.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"')) { theVid.src = vidPath + "vid.mp4"; }

        /*
         * handle part navigation
         */
        var controls_wrapper = this.video_element.player.controls;
        controls_wrapper.removeClass('no-part-navigation');

        if(this.clip_parts.length > 1)
        {
            this.renderPartNavigation();
        }
        else
        {
            controls_wrapper.addClass('no-part-navigation');
        }

        
        this.logger('before load');
        this.video_element.load();
        this.logger('after load');



        this.playClip();

        // gemius new stream
        this.proxy.tracking.obj.gemiusNewStream(this.proxy.player.active_clip, this.clip_part_index+1);

    },

    renderPartNavigation: function() {
        var that = this;

        this.logger('renderPartNavigation');
        this.logger(this.clip_parts.length);

        var controls_wrapper = this.video_element.player.controls;

        if(controls_wrapper)
        {
            $$('.vjs-parts-navigation').dispose();
            $$('.current-clip-part').dispose();

            var parts_navigation_wrapper = new Element('div', {
                'class': 'vjs-parts-navigation'
            });

            this.logger('render prev');
            // prev
            var prev = new Element('a', {
                'text': 'prev',
                'class': 'prev'
            });
            if(this.clip_part_index-1 < 0)
            {
                prev.addClass('disabled');
            }
            else
            {
                prev.addEvent('click', function(event) {
                    event.stop();
                    that.playPrevPart();
                });
            }

            this.logger('render next');
            // next
            var next = new Element('a', {
                'text': 'next',
                'class': 'next'
            });
            if(this.clip_part_index+1 >= this.clip_parts.length)
            {
                next.addClass('disabled');
            }
            else
            {
                next.addEvent('click', function(event) {
                    event.stop();
                    that.playNextPart();
                });
            }

            this.logger('render position');

            var position = new Element('span', {
                'text': 'Teil ' + parseInt(this.clip_part_index+1) + '/' + this.clip_parts.length
            });

            var current_part = new Element('span', {
                'class': 'current-clip-part',
                'text': 'Teil ' + parseInt(this.clip_part_index+1)
            });

            this.logger('parts_navigation_wrapper adopt');

            parts_navigation_wrapper.adopt([
                prev,
                next,
                position
            ]);

            this.logger('controls_wrapper grab');

            controls_wrapper.grab(parts_navigation_wrapper);
            controls_wrapper.grab(current_part);
        }
        else
        {
            this.logger('no controls wrapper found');
        }
    }
});

