/*
	Class: Quickie.js
	
	Creates and returns a QuickTime embed object using supplied parameters. I followed the documentation provided in this article: http://support.apple.com/kb/TA26444?viewlocale=en_US Due to behavior differences amongst browsers, Apple says the best way to embed a QuickTime movie is to use an <embed> element wrapped in an <object> element so that all browsers behave appropriately. Since we are using JavaScript, we can check which browser is in use and dish out the correct HTML code without the need for redundant object/embed elements.
	
	Syntax:
	
	>	var myQuickie = new Quickie(path[, options]);
	
	Arguments:
		
		path - The path to the QuickTime file. Can be relative or absolute.
		options - (object, optional) See Options below.
			
	Options:
		id - (string: defaults to 'Quickie_' + unique id) The id of the Quickie object.
		width - (number: defaults to 1) The width of the Quickie object.
		height - (number: defaults to 1) The height of the Quickie object.
		container - (element) The container into which the Quickie object will be injected.
		attributes - (object) QuickTime attributes for the element. See http://www.apple.com/quicktime/tutorials/embed.html for possible attributes.
		
	Returns:
		
		- (element) A new HTML object Element with browser appropriate QuickTime embed code.
	
	Example:
	
	(start example)
	var myQuickie = new Quickie('myMovie.mov', {
		id: 'myQuicktimeMovie',
		width: 640,
		height: 480,
		container: 'qtmovie',
		attributes: {
	  	controller: 'true',
	  	autoplay: 'false'
		}
	});
	(end example)
	
	Credits:
	
	Based off the Swiff class provided by MooTools.
	
	License:
	
	MIT-Style License
*/

var Quickie = new Class({

	Implements: [Options],

	options: {
		id: null,
		height: 1,
		width: 1,
		container: null,
		attributes: {}
	},
	
	toElement: function(){
		return this.object;
	},
	
	initialize: function(path, options){
		this.instance = 'Quickie_' + $time();
		this.setOptions(options);
		options = this.options;
		this.id = options.id || this.instance;
		var id = this.id;
		var container = $(options.container);
		var attributes = options.attributes;
		// Check if controller is enabled and add 16px to height if needed
		var height = (!$chk(attributes.controller) || attributes.controller == 'true') ? options.height + 16 : options.height; 
		var width = options.width;
		var build = '';
		
		attributes.src = path;
		
		// Check if browser is IE and build the appropriate object for the browser.
		if (Browser.Engine.trident) {
			build = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"';
			build += ' id="' + id + '"';
			build += ' width="' + width + '"';
			build += ' height="' + height + '"';
			build += '>';
			for (var attribute in attributes) {
				if (attributes[attribute]) {
					build += '<param name="' + attribute + '" value="' + attributes[attribute] + '" />';
				}
			}
			build += '</object>';
		} else {
			build = '<embed ';
			build += ' id="' + id + '"';
			build += ' width="' + width + '"';
			build += ' height="' + height + '"';
			for (attribute in attributes) {
				if (attributes[attribute]) {
					build += ' ' + attribute + '="' + attributes[attribute] + '"';
				}
			}
			build += ' pluginspage="http://www.apple.com/quicktime/download/"></embed>';
		}
		
		this.object = ((container) ? container.empty() : new Element('div')).set('html', build).firstChild;
	},

	replaces: function(element){
		element = $(element, true);
		element.parentNode.replaceChild(this.toElement(), element);
		return this;
	},

	inject: function(element){
		$(element, true).appendChild(this.toElement());
		return this;
	}

});//end