// $Id: pnpblock.js 51 2008-05-19 15:28:32Z willem $
// data class for pnp-block tool
var PNPBlock = function() {
	// init()
	this.init(arguments);
}

PNPBlock.prototype.current_episode = null;
PNPBlock.prototype.id = null;
PNPBlock.prototype.width = null;
PNPBlock.prototype.height = null;
PNPBlock.prototype.prefix = null;
PNPBlock.prototype.url = null;
PNPBlock.prototype.background_color = null;
PNPBlock.prototype.border_color = null;
PNPBlock.prototype.thumbcount = null;
PNPBlock.prototype.thumbrow = null;
PNPBlock.prototype.thumbsort = null;
PNPBlock.prototype.args = {};
PNPBlock.prototype.debug = false;

PNPBlock.prototype.init = function(args) {
	dat = new Array();
	for(i = 0; i < args.length; i++)
		dat.push(args[i]);	
	
	if (dat.length > 0) {
		this.setArgs(dat.shift());	
	}
	// find magic debug hostname
	var parts = window.location.href.split('/');
	if (parts)
		this.debug = (parts[2].indexOf('magic_debug')> -1);
}

PNPBlock.prototype.setArgs = function(querystring) {
	if (! querystring) return 0;

	var paramstr = querystring.split('&');
	for (var i=0; i < paramstr.length; i++) {
		var keyval = paramstr[i].split('=');
		
		//alert(i + "-> " + keyval.join(': '));
		this.args[keyval[0]] = keyval[1];			
	}
	return i;
}

PNPBlock.prototype.browse = function(start, targ) {
	
	var args = new Array(
				'genre=' + this.id,
				'size=' + this.width + 'x' + this.height
				);
	if (start)
		args.push('start=' + start);
	
	// Value is optional
	if (this.thumbcount)
		args.push('count=' + this.thumbcount);

	// Value is optional
	// Number of thumbs that consist a row of
	if (this.thumbrow)
		args.push('row=' + this.thumbrow);
	if (this.thumbsort)
		args.push('sort=' + this.thumbsort);

	xmlClass = new FSide();
	
	xmlClass.rewriteTag = targ;
	xmlClass.callback = "play";

	xmlClass.xmlObj.open("GET", this.prefix + "browse/index.php?" + args.join('&'), true);
	xmlClass.xmlObj.send(null);
}

PNPBlock.prototype.tekst = function(targ) {
	var args = new Array(
				'genre=' + this.id,
				'episode=' + this.current_episode,
				'size=' + this.width + 'x' + this.height
				);
	episodeClass = new FSide();
	
	episodeClass.rewriteTag = targ;
	episodeClass.xmlObj.open("GET", this.prefix + "episode/index.php?" + args.join('&'), true);
	episodeClass.xmlObj.send(null);
}

PNPBlock.prototype.play = function(episode, targ) {
	// Load text into place before the applet
	
	if (!episode && !this.current_episode) {
		var tmp = document.getElementById("current_episode");
		if (tmp)
			episode = tmp.className;
	}
	
	if (! episode) return;
		this.current_episode = episode;
	var so = new SWFObject(this.url, 
							"mymovie", 
							"100%", "100%", "8", 
							"#" + this.background_color);
	
	so.addParam("allowfullscreen", true);
	so.addParam("allowscriptaccess", "samedomain");
	so.addParam("wmode", "transparent");
	so.addParam("menu", true);
	
	// Add parameter list
	for(var key in this.args)
		so.addVariable(key, this.args[key]);

	so.addVariable("id", this.current_episode);

	if (this.debug) {
		str = "Loading flash applet\n" + this.url + "\nwith params:\n";
		for(var key in this.args)
			str += key + "=" + this.args[key] + ", ";
		alert(str);
	}

	if (this.border_color)
		so.addVariable('pcolor', '0x' + this.border_color);

	so.write(targ);
}
