// setup plugin information

sm_plugin_debug = false
sm_pluginVersion = new Array()
sm_pluginVersion['flash'] = 0
sm_pluginVersion['shockwave'] = 0

sm_gatherPluginInfo()

function sm_gatherPluginInfo(){
	// check standard way
	if (navigator.plugins){
		for (var i=0; i<navigator.plugins.length; i++){
			var desc = navigator.plugins[i].description
			if (desc.indexOf("Flash")!=-1){
				sm_pluginVersion['flash'] = trim(desc.substring(desc.indexOf(".")-2, desc.length))	
			} else if (desc.indexOf("Director")!=-1){
				sm_pluginVersion['shockwave'] = trim(desc.substring(desc.indexOf(".")-2, desc.length))
			}
		}
	}
	
	// check PC IE way
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');

	// check for shockwave version
	document.write('sm_success = false \n');
	document.write('sm_success = (IsObject(CreateObject("SWCtl.SWCtl")))\n');
	document.write('sm_swVersion = (CreateObject("SWCtl.SWCtl")).ShockwaveVersion("")\n');
	document.write('If sm_success Then \n sm_pluginVersion.shockwave = sm_swVersion \n  End If\n');
	
	// check for flash version
	document.write('sm_TestVersions = Array("3","4","5","6","7","8") \n');
	document.write('For Each sm_Version in sm_TestVersions \n');
	document.write('sm_success = false \n');
	document.write('sm_success = (IsObject(CreateObject("ShockWaveFlash.ShockWaveFlash."&sm_Version)))\n');
	document.write('If sm_success Then \n sm_pluginVersion.flash = sm_Version \n End If\n');
	document.write('Next \n');
	
	document.write('</SCRIPT\> \n');
}

function sm_checkPlugins(details){
	// split details into parts
	var dArray = details.split(';')
	
	// trim values
	for (var i=0; i<dArray.length; i++){
		dArray[i] = trim(dArray[i])
	}
	
	// create array for details
	var commands = new Array();
	for (var i=0; i<dArray.length; i++){
		var tempArray = dArray[i].split('=')
		var key = (trim(tempArray[0])).toLowerCase()
		var value = (trim(tempArray[1])).toLowerCase()
		if (key=='require'){
			var pluginArray = value.split(',')
			var requireValue = new Array()
			for (var j=0; j<pluginArray.length; j++){
				var pluginData = new Array()
				var dataArray = (trim(pluginArray[j])).split(':')	
				pluginData['plugin'] = (trim(dataArray[0])).toLowerCase()
				pluginData['version'] = trim(dataArray[1])
				requireValue[requireValue.length] = pluginData
			}
			value = requireValue
		}
		commands[key] = value
	}
	
	// check for all valid syntax
	if (commands.failpage+''!='undefined' && commands.successpage+''!='undefined' && commands.require+''!='undefined'){
		var requireInfo = ""
		for (var i=0; i<commands.require.length; i++){
			requireInfo += commands.require[i].plugin+" version "+commands.require[i].version+"\n"
		}
		if (sm_plugin_debug) alert('Requires:\n' +requireInfo+ '\nSuccessPage:\n' +commands.successpage+ '\n\nFailPage:\n' +commands.failpage)
		if (sm_plugin_debug) alert('Detected\nFlash version: '+sm_pluginVersion['flash'] +"\nShockwave version: "+ sm_pluginVersion['shockwave'])
		
		// compare required plugins with actual plugins
		var pluginsOK = true
		
		for (var i=0; i<commands.require.length; i++){
			if (!versionOK(commands.require[i].version, sm_pluginVersion[commands.require[i].plugin])){
				if (sm_plugin_debug) alert(commands.require[i].plugin+" is too old")
				pluginsOK = false
				break
			} else {
				if (sm_plugin_debug) alert(commands.require[i].plugin+" is good")
			}
		}	
		if (sm_plugin_debug) alert('Plugins OK:'+pluginsOK)
		
		if (!sm_plugin_debug){
			if (pluginsOK){
				document.location.replace(commands.successpage)
			} else {
				document.location.replace(commands.failpage)
			}
		}
	}
}

function versionOK(requireStr, actualStr){
	if (actualStr==0) return false
	var reqV = convertToVersion(requireStr)
	var actV = convertToVersion(actualStr)

	var vOK = false
	
	if (actV.major > reqV.major){
		vOK = true
	} else if (actV.major == reqV.major){
		if (actV.minor > reqV.minor){
			vOK = true
		} else if (actV.minor == reqV.minor){
			if (actV.revision > reqV.revision){
				vOK = true
			} else if (actV.revision == reqV.revision){
				if (actV.release >= reqV.release){
					vOK = true
				} 
			} 
		} 
	} 		
	return vOK
}

function convertToVersion(str){
	str = str+''
	var version = new Array()
	
	// strip out release if there is one
	rIndex = str.indexOf("r")
	if (rIndex!=-1){
		version["release"] = parseInt(str.substring(rIndex+1,str.length))
		str = str.substring(0,rIndex)
	} else {
		version["release"]= 0
	}

	var sections = str.split(/\s|\./)
	var partNames = new Array('major','minor','revision')
	
	for (var i=0; i<partNames.length; i++){
		if (sections.length>i){
			version[partNames[i]] = parseInt(sections[i])
			if (isNaN(version[partNames[i]])) version[partNames[i]] = 0
		} else {
			version[partNames[i]] = 0
		}
	}
	return version
}

function trim(str){
	// force to string
	str = str+''
	
	// trim from start
	while (str.charAt(0)==' '){
		str = str.substring(1,str.length)
	}
	// trim from end
	while (str.charAt(str.length-1)==' '){
		str = str.substring(0,str.length-1)
	}	
	return str
}