
var playClicks = 0;
var playCountDown = 0;
var problemURL;

//**************** NON IE FUNCTIONS *********************
function detectQuickTime() {
    return detectPlugin('QuickTime');
}

function detectWindowsMedia() {
    return detectPlugin('Windows Media');
}

function detectPlugin(strPluginName) {

    // check for the navigator array
    if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;

		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
			// if desired plugin name is found in either plugin name or description
			if( (navigator.plugins[pluginsArrayCounter].name.indexOf(strPluginName) >= 0) || 
				(navigator.plugins[pluginsArrayCounter].description.indexOf(strPluginName) >= 0) ) {
				// this name was found
				return true;
			}   	
		}
		return false;
	}
	return false;
}

//This assumes we not on IE
function isQuickTimeConfigured()
{
	//Grab our agent object
	var agent = navigator.userAgent;
	agent = agent.toLowerCase();

	//Make sure we can get to our MIDI type
	var midiType = navigator.mimeTypes["audio/x-midi"];

	//Check the configuration. 
	if(midiType)
	{
		var plug = midiType.enabledPlugin;

		if(plug)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	else{
		return false;
	}
	//Just in case
	return false;
}

//Detect if we are using a supported version of IE
function isIEAbove55(){
	var strAgent = new String();
	strAgent = navigator.userAgent;
	
	var iIndex;
	var fVersion;
	iIndex = strAgent.indexOf("MSIE");
	
	//Found IE 
	if(iIndex!=-1){
		if(navigator.plugins.length == 0){ //Make sure someone isn't spoofing IE. 
			var temp = new Array;
			temp = strAgent.split("MSIE");
			fVersion = parseFloat(temp[1]);		
						
			//if fVersion <= 0 then someone probably just spoofed the MSIE string, we should ignore it
			if (fVersion < 5.5 && fVersion > 0){
				return false;
			}
			return true;			
		}		
	}
	return true;
}

//*********************** DETECTION FUNCTIONS *******************************
//detectImmediateProblems() - First round check of the user's browser, if any 
//problems are detect, this will return a url for a pop-up on where to go. 
function detectImmediateProblems(){
	//Get browser and OS data
	var data = new Array();
	data = detectBrowser();
	
	if(data['os']=="Windows"){
		if(data['browser']=="MSIE"){
			if(!isIEAbove55())
				return "/tsfaqs/windows/unsupportedie.html";
			if(detectActiveXControl('MediaPlayer.MediaPlayer.1'))
				return;
			if(detectQuickTimeActiveXControl())
				return;
			return "/tsfaqs/windows/nopluginsfound-ie.html"
		}//MSIE
		else{ //Mozilla based browser
			if(detectQuickTime()){
				if(isQuickTimeConfigured())
					return;
				else
					return "/tsfaqs/windows/problemsplayingpopup-moz-qtnotconfigured.html";
			}
			else
				return "/tsfaqs/windows/nopluginsfound-moz.html";
		}//Mozilla				
	}//Windows
	
	if(data['os']=="Macintosh"){
		if(!detectQuickTime())
			return "/tsfaqs/osx/nopluginsfound.html";
		return;
	}
	
	if(data['os']=="Linux"){
		if(!detectPlugin("Plugger"))
			return "/tsfaqs/linux/nopluginsfound.html";
		return;	
	}
}//detectImmediateProblems

//detectProblemsPlaying() - This is for when the user presses the Problems Playing button. This will always return a URL
function detectProblemsPlaying(){
	//Get browser and OS data
	var data = new Array();
	data = detectBrowser();
	
	if(data['os']=="Windows"){
		if(data['browser']=="MSIE"){
			if(!isIEAbove55())
				return "/tsfaqs/windows/unsupportedie.html";
			if(detectActiveXControl('MediaPlayer.MediaPlayer.1'))
				return "/tsfaqs/windows/problemsplayingpopup-ie.html";
			if(detectQuickTimeActiveXControl())
				return "/tsfaqs/windows/problemsplayingpopup-ie-qt.html";
			return "/tsfaqs/windows/nopluginsfound-ie.html"
		}//MSIE
		else{ //Mozilla based browser
			if(detectQuickTime()){
				if(isQuickTimeConfigured())
					return "/tsfaqs/windows/problemsplayingpopup-moz-qtok.html";
				else
					return "/tsfaqs/windows/problemsplayingpopup-moz-qtnotconfigured.html";
			}
			else
				return "/tsfaqs/windows/nopluginsfound-moz.html";
		}//Mozilla				
	}//Windows
	
	if(data['os']=="Macintosh"){
		if(!detectQuickTime())
			return "/tsfaqs/osx/nopluginsfound.html";
		return "/tsfaqs/osx/problemsplayingpopup.html";
	}
	
	if(data['os']=="Linux"){
		if(!detectPlugin("Plugger"))
			return "/tsfaqs/linux/nopluginsfound.html";
		return "/tsfaqs/linux/problemsplayingpopup.html";	
	}	
	return;
} //detectProblemsPlaying

function showHelp() {
	var url;

	if (problemURL) {
		url = problemURL;
	} else{
		url = detectProblemsPlaying();
		document.getElementById("ProblemsDiv").innerHTML = '';
	}

	win=window.open(url,'tspopup','width=444,height=300,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no');
	if (win.focus) {
		win.focus(); 
	}
}

function showProblemButton() {
	document.getElementById("ProblemsDiv").innerHTML = '<a href="javascript:showHelp()" onMouseOver="document.getElementById(\'ProblemsButton\').src = \'/generate/images/problemsplaying_over.gif\'" onMouseOut="document.getElementById(\'ProblemsButton\').src = \'/generate/images/problemsplaying.gif\'"><img src="/generate/images/problemsplaying.gif" alt="problems playing?" width="77" height="9" hspace="3" vspace="6" border="0" id="ProblemsButton"></a>';
}

function playClick() {
	playTone();
	playClicks++;
	if ((playClicks > 2) && (playCountDown > 0)) {
		showProblemButton();
		playCountDown = 0;
		return;
	}
	if (playCountDown == 0) {
		playCountDown = 11;
		playClockTick();
	}
}
                                                                                                                             
function playClockTick() {
	if (playCountDown > 0) {
		playCountDown--;
		setTimeout("playClockTick()", 1000);
	}
}

function problemDetect() {
	var url;
	if (url = detectImmediateProblems()) {
		problemURL = url;
		showProblemButton();
	}
}

