var MiniVideoPlayer = new Class({
    Implements: [Options, Events],
    options:
    {
        container: null,
        video: '',
        bgColor: '#f1f9fc',
        isShow: false,
        isInit: false,
        playerPath: 'flash/minivideoplayer.swf?' + Math.random(),
        delay: 1000
    },
    initialize: function(options)
    {
        this.setOptions(options);
        this.options.container = $(this.options.container);
        this._initIcon();
        this._initPlayer();
        this._initBG();
        this._initSlider();
    },
    _initPlayer: function()
    {
        this.options.playerContainer = new Element('div', {
            styles:
            {
                position: 'absolute',
                zIndex: 3,
                right: 11,
                top: Math.round(this.options.container.getHeight()/2 - 120),
                backgroundColor: this.options.bgColor,
                visibility: 'hidden'
            }
        }).inject(this.options.container);
    
        this.options.player = new Swiff(this.options.playerPath, {
            id: 'videoplayer',
            width: 306,
            height: 220,
            params: {
                wmode: 'opaque',
                bgcolor: this.options.bgColor,
                swLiveConnect: true,
                allowScriptAccess: 'always'
            }

        }).inject(this.options.playerContainer);
        
    },
    _initBG: function()
    {
        this.options.bg = new Element('div', {
            styles:
            {
                position:'absolute',
                zIndex: 2,
                right: -1,
                top: 0,
                backgroundColor: this.options.bgColor,
                width: 0,
                height: this.options.container.getHeight()
            }
        }).inject(this.options.container);
    
        this.options.bar = new Element('img', {
            src: 'images/arrow_right.gif',
            styles:
            {
                width: 10,
                height: 261,
                position: 'absolute',
                left: 0,
                top: 0,
                zIndex: 4,
                cursor: 'pointer',
                display: 'none'
            },
            events:
            {
                click: this.togglePlayer.bindWithEvent(this)
            }
        }).inject(this.options.bg);
    },
    _initSlider: function()
    {
        this.options.slider = new Fx.Morph(this.options.bg, {
            transition: Fx.Transitions.Circ.easeOut,
            onComplete: function()
            {
                if(!this.options.isShow)
                {
                    this.options.playerContainer.setStyle('visibility', 'visible');
                    this.options.bar.setStyle('display', 'inline');
                    this.options.isShow = true;
                    Browser.Engine.gecko ? (function(){this.getPlayer().play()}).bind(this).delay(this.options.delay) : (function(){Swiff.remote($('videoplayer'), 'play');}).delay(this.options.delay);
                }    
                else
                {
                    this.options.bar.setStyle('display', 'none');
                    this.options.isShow = false;
                }
            }.bind(this)
        })
    },
    _initIcon: function()
    {
        var div = new Element('div', {
            styles: 
            {
                position: 'absolute',
                zIndex: 2,
                left: 764,
                bottom: 9
            }
        }).inject(this.options.container);
        new Element('img', {
            src: 'images/videoicon.png',
            styles:
            {
                cursor: 'pointer'
            },
            events:
            {
                click: this.togglePlayer.bindWithEvent(this)
            }
        }).addClass('pngfix').inject(div);
    },
    togglePlayer: function()
    {
        this.options.slider.cancel();
        
        if(!this.options.isShow)
            this.options.slider.start({'width': [this.options.bg.getWidth(), 339]});
        else
        {
            this.options.playerContainer.setStyle('visibility', 'hidden');
            Browser.Engine.gecko ? this.getPlayer().pause() : Swiff.remote($('videoplayer'), 'pause');
            this.options.slider.start({'width': [this.options.bg.getWidth(), 0]});
        }
        
        if(!this.options.isInit)
        {
            this.options.isInit = true;
            Browser.Engine.gecko ? (function(){this.getPlayer().playNext(this.options.video, true, '');}).bind(this).delay(this.options.delay) : (function(){Swiff.remote($('videoplayer'), 'playNext', this.options.video, true, '');}).bind(this).delay(this.options.delay);
        }
        
    },
    getPlayer: function()
    {
        var flashObj;
        if (navigator.appName.indexOf("Microsoft") != -1) {
            flashObj = window["videoplayer"];
        }
        else {
            flashObj = document["videoplayer"];
            if(flashObj && flashObj.length)
                flashObj = flashObj[1];
        }
        
        return flashObj;
    },
    loaded: function()
    {
        
    }
});