var PlanetPlayer = Class.create();
PlanetPlayer.prototype = {
	initialize: function(obj) {
		this.playerPath = obj.playerPath;
		this.file = obj.file;
		this.name = obj.name;
		this.config = obj.config;
		this.width = obj.width;
		this.height = obj.height;
		this.displaywidth = obj.displaywidth;
		this.displayheight = obj.displayheight;
		this.version = obj.version;

		// create the flash object
		this.so = new SWFObject(
			this.playerPath,
			this.name,
			this.width,
			this.height,
			this.version,
			'#000000'
		);

		// set essential player defaults
		this.addParam("allowscriptaccess","always")
			.addParam("allowfullscreen","true")
			.addVariable("file",this.file);
		if(this.config) this.addVariable("config",this.config);
		if(this.name) this.addVariable("javascriptid",this.name);
		if(this.width) this.addVariable("width",this.width);
		if(this.height) this.addVariable("height",this.height);
		if(this.displaywidth) this.addVariable("displaywidth",this.displaywidth);
		if(this.displayheight) this.addVariable("displayheight",this.displayheight);
	},
	getPlaylist: function(url) {
		url = decodeURIComponent(url);
		var t17 = this;
		new Ajax.Request(url, {
			method:'get',
			asynchronous:false,
			onSuccess: function(transport){
				t17.playlist = transport.responseXML.documentElement;
			}
		});
	},
	addParam: function(key, value) {
		this.so.addParam(key,value);
		return this;
	},
	setLogo: function() {
		metatags = this.playlist.getElementsByTagName("meta");
		for(var i = 0; i < metatags.length; i++) {
			if(metatags[i].hasChildNodes()) {
				if(metatags[i].getAttribute("rel") == "logo") {
					this.addVariable("logo", metatags[i].firstChild.data);
					return;
				}
			}
		}
	},
	addVariable: function(key, value) {
		if(key == "file") {
			this.getPlaylist(value);
			this.setLogo();
		};

		this.so.addVariable(key,value);
		return this;
	},
	write: function(dst) {
		this.so.write(dst);
		return this;
	},
	loadFile: function(obj) {
		this.getPlaylist(obj.file);
		this.setLogo();

		$(this.name).loadFile(obj);
		return this;
	},
	sendEvent: function(typ,prm) {
		$(this.name).sendEvent(typ,prm);
	},
	addItem: function(obj,idx) {
		$(this.name).addItem(obj,idx);
	},
	removeItem: function(idx) {
		$(this.name).removeItem(idx);
	},
	getItemData: function(idx) {}
};

function getUpdate(typ,pr1,pr2,pid) {};

var flvPlayers = [];
/**
 * playerInit(dst, name, width, height, sendungId, [kapitel], noTrackList]);
 */
function playerInit(obj) {
	var flvPlayer = "player/mediaplayer.swf";
	var playList  = "player/playlist.php";
	var configXML = "player/config.xml";
	obj.playerPath = flvPlayer;
	obj.version = "8";
	obj.config = configXML;

	if(!obj.dst) { return; }
	if(!obj.name) { return; }
	if(!obj.width) { obj.width = 320; }
	if(!obj.height) { obj.height = 300; }
	if(!obj.displaywidth) { obj.displaywidth = obj.width; }
	if(!obj.displayheight) { obj.displayheight = obj.height - 60; }

	if(obj.noplaylist == 1) {
		obj.displayheight = obj.height - 20;
	}

	var queryParam = "";
	if(obj.sendungId) {
		queryParam += "sendungId=" + obj.sendungId;
		if(obj.kapitel) {
			queryParam += "&kapitel=" + obj.kapitel;
		}
	}
	else if(obj.clipId) {
		queryParam += "clipId=" + obj.clipId;
	}
	obj.file = playList + "?" + encodeURIComponent(queryParam);

	pPlayer = new PlanetPlayer(obj);
	pPlayer.write(obj.dst);
}
