/*--------------------------------------------------------------------------
 *
 *  Flash.js, version 0.1
 *  © 2008 upstruct berlin oslo
 *
 *  For details, see the upstruct garage website: http://garage.upstruct.com/
 *
 *--------------------------------------------------------------------------*/
 
var Flash = Class.create({
	
	defaults : {
		container : 'flash-container',
		version : 9,
		src : null,
		width: 1024,
		height : 738,
		parameters : {
			wmode : 'transparent'
		},
		attributes : { }
	},
	
	/**
	 * undocumented function
	 * @param  id, flashversion
	 **/
	initialize: function(parameters) {
		
		this.options = Object.extend(this.defaults, parameters || {});
		
		this.container = this.options.container;
		this.flashVersion = parseFloat(this.options.version);
		
		this.html = null;
		this.fallbackHTML = '';
		
		this.container = $(this.options.container);
		if (!this.container) {
			document.write('<div id="' + this.options.container + '"></div>');
			this.container = $(this.options.container);
		}
			
		if (!this.options.src)
			throw new Error('Specify the path to the flash file to display');
			
		this.render(this.options);
	},
	
	/**
	 * Add preview image to for the video
	 * @param  params:object -> {src [params:{width, height, etc}]}
	 **/
	fallback: function(fallback) {
		
		options = Object.extend({ redirect : null, html : null }, fallback || { });
		if (options.redirect)
			this.fallbackHTML = new Template(
				'<script type="text/javascript">' +
					'location.href = \'#{fallback}\'' +
				'</script>'
			).evaluate({ fallback : options.redirect });
		else if (options.html)
			this.fallbackHTML = options.html;
		
		return this;
	},
	
	/**
	 * Add video to the the
	 * @param  params:object -> {src [params:{width, height, etc}]}
	 **/
	render: function(options){
		
		var parameters = '', objectAttibutes = '';
		
		$H(options.parameters).each(function (attribute) {
			parameters += '<param name="' + attribute.key + '" value="' + attribute.value +'" />';
		});
		
		$H(options.attributes).each(function (attribute) {
			objectAttibutes += ' ' + attribute.key + '="' + attribute.value + '"';
		});

		
		this.html = new Template(
			'<object type="application/x-shockwave-flash" ' +
			        'data="#{options.src}"' +
			        'width="#{options.width}"' +
			        'height="#{options.height}" #{attributes}>' +
				'<param name="src" value="#{options.src}"' + 
				'<param name="width" value="#{options.width}"' + 
				'<param name="height" value="#{options.height}"' + 
				'#{parameters} #{fallback}' +
			'</object>'
		).evaluate({
			options : this.options, 
			parameters : parameters, 
			fallback : this.fallbackHTML}
		);
		
		return this;
	},
	
	toString: function() {
		return this.html;
	},
	
	/**
	 * Updates the container-div with the requested html 
	 **/
	write: function() {
		
		if(this.flashVersion <= parseFloat(Browserdetect.flash.version))
			this.container.update(this);
		else
			this.container.update(this.fallbackHTML);

		return this;
	}
	
});
