Playa = {};


Playa.__getPlaya = function() {
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		var p = window["pl"];
	} else {
		var p = document["pl"];
	}
	if (typeof(p) != "undefined") {
		if (p.PercentLoaded() == 100) {
			return p;
		}
	}
	return null;
};

Playa.poll = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		var params = {};
		params.title = p.GetVariable("jsTitle");
		params.playListPosition = parseInt(p.GetVariable("jsPlayListPosition"));
		params.path = p.GetVariable("jsPath");
		params.btnState = p.GetVariable("jsBtnState");
		params.playlistSize = parseInt(p.GetVariable("jsPlaylistSize"));

		Playa.updateInterface(params);
	}
	setTimeout("Playa.poll()", 200);
};

Playa.start = function() {
	if (Playa.__isStarted) {
		return;
	}
	Playa.poll();
	Playa.__isStarted = true;
};

Playa.doPlayStop = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "PlayStop");
	}
};

Playa.doPlay = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "Play");
	}
};

Playa.doStop = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "Stop");
	}
};

Playa.doNext = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "Next");
	}
};

Playa.updateInterface = function(params) {
	sTitle = (params.playListPosition+1) + "/" + params.playlistSize + " ";
	if (params.path == "") {
		sTitle += params.title;
	} else {
		sTitle += "<a href=\"" + params.path + "\" target=\"_blank\" title=\"Click to download\">" + params.title + "</a>";
	}

	document.getElementById("songTitle").innerHTML = sTitle;
	document.getElementById("btnPlayStop").value = params.btnState;
}

Playa.start();
