
    var videoPlayer = {
        // instance videoPlayeru
        instances: [],

        instance: function(wrapper) {
            // vytvorime prehravac
            var player = new this.player(wrapper);

            return player;
        },

        player: function(wrapper) {
            // wrapper
            this.wrapper = wrapper;

            this.addFile = function(uri) {
                // pridame do playlistu
                return this.playList.addItem(uri);
            }

            // reklama
            this.addAdvert = function(uri, link) {
                return this.addFile(uri).setAdvert(link ? link : true);
            }

            this.create = function() {
                // identifikator
                this.id = 'videoPlayer_' + (videoPlayer.instances.length + 1);

                // instance
                videoPlayer.instances[this.id] = this;

                // textovy identifikator instance .. pro event handlery apod.
                this.instanceString = 'videoPlayer.instances["' + this.id + '"]';

                // velikost
                this.width = this.playList.maxWidth;
                this.height = this.playList.maxHeight + 19;
                document.getElementById(this.wrapper).style.width = this.width + 'px';
                document.getElementById(this.wrapper).style.height = this.height + 'px';

                // nacteme soubor
                this.load();
            }

            this.load = function() {
                // aktualni playlist item
                var item = this.playList.getItem();

                // odebereme SWF player
                swfobject.removeSWF(this.id);

                // vytvorime novy SWF player

                // velikost
                //this.width = item.width;
                //this.height = item.height + 19;
                //document.getElementById(this.wrapper).style.width = this.width + 'px';
                //document.getElementById(this.wrapper).style.height = this.height + 'px';

                // container
                var container = this.wrapper + '_container';
                document.getElementById(this.wrapper).innerHTML = '<div id="' + container + '" style="width:' + this.width + 'px; height:' + this.height + 'px: background: black"></div>';

                var flashvars = {
                    // soubor
                    file: item.uri,
                    // autostart .. pokud neni prvni z playlistu, automaticky prehravame
                    autostart: (item.position > 1 ? 'true' : 'false'),
                    // pro reklamu nezobrazujeme controlbar
                    controlbar: (item.isAdvert ? 'none' : 'bottom'),
                    // vypneme dock
                    dock: 'false'
                };
                
                // titulky
                if (item.captions) {
                    flashvars['plugins'] = 'captions-1';
                    flashvars['captions.file'] = item.captions;
                    flashvars['captions.state'] = 'true';
                }

                // pro reklamu klikaci plugin
                if (item.isAdvert) {
                    flashvars.plugins += ',clickproxy';
                    flashvars['clickproxy.listener'] = this.instanceString + '.click';
                }

                var params = {
                    allowfullscreen: 'true',
                    allowscriptaccess: 'always'
                }

                var attributes = {
                    id: this.id,
                    name: this.id
                }

                swfobject.embedSWF('http://img.csfd.cz/tools/videoplayer/player.swf', container, this.width, this.height, '9.0.115', false, flashvars, params, attributes);
            }

            this.click = function() {
                var link = this.playList.getItem().link;

                if (link) {
                    document.location = link;
                }
            }

            this.ready = function() {
                // playerObject
                this.playerObject = document.getElementById(this.id);
                // zmena stavu
                this.playerObject.addModelListener('STATE', this.instanceString + '.onStateChange');
            }

            this.onStateChange = function(obj) {
                switch (obj.newstate) {
                    case 'COMPLETED':
                        // dalsi item v playlistu nebo rewind na zacatek
                        this.playList.moveNext();
                        
                        // nacteme
                        setTimeout(this.instanceString + '.load()', 1);

                        break;

                    case 'PAUSED':
                        // po odkliku na item.link se prehravani zastavi, pustime ho dal
                        if (this.playList.getItem().link) {
                            this.playerObject.sendEvent('PLAY');
                        }

                        break;
                }
            }

            // playlist
            this.playList = {
                // aktualni pozice
                position: 1,

                // prvky playlistu
                items: [],

                // velikost nejvetsiho videa
                maxWidth: 0,
                maxHeight: 0,

                // novy soubor v playlistu
                addItem: function(uri) {
                    // instance prvku playlistu
                    var item = new this.item(uri, this);

                    // pridame do pole itemu
                    this.items.push(item);

                    // pozice
                    item.position = this.items.length;

                    return item;
                },

                getItem: function(position) {
                    return this.items[(position ? position : this.position) - 1];
                },

                moveNext: function() {
                    // posuneme na dalsi item
                    this.position++;

                    // item
                    if (this.getItem()) {
                        return true;
                    }
                    else {
                        this.position = 1;
                        return false;
                    }
                },

                item: function(uri, playList) {
                    // cesta k souboru
                    this.uri = uri;

                    // reference na playlist
                    this.playList = playList;

                    // titulky
                    this.captions = null;

                    // velikost
                    this.width;
                    this.height;

                    // odklik
                    this.link;

                    // je to reklama
                    this.isAdvert = false;

                    this.setCaptions = function(uri) {
                        this.captions = uri;
                        return this;
                    }

                    this.click = function() {
                        alert(this.link);
                    }

                    this.setSize = function(width, height) {
                        // sirka
                        this.width = width;

                        if (width > this.playList.maxWidth) {
                            this.playList.maxWidth = width;
                        }

                        // vyska
                        this.height = height;

                        if (height > this.playList.maxHeight) {
                            this.playList.maxHeight = height;
                        }

                        return this;
                    }

                    this.setLink = function(link) {
                        this.link = link;
                        return this;
                    }

                    this.setAdvert = function(value) {
                        // value muze byt odkaz
                        if (typeof(value) == 'string') {
                            this.setLink(value);
                            value = true;
                        }

                        // pokud neni definovano => true
                        if (value == undefined) {
                            value = true;
                        }

                        this.isAdvert = value;

                        return this;
                    }
                }
            }
        }
    }

    function playerReady(obj) {
        videoPlayer.instances[obj.id].ready();
    }

