

/**
 *
 *  Fichier nécessaire au bon fonctionnement du framework fusioncharts. Ce fichier ne doit normalement pas être édité
 *
 * FusionCharts: Flash Player detection and Chart embedding.
 * Version: 1.2.4 (16th February, 2009) - Added fix for chart with % width/height.
 * Version: 1.2.3 (15th September, 2008) - Added fix for % and & characters. Additional fixes to properly handle double quotes and single quotes in setDataXML() function.
 * Version: 1.2.2 (10th July, 2008) - Added fix for % scaled dimensions, fixes in setDataXML() and setDataURL() functions
 * Version: 1.2.1 (21st December, 2007) - Added setting up transparent/opaque mode: setTransparent() function
 * Version: 1.2 (1st November, 2007) - Added FORM fixes for IE
 * Version: 1.1 (29th June, 2007) - Added Player detection, New conditional fixes for IE
 *
 * Morphed from SWFObject (http://blog.deconcept.com/swfobject/) under MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
(function() {
	//'use strict';
	var window = this,
			document = window.document;

	if (!window.infosoftglobal) {
		window.infosoftglobal = {};
	}
	if (!window.infosoftglobal.FusionChartsUtil) {
		window.infosoftglobal.FusionChartsUtil = {};
	}

	var nullFunction = function() {};

	window.infosoftglobal.FusionCharts = function(swf, id, w, h, debugMode, registerWithJS, c, scaleMode, lang, detectFlashVersion, autoInstallRedirect) {
		if (!document.getElementById) { return; }

		//Flag to see whether data has been set initially
		this.initialDataSet = false;

		//Create container objects
		this.params = {};
		this.variables = {};
		this.attributes = [];

		//Set attributes for the SWF
		if (swf) { this.setAttribute('swf', swf); }
		if (id) { this.setAttribute('id', id); }

		this.addVariable('debugMode', debugMode || 0);

		w = w.toString().replace(/\%$/, '%25');
		if (w) { this.setAttribute('width', w); }
		h = h.toString().replace(/\%$/, '%25');
		if (h) { this.setAttribute('height', h); }


		//Set background color
		if (c) { this.addParam('bgcolor', c); }

		//Set Quality
		this.addParam('quality', 'high');

		//Add scripting access parameter
		this.addParam('allowScriptAccess', 'always');

		//Pass width and height to be appended as chartWidth and chartHeight
		this.addVariable('chartWidth', w);
		this.addVariable('chartHeight', h);

		//Whether in debug mode
		//Pass DOM ID to Chart
		this.addVariable('DOMId', id);
		//Whether to registed with JavaScript
		this.addVariable('registerWithJS', registerWithJS || 0);

		//Scale Mode of chart
		this.addVariable('scaleMode', scaleMode || 'noScale');

		//Application Message Language
		this.addVariable('lang', lang || 'EN');

		//Whether to auto detect and re-direct to Flash Player installation
		this.detectFlashVersion = detectFlashVersion || 1;
		this.autoInstallRedirect = autoInstallRedirect || 1;

		//Ger Flash Player version
		this.installedVer = window.infosoftglobal.FusionChartsUtil.getPlayerVersion();

		if (!window.opera && document.all && this.installedVer.major > 7) {
			// Only add the onunload cleanup if the Flash Player version supports External Interface and we are in IE
			window.infosoftglobal.FusionCharts.doPrepUnload = true;
		}
	};

	window.infosoftglobal.FusionCharts.prototype = {
		setAttribute: function(name, value) {
			this.attributes[name] = value;
		},
		getAttribute: function(name) {
			return this.attributes[name];
		},
		addParam: function(name, value) {
			this.params[name] = value;
		},
		getParams: function() {
			return this.params;
		},
		addVariable: function(name, value) {
			this.variables[name] = value;
		},
		getVariable: function(name) {
			return this.variables[name];
		},
		getVariables: function() {
			return this.variables;
		},
		getVariablePairs: function() {
			var variablePairs = [], variables = this.getVariables(), key;
			for (key in variables) {
				if (variables.hasOwnProperty(key)) {
					variablePairs.push(key + '=' + variables[key]);
				}
			}
			return variablePairs;
		},
		getSWFHTML: function() {
			var swfNode = '', params, pairs, key;
			if (window.navigator.plugins && window.navigator.mimeTypes && window.navigator.mimeTypes.length) {
				// netscape plugin architecture
				swfNode = '<embed type="application/x-shockwave-flash" src="' + this.getAttribute('swf') + '" width="' + this.getAttribute('width') + '" height="' + this.getAttribute('height') + '"  ';
				swfNode += ' id="' + this.getAttribute('id') + '" name="' + this.getAttribute('id') + '" ';
				params = this.getParams();
				for (key in params) {
					if (params.hasOwnProperty(key)) {
						swfNode += [key] + '="' + params[key] + '" ';
					}
				}
				pairs = this.getVariablePairs().join('&');
				if (pairs.length > 0) { swfNode += 'flashvars="' + pairs + '"'; }
				swfNode += '/>';
			} else { // PC IE
				swfNode = '<object id="' + this.getAttribute('id') + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.getAttribute('width') + '" height="' + this.getAttribute('height') + '">';
				swfNode += '<param name="movie" value="' + this.getAttribute('swf') + '" />';
				params = this.getParams();
				for (key in params) {
					if (params.hasOwnProperty(key)) {
						swfNode += '<param name="' + key + '" value="' + params[key] + '" />';
					}
				}
				pairs = this.getVariablePairs().join('&');
				if (pairs.length > 0) {swfNode += '<param name="flashvars" value="' + pairs + '" />';}
				swfNode += '</object>';
			}
			return swfNode;
		},
		setDataURL: function(strDataURL) {
			//This method sets the data URL for the chart.
			//If being set initially
			if (!this.initialDataSet) {
				this.addVariable('dataURL', strDataURL);
				//Update flag
				this.initialDataSet = true;
			}else {
				//Else, we update the chart data using External Interface
				//Get reference to chart object
				var chartObj = window.infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
				if (!chartObj.setDataURL) {
					window.__flash__addCallback(chartObj, 'setDataURL');
				}
				chartObj.setDataURL(strDataURL);
			}
		},
		//This function :
		//fixes the double quoted attributes to single quotes
		//Encodes all quotes inside attribute values
		//Encodes % to %25 and & to %26;
		encodeDataXML: function(strDataXML) {
			var arrDQAtt = strDataXML.match(/[=][\t ]*".*?"/g),
					//regExpReservedCharacters = ['\\$', '\\+'],
					i, repStr, strTo, repStrr, strStart, strEnd;
			if (arrDQAtt) {
				for (i = 0; i < arrDQAtt.length; i++) {
					repStr = arrDQAtt[i].replace(/^=[\t ]*"|"$/g, '');
					repStr = repStr.replace(/\'/g, '%26apos;');
					strTo = strDataXML.indexOf(arrDQAtt[i]);
					repStrr = "='" + repStr + "'";
					strStart = strDataXML.substring(0, strTo);
					strEnd = strDataXML.substring(strTo + arrDQAtt[i].length);
					strDataXML = strStart + repStrr + strEnd;
				}
			}
			strDataXML = strDataXML.replace(/\"/g, '%26quot;');
			strDataXML = strDataXML.replace(/%(?![\da-f]{2}|[\da-f]{4})/ig, '%25');
			strDataXML = strDataXML.replace(/\&/g, '%26');
			return strDataXML;
		},
		setDataXML: function(strDataXML) {
			//If being set initially
			if (!this.initialDataSet) {
				//This method sets the data XML for the chart INITIALLY.
				this.addVariable('dataXML', this.encodeDataXML(strDataXML));
				//Update flag
				this.initialDataSet = true;
			}else {
				//Else, we update the chart data using External Interface
				//Get reference to chart object
				var chartObj = window.infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
				chartObj.setDataXML(strDataXML);
			}
		},
		setTransparent: function(isTransparent) {
			//Sets chart to transparent mode when isTransparent is true (default)
			//When no parameter is passed, we assume transparent to be true.
			if (isTransparent === undefined) {
				isTransparent = true;
			}
			//Set the property
			if (isTransparent) {
				this.addParam('WMode', 'transparent');
			} else {
				this.addParam('WMode', 'Opaque');
			}
		},

		render: function(elementId) {
			//First check for installed version of Flash Player - we need a minimum of 8
			if ((this.detectFlashVersion === 1) && (this.installedVer.major < 8)) {
				if (this.autoInstallRedirect === 1) {
					//If we can auto redirect to install the player?
					var installationConfirm = window.confirm('You need Adobe Flash Player 8 (or above) to view the charts. It is a free and lightweight installation from Adobe.com. Please click on Ok to install the same.');
					if (installationConfirm) {
						window.location = 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash';
					}else {
						return false;
					}
				}else {
					//Else, do not take an action. It means the developer has specified a message in the DIV (and probably a link).
					//So, expect the developers to provide a course of way to their end users.
					//window.alert("You need Adobe Flash Player 8 (or above) to view the charts. It is a free and lightweight installation from Adobe.com. ");
					return false;
				}
			}else {
				//Render the chart
				var n = (typeof elementId === 'string') ? document.getElementById(elementId) : elementId;
				// If loaded in IE and scaleMode and width/height specified in %, load the chart using onload event
				if (this.getVariable('scaleMode').search(/noscale/i) >= 0 &&
						(this.getAttribute('width').search('%') > 0 ||
						this.getAttribute('height').search('%') > 0))
				{
					//store current object reference
					var obj = this;
					if (window.addEventListener) {
						//add onload event on firefox
						window.addEventListener('load', function()
								{ n.innerHTML = obj.getSWFHTML(); },false);
					} else if (window.attachEvent) {
						//add onload event on IE
						window.attachEvent('onload', function()
								{ n.innerHTML = obj.getSWFHTML(); });
					} else {
						// if all onload fails fails
						n.innerHTML = this.getSWFHTML();
					}
				} else {
					//Normal case. Instantly load the chart
					n.innerHTML = this.getSWFHTML();
				}
				//Added <FORM> compatibility
				//Check if it's added in Mozilla embed array or if already exits
				if (!document.embeds[this.getAttribute('id')] && !window[this.getAttribute('id')]) {
					window[this.getAttribute('id')] = document.getElementById(this.getAttribute('id'));
				}
				//or else document.forms[formName/formIndex][chartId]
				return true;
			}
		}
	};

	/* ---- detection functions ---- */
	window.infosoftglobal.FusionChartsUtil.getPlayerVersion = function() {
		var PlayerVersion = new window.infosoftglobal.PlayerVersion([0, 0, 0]),
				axo;
		if (window.navigator.plugins && window.navigator.mimeTypes.length) {
			var x = window.navigator.plugins['Shockwave Flash'];
			if (x && x.description) {
				PlayerVersion = new window.infosoftglobal.PlayerVersion(x.description.replace(/([a-zA-Z]|[\t ])+/, '').replace(/([\t ]+r|[\t ]+b[0-9]+)/, '.').split('.'));
			}
		}else if (window.navigator.userAgent && window.navigator.userAgent.indexOf('Windows CE') >= 0) {
			//If Windows CE
			var counter = 3;
			axo = 1;
			while (axo) {
				try {
					counter++;
					axo = new window.ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + counter);
					PlayerVersion = new window.infosoftglobal.PlayerVersion([counter, 0, 0]);
				} catch (e) {
					axo = null;
				}
			}
		} else {
			// Win IE (non mobile)
			// Do minor version lookup in IE, but avoid Flash Player 6 crashing issues
			try {
				axo = new window.ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
			}catch (e2) {
				try {
					axo = new window.ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
					PlayerVersion = new window.infosoftglobal.PlayerVersion([6, 0, 21]);
					axo.AllowScriptAccess = 'always'; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
				} catch (e3) {
					if (PlayerVersion.major === 6) {
						return PlayerVersion;
					}
				}
				try {
					axo = new window.ActiveXObject('ShockwaveFlash.ShockwaveFlash');
				} catch (e4) {}
			}
			if (axo) {
				PlayerVersion = new window.infosoftglobal.PlayerVersion(axo.GetVariable('$version').split(' ')[1].split(','));
			}
		}
		return PlayerVersion;
	};
	window.infosoftglobal.PlayerVersion = function(arrVersion) {
		this.major = arrVersion[0] ? parseInt(arrVersion[0], 10) : 0;
		this.minor = arrVersion[1] ? parseInt(arrVersion[1], 10) : 0;
		this.rev = arrVersion[2] ? parseInt(arrVersion[2], 10) : 0;
	};
	// ------------ Fix for Out of Memory Bug in IE in FP9 ---------------//
	/* Fix for video streaming bug */
	window.infosoftglobal.FusionChartsUtil.cleanupSWFs = function() {
		var objects = document.getElementsByTagName('OBJECT'), i, x;
		for (i = objects.length - 1; i >= 0; i--) {
			objects[i].style.display = 'none';
			for (x in objects[i]) {
				if (typeof objects[i][x] === 'function') {
					objects[i][x] = nullFunction;
				}
			}
		}
	};
	// Fixes bug in fp9
	if (window.infosoftglobal.FusionCharts.doPrepUnload) {
		if (!window.infosoftglobal.unloadSet) {
			window.infosoftglobal.FusionChartsUtil.prepUnload = function() {
				window.__flash_unloadHandler = nullFunction;
				window.__flash_savedUnloadHandler = nullFunction;
				window.attachEvent('onunload', window.infosoftglobal.FusionChartsUtil.cleanupSWFs);
			};
			window.attachEvent('onbeforeunload', window.infosoftglobal.FusionChartsUtil.prepUnload);
			window.infosoftglobal.unloadSet = true;
		}
	}
	/* Add document.getElementById if needed (mobile IE < 5) */
	if (!document.getElementById && document.all) { document.getElementById = function(id) { return document.all[id]; };}
	/* Add Array.push if needed (ie5) */
	if (!Array.prototype.push) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; };}

	/* Function to return Flash Object from ID */
	window.infosoftglobal.FusionChartsUtil.getChartObject = function(id) {
		var chartRef = null;
		if (window.navigator.appName.indexOf('Microsoft Internet') < 0) {
			if (document.embeds && document.embeds[id]) {
				chartRef = document.embeds[id];
			}
			else {
				chartRef = document[id];
			}
		}
		else {
			chartRef = window[id];
		}
		if (!chartRef) {
			chartRef = document.getElementById(id);
		}

		return chartRef;
	};
	/* Aliases for easy usage */
	window.getChartFromId = window.infosoftglobal.FusionChartsUtil.getChartObject;
	window.FusionCharts = window.infosoftglobal.FusionCharts;
}());

