﻿var VideoPlayer = new Class({
    Implements: Options,
    options: {
        width: 640,
        height: 360,
        element: 'video',
        video_folder: '/video/',
        thumb_folder: '/video/thumb/'
    },
    
    initialize: function(video, thumbnail, options) {
        this.video = video;
        this.thumbnail = thumbnail;
        this.setOptions(options);
        this.element = $(this.options.element);
        if (this.element) this.createThumbnail();
    },
    
    createThumbnail: function() {
        this.element.empty();
        new Element('img', { 'alt': 'video', 'class': 'video-thumb', 'src': this.options.thumb_folder + this.thumbnail, 'events': { 'click': function() {
            this.showVideo();
        }.bind(this) }}).inject(this.element);
    },
    
    showVideo: function() {
        var size = window.getSize();
        var left = (size.x - this.options.width - 20) / 2;
        var top = (size.y - this.options.height - 60) / 2 + window.getScroll().y;
        
        this.container = new Element('div', { 'class': 'video-container', 'styles': { 'position': 'absolute', 'top': top + 'px', 'left': left + 'px', 'width': (this.options.width + 20) + 'px', 'height': (this.options.height + 60) + 'px' }}).inject($(document.body));
        new Element('div', { 'id': 'player' }).inject(this.container);
        new Element('input', { 'type': 'button', 'value': 'Close', 'events': { 'click': function() {
            this.container.destroy();
        }.bind(this) }}).inject(new Element('div', { 'class': 'buttons' }).inject(this.container));
        
        var so = new SWFObject('/swf/player.swf','mpl',this.options.width,this.options.height,'9');
        so.addParam('allowfullscreen','true');
        so.addParam('allowscriptaccess','always');
        so.addParam('wmode','transparent');
        so.addVariable('file',this.options.video_folder + this.video);
        so.addVariable('image',this.options.thumb_folder + this.thumbnail);
        //so.addVariable('stretching','exactfit');
        so.addVariable('autostart','true');
        so.write('player');
    }
});
