/**
 * Opens a new browser window to display the specified quicktime file.
 * @param filename A string representing the path of the movie to watch.
 */
function watchMovie(filename, size) {
	var d, w, h, x, y, t, p;
	d = getMovieDimensions(size);
	t = getMovieTitle(size);
	w = d.width;
	h = d.height;
	
	// Add room for the controller to appear
	h += 16;
	
	// Set the padding for the browser window
	p = 80;
	
	// Attempt to center the window to the screen
	if (screen) {
		x = (screen.availWidth  - (w + p)) / 2;
		y = (screen.availHeight - (h + p)) / 2;
	} else {
		x = 100;
		y = 100;
	}
		
	// Setup the attributes for the window
	var props = "";
	props += "width=" 	+ (w + p) + ",";
	props += "height=" 	+ (h + p) + ",";
	props += "left=" 	+ x + ",";
	props += "top=" 	+ y + ",";	
	props += "toolbar=no,";
	props += "scrollbars=no,";
	props += "resizable=yes,";
	props += "status=yes";
	
	// Open a viewer window and write to the body to generate the quicktime movie
	var popup = window.open("", "movie", props);
	if (popup != null) {
		popup.document.write(getMovieSource(filename, t, w, h));
		popup.document.title = t;
		popup.document.close();
		popup.onload = function() {
			popup.document.title = t;
			popup.focus();
		}			
	}		
}

/**
 * Gets an object containing the dimensions for the movie based on the specified size.
 * @param size A string representing the size of the movie to get the dimensions for.
 */
function getMovieDimensions(size) {
	var d;
	switch(size.toLowerCase()) {
		case "medium":
			d = {width:480, height:272};
			break;
		case "large":
		case "h264":
			d = {width:688, height:416};
			break;
		default:
			d = {width:800, height: 484};
			break;
	}
	return d;
}

/**
 * Gets the title for the movie based on the specified size.
 * @param size A string representing the size of the movie to get the dimensions for.
 */
function getMovieTitle(size) {
	return "MOONGIRL: ACT ONE";
}

/**
 * Gets the HTML necessary to display the movie in a new browser window.
 * @param size A string representing the size of the movie to get the dimensions for.
 */
function getMovieSource(filename, title, width, height) {
	var source = "";
	
	source += "<html>\n";
	source += "<head><title>{1}</title></head>\n";
	source += "<body bgcolor=\"#000000\" leftmargin=\"40\" topmargin=\"40\" marginwidth=\"40\" marginheight=\"40\">\n";
	source += "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" width=\"{2}\" height=\"{3}\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0\">\n";
	source += "    <param name=\"controller\" value=\"TRUE\">\n";	
	source += "    <param name=\"type\" value=\"video/quicktime\">\n";
	source += "    <param name=\"autoplay\" value=\"true\">\n";
	source += "    <param name=\"target\" value=\"myself\">\n";
	source += "    <param name=\"src\" value=\"{0}\">\n";
	source += "    <param name=\"pluginspage\" value=\"http://www.apple.com/quicktime\">\n";
	source += "    <embed width=\"{2}\" height=\"{3}\" controller=\"TRUE\" target=\"myself\" src=\"{0}\" type=\"video/quicktime\" bgcolor=\"#000000\" border=\"0\" pluginspage=\"http://www.apple.com/quicktime\"></embed>\n";
	source += "</object>\n";
	source += "</body>\n";
	source += "</html>\n";
	
	source = source.replace(/\{0\}/g, filename);
	source = source.replace(/\{1\}/g, title);
	source = source.replace(/\{2\}/g, width);
	source = source.replace(/\{3\}/g, height);
	
	return source;
}

/**
 * Resizes the window for the site if the current window is not large enough to 
 * display the entire movie.
 */
function resizeToContent() {

	// Get the current width of the body element for the site.
	var w, h;
	if (self.innerHeight) // all except Explorer
	{
		w = self.innerWidth;
		h = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	{
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	
	// Determine if either the width or height of the window is two small and requires a resize.
	var resizeRequired = false;
	if (w < 800) {
		resizeRequired = true;
		w = 800 - w;
	} else {
		w = 0;
	}	
	if (h < 680) {
		resizeRequired = true;
		h = 680 - h;
	} else {
		h = 0;
	}
	
	// If a resize is required, resize the window by the current width and height offsets. 
	if (resizeRequired) {
		// Safari fails to execute resizeBy(), so if it is the userAgent, call a resizeTo() using
		// the current screen dimensions.
		if (navigator.userAgent.toLowerCase().indexOf("safari") > -1) {
			window.moveTo(0, 0);
			window.resizeTo(screen.availWidth, screen.availHeight);
		} else {
			window.resizeBy(w, h);
		}
		
	}
}