// used to build a video player with a queue
// relies on there being a Flash player and a location for the player HTML structure
var VideoPlayer = Class.create({
	
  initialize: function(queue, player_container, content_container, clipautoplay, video_embed_field) {
		this.queue = queue;
		this.player_container = player_container;
		this.content_container = content_container;
		this.q_index = 0;
		this.clipautoplay = clipautoplay;
		if (video_embed_field) {
			this.video_embed_field = video_embed_field;
			$(this.video_embed_field).observe('focus', function () { this.select(); } );
		}
  },
	
	load_video: function() {
		var flashvars = {
			clipURL: this.queue[this.q_index]['video_url'],
			clipinfo: this.queue[this.q_index]['title'],
			clipautoplay: this.clipautoplay
		};
		var params = {
			allowFullScreen: "true",
			bgcolor: "#ffffff",
			wmode: "transparent"
		};
		swfobject.embedSWF("/flash/apt11_player.swf", this.player_container, "540", "426", "9.0.0", null, flashvars, params);
		$(this.content_container).update(this.queue[this.q_index]['content'].replace(/\n/g, "<br />"));
		if (this.video_embed_field) {
			$(this.video_embed_field).hide();
			$(this.video_embed_field).value = '<iframe src="http://' + document.domain + '/videos/embed/' + this.queue[this.q_index]['id'] + '" height="510" width="540" frameborder="0"></iframe>';
		}
	},
	
	next: function() {
		this.q_index++;
		if (this.q_index == this.queue.length) {
			this.q_index = 0;
		}
		this.load_video();
	},
	
	previous: function() {
		this.q_index--;
		if (this.q_index < 0) {
			this.q_index = this.queue.length - 1;
		}
		this.load_video();		
	},
	
	show_embed: function() {
		$(this.video_embed_field).show();
	},
	
	hide_embed: function() {
		$(this.video_embed_field).hide();
	}
	
});
