
function PodcastPlayer(feedURL, renderInto) {
	PodcastPlayer._instance = this;
	this._feedURL = feedURL;
	this._targetDIV = document.getElementById(renderInto);
	this._items = [];
	try {
		this._initSWFPlayer();
	} catch(err) {
		debug("InitSWFPlayer: "+err);
	}
}
// public vars
PodcastPlayer.prototype.onCreate = null;
PodcastPlayer.prototype.onCreateItem = null;
PodcastPlayer.prototype.onItemPlayState = null;
PodcastPlayer.prototype.onItemLoadState = null;
PodcastPlayer.prototype.plays = false;

// private static vars
PodcastPlayer._instance = null;
PodcastPlayer._feedReq = null;
// private dynamic vars
PodcastPlayer.prototype._swfPlayer = null;
PodcastPlayer.prototype._feedURL = null;
PodcastPlayer.prototype._targetDIV = null;
PodcastPlayer.prototype._items = null;
PodcastPlayer.prototype.setup = function() {
	// load feed
	PodcastPlayer._feedReq = new XMLHTTPObject();
	PodcastPlayer._feedReq.onreadystatechange = this._handleReq;
	PodcastPlayer._feedReq.open("GET", this._feedURL+"?time="+(new Date().getTime()), true);
	PodcastPlayer._feedReq.send("");
}
PodcastPlayer.prototype._handleReq = function() {
	switch (PodcastPlayer._feedReq.readyState) {
		case 0: // not inited
			break;
		case 1: // loading
			break;
		case 2: // loaded
			break;
		case 3: // interactive
			break;
		case 4: // finished
			try {
				var xml = PodcastPlayer._feedReq.responseXML;
				var test = xml.firstChild;
			} catch(err) {
				try {
					var d = document.createElement("DIV");
					d.innerHTML = PodcastPlayer._feedReq.responseText;
					document.body.appendChild(d);
					var xml = d.firstChild;
				} catch (err2) {
					debug("XML error: "+err2);
				}
			}
			PodcastPlayer._instance._renderXML(xml);
			break;
	}
}
PodcastPlayer.prototype._renderXML = function(xml) {
	try {
		this._renderNode(xml);
		if (this.onCreate) this.onCreate(this);
	} catch(err) {
		debug("RenderXML: "+err);
	}
}
PodcastPlayer.prototype._renderNode = function(node) {
	if (node.nodeType == 1) {
		switch (node.nodeName) {
			case "item":
				var itm = new PodcastPlayerItem(node,this._items.length);
				this._items.push(itm);
				this._targetDIV.appendChild(itm.elementNode);
				if (this.onCreateItem) this.onCreateItem(itm,this);
				return;
		}
	} else if (node.nodeType == 3) {
		return;
	}
	for (var i=0;i<node.childNodes.length;i++) this._renderNode(node.childNodes[i]);
}
PodcastPlayer.prototype.itemBySource = function(src) {
	for (var i=0;i<this._items.length;i++) if (unescape(this._items[i].source) == unescape(src)) return this._items[i];
}
PodcastPlayer.prototype.nextItemBySource = function(src) {
	for (var i=0;i<this._items.length;i++) if (unescape(this._items[i].source) == unescape(src)) if (this._items.length > i+1) return this._items[i+1];
}
PodcastPlayer.prototype._current = null;
PodcastPlayer.prototype.stop = function(src) {
	this._swfPlayer.stop();
	this._current = null;
}

PodcastPlayer.prototype.play = function(src) {
	var crn = this._current;
	if (this._current) this.stop(this._current);
	if (unescape(crn) != unescape(src)) {
		this._swfPlayer.play(stripslashes(src));
		this._current = src;
	}
}
PodcastPlayer.prototype.playFirst = function(src) {
	if (this._items.length > 0) this.play(this._items[0].source);
}
PodcastPlayer.prototype.playNext = function(src) {
	var next = PodcastPlayer._instance.nextItemBySource(src);
	if (next) PodcastPlayer._instance.play(next.source);
}

PodcastPlayer.prototype._initSWFPlayer = function() {
	this._swfPlayer = new SWFSoundPlayer();
//	SWFSoundPlayer.setup();
	this._swfPlayer.onStartLoad = function(src) {
		var item = PodcastPlayer._instance.itemBySource(src)
		if (PodcastPlayer._instance.onItemLoadState) item.setLoad(true);
	}
	this._swfPlayer.onFinishLoad = function(src) {
		var item = PodcastPlayer._instance.itemBySource(src)
		if (PodcastPlayer._instance.onItemLoadState) item.setLoad(false);
	}
	this._swfPlayer.onStartPlay = function(src) {
		PodcastPlayer.prototype.plays = true;
		var item = PodcastPlayer._instance.itemBySource(src);
		if (PodcastPlayer._instance.onItemPlayState) item.setPlay(true);
	}
	this._swfPlayer.onFinishPlay = function(src) {
		// reset current
//		if (PodcastPlayer._instance._current == null) return;
		PodcastPlayer.prototype.plays = false;
		PodcastPlayer._instance.itemBySource(src).reset();
		
		// play next
		PodcastPlayer._instance.playNext(src);
		// play next if exists
	}
	this._swfPlayer.onCancelPlay = function(src) {
		PodcastPlayer.prototype.plays = false;
		PodcastPlayer._instance.itemBySource(src).reset();
	}
	this._swfPlayer.onProgress = function(progress,src) {
		PodcastPlayer._instance.itemBySource(src).setProgress(progress);
	}
	this._swfPlayer.onLoadProgress = function(progress,src) {
		PodcastPlayer._instance.itemBySource(src).setLoadProgress(progress);
	}
	this._swfPlayer.onTimecode = function(time,src) {
		PodcastPlayer._instance.itemBySource(src).setTimecode(time);
	}
}












function PodcastPlayerItem(xmlNode,id) {
	this.published = new Date();
	this._fromXML(xmlNode);
	this._createElement("playerItem_"+id);
	this.n = id;
}
PodcastPlayerItem.prototype._loadstate = false;
PodcastPlayerItem.prototype._playstate = false;
PodcastPlayerItem.prototype.n = null;
PodcastPlayerItem.prototype.buttonNode = null;
PodcastPlayerItem.prototype.buttonstateNode = null;
PodcastPlayerItem.prototype.elementNode = null;
PodcastPlayerItem.prototype.timecodeNode = null;
PodcastPlayerItem.prototype.titleNode = null;
PodcastPlayerItem.prototype.progressNode = null;
PodcastPlayerItem.prototype.loadProgressNode = null;
PodcastPlayerItem.prototype._createElement = function(id) {
	this.elementNode = document.createElement("DIV");
	this.elementNode.setAttribute("className","item");
	this.elementNode.setAttribute("class","item");
	this.elementNode.setAttribute("id",id);
	
	// progress bar
	this.progressNode = document.createElement("DIV");
	this.progressNode.setAttribute("className","progress");
	this.progressNode.setAttribute("class","progress");

	var e4 = document.createElement("DIV");
	e4.setAttribute("className","progressBar");
	e4.setAttribute("class","progressBar");
	e4.appendChild(this.progressNode);
	this.elementNode.appendChild(e4);

	this.loadProgressNode = document.createElement("DIV");
	this.loadProgressNode.setAttribute("className","loadProgress");
	this.loadProgressNode.setAttribute("class","loadProgress");

	var e5 = document.createElement("DIV");
	e5.setAttribute("className","loadProgressBar");
	e5.setAttribute("class","loadProgressBar");
	e5.appendChild(this.loadProgressNode);
	this.elementNode.appendChild(e5);

	// title
	this.titleNode = document.createElement("a");
	this.titleNode.setAttribute("className","title");
	this.titleNode.setAttribute("class","title");
	this.titleNode.setAttribute("title",this.title);
//	var s = this.source.split("/").pop();
//	var e3 = document.createTextNode(this.title+" ("+s+")");
	var e3 = document.createTextNode(this.title);
	this.titleNode.appendChild(e3);
	this.elementNode.appendChild(this.titleNode);

	var bc =  document.createElement("div");
	bc.setAttribute("className","button_container");
	bc.setAttribute("class","button_container");
	this.elementNode.appendChild(bc);
	
	this.buttonNode = document.createElement("a");
	this.buttonNode.setAttribute("className","button");
	this.buttonNode.setAttribute("class","button");
	this.buttonNode.setAttribute("href","javascript:PodcastPlayer._instance.play('"+addslashes(this.source)+"');");
	bc.appendChild(this.buttonNode);
	
	this.buttonstateNode = document.createElement("span");
	this.buttonstateNode.setAttribute("className","state");
	this.buttonstateNode.setAttribute("class","state");
	this.buttonNode.appendChild(this.buttonstateNode);
	
	this.timecodeNode = document.createElement("div");
	this.timecodeNode.setAttribute("className","timecode");
	this.timecodeNode.setAttribute("class","timecode");
	this.elementNode.appendChild(this.timecodeNode);

}
PodcastPlayerItem.prototype.reset = function() {
	this.setLoad(false);
	this.setPlay(false);
	this.setProgress(0);
	this.setTimecode(-1);
	this.setLoadProgress(0);
}
PodcastPlayerItem.prototype.setProgress = function(progr) {
	this.progressNode.style.width = Math.round(progr*100)+"%";
}
PodcastPlayerItem.prototype.setTimecode = function(time) {
	if (time < 0) {
		this.timecodeNode.innerHTML = "";
		return;
	}
	var secs = Math.round(time/1000);
	var m = ("00"+Math.floor(secs/60)).substr(-2);
	var s = ("00"+(secs%60));
	s = s.substr(s.length-2,2);
	m = m.substr(m.length-2,2);
	this.timecodeNode.innerHTML = m+":"+s;
}
PodcastPlayerItem.prototype.setLoadProgress = function(progr) {
	this.loadProgressNode.style.width = Math.round(progr*100)+"%";
}
PodcastPlayerItem.prototype.setPlay = function(p) {
	if (this._playstate != p && PodcastPlayer._instance.onItemPlayState) PodcastPlayer._instance.onItemPlayState(this,p);
	this._playstate = p;
}
PodcastPlayerItem.prototype.setLoad = function(l) {
	if (this._loadstate != l && PodcastPlayer._instance.onItemLoadState) PodcastPlayer._instance.onItemLoadState(this,l);
	this._loadstate = l;
}
PodcastPlayerItem.prototype.title = "";
PodcastPlayerItem.prototype.description = "";
PodcastPlayerItem.prototype.published = null;
PodcastPlayerItem.prototype.source = "";

PodcastPlayerItem.prototype._fromXML = function(xmlNode) {
	if (xmlNode.nodeType == 3) return;
	switch (xmlNode.nodeName) {
		case "title":
			this.title = xmlNode.firstChild.nodeValue;
			return;
		case "pubDate":
			var parts = xmlNode.firstChild.nodeValue.split("-");
			this.published.setFullYear(parts[0]);
			this.published.setMonth(parts[1]-1);
			this.published.setDate(parts[2]);
			return;
		case "description":
			this.description = xmlNode.firstChild.nodeValue;
			return;
		case "enclosure":
			this.source = unescape(xmlNode.getAttribute("url"));
			return;
	}
	for (var i=0;i<xmlNode.childNodes.length;i++) this._fromXML(xmlNode.childNodes[i]);
}





function addslashes(s) {
	return s.split("'").join("\\'").split('"').join('\\"')
}
function stripslashes(s) {
	return s.split("\\'").join("'").split('\\"').join('"')
}