// Script de détection pour Via
// par Nicolas Dion-Bouchard

var viaDetect = new Object();
	viaDetect.jsOK = false;
	viaDetect.successHideDiv = null;
	viaDetect.onFinish = null;
	viaDetect.installedFlashVer = "0.0";
	
	// Holder Flash
	document.write('<div id="flashDetect_flashHolder" style="position:relative; left: -1000px;"><div id="flashDetect_flashPlacement"></div></div>');

	// Initialisation
	viaDetect.init = function(params) {
		// Enregistrement des settings
		if(params) {
			for(var p in params) this[p] = params[p];
		}
			
		// Écriture de la validation JavaScript
		document.write('<SCRIPT LANGUAGE="JavaScript' + this.requiredJSVer + '">');
		document.write('	viaDetect.jsOK = true;');
		document.write('</SCRIPT>');
		
		// Préparation détection FlashPlayer
		var delayedFailed = false;
		try {
		    if(document.location.search.indexOf('noFlash') != -1) delayedFailed = true;
		    else if(swfobject) swfobject.embedSWF("flash/flashDetect.swf", "flashDetect_flashPlacement", "150", "100", String(this.requiredFlashVer), null, null, null, null, this.flashDetected);
		    else delayedFailed = true;
		} catch(e) {
		    delayedFailed = true;
		}
		if(delayedFailed) {
		    window.onload = function() { viaDetect.runTests(false); }
		}
	}
	
	// Application d'une propriété de style
	viaDetect._setStyle = function(o, p, n) {
		if(typeof(o)== "string") o=document.getElementById(o); if(!o) return; try { if(!o.style) o[p] = n; else o.style[p] = n; } catch(e) {}
	}
	
	viaDetect.flashDetected = function(e) {
	    try { viaDetect.installedFlashVer = String(swfobject.ua.pv).replace(/\,/ig,"."); }
	    catch(e) { /*alert("error in _flashDetectCallBack: " + e);*/ }
	    viaDetect.runTests(false);
	}
	
	// Validations et affichage des résultats
	viaDetect.runTests = function(returnResult) {
		if(returnResult != false) returnResult = true;
		
		function showResultDiv(divName, obj) {
			var tarDiv = document.getElementById(divName);
			if(tarDiv) {
				var content = String(tarDiv.innerHTML);
				for(var p in obj) {
					var pName = "%"+p+"%";
					while(content.indexOf(pName) != -1) {
						content = content.substring(0,tarDiv.innerHTML.indexOf(pName)) + obj[p] + content.substring(content.indexOf(pName)+ pName.length, content.length);
					}
				}
				tarDiv.innerHTML = content;
				if(obj.tagName == "table") obj._setStyle(tarDiv, "display", "table");
				else if(obj.tagName == "span") obj._setStyle(tarDiv, "display", "inline");
				else obj._setStyle(tarDiv, "display", "block");
				return true;
			}
			return false;
		}
		
		// Fin du test
		this._setStyle(this.inProgressDiv, "display", "none");
		this._setStyle("flashDetect_flashHolder", "display", "none");
		
		var flashVerArr = this.installedFlashVer.split(".");
		var fVerNum = Number(flashVerArr[0] + "." + flashVerArr[1]);
		
		if(fVerNum <= 0) {
			// Flash non installé (délais dépassé)
			var resultShown = showResultDiv(this.flashErrorDiv, this);
			if(!resultShown) alert("Flash not detected (result box not found: " + this.flashErrorDiv + " / " + document.getElementById(this.flashErrorDiv) + ").");
			if(this.onFinish != null && typeof(this.onFinish) == "function") this.onFinish(false);
			if(returnResult) return false;
			else return;
		}
		
		//alert("Flash Player found: " + flashVerArr + " (needed: " + this.requiredFlashVer + ")");
		if(fVerNum < this.requiredFlashVer) {
			// Flash trop ancien
			var resultShown = showResultDiv(this.flashVersionErrorDiv, this);
			if(!resultShown) alert("Old version of Flash Player detected (installed: " + this.installedFlashVer + ", required: " + this.requiredFlashVer + ", result box not founde).");
			if(this.onFinish != null && typeof(this.onFinish) == "function") this.onFinish(false);
			if(returnResult) return false;
			else return;
		}
		
		// Validation de JavaScript
		if(!this.jsOK) {
			// Version non compatible
			var resultShown = showResultDiv(this.jsErrorDiv, this);
			if(!resultShown) alert("Version JavaScript non compatible (result box not found).");
			if(this.onFinish != null && typeof(this.onFinish) == "function") this.onFinish(false);
			if(returnResult) return false;
			else return;
		}
		
		// Pas d'erreurs, tout est beau
		var resultShown = showResultDiv(this.successDiv, this);
		if(!resultShown) alert("All tests succeded! (result box not found).");
		else if(this.successHideDiv != null) this._setStyle(this.successHideDiv, "display", "none");
		if(this.onFinish != null && typeof(this.onFinish) == "function") this.onFinish(true);
		if(returnResult) return true;
		else return;
	}
	