/*
Note: SWFObject 1.5 and 2.x behave differently at the markup level.  
For example, 1.5 fills the targeted element, while 2.x replaces it.

The way to compensate for this behavior is by separation of style and 
target for flash embedding:
<div class="flash-style"><div id="flash-target">Alternate content</div></div>

In this way, you can use either version of SWFObject on your page.

*/
(function () {
	if (!window.Df) window.Df={};
	var loadQueue=[], loaded=false;
	
	/**
	 *  version 0.1a-libless
	 * @param	opts	{
	 *					swfFile	[required]
	 *					targetId	[required]
	 *					width	[default: target width]
	 *					height	[default: target height]
	 *					bgcolor	[default: none]
	 * 			}
	 * @param	params	NVP object of flash parameters to overload GSI defaults
	 * @param	vars		NVP object of flashVars to overload those passed in for akamai
	 **/	
	var _swfEmbed = Df.swfEmbed = function swfEmbed(opts, params, vars) {
		
		//QAD version of Prototype#$
		function $(i){return document.getElementById(i);}
		//QAD version of Object.extend / $.extend
		function x(d,s){if(!d)d={};if(!s)s={};for(var i in s)d[i]=s[i];return d;}
		
		if (!opts) throw new Error("Df.swfEmbed called without arguments");
		if (!opts.swf || !opts.targetId)
			throw new Error("Df.swfEmbed REQUIRES that the first argument have the members 'swf' and 'targetId' set");
		if (!loaded && !$(opts.targetId)) {
			loadQueue.push({opts:opts,params:params,vars:vars});
			return;
		}
		
		//If width/height are unspecified, use the target's dimensions
		opts = x({
			width: $(opts.targetId).offsetWidth,
			height: $(opts.targetId).offsetHeight,
			minVer: 9,
			bgcolor: ''
		}, opts);
		
		//Use GSI's default parameters, and overload with those specified in params
		params = x({
			quality:"high",
			swliveconnect:"true",
			wmode:"transparent",
			allowScriptAccess:"always",
			base:".",
			type:"application/x-shockwave-flash",
			pluginspace:"http://www.adobe.com/go/getflashplayer",
			allowScriptAccess:"always",
			autostart:"true",
			bgcolor: opts.bgcolor
		}, params);
		
		//window.gsiVars should contain the correct values for
		//FlashStaticCont, FlashDynamicCont, and FlashBulkCont
		//copy this object and overload it with the content of vars
		vars = x(x({},Df.swfEmbedVars),vars);
		//Whichever SWFObject is available to us, use it
		if (!!window.swfobject) {
			//prefer swfobject v2.x
			swfobject.embedSWF(opts.swf, opts.targetId, opts.width, opts.height, opts.minVer, flashVars, params);
		} else if (!!window.SWFObject) {
			//but use v1.5 if it's all we have.
			var objSwf = new SWFObject(opts.swf, opts.targetId+"-flashobject", opts.width, opts.height, opts.minVer, opts.bgcolor);
			for (var i in params) objSwf.addParam(i, params[i]);
			for (var i in vars) objSwf.addVariable(i, vars[i]);
			objSwf.write(opts.targetId);
			//if there's no SWFObject, fail and instruct.
		} else throw new Exception("Df.swfEmbed depends on SWFObject 1.5+; please add it to templateJsLibraries");
	}
	var select = null;
	if (!!window.Prototype) select = $$;
	if (!!window.jQuery) select = jQuery;
	if (!!select)
		/**
		 *  Overload of Df.swfEmbed to enable support for a targetSelector rather than a targetId
		 * @param	opts	{
		 *					swfFile			[required]
		 *					targetId			[required if no targetSelector]
		 *					targetSelector	[required if no targetId]
		 *					width			[default: target width]
		 *					height			[default: target height]
		 *					bgcolor			[default: none]
		 * 			}
		 * @param	params	NVP object of flash parameters to overload GSI defaults
		 * @param	vars		NVP object of flashVars to overload those passed in for akamai
		 **/	
		Df.swfEmbed = function swfEmbed(opts, params, vars) {
			if (!!opts.targetSelector) {
				if (!!opts.targetId) 
					_swfEmbed(opts, params, vars);
				select(opts.targetSelector).each(function (element) {
					//If this is jQuery, /element/ will be the index, and /this/ will be the element.
					if (!!this.nodeName && this != window) element=this;
					if (!element.id)
						element.id = selector.replace(/[^\w\d\_]+/g,'_').replace(/_+/g,'_')+Math.floor(Math.random()*89999+10000);
					opts.targetId = element.id;	
					_swfEmbed(opts, params, vars);
				});
			} else _swfEmbed(opts, params, vars);
		};
	var processLoadQueue = function processLoadQueue() {
		loaded = true;
		for (var i=0; i<loadQueue.length; i++) 
			Df.swfEmbed(loadQueue[i].opts, loadQueue[i].params, loadQueue[i].vars);		
	};
	if (!!Prototype) Event.observe(document, 'dom:loaded', processLoadQueue);
	else if (!!jQuery) jQuery(processLoadQueue);
	else if (!!window.addEventListener) window.addEventListener('load',processLoadQueue,false);
	else if (!!window.attachEvent) window.attachEvent('onload', processLoadQueue);
	else window.onload = processLoadQueue;
})();
