
//**CHECK FOR ANY OVERIDDEN CONSTANTS***********************************************************//

if(!expireHour){var expireHour = cookieExpiresInHours;}
if(!versionNeeded){var versionNeeded = flashVersionNeeded;}
if(!directToFail){var directToFail = directToFailPage;}
if(!directToPass){var directToPass = directToPassPage;}
if(!alertOnFail){var alertOnFail = showAlertOnFail;}
if(!useCookies){var useCookies = enableCookies;}


//**********************************************************************************************//


//**JS SCRIPT FUNCTION USED FOR NON-IE BROWSERS*************************************************//

function GetVersionNonIE(){

	if(window.navigator.plugins){
		var PI = window.navigator.plugins;
			for(var i=0; i < PI.length; i++){
				if(window.navigator.plugins[i].name.indexOf('Flash') != -1){
				//split the description into parts (Shockwave Flash 0.0 r00)
				var versArray = window.navigator.plugins[i].description.split(" ");
				
				//get the Major and Minor versions
				var tempMaVer = versArray[2].split(".");
				var MaVer = tempMaVer[0];
				var MiVer = tempMaVer[1];
				
				//now get the Revision
				if(versArray[3] != ""){ tempReVer = versArray[3].split("r"); }
				else{tempReVer = versArray[4].split("r"); }
				
				var ReVer = tempReVer[1] > 0 ? tempReVer[1] : 0;
				
				//now build a return string
				var vers = MaVer +"."+ MiVer +"."+ ReVer;
				
				//character at 16 is the flash version number
				//var vers = window.navigator.plugins[i].description.charAt(16);
 				}
	}
	//return the flash version
	return vers;
	}else {
	//the browser cannot understand the request for plug-in info
	return false;
	}
 
}

//**********************************************************************************************//

//**DETECT FOR IE BROWSER************************************************************************//

// Detect WIN/IE Configuration and throw VB at it to get the flash version
if(navigator.appVersion.indexOf("MSIE") != -1){var IE = true;}else{ var IE = false;}
if(navigator.appVersion.toLowerCase().indexOf("win") != -1){var WIN = true;}else{ var WIN = false;}
if(navigator.userAgent.indexOf("Opera") != -1){var OP = true;}else{var OP = false;}


//**********************************************************************************************//

//**DETECT FOR IE BROWSER***********************************************************************//

function getFlashVersion(){

	// Write vbscript detection on ie win. IE on Windows doesn't support regular
	// JavaScript plugins array detection.
	if(IE && WIN && !OP){
		for(var i=20; i>0; i--){
			var flasharray = GetVersionIE(i);
		}
		
		//if no flash was detected, exit to fail page
		if(flasharray == 'undefined' || flasharray == 0){return 0;}
		
		//VB returns an array of the plug-in info, so we will extract the major version
		var flashinfo = flasharray.split(" ");  //["WIN", "n,n,n,n"]
		var versioninfo = flashinfo[1];  //"n,n,n,n"
		var versionarray = versioninfo.split(",");
		
	}else{
		var versioninfo = GetVersionNonIE();
		//if no flash was detected, exit to fail page
		if(versioninfo == undefined || versioninfo < 1){return 0;}
		
		var versionarray = versioninfo.split(".");
	}
	
	//this is the version info
	var flashMajorVersion = versionarray[0];
	var flashMinorVersion = versionarray[1];
	var flashRevVersion = versionarray[2];
	
	//build a return string
	var flashVersion = flashMajorVersion +"."+ flashMinorVersion +"."+ flashRevVersion;
	//alert( flashVersion );
	
	return flashVersion;

}
//************************************************************************************************//


//*FUNCTION FOR SETTING COOKIE********************************************************************//

function set_detect_cookie(userVer){
	
	expireDate = new Date;
	expireDate.setHours(expireDate.getHours()+(expireHour));
	
	var flashPair = 'flashVer='+userVer+';expires='+expireDate.toGMTString();
	document.cookie = flashPair;

}

//************************************************************************************************//


//*FUNCTION FOR CLEARING COOKIE********************************************************************//

//function for clearing cookie so that users can download the new flash player and try again.
function clear_detect_cookie(cookieName){
	var thisCookie = document.cookie.split('; ');
	expireDate = new Date;
	expireDate.setHours(expireDate.getDate()-1);
	
	for(i=0; i<thisCookie.length; i++){
		if(cookieName == thisCookie[i].split('=')[0]){
			thisCookie[i] = 'flashVer='+userVer+';expires='+expireDate.toGMTString();
		}
	}	
}

//************************************************************************************************//


//*FUNCTION FOR GETTING COOKIE********************************************************************//

function get_detect_cookie_value(cookieName){
	var thiscookie = document.cookie.split('; ');
	for(i=0; i<thiscookie.length; i++){
		if(cookieName == thiscookie[i].split('=')[0]){
			return thiscookie[i].split('=')[1];
		}
	}
	//if we got no matches
	return false
}

//************************************************************************************************//


//*FUNCTION TO ANALYZE FLASH STRING***************************************************************//

	function getVal(str){
		if(!str || str == "" || str == 'undefined'){
			var tempval = 0;
		}else{
			var tempval = str;
		}
		
		return tempval;
		
	}

//************************************************************************************************//


//*FUNCTION TO ENVOKE DETECT**********************************************************************//

function beginFlashDetection(){

if(document.cookie.indexOf('flashVer') != -1){
	//user has cookie already, lets get the version
	var flashVersion = get_detect_cookie_value('flashVer');
}else{

	// Version check based upon the values entered above in "Globals"
	var flashVersTest = getFlashVersion();
	
	//if the test returned 0, no flash was detected
	if(flashVersTest == 0){
		
		return 0;
		
	}else{
	
		var flashVersInfo = flashVersTest.split(".");
		var flashVersNeed = versionNeeded.split(".");
	
	}
	
	//init the result
	var hasReqVer = false;
	
	//convert version and required to decimal strings
	var InfoToDecimal = "0."+ getVal(flashVersInfo[0])+''+getVal(flashVersInfo[1])+''+getVal(flashVersInfo[2]);
	var NeedToDecimal = "0."+ getVal(flashVersNeed[0])+''+getVal(flashVersNeed[1])+''+getVal(flashVersNeed[2]);
	
	if(InfoToDecimal >= NeedToDecimal){
		var hasReqVer = true;
	}else{
		//if the test failed, determine the correct update method
		var updateMethod = (InfoToDecimal >= 0.6065) ? 1 : 0;
	}
		
	
}

// Check to see if the version meets the requirements for playback
if (hasReqVer || flashVersion >= versionNeeded || flashVersTest.indexOf('10.') > -1 ) {  // if we've detected an acceptable version
	
	//set a cookie with version
	if(!flashVersion){
		//set cookie if option is enabled
		if(useCookies){set_detect_cookie(getFlashVersion());}
	}

		return 2;
  
} else {  // flash is too old or we can't detect the plugin
	//clear the cookie that we set so that the user can download the lfash player and try again
	if(useCookies){clear_detect_cookie('flashVer');}
	//show js alert, if set to true
	if(alertOnFail){alert('This page requires Flash Plug-In version '+ versionNeeded +'. Please upgrade.');}
	
	//redirect to our non-flash page. If set to '', direct the user to Adobe download page
	//we need to add a switch here to send them to the correct update method
	if(updateMethod == 1){ return 1;}else{ return 0;}
	//if(directToFail == ''){window.location = 'http://www.adobe.com/shockwave/download/';}else{window.location = directToFail;}
}


}



