/**
* Filename.......: videoplayer.js
* Last Modified..: 14.1.2009
* Copyright......: 2009 POMO Media Group s.r.o.
*/ 

	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 + 20;
				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

				// 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'),
					// po kliknuti na display presmerovani na link, nebo pause/resume (neplati pro reklamu)
					displayclick: (item.link ? 'link' : (item.isAdvert ? 'none' : 'play')),
					// odklik
					link: item.link,
					// titulky
					captions: (item.captions ? item.captions : 'false'),
					// plugins
					plugins: 'accessibility'
				}

				var params = {
					allowfullscreen: 'true', 
					allowscriptaccess: 'always'
				}

				var attributes = {
					id: this.id,  
					name: this.id
				}

				swfobject.embedSWF('http://img.csfd.cz/tools/videoplayer/399/player.swf', container, this.width, this.height, '9.0.115', false, flashvars, params, attributes);
			}

			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 prvni
						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.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();
	}

