var Swiff = function(source, props){
	if (!Swiff.fixed) Swiff.fix();
	var instance = 'Swiff' + Swiff.count++;
	Swiff.vars[instance] = {};
	props = Object.extend({
		width: 0, height: 0, id: instance, wmode: 'transparent', bgcolor: '#ffffff', vars: {'onLoad': Class.empty}
	}, props || {});
	var append = [];
	var count = -1;
	for (var p in props.vars){
		count++;
		Swiff.vars[instance][p] = props.vars[p];
		append[count] = p+'=Swiff.vars.'+instance+'.'+p;
	}
	var swf = source + '?' + append.join('&');
	var html =
		'<object width="'+props.width+'" height="'+props.height+'" id="'+props.id+'" type="application/x-shockwave-flash" data="'+swf+'">'+
			'<param name="allowScriptAccess" value="sameDomain" />'+'<param name="movie" value="'+swf+'" />'+
			'<param name="bgcolor" value="'+props.bgcolor+'" />'+'<param name="scale" value="noscale" />'+
			'<param name="salign" value="lt" />'+'<param name="wmode" value="'+props.wmode+'">'+
		'</object>';
	return new Element('div').setHTML(html).getFirst();
};

Swiff.extend = Object.extend;

Swiff.extend({
	
	count: 0,
	
	callBacks: {},
	
	vars: {},

	//from swfObject, fixes bugs in ie+fp9
	fix: function(){
		Swiff.fixed = true;
		window.addEvent('beforeunload', function(){
			__flash_unloadHandler = __flash_savedUnloadHandler = Class.empty;
		});
		if (!window.ie) return;
		window.addEvent('unload', function(){
			$each(document.getElementsByTagName("object"), function(swf){
				swf.style.display = 'none';
				for (var p in swf){
					if (typeof swf[p] == 'function') swf[p] = Class.empty;
				}
			});
		});
	},
	
	/*
	Function: Swiff.getVersion
		gets the major version of the flash player installed.

	Returns:
		a number representing the flash version installed, or 0 if no player is installed.
	*/
	
	getVersion: function(){
		var version, x;
		if(navigator.plugins && navigator.mimeTypes.length){
			x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) version = x.description;
		} else if (window.ActiveXObject){
			try {
				x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				version = x.GetVariable("$version");
			} catch(e){}
		}
		return version ? parseInt(version.match(/\d+/)[0]) : 0;
	},
	
	/*
	Function: Swiff.remote
		Calls an ActionScript function from javascript. Requires ExternalInterface.

	Returns: 
		Whatever the ActionScript Returns
	*/
	
	remote: function(object, name){
		var value = object.CallFunction("<invoke name=\"" + name + "\" returntype=\"javascript\">" + __flash__argumentsToXML(arguments, 2) + "</invoke>");
		return eval(value);
	}
	
});
