// window.movie.js
// Displays a streaming Quick Time movie in a window, requires xilinus windows.js,
// prototype.js and scriptaculous effects.js
//
// Copyright (c) 2007 Modern Webspace, Inc. (http://modernwebspace.com, http://bgok.net)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice, associated urls and this permission notice
// shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

// Quicktime detection code
Object.extend(Prototype, { haveQT: false })
if (Prototype.Browser.IE && window.ActiveXObject) {
	var aX = new ActiveXObject("QuickTimeCheckObject.QuickTimeCheck.1");
	if (aX && aX.IsQuickTimeAvailable(0)) Prototype.haveQT = true;
}
else if (navigator.plugins) {
	for (i=0; i < navigator.plugins.length; i++ )
		if (navigator.plugins[i].name.indexOf("QuickTime") >= 0) Prototype.haveQT = true;
}
// End of Quicktime detection code

Dialog.movie = function (url, opts) {
	var options = Object.extend({
		stream:			'',	// If streaming from a QT server, stream url goes here and 'url' points to the poster movie
		className:		'darkX',
		top:			10,
		left:			10,
		width:			320,
		height:			180,
		zIndex:			Math.max(100, Windows.maxZIndex + 1),
		resizable:		false, // Don't know why you'd want to resize a movie window, but here's the option
		title:			'Movie',
		draggable:		true,
		wiredDrag:		false,
		controller:		true,
		autoplay:		true
	}, opts || {});
	
	if (options.controller)
		options.height += 16;
	
	var win = new Window({
		className:		options.className,
		top:			options.top,
		left:			options.left,
		width:			options.width,
		height:			options.height,
		resizable:		options.resizable,
		title:			options.title,
		draggable:		options.draggable,
		zIndex:			options.zIndex,
		minimizable:	false,
		maximizable:	false,
		wiredDrag:		options.wiredDrag,
		destroyOnClose:	true
	});
	var name = win.getId() + "_movie";
	if (Prototype.haveQT) {
		var content = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"' +
			'width="' + options.width + '" ' +
			'height="' + options.height + '" ' +
			'codebase="http://www.apple.com/qtactivex/qtplugin.cab" ' +
			'id="' + name + '">' +
			'<PARAM name="SRC" VALUE="' + url + '">' +
			'<PARAM name="AUTOPLAY" VALUE="' + options.autoplay + '">' +
			'<PARAM name="CONTROLLER" VALUE="' + options.controller + '">' +
			(options.stream == '' ? '' : ('<PARAM name="QTSRC" VALUE="' + options.stream + '">')) +
		    '<embed src="' + url + '" ' +
				'width="' + options.width + '" ' +
				'height="' + options.height + '" ' +
				'target="myself" ' +
				'controller="' + options.controller + '" ' +
				'autoplay="' + options.autoplay + '" ' +
				(options.stream == '' ? '' : ('qtsrc="' + options.stream + '" ')) +
				'type="video/quicktime" ' +
				'pluginspage="www.apple.com/quicktime/download" ' +
				'EnableJavaScript="true" ' +
				'name="' + name + '" ' +
			'></embed></OBJECT>';
		win.setHTMLContent(content);
		
		// Because the scourge know as IE doesn't do things like other browsers
		win.setCloseCallback(function() { if (Prototype.Browser.IE) $(name).Stop(); return true;});
		// Argh! IE keeps downloading after I stop the playback and destroy the window. How do I make it stop?
		
	} else {
		win.setHTMLContent("<br /><br /><h3 style='text-align:center'>Apple QuickTime is required to play this video. Please <a href='http://www.apple.com/quicktime/download/' target='_blank'>install QuickTime</a> then try again.</h3>");
	}
	win.show();
}
