﻿

function Clip()
{
    this.url = '';
    this.title = '';
    this.description = '';
}

function VideoGallery(playerFile, streamFile)
{
    var _this = this;
    var _display = null;
    var _items = null;
    var _buttons = null;
    var _player = null;
    var _playerFile = playerFile;
    var _streamFile = streamFile;
    
    var _currentIndex = 0;
    var BUTTON_OFFSET = 2;
    
    var _videos = new Array();

    var _content = {
        title: null,
        description: null
    };
    
    _getButtons = function()
    {
        if (_buttons == null)
            _buttons = $('.gallery .bottom .button');
        return _buttons;
    }

    _getClips = function()
    {
        return _videos;
    }
    
    _getClip = function(index)
    {
        return _videos[index];
    }
    
    _getMaxIndex = function()
    {
        return _getClips().length - 1;
    }

    _setCurrentButton = function(index)
    {
        var buttons = _getButtons();

        buttons
            .css({ color: '#444242', backgroundColor: '#c3c2c2' })
            .eq(index + BUTTON_OFFSET).css({ color: '#fff', backgroundColor: '#ce2f2a' });
    }
    
    _setCurrentIndex = function(index)
    {
        _currentIndex = index;
    }

    _setContent = function(videoClip)
    {
        _content.title.html(videoClip.title);
        _content.description.html(videoClip.description);
    }

    _setCurrentClip = function(index)
    {
        _setCurrentIndex(index);
        _setCurrentButton(index);
        _setContent(_videos[index]);
    }

    _playClip = function(index)
    {
        _player.setClip(_videos[index].url);
        _player.play();
    }

    _initializePlayer = function()
    {
        var redColor = '#CE2F2A';
        var videoClip = _videos[0];

        _display.find('.player').attr('href', videoClip.url);
        
        _player = $f(_display.find('.player').attr('id'),
        {
            src: _playerFile,
            wmode: 'opaque'
        }, {
            plugins:
            {
                controls:
                {
                    backgroundColor: '#000',
                    backgroundGradient: 'none',
                    scrubber: true,
                    sliderColor: '#333333',
                    progressColor: redColor,
                    progressGradient: 'none',
                    bufferColor: '#666666',
                    buttonColor: '#666666',
                    buttonOverColor: redColor,
                    timeColor: '#ffffff',
                    timeBgColor: redColor,
                    volumeSliderColor: '#666666',
                    tooltipColor: redColor
                },
                pseudo: { url: _streamFile }
            },

            canvas:
            {
                backgroundGradient: [0.3, 0]
            },

            clip:
            {

                autoPlay: false,
                provider: 'pseudo',
                url: videoClip.url
            }
        });

        
        _setContent(videoClip);
    }

    this.initialize = function(videos)
    {
        _display = $('.gallery .display');
        _content.title = _display.find('.content h2');
        _content.description = _display.find('.content div');

        if (videos)
        {
            _videos = videos;
            _setCurrentClip(0);
        }

        _initializePlayer();
    }

    this.getClip = function(index)
    {
        return _getClip(index);
    }

    this.getClips = function()
    {
        return _getClips();
    }

    this.preview = function(index)
    {
        if (index <= 0)
        {
            index = 0;
        }
        else if (index > _getMaxIndex())
        {
            index = 0;
        }

        _setCurrentClip(index);
        _playClip(index);
    }

    this.previous = function()
    {
        _this.preview(_currentIndex - 1);
    }

    this.next = function()
    {
        _this.preview(_currentIndex + 1);
    }

}




