/**
 * Chatventure cobrowse launcher for Firefox
 */
var jpxFireFoxLauncher = function () { }

jpxFireFoxLauncher.prototype.errorMessage         = "notplugin_FF";
jpxFireFoxLauncher.prototype.isCompatibleVar      = false;
jpxFireFoxLauncher.prototype.swfobject            = {};
jpxFireFoxLauncher.prototype.sniffer              = {};
jpxFireFoxLauncher.prototype.setTimeOutAutloadInt = 1500;
jpxFireFoxLauncher.prototype.onActiveXObjectInstalledToggle = false;

/**
 * Check whether the loader is able to launch the activity for firefox. Please be 
 * aware of the fact that the returned value is only reliable after the automatic
 * checks have been completed.
 * @return bool
 */
jpxFireFoxLauncher.prototype.canLaunch = function ()
{
	return this.onActiveXObjectInstalledToggle;
}

/**
 * Launches a WLM activity with a specific id.
 * @param appId int
 */
jpxFireFoxLauncher.prototype.launchActivity = function (appId)
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'launchActivity', 'called');
	
	this.cvLaunchApplicationFromId(appId);
}

/**
 * Returns the error message of the last error detected by the application.
 * @return string
 */
jpxFireFoxLauncher.prototype.getErrorMessage = function ()
{
	return this.errorMessage;
}

/**
 * Performes the check whether the current browser is compatible to load the WLM activity.
 * @return
 */
jpxFireFoxLauncher.prototype.isCompatible = function ()
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'isCompatible', 'called');
	
	this.isCompatibleVar = false;
	
	// Check if the version state is valid
	if (this.sniffer.version >= 3 )
	{	
		this.isCompatibleVar = true;
	}
	else
	{
		jpxDebug.debugMessage('jpxFireFoxLauncher', 'isCompatible', 'old...');
		this.errorMessage = 'warning_firefox_old';	
	}
	
	return this.isCompatibleVar;
}

/**
 * Performs checks before the other functions are reliable.
 * @return bool
 */
jpxFireFoxLauncher.prototype.preFilter = function ()
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'preFilter', 'called');
	
	this.cvNotifyFirefoxAddonInstalled();
	return true;
}

/**
 * Starts the installation of the Firefox Addon.
 */
jpxFireFoxLauncher.prototype.startAddonInstallation = function ()
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'startAddonInstallation', 'called');
	
	var params = {
		"JPX - Shop With Your Friends addon": {
			URL: STATIC_HOST + 'extension/firefox/LaunchApp.xpi',
			IconURL: STATIC_HOST + 'extension/firefox/icon-32x32.png',
			toString: function () { return this.URL; }
		}
	}
	InstallTrigger.install(params);
}


// ----------------------------------------------------------------
// Chatventure Data eXchange Element manipulation
// ----------------------------------------------------------------
//
// The Chatventure Data eXchange Element is used to pass data to
// and from the Chatventure Firefox addon. The element is placed in
// the document tree as the last sibling of the <body> element.
//
// NOTE that you need never manipulate or query the attributes
// of this element yourself; instead, use the functions provided
// below.
//
// ----------------------------------------------------------------

/**
 * Returns the Chatventure Data eXchange Element. If the element
 * does not yet exist in the document tree, it is created and
 * inserted into the tree.
 * @return DOM-el
 */
jpxFireFoxLauncher.prototype.cvGetDXElement = function ()
{
	// Check whether the Chatventure data exchange element already
	// exists in the document.
	var cvNodeList = document.documentElement.getElementsByTagName('cvFirefoxAddonData');
  
	// If the length of the returned list is zero, the element does
	// not yet exist in the document. In that case, create it and
	// add it to the document tree.
	if (cvNodeList.length == 0)
	{
		// Create the Chatventure element.
		var cvElement = document.createElement('cvFirefoxAddonData');
	    
		// Add the newly created and initialized element to the
		// document tree.
		document.documentElement.appendChild(cvElement);
	  
		// return the newly created element.
		return cvElement;
	}
 
	// The element already exists in the document tree, so return it.
	return cvNodeList.item(0);
}

/**
 * Returns the version of the Chatventure Firefox addon. This only
 * returns a valid value agter you've used the
 * cvNotifyFirefoxAddonInstalled function to request the addon to
 * fill in its version number. Before that, the return value is null.
 * @return string
 */
jpxFireFoxLauncher.prototype.cvGetFirefoxAddonVersion = function ()
{
	// Retrieve the Chatventure element.
	var cvElement = this.cvGetDXElement();

	// Return the element's addonVersion attribute.
	return cvElement.getAttribute('addonVersion');
}

		// Sets the application id of the application that is to be launched.
jpxFireFoxLauncher.prototype.cvSetApplicationId = function(applicationId)
{
	// Retrieve the Chatventure element.
	var cvElement = this.cvGetDXElement();
	
	// Set the element's applicationId attribute to the supplied value.
	cvElement.setAttribute('applicationId', applicationId);
}

/**
 * Returns the error code currently set in the Chatventure Data
 * exchange element.
 */
jpxFireFoxLauncher.prototype.cvGetFirefoxAddonError = function()
{
	var cvElement = this.cvGetDXElement();
	return cvElement.getAttribute('errorCode');
}


// ----------------------------------------------------------------
// Addon / ActiveX object notification requests
// ----------------------------------------------------------------
//
// Use the notification request functions below to determine:
//
// * Whether the Chatventure Firefox addon is installed
//
// * The version of the Chatventure Firefox addon that is
//   installed.
//
// * Whether the MSNMessenger.P4QuickLaunch ActiveX object is
//   installed
//
// NOTE that these function work asynchronously. That is, you need
// the provide the functions with callback functions that are to be
// called when the requested information is available.
//
// ----------------------------------------------------------------

/** 
 * Use this function to determine whether the Chatventure Firefox
 * installed, the supplied onNotifyInstalled function will be called.
 * If the addon is not installed, the supplied onNotifyNotInstalled
 * function will be called.
 */
jpxFireFoxLauncher.prototype.cvNotifyFirefoxAddonInstalled = function()
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'cvNotifyFirefoxAddonInstalled', 'called');
	
	// create new Image object
	var image = new Image;
	// set up event handlers for the Image object
	image._launcher = this;
	image.onload  = function () { this._launcher.onFirefoxAddonInstalled(); };
	image.onerror = function () { this._launcher.onFirefoxAddonNotInstalled(); };
	image.onabort = function () { this._launcher.onFirefoxAddonNotInstalled(); };
	// Set the source of the image to the url of the bitmap.
	image.src = "chrome://launchapp/content/check.bmp";
}
		
/**
 * Use this function to determine what version of the Chatventure
 * Firefox addon is installed on the client machine. The addon will
 * call the supplied callback function to notify this page that the
 * version information is available in the Chatventure data
 * exchange element.
 */
jpxFireFoxLauncher.prototype.cvNotifyFirefoxAddonVersion = function()
{   
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'cvNotifyFirefoxAddonVersion', 'called');
	
	// Make sure the supplied callback function is called when the
	// version information is available.
	document.addEventListener('cbFirefoxAddonError'  , this.onFirefoxAddonError, false, true);
	document.addEventListener('cbFirefoxAddonVersion', this.onFirefoxAddonVersion, false, true);

	// Create and initialize a new event that will be used to tell
	// the addon that it must let us know (via the above callback
	// function) what version of the Firefox addon is installed.
	var cvNotifyFirefoxAddonVersionEvt = document.createEvent('Events');
	cvNotifyFirefoxAddonVersionEvt.initEvent('cvNotifyFirefoxAddonVersion', true, false);

	// Fire the event on the Chatventure data exchange element. When
	// the version number is available in the Chatventure data
	// exchange element, the above callback function will be called
	// by the addon.
	var cvElement = this.cvGetDXElement();
	cvElement.dispatchEvent(cvNotifyFirefoxAddonVersionEvt);
}

/**
 * Use this function to determine whether the
 * MSNMessenger.P4QuickLaunch ActiveX object is installed on the
 * client machine. If the ActiveX object is installed, the supplied
 * onNotifyInstalled function will be called. If the ActiveX object is
 * not installed, the supplied onNotifyNotInstalled function will be
 * called.
 */
jpxFireFoxLauncher.prototype.cvNotifyActiveXObjectInstalled = function()
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'cvNotifyActiveXObjectInstalled', 'called');
	
	// Make sure the supplied functions are called in case their
	// corresponding event is fired.
	document.addEventListener('cbFirefoxAddonError'          , this.onFirefoxAddonError, false, true);
	document.addEventListener('cbActiveXObjectIsInstalled'   , this.onActiveXObjectInstalled, false, true);
	document.addEventListener('cbActiveXObjectIsNotInstalled', this.onActiveXObjectNotInstalled, false, true);
   
	// Create and initialize a new event that will be used to tell
	// the addon that it must let us know (via the above callback
	// functions) whether the ActiveX object is properly installed.
	var cvNotifyActiveXObjectInstalledEvt = document.createEvent('Events');
	cvNotifyActiveXObjectInstalledEvt.initEvent('cvNotifyActiveXObjectInstalled', true, false);

	// Fire the event on the Chatventure data exchange element. If
	// the ActiveX object is properly installed, the onIsInstalled
	// function will be called by the plugin. Otherwise, the
	// onIsNotInstalled function will be called by the plugin.
	var cvElement = this.cvGetDXElement();
	cvElement.dispatchEvent(cvNotifyActiveXObjectInstalledEvt);
	return;
	
}

// ----------------------------------------------------------------
// Application launching
// ----------------------------------------------------------------
//
// Use the functions below to launch an application.
//
// ----------------------------------------------------------------

/**
 * Launches the application using the application id that has been
 * set using cvSetApplicationId.
 */
jpxFireFoxLauncher.prototype.cvLaunchApplication = function()
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'cvLaunchApplication', 'called');
	
	document.addEventListener('cbFirefoxAddonError', this.onFirefoxAddonError, false, true);

	// Create and initialize a new event that will be used to tell
	// the addon that it must launch the application, using the id
	// that is currently stored in the 'applicationId' attribute of
	// the Chatventure data exchange element.
	var cvLaunchApplication = document.createEvent('Events');
	cvLaunchApplication.initEvent('cvLaunchApplication', true, false);

	// Retrieve the Chatventure data exchange element and fire the
	// newly created event on it.
	var cvElement = this.cvGetDXElement();
	cvElement.dispatchEvent(cvLaunchApplication);
}

/**
 * Launches the application corresponding to the supplied
 * application id.
 */
jpxFireFoxLauncher.prototype.cvLaunchApplicationFromId = function(applicationId)
{
	jpxDebug.debugMessage('jpxFireFoxLauncher', 'cvLaunchApplicationFromId', 'called');
	
	// Set the application id to use and launch the application.
	this.cvSetApplicationId(applicationId);
	this.cvLaunchApplication();
}


// -------------------------------------------------------------------------
// Chatventure Firefox addon error handling
// -------------------------------------------------------------------------

/**
 * If a error from within the FF Addon is triggered then add a debug message.
 */
jpxFireFoxLauncher.prototype.onFirefoxAddonError = function(e)
{
	if(typeof jpxDebug != "undefined") { jpxDebug.debugMessage('jpxFireFoxLauncher', 'onFirefoxAddonError', 'Addon error: ' + cvGetFirefoxAddonError()); }
}

/**
 * If FF plugin installed and no debug errormessage then triggers the check for testAddonVersion.
 */
jpxFireFoxLauncher.prototype.onFirefoxAddonInstalled = function()
{
	if(typeof jpxDebug != "undefined") { jpxDebug.debugMessage('jpxFireFoxLauncher', 'onFirefoxAddonInstalled', 'called'); }
	
	this.cvNotifyFirefoxAddonVersion();
}

/**
 * If no FF plugin installed, sets the global FF onActiveXObjectInstalledToggle = false
 * sets the errormessage on no-pluginFF so the user can install it.
 * @return false;
 */
jpxFireFoxLauncher.prototype.onFirefoxAddonNotInstalled = function()
{
	if(typeof jpxDebug != "undefined") { jpxDebug.debugMessage('jpxFireFoxLauncher', 'onFirefoxAddonNotInstalled', 'called'); }
	
	this.onActiveXObjectInstalledToggle = false;
	this.errorMessage = 'notplugin_FF';
	return false;
}


/**
 * Checks the plugin FF messenger version. default 1.0 > and higher
 * otherwise sets a error warning that version is oudated
 */
jpxFireFoxLauncher.prototype.onFirefoxAddonVersion = function(e)
{
	if(typeof jpxDebug != "undefined") { jpxDebug.debugMessage('jpxFireFoxLauncher', 'onFirefoxAddonVersion', 'called'); }
	
	/**
	 * For some stupid (IMO) kind of reason, the this keyword doesn't refer
	 * to the active object, but document. Probably this is caused by a non-
	 * normal execution flow. This function is an EventListener for an event,
	 * which is dispatched by an addon.
	 * So we need a ref object!
	 */
	var real_this = window.jpxBrowser;
	
	var version = real_this.cvGetFirefoxAddonVersion().split('.');

	// minimal version 1
	if (parseInt(version[0]) >= 1)
	{
		real_this.cvNotifyActiveXObjectInstalled();
	}
	else
	{
		real_this.errorMessage = 'invalid_FF_addon_version';
	}
}

/**
 * Sets the global FF onActiveXObjectInstalledToggle on true if  activeX
 * object is installed.
 * @return true
 */
jpxFireFoxLauncher.prototype.onActiveXObjectInstalled = function()
{
	if(typeof jpxDebug != "undefined") { jpxDebug.debugMessage('jpxFireFoxLauncher', 'onActiveXObjectInstalled', 'called'); }
	
	/**
	 * See documentation in this.onFirefoxAddonVersion.
	 */
	var real_this = window.jpxBrowser;
	real_this.onActiveXObjectInstalledToggle = true;
}

/**
 * Sets the global FF onActiveXObjectInstalledToggle on false if no activeX
 * object is installed en shows message that no MSN compatible system is
 * @return false
 */
jpxFireFoxLauncher.prototype.onActiveXObjectNotInstalled = function()
{
	if(typeof jpxDebug != "undefined") { jpxDebug.debugMessage('jpxFireFoxLauncher', 'onActiveXObjectNotInstalled', 'called'); }
	
	/**
	 * See documentation in this.onFirefoxAddonVersion.
	 */
	var real_this = window.jpxBrowser;
	real_this.onActiveXObjectInstalledToggle = false;
	real_this.errorMessage = 'notcompatible_MSN';
}