/*------------------------------------------------------------------------------

Class: OneShotEventRegister
Abstract FIFO event register that expires after a single event. Allows subsequent calls to be optionally executed or passed to an overflow method.

Parameters:
element - element reference
eventname - string
overflow(optional) - boolean
overflower(optional) - function

Returned Value:
Object

------------------------------------------------------------------------------*/

var OneShotEventRegister = Class.create({
	initialize: function(element, eventname, overflow, overflower) {
		this.expired = false;
		this.queue = [];
		this.overflow = !!overflow;
		this.overflower = overflower;
		Event.observe(element, eventname, this.expire.bindAsEventListener(this));
	},
/*------------------------------------------------------------------------------

	Method: expire
	Execute and remove all queued functions
	
	Returned Value:
	None
	
------------------------------------------------------------------------------*/
	expire: function() {
		var i;
		while(i=this.queue.shift()) {
			i();
		}
		this.expired = true;
	},
/*------------------------------------------------------------------------------

	Method: register
	Add function to queue or if overflow enabled pass thru function
	
	Returned Value:
	Boolean
	
------------------------------------------------------------------------------*/
	register: function(func) {
		if(!this.expired) {
			this.queue.push(func);
		} else if(this.overflower) {
			this.overflower(func)
		} else if(this.overflow) {
			func();
		} else {
			return false;
		}
		return true;
	}
});

/*------------------------------------------------------------------------------

Function: registerOnLoad
Execute a specified callback when the root document is loaded.

Parameters:
func - function object reference

Returned Value:
None

------------------------------------------------------------------------------*/
var registerOnLoadFunc = (
registerOnLoad = (function() {
	var _exec = new OneShotEventRegister(window, "load", true);
	return function(func) { 
		_exec.register(func); 
	}
})());


/*------------------------------------------------------------------------------

Function: registerOnReady
Execute a specified callback when the root document is ready.

Parameters:
func - function object reference

Returned Value:
None

------------------------------------------------------------------------------*/

var registerOnReady = (function() {
	var _exec = new OneShotEventRegister(document, "dom:loaded", true, registerOnLoad);
	return function(func) {
		_exec.register(func);
	}
})();

function mySwfOmListener(event) {
	var id = event.memo.id;
	if(!id || id != "swfwrapper") { return; }
	$($(id).firstDescendant()).observe("mouseup", run_omniture_for_cps);
	
}

function removeListeners() {
	$(document).stopObserving("swfobject:rendered", mySwfOmListener);
	$($("swfwrapper").firstDescendant()).stopObserving("mouseup", run_omniture_for_cps);
}

function run_omniture_for_cps() {
	var s=s_gi(s_accountName);
	s.linkTrackVars='eVar38,events';
	s.linkTrackEvents='event42';
	s.eVar38='Flash Platform Distribution Manager AIR App';
	s.events='event42';
	s.tl(this,'d','Flash Platform Distribution Manager AIR App');
	
	removeListeners();
}

var isExpressInstallActive = false;
var storedAltContent = false;

// -----------------------------------------------------------------------------
// AppId
var appId = "com.adobe.distributionmanager"; // e.g. "com.adobe.distributionmanager"
// Adobe Wave Publisher Id (Adobe)
var pubId = "4875E02D9FB21EE389F73B8D1702B320485DF8CE.1"; // e.g. "1AD9E5A3D860D5179D88EDDA854FDAA31E441990.1" or "4875E02D9FB21EE389F73B8D1702B320485DF8CE.1"
// AIR Version Required
var airVersion = "1.5";
// AIR Application Version (this is a version of the new AIR file we are about to install
var airApplicationVersion = "1.5.0.147";// e.g. 1.0.0.411
// AIR Application Name
var airApplicationName = "Adobe Flash Platform Services Distribution Manager";
// AIR Application URL
var airApplicationURL = "http://download.macromedia.com/air/distribution/service/DistributionManager.air"; // e.g. "http://localhost:8080/DistributionManager.air"
// AIR Application Image
var airApplicationImage = "/flashplatform/services/distribution/installer.jpg"; // // URL for an image (JPG, PNG, GIF) or SWF to display in the badge (205px wide, 170px high)

var displaySWF = function() {
	var props = new Object();
	props.swf = "/flashplatform/services/distribution/AIRInstallBadge.swf";
	props.id = "Badge";
	props.ver = "9.0.115";
	props.w = "215";
	props.h = "180";
	props.c = "ffffff";
	props.wmode= "opaque"; 
											
	var swfo = new SWFObject( props );
	swfo.addParam ( "allowscriptaccess", "all" ); 
	swfo.addVariable("airversion", airVersion); // version of AIR runtime required
	swfo.addVariable("appname", airApplicationName); // application name to display to the user
	swfo.addVariable("appurl", airApplicationURL); // absolute URL (beginning with http or https) of the application ".air" file
	
	// these parameters are required to support launching apps from the badge (but optional for install):
	swfo.addVariable("appid", appId); // the qualified application ID (ex. com.gskinner.air.MyApplication)
	swfo.addVariable("pubid", pubId); // publisher id   
	
	// this parameter is required in addition to the above to support upgrading from the badge:
	swfo.addVariable("appversion", airApplicationVersion); // AIR application version 
	
	// these parameters are optional:
	swfo.addVariable("image", airApplicationImage); // URL for an image (JPG, PNG, GIF) or SWF to display in the badge (205px wide, 170px high)
	swfo.addVariable("appinstallarg", "installed from web"); // passed to the application when it is installed from the badge
	swfo.addVariable("applauncharg", "launched from web"); // passed to the application when it is launched from the badge
	swfo.addVariable("helpurl", "help.html"); // optional url to a page containing additional help, displayed in the badge's help screen
	swfo.addVariable("hidehelp", "true"); // hides the help icon if "true"
	swfo.addVariable("skiptransition", "false"); // skips the initial transition if "true"
	swfo.addVariable("titlecolor", "#00AAFF"); // changes the color of titles
	swfo.addVariable("buttonlabelcolor", "#FFFFFF"); // changes the color of the button label
	swfo.addVariable("appnamecolor", "#00AAFF"); // changes the color of the application name if the image is not specified or loaded
	
	// these parameters allow you to override the default text in the badge:
	// supported strings: str_error, str_err_params, str_err_airunavailable, str_err_airswf, str_loading, str_install, str_launch, str_upgrade, str_close, str_launching, str_launchingtext, str_installing, str_installingtext, str_tryagain, str_beta3, str_beta3text, str_help, str_helptext
	swfo.addVariable("str_err_airswf", "<u>Running locally?</u><br/><br/>The AIR proxy swf won't load properly when this is run from the local file system."); // overrides the error text when the AIR proxy swf fails to load
	
	var doXI = true;
	var reqVersion = ("9.0.115").split(".");
	var detectedVersion = SWFObjectUtil.getPlayerVersion();
	storedAltContent = $("swfwrapper").innerHTML;
	
	if (detectedVersion.major > reqVersion[0]) {
		doXI = false;
	} else if (detectedVersion.major == reqVersion[0] && detectedVersion.minor >= reqVersion[1] && detectedVersion.rev >= reqVersion[2]) {
		doXI = false;
	}
											
	if (doXI) {
		var xiObj = new Object();
		isExpressInstallActive = true;
		xiObj.swf = "http://wwwimages.adobe.com/www.adobe.com/swf/lib/expressInstall.swf";
		xiObj.id = "expressInstaller";
		xiObj.w = "215";
		xiObj.h = "180";
		xiObj.ver = "6.0.65";
																		
		var xi = new SWFObject( xiObj );
		xi.addVariable("MMredirectURL", escape( window.location ));
		document.title = document.title.slice(0, 47) + " - Flash Player Installation";
		xi.addVariable("MMdoctitle", document.title);
												
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { 
			xi.addVariable("MMplayerType", "PlugIn"); 
		} else {
			xi.addVariable("MMplayerType", "ActiveX")
		}
												
		xi.write("swfwrapper" );
	} else {
	   swfo.write("swfwrapper" );
	}
									
	var swfobject = {};
	swfobject.expressInstallCallback = expressInstallCallback;
}

function expressInstallCallback() {
	if (isExpressInstallActive) {
		var obj = $( "swfwrapper" );
		obj.innerHTML = storedAltContent;
		storedAltContent = null;
		isExpressInstallActive = false;
	}
}

registerOnReady(function() { $(document).observe("swfobject:rendered", mySwfOmListener); });
registerOnReady(displaySWF);
registerOnReady(function() { $("swfwrapper").show(); });


