/**
 * USAGE:
 * var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
 */

/**
 * JavaScript helper required to detect Flash Player PlugIn version information
 */
function JSGetSwfVer () {
    var flashVer;
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
            var tempArrayMinor;
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		flashVer = -1;
	}
    // make sure result is a string
	return "" + flashVer;
}

/**
 * When called with reqMajorVer, reqMinorVer, reqRevision returns true
 * if that version or greater is available
 */
function DetectFlashVer (reqMajorVer, reqMinorVer, reqRevision) {
    var versionArray = getFlashVersionArray();
    if (versionArray != null) {
        versionMajor = versionArray[0];
        versionMinor = versionArray[1];
        versionMinor = versionArray[2];

        if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
    }
    // if array was null or versions too low
    return false;
}

/**
 * if arg <code>varr</code> supplied, use that, otherwise try to determine
 */ 
function getFlashVersionString (varr) {
    if (varr === undefined)
        varr = getFlashVersionArray();
    if (varr) {
        return varr[0] + "." + varr[1] + "." + varr[2];
    }
    return null;
}


/**
 * Return array of [major, minor, rev]
 */
function getFlashVersionArray () {
    var versionArray = null;
    if (isWinIE()) {
        // on Win/IE, loop backwards through the versions until we find the newest version
        for (i = 25; i > 0; i--) {
            // call a VB Script function, defined in FlashDetect.vbs
            var versionStr = VBGetSwfVer(i);
            // for some reason, if it returns -1 need to give up
            if (versionStr == -1)
                return null;
            if (versionStr != 0) {
                var tempArray = versionStr.split(" ");
                var tempString = tempArray[1];
                versionArray = tempString.split(",");
            }
        }
    } else {
        var versionStr = JSGetSwfVer();
        if (versionStr != "-1")
            versionArray = versionStr.split(".");
    }
    return versionArray;
}


function isWinIE () {
    // Detect Client Browser type
    if (navigator.appVersion.indexOf("MSIE") == -1) return false;
    if (navigator.appVersion.toLowerCase().indexOf("win") == -1) return false;
    if (navigator.userAgent.indexOf("Opera") != -1) return false;
    return true;
}

//var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
//var hasReqestedVersion = DetectFlashVer(7, 0, 0);
//alert(hasReqestedVersion);
