var cTrailerTv = Class.create(cView, {
	initialize: function($super, _name, _container, _reference) {
		$super(_name, _container, _reference);
		this.data = [];	
	},
	init: function() {
		this.getData(['start']);
		this.actual = 0;	
	},
	getData: function(options) {
		options = options || [];
		var keres = new Ajax.Request('trailerTv.php?' + options.join('&'), {
			options: options,
			method:'get',
			onSuccess: this.getDataCallback.bind(this),
			onFailure: function() {
				// todo
			}
		});	
	},
	getDataCallback: function(transport) {
		var ret = transport.responseText.evalJSON();
		for (var i=0; i<ret.length; i++) {
			this.data.push(ret[i]);
		}
		if (transport.request.options.options.indexOf('start') != -1) {
			this.render();
		}
	},
	render: function() {
		templater.render(this.container, 'template_trailertv', this.data, this.afterRender.bind(this));
	},
	afterRender: function() {
		templater.observeElement($('trailerTvPrev'), 'click', this.playPrev.bind(this));
		templater.observeElement($('trailerTvNext'), 'click', this.playNext.bind(this));
		this.renderMovie();
		this.renderThumb();
		this.renderComming();
	},
	renderMovie: function() {
		var videoID = '';
		var movie = this.data[this.actual];
		for (var j=0; j<movie.linkek.length; j++) {
			if (movie.linkek[j].name == 'trailer') {
				videoID = movie.linkek[j].url.unescapeHTML();
			}
		}
		
		window['onYouTubePlayerReady'] = this.afterRenderMovie.bind(this);
		
		// Lets Flash from another domain call JavaScript
		var params = { allowScriptAccess: "always" };
		// The element id of the Flash embed
		var atts = { id: "trailerTvPlayer" };
		// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
		swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
		                 "&enablejsapi=1&playerapiid=player1",
		                 "trailerTvPlayer", "480", "295", "8", null, null, params, atts);
		this.player = $('trailerTvPlayer');	
	},
	afterRenderMovie: function() {
		window['onYouTubePlayerStateChanged'] = this.playerStateChanged.bind(this);
		this.player.addEventListener('onStateChange', "onYouTubePlayerStateChanged");
		this.player.playVideo();
	},
	playerStateChanged: function(state) {
		if (state == 0) {
			this.playNext();
		} else {
			//console.log(state);
		}
	},
	playPrev: function() {
		if (this.actual > 0) {
			this.playIndex(this.actual-1);
		}
		return false;
	},
	playNext: function() {
		this.playIndex(this.actual+1);
		return false;
	},
	playIndex: function(index) {
		this.actual = index;	
		this.loadMovie();
		this.renderThumb();
		this.renderComming();
		if (this.data.length <= this.actual + 2) {
			this.getData();
		}
	},
	loadMovie: function() {
		var videoID = '';
		var movie = this.data[this.actual];
		for (var j=0; j<movie.linkek.length; j++) {
			if (movie.linkek[j].name == 'trailer') {
				videoID = movie.linkek[j].url.unescapeHTML();
			}
		}
		this.player.loadVideoById(videoID);
	},
	renderThumb: function() {
		$('trailerTvThumb').innerHTML = '';
		var thumb = new cThumb($('trailerTvThumb'));
		thumb.loadData(FilmX.apply({type: 'movie_big'}, {movie: this.data[this.actual]}));
	},
	renderComming: function() {
		$('trailerTvComming').innerHTML = '<img src="' + this.data[this.actual+1].pic + '" title="">';
	}
});
