var ua = navigator.userAgent.toLowerCase();
var isIE = ua.indexOf("msie") != -1;
var isSafari = ua.indexOf("safari") != -1;

function jsqtPlayer(movieID)
{
	this.movieID = movieID;
	this.movie = null;
	this.playBtn = null;
	this.fwdBtn = null;
	this.backBtn = null;
	this.container = null;
	this.imgDiv = null;
	this.img = null;
	this.transportDiv = null;
	this.listDiv = null;
	this.listTable = null;
	this.statusDiv = null;
	this.ffwdBtn = null;

	this.defaultImg = null;
	
	this.playBtnImg = null;

	this.currentTrack = null;

	this.xmlhttp = null;
	this.playlistLoaded = false;

	this.playlistItems = new Array();

	this.playing = false;

	var thisObj = this;
	this.cleanup = function() { jsqtPlayer_cleanup.call(thisObj); };

	this.init();
}
jsqtPlayer.prototype.create = jsqtPlayer_create;
jsqtPlayer.prototype.init = jsqtPlayer_init;
jsqtPlayer.prototype.setup = jsqtPlayer_setup;
jsqtPlayer.prototype.play = jsqtPlayer_play;
jsqtPlayer.prototype.stop = jsqtPlayer_stop;
jsqtPlayer.prototype.back = jsqtPlayer_back;
jsqtPlayer.prototype.forward = jsqtPlayer_forward;
jsqtPlayer.prototype.ffwd = jsqtPlayer_ffwd;
jsqtPlayer.prototype.getPlaylist = jsqtPlayer_getPlaylist;
jsqtPlayer.prototype.parsePlaylist = jsqtPlayer_parsePlaylist;
jsqtPlayer.prototype.displayPlaylist = jsqtPlayer_displayPlaylist;
jsqtPlayer.prototype.attachPlaylistEvents = jsqtPlayer_attachPlaylistEvents;
jsqtPlayer.prototype.playlistMouseover = jsqtPlayer_playlistMouseover;
jsqtPlayer.prototype.playlistMouseout = jsqtPlayer_playlistMouseout;
jsqtPlayer.prototype.playlistClick = jsqtPlayer_playlistClick;
jsqtPlayer.prototype.poll = jsqtPlayer_poll;
jsqtPlayer.prototype.stopPolling = jsqtPlayer_stopPolling;
jsqtPlayer.prototype.setStatus = jsqtPlayer_setStatus;

jsqtPlayer.prototype.border = "1px solid black";
jsqtPlayer.prototype.hiliteColor = "#CCCCCC";
jsqtPlayer.prototype.overColor = "#999966";

var playImg = new Image();
playImg.src = "/img/jsqt/play.gif";
var pauseImg = new Image();
pauseImg.src = "/img/jsqt/pause.gif";
jsqtPlayer.prototype.playImg = playImg;
jsqtPlayer.prototype.pauseImg = pauseImg;

function jsqtPlayer_cleanup()
{
	this.movie = null;
	this.playBtn = null;
	this.container = null;
	this.imgDiv = null;
	this.img = null;
	this.transportDiv = null;
	this.listDiv = null;
	this.listTable = null;
	this.statusDiv = null;
	this.playBtnImg = null;
	this.ffwdBtn = null;

	this.xmlhttp = null;
}

function jsqtPlayer_init()
{
	var thisObj = this;
	if(isIE)
	{
		window.attachEvent("onload", function() { thisObj.setup(); });
		window.attachEvent("onunload", this.cleanup);
	}
	else
	{
		window.addEventListener("load", function() { thisObj.setup(); }, false);
		window.addEventListener("unload", this.cleanup, false);
	}
	
	this.create();
}

function jsqtPlayer_create()
{
	document.write("<div id='");
	document.write(this.movieID);
	document.write("_container' >");
	document.write("</div>");
}

function jsqtPlayer_setup()
{
	var thisObj = this;

	this.movie = document.getElementById(this.movieID);
	if(!isSafari && !isIE)
	{
		this.movie = this.movie.getElementsByTagName("EMBED")[0];
	}
	this.movie.SetKioskMode(true);
	this.movie.SetResetPropertiesOnReload(false);

	this.container = document.getElementById(this.movieID + "_container");
	this.container.style.border = this.border;

	this.imgDiv = document.createElement("DIV");
	this.imgDiv.style.width = "100px";
	this.imgDiv.style.height = "100px";
	this.imgDiv.style.backgroundColor = "#FFFFFF";
	this.imgDiv.style.borderRight = this.border;
	this.imgDiv.style.borderBottom = this.border;
	this.imgDiv.style.styleFloat = "left";
	this.imgDiv.style.cssFloat = "left";
	this.container.appendChild(this.imgDiv);

	this.img = document.createElement("IMG");
	this.img.height = "100";
	this.img.width = "100";
	this.img.style.display = "none";
	this.imgDiv.appendChild(this.img);

	if(this.defaultImg)
	{
		this.img.src = this.defaultImg;
		this.img.style.display = "";
	}

	this.transportDiv = document.createElement("DIV");
	this.transportDiv.style.borderBottom = this.border;
	this.transportDiv.style.width = "100%";
	this.transportDiv.style.height = "18px";
	this.transportDiv.style.backgroundColor = "#E7E7E7";
	this.container.appendChild(this.transportDiv);
	
	this.listDiv = document.createElement("DIV");
	this.listDiv.style.width = (this.container.offsetWidth - this.imgDiv.offsetWidth - 8) + "px";
	//this.listDiv.style.height = (parseInt(this.imgDiv.style.height) - this.transportDiv.offsetHeight - 40) + "px";
	this.listDiv.style.overflow = "auto";
	this.container.appendChild(this.listDiv);

	this.listTable = document.createElement("TABLE");
	this.listTable.style.fontSize = "8pt";
	//this.listTable.width = "100%";
	this.listTable.width = this.container.offsetWidth - this.imgDiv.offsetWidth - 25;
	this.listDiv.appendChild(this.listTable);
	
	this.statusDiv = document.createElement("DIV");
	this.statusDiv.style.height = "12px";
	this.statusDiv.style.fontSize = "8pt";
	this.statusDiv.style.clear = "both";
	this.statusDiv.style.borderTop = this.border;
	this.container.appendChild(this.statusDiv);
	
	this.backBtn = document.createElement("A");
	this.backBtn.style.cursor = "hand";
	this.backBtn.style.marginLeft = "5px";
	this.backBtn.style.marginRight = "5px";
	this.backBtn.title = "Previous Track";
	this.backBtn.onclick = function () { thisObj.back(); };
	var backBtnImg = document.createElement("IMG");
	backBtnImg.src = "/img/jsqt/back.gif";
	backBtnImg.height = 17;
	backBtnImg.border = 0;
	backBtnImg.alt = "Previous Track";
	this.backBtn.appendChild(backBtnImg);
	this.transportDiv.appendChild(this.backBtn);
	backBtnImg = null;
		
	this.playBtn = document.createElement("A");
	//this.playBtn.innerHTML = "Play";
	this.playBtn.style.cursor = "hand";
	this.playBtn.title = "Play";
	this.playBtn.onclick = function() { thisObj.play(); };
	this.transportDiv.appendChild(this.playBtn);

	this.playBtnImg = document.createElement("IMG");
	this.playBtnImg.src = this.playImg.src;
	this.playBtnImg.height = 17;
	this.playBtnImg.border = 0;
	this.playBtnImg.alt = "Play";
	this.playBtn.appendChild(this.playBtnImg);

	this.fwdBtn = document.createElement("A");
	this.fwdBtn.style.cursor = "hand";
	this.fwdBtn.style.marginLeft = "5px";
	this.fwdBtn.style.marginRight = "5px";
	this.fwdBtn.title = "Next Track";
	this.fwdBtn.onclick = function () { thisObj.forward(); };
	var fwdBtnImg = document.createElement("IMG");
	fwdBtnImg.src = "/img/jsqt/fwd.gif";
	fwdBtnImg.height = 17;
	fwdBtnImg.border = 0;
	fwdBtnImg.alt = "Next Track";
	this.fwdBtn.appendChild(fwdBtnImg);
	this.transportDiv.appendChild(this.fwdBtn);
	fwdBtnImg = null;

	/*
	this.ffwdBtn = document.createElement("A");
	this.ffwdBtn.href = "javascript:void(0);";
	this.ffwdBtn.innerHTML = "SPEED!";
	this.ffwdBtn.onmousedown = function() { thisObj.ffwd(.7); };
	this.ffwdBtn.onmouseup = function () { thisObj.ffwd(1); }; 
	this.transportDiv.appendChild(this.ffwdBtn);
	*/

	this.listDiv.style.height = (this.imgDiv.offsetHeight - this.transportDiv.offsetHeight) + "px";

	this.getPlaylist();
}

function jsqtPlayer_play()
{
	if(this.currentTrack == null)
	{
		this.playlistClick(0);
		return;
	}

	var movieStatus = this.movie.GetPluginStatus();
	var loaded = this.movie.GetMaxTimeLoaded();
	var totalTime = this.movie.GetDuration();
	var pctLoaded = (loaded / totalTime) * 100;
	if(movieStatus == "Waiting" || movieStatus == "Loading" || pctLoaded < 10)
	{
		this.setStatus("Buffering: " + Math.floor(pctLoaded) + "%");
		var thisObj = this;
		window.setTimeout(function() { thisObj.play(); }, 10);
		return;
	}

	if(this.playing)
	{
		this.stop();
	}
	else
	{
		var duration = this.movie.GetDuration();
		var scale = this.movie.GetTimeScale();		
		var maxPos = Math.floor(duration / scale)/60;
		var maxPosMin = Math.floor(maxPos);
		var maxPosSec = Math.floor((maxPos - maxPosMin) * 60);
		if(maxPosSec < 10) maxPosSec = "0" + maxPosSec;
		this.maxPosTime = maxPosMin + ":" + maxPosSec;
		
		var thisObj = this;
		this.interval = window.setInterval(function() { thisObj.poll(); }, 500);
		this.playBtnImg.src = this.pauseImg.src;
		this.playBtnImg.alt = "Pause";
		this.playBtn.title = "Pause";
		window.setTimeout(function() { thisObj.movie.Play(); thisObj.setStatus("Playing");}, 500);
		this.playing = true;
	}
}

function jsqtPlayer_stop()
{
	this.stopPolling();
	this.movie.Stop();
	this.playing = false;
	this.playBtnImg.src = this.playImg.src;
	this.playBtnImg.alt = "Play";
	this.playBtn.title = "Play";
	this.setStatus("Stopped");
}

function jsqtPlayer_back()
{
	if(this.currentTrack != null && this.currentTrack > 0)
		this.playlistClick(this.currentTrack - 1);
}

function jsqtPlayer_forward()
{
	if(this.currentTrack != null && this.currentTrack < (this.playlistItems.length - 1))
		this.playlistClick(this.currentTrack + 1);
}


function jsqtPlayer_ffwd(rate)
{
	if(!this.playing)
		return;

	this.movie.SetRate(rate);
}

function jsqtPlayer_getPlaylist()
{
	var thisObj = this;
	var parseFn = function() { thisObj.parsePlaylist(); };
	try
	{
		if(isIE)
		{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			this.xmlhttp.onreadystatechange = parseFn;
		}
		else
		{
			this.xmlhttp = new XMLHttpRequest();
			this.xmlhttp.onload = parseFn;
		}
	}
	catch (e)
	{
		alert(e.message);
	}

	if(!this.xmlhttp)
	{
		alert("error getting playlist");
		return;
	}

	var baseurl = window.location.protocol + "//" + window.location.host;
	this.xmlhttp.open("GET", baseurl + "/likeknives.xml", true);
	this.xmlhttp.send(null);
}

function jsqtPlayer_parsePlaylist()
{
	if(this.xmlhttp.readyState == 4)
	{
		if(this.xmlhttp.status != 200)
		{
			return;
		}
		
		var items = this.xmlhttp.responseXML.getElementsByTagName("track");
		for(var i=0; i<items.length; i++)
		{
			var item = new playlistItem();
			for(var j=0; j<items[i].childNodes.length; j++)
			{
				var attr = items[i].childNodes[j].nodeName;
				try
				{
					if(attr == "#text") continue;

					var val = items[i].childNodes[j].firstChild.data;
					switch(attr)
					{
						case "location":
							item.url = val;
							break;
						case "title":
							item.title = val;
							break;
						case "image":
							item.img = val;
							break;
						case "creator":
							item.perf = val;
							break;
					}
				}	
				catch (e)
				{
					alert(j + ": invalid playlist xml\n\n" + e.message);
					//break;
				}
			}
			this.playlistItems.push(item);
		}
		this.playlistLoaded = true;

		this.displayPlaylist();
	}
}


function jsqtPlayer_displayPlaylist()
{
	for(var i=0; i<this.playlistItems.length; i++)
	{
		var item = this.playlistItems[i];

		var tr = this.listTable.insertRow(-1);
		var td = tr.insertCell(-1);
		tr.style.cursor = "hand";
		this.attachPlaylistEvents(tr);

		td.innerHTML = item.title;
	}
}

function jsqtPlayer_attachPlaylistEvents(tr)
{
	var thisObj = this;
	var over = function() { thisObj.playlistMouseover(tr.rowIndex); };
	var out = function() { thisObj.playlistMouseout(tr.rowIndex); };
	var click = function() { thisObj.playlistClick(tr.rowIndex); };
	if(isIE)
	{
		tr.attachEvent("onmouseover", over);
		tr.attachEvent("onmouseout", out);
		tr.attachEvent("onclick", click);
	}
	else
	{
		tr.addEventListener("mouseover", over, true);
		tr.addEventListener("mouseout", out, true);
		tr.addEventListener("click", click, true);
	}
}

function jsqtPlayer_playlistMouseover(ind)
{
	this.listTable.rows[ind].style.backgroundColor = this.overColor;
}

function jsqtPlayer_playlistMouseout(ind)
{
	
	this.listTable.rows[ind].style.backgroundColor = ((this.currentTrack == ind)?this.hiliteColor:"");
}

function jsqtPlayer_playlistClick(ind)
{
	if(this.currentTrack == ind)
		return;

	var item = this.playlistItems[ind];

	this.img.src = item.img;
	this.img.style.display = "";

	if(this.currentTrack != null)
		this.listTable.rows[this.currentTrack].style.backgroundColor = "";

	this.listTable.rows[ind].style.backgroundColor = this.hiliteColor;

	try
	{
	this.stopPolling();
	if(this.playing)
	{
		this.movie.Stop();
		this.playing = false;
	}
	this.currentTrack = ind;
	this.movie.SetURL(item.url);
	this.play();
	}
	catch (e)
	{
		if(isIE)
		{
			var facility = e.number>>16 & 0x1FFF;
			var errcode = e.number & 0xFFFF;
			alert("Error:\nFacility Code: " + facility + "\nError Code: " + errcode + "\nMessage: " + e.message);
		}
		else
			alert(e.message);
	}

}

function jsqtPlayer_poll()
{
	var time = this.movie.GetTime();
	var duration = this.movie.GetDuration();

	if(time >= duration)
	{
		if(this.currentTrack < (this.playlistItems.length - 1))
		{
			this.playlistClick(this.currentTrack + 1);
		}
		else
		{
			this.stop();
		}
	}
	//window.status = time + "/" + duration;
	var scale = this.movie.GetTimeScale();
	var curPos = Math.floor(time / scale)/60;
	var curPosMin = Math.floor(curPos);
	var curPosSec = Math.floor((curPos - curPosMin) * 60);
	if(curPosSec < 10) curPosSec = "0" + curPosSec;
	var curPosTime = curPosMin + ":" + curPosSec;
	this.setStatus(curPosTime + "/" + this.maxPosTime);

	return true;
}

function jsqtPlayer_stopPolling()
{
	if(this.interval)
	{
		window.clearInterval(this.interval);
		this.interval = null;
	}

}

function jsqtPlayer_setStatus(str)
{
	if(this.statusDiv.innerHTML != str)
		this.statusDiv.innerHTML = str;
}
/*****************************/
/** BEGIN playlistItem      **/
//****************************/
function playlistItem(url, title, img, perf)
{
	this.url = url;
	this.title = title;
	this.img = img;
	this.performer = perf;
}

/*****************************/
/** END playlistItem      **/
//****************************/

