var YoutubePlayer = {
    flashplayerdivid : 'flashplayer',
    flashplayer : null,
    init : function()
    {
        var videocount = 0;
        $$('a').each(function(object, key) {
            if (YoutubePlayer.getVideoIdFromString(object.rel) !== false) {
                object.href = "#video";
                object.addEvent('click', function() {
                    YoutubePlayer.playVideo(this);
                    return false;
                });
                videocount++;
            }
        });

        $('paginationright').addEvent('click', function(){
            YoutubePlayer.switchTile('right');
        });

        $('paginationleft').addEvent('click', function(){
            YoutubePlayer.switchTile('left');
        });

        $('vidcount').set('html', '(' + (videocount / 2) + ' video' + (videocount / 2 == 1 ? '' : 's')  + ')');

        this.checkWrapperHeight();
    },

    getVideoIdFromString : function (string)
    {
        regex = new RegExp('youtube_video\\[(.*)\\]');

        var matched = string.match(regex);

        if (matched)
            id = matched[1];
        else
            return false;

        return id;
    },

    loaded      : true,
    initialized : false,
    initstarted : false,
    playVideo   : function(obj, autoplay)
    {
     //   if (this.flashplayer)
     //      Swiff.remote(this.flashplayer, 'pauseVideo');

        if (typeof obj == 'object') {
            if (!(id = this.getVideoIdFromString(obj.rel))) {
                return;
            }
        } else {
            id = obj
        }

        if (!this.videodata[id] && YoutubePlayer.loaded == true)
            this.getVideo(id);

        if (YoutubePlayer.loaded == false) {
            setTimeout("YoutubePlayer.playVideo(" + id + ")", 100);
            return;
        }

        var video = this.videodata[id];

        if (!this.initstarted) {
            this.flashplayer = new Swiff('http://www.youtube.com/v/' + video['videoid'] + '&hl=nl&fs=1&enablejsapi=1&hd=1&hq=1', {
           // this.flashplayer = new Swiff('http://www.youtube.com/apiplayer?enablejsapi=1', {
                id      : 'player',
                width   : 440,
                height  : 353,
                params  : {
                    wmode               : 'opaque',
                    bgcolor             : '#fff',
                    allowFullScreen     : 'true',
                    allowscriptaccess   : 'always'
                }
            }).inject(this.flashplayerdivid);
            this.initstarted = true;

            setTimeout("YoutubePlayer.playVideo(" + id + ")", 250);
        } else if (this.initstarted && !this.initialized) {
            setTimeout("YoutubePlayer.playVideo(" + id + ")", 100);
        } else if (this.initstarted && this.initialized) {
            $('player').loadVideoById( video['videoid'] );
        }

        $('videocontentwrapper').setStyle('height', $('videocontent').getDimensions().y + 10);
        $('videocontent').set('html', video['content'].replace(/\n/g, "<br />"));
        $('videotitle').set('html', video['title']);

        var fx = new Fx.Tween($('videocontentwrapper')).start('height', $('videocontent').getDimensions().y + 10);
        window.scrollTo(0, $('videotitle').getPosition().y );
    },

    videodata   : [],
    getVideo    : function(id)
    {
        YoutubePlayer.loaded = false;
        var jsonRequest = new Request.JSON({url: "/dconn/json/entity.get.php", onComplete: function(data){
            YoutubePlayer.videodata[ id ] = data['objects'][0];
            YoutubePlayer.loaded = true;
        }}).get({'ent' : 'Youtube_video','id' : id});
    },

    currentTile : 0,
    switchTile : function (dir) {
        tiles = $$('.tile');

        if (dir == 'right') {
            if (tiles[ this.currentTile + 1]) {
                new Fx.Tween($('tileslider')).start('left', parseInt($('tileslider').getStyle('left').substring(0,$('tileslider').getStyle('left').length - 2)) + tiles[this.currentTile  ].getDimensions().x * -1);
                this.currentTile += 1;
            }
        } else if (this.currentTile - 1 >= 0) {
            new Fx.Tween($('tileslider')).start('left', parseInt($('tileslider').getStyle('left').substring(0,$('tileslider').getStyle('left').length - 2)) + tiles[this.currentTile - 1 ].getDimensions().x);
            //$('tileslider').setStyle('left',  );
            this.currentTile -= 1;
        }

        this.checkWrapperHeight();
    },
    checkWrapperHeight : function() {
        var tiles = $$('.tile');

        if (tiles[this.currentTile].getElements('div.row').length == 1) {
            new Fx.Tween($('tilewrapper')).start('height', 220);
        } else {
            new Fx.Tween($('tilewrapper')).start('height', 280);
        }
    }
}

window.addEvent('domready', function() {
    YoutubePlayer.init();
});
function onYouTubePlayerReady(playerId) {
  YoutubePlayer.initialized = true;
}
