var PLUGIN_DETECTED_NO_VERSION = -3;
var PLUGIN_NOT_DETECTED = -2;
var PLUGIN_DETECTION_FAILED = -1;
var PLUGIN_UNKNOWN = 0;

var arrayNoPlugin = new Array("", "Plugin detection failed", "Plugin not detected", "Plugin installed. Unable to determine version");

function detectNSPlugin(pluginName, mimeTypeName) {
	var detectJava = (pluginName.toLowerCase() == "java");
	var detectedPlugin = false;
	var detectedVersion;
	var oldVersion = 0;
	var detectedVersionString = "";
	
	for(i = 0; i < navigator.plugins.length; i++) {
		plugin = navigator.plugins[i];
		if(plugin.name.indexOf(pluginName)>-1 || plugin.description.indexOf(pluginName)>-1 ) {
			if (!detectJava) return(pluginNSVersion(plugin.name, plugin.description));
		}
		if (plugin.length > 0) {
			for(j = 0; j < plugin.length; j++) {
				mimeType = plugin[j];
				if (mimeType.type.indexOf(mimeTypeName) > -1) {
					if (detectJava) {
						indexVersion = mimeType.type.toLowerCase().indexOf("version=");
						if (indexVersion > -1) {
							thisVersion = mimeType.type.substring(indexVersion + 8);
							if (thisVersion.length > 0) {
								thisVersion = thisVersion.replace(".", "");
								if (thisVersion > oldVersion) {
									oldVersion = thisVersion;
									detectedVersionString = mimeType.type.substring(indexVersion + 8);
								}
							}
						}
					} else {
						return(pluginNSVersion(plugin.name, plugin.description));
					}
				}
			}
		}
	}
	if (detectJava && detectedVersionString != "") {
		return detectedVersionString;
	} else {
		return PLUGIN_NOT_DETECTED;
	}
}

function pluginNSVersion(name, description) {
	if (name.indexOf('.') > -1) {
		description = name;
	}
	point = description.indexOf('.');
	space = description.indexOf(' ', point);
	if (point > -1) {
		if (space > -1) {
			return(description.substring(point - 1, space));
		} else {
			return(description.substring(point - 1));
		}
	} else {
		return(PLUGIN_DETECTED_NO_VERSION);
	}
}

function detectIEPluginVersion(activeXControlName, maxVersion) {
	rc = PLUGIN_NOT_DETECTED;
	document.write('<SCRIPT LANGUAGE="VBScript">\n');
	document.write('On Error Resume Next \n');
	document.write('If ScriptEngineMajorVersion >= 2 Then \n');
	document.write('	If ' + maxVersion + ' > 0 Then \n');
	document.write('		For s = 0 To ' + maxVersion + ' \n');
	document.write('			If Not IsObject(CreateObject("' + activeXControlName + '." & s)) Then \n');
	document.write('			Else \n');
	document.write('				 rc = s');
	document.write('			End If \n');
	document.write('		Next \n');
	document.write('	Else \n');
	document.write('		If Not IsObject(CreateObject("' + activeXControlName + '")) Then \n');
	document.write('		Else \n');
	document.write('			rc = PLUGIN_DETECTED_NO_VERSION \n');
	document.write('		End If \n');
	document.write('	End If \n');
	document.write('Else \n');
	document.write('	rc = PLUGIN_DETECTION_FAILED \n');
	document.write('End If</' + 'SCRIPT>\n');
	return rc;
}

function detectPlugin(pluginName, mimeTypeName, activeXControlName, activeXMaxVersion) {
	var v;
	if (navigator.plugins && navigator.plugins.length || navigator.mimeTypes && navigator.mimeTypes.length) {
		v = detectNSPlugin(pluginName, mimeTypeName);
	} else {
		v = detectIEPluginVersion(activeXControlName, activeXMaxVersion);
	}
	if (v < 0) v *= -1;
	return v;
}

function openPopup(popupLocation, popupWidth, popupHeight, scrollBars) {
		var parameters = 'width=' + popupWidth + ',height=' + popupHeight + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=' + scrollBars;
		popupWindow = open(popupLocation, 'popupWindow', parameters).focus();
}

function plugix(pName, pFullName, pNSName, pNSMime, pAXName, pMaxV) {
	this.name			= pName;
	this.fullName	= pFullName;
	this.nsName		= pNSName;
	this.nsMime		= pNSMime;
	this.axName		= pAXName;
	this.version 	= detectPlugin(pNSName, pNSMime, pAXName, pMaxV);
}

function objectPlugix() {
	this.plugixArray = new Array();
	this.plugixArray[this.plugixArray.length] = new plugix("Flash", "MacroMedia Flash", "Shockwave Flash", "application/x-shockwave-flash", "ShockwaveFlash.ShockwaveFlash", 20);
	
	this.getPluginVersion = function(sName) {
		for (i = 0; i < this.plugixArray.length; i++) {
			if (this.plugixArray[i].name.toLowerCase() == sName.toLowerCase()) {
				return (this.plugixArray[i].version);
			}
		}
		return 0;
	}
	
}

var p = new objectPlugix();	

function detectFlash() {
	return (parseInt(p.getPluginVersion("Flash"))>=6);
}
