// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

function getItemData(idx) {
	var obj = thisMovie("mpl").itemData(idx);
	var movieMetaData = "";
	
	if(obj.category != "postroll" && obj.category != "preroll") {
		movieMetaData = "<h4 style=\"margin-bottom: 0;\">" + obj.author + "</h4><p style=\"margin-top:0;margin-bottom:0;\"><em>by " + obj.title + "</em></p><div id=\"starRating\"></div><input id=\"CurPage\" name=\"CurPage\" type=\"hidden\" value=\"1\" />";
		if(obj.description != "null") {
			movieMetaData += "<p>" + obj.description.replace(/\|/g, "<br />") + "</p>";
		}
		movieMetaData += "<p><a href=\"/comment.asp?id=" + obj.id + "\" onclick=\"showCommentForm(" + obj.id + "); return false;\">Leave a Comment</a></p>";
		document.getElementById("movieMetaData").innerHTML = movieMetaData;
		reqComments(obj.id);
		reqStars(obj.id);
		incrementPlayCount(obj.id);
		if($('permalink')) {
			$('permalink').update("(<a href=\"/play.asp?id=" + obj.id + "\">permalink</a>)");
		}
	}
};

function getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100); }
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
};

function pageJump(id, page) {
	$('CurPage').value = page;
	reqComments(id);
}

function reqComments(id) {
	// Build hash of values to POST
	var h = $H({
		CurPage: document.getElementById('CurPage').value,
		mediaId: id
	});
	
	new Ajax.Request('/ajax/comments.asp', {
		parameters: h.toQueryString(),
		onSuccess: function(transport) {
			if(transport.responseText == null) {
				$('comments').update("");
			}
			else {
				$('comments').update(transport.responseText);
			}
		},
		onFailure: function() {
			$('comments').update('<p>An error has occured and the comments for this story could not be accessed.</p>');
		}
	});
}

function reqCommentsDelay(id) {
	window.setTimeout('reqComments('+id+');', 30);
}

function showCommentForm(id) {
	window.open("/comment.asp?id=" + id, "_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=400, height=400");
}

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};