var minJavaVer;
var minFlashVer;
var promptForJava;
var bgColor			= "#FFFFFF";
var preloaderUrl 	= "";	
var appUrl 			= "";
var jarUrl 			= "";
var jarUrlOsxff 	= "java/MidiAppletOsxff.jar";
var appletCodeOsxff = "MidiAppletOsxff";


var jsReady 		= false;
var javaReady 		= false;
function noFlash(){var s = "<div>For this site you need Flash plugin version " + minFlashVer + " or higher:" +
					"<ul><li>click <a href='http://www.adobe.com/go/getflashplayer' target='_blank'>here</a> to install or update</li></ul></div>" +
					"<div><a href='http://www.adobe.com/go/getflashplayer' target='_blank'><img src='/flashmusic/images/flash_icon.jpg' width='75' height='75' border='0' /></a></div>";
					return s;}
var hasFlash		= "Flashplayer detected: ";
function noJava(){var s	= "<div>For this site you need Java plugin version " + minJavaVer + " or higher:" +
					"<ul><li>click <a href='javascript:loadJava()'>here</a> if your Java Plugin version is " + minJavaVer + " or higher</li>" +
					"<li>click <a href='http://java.com/en/download/index.jsp' target='_blank'>here</a> to install the Java plugin.</li>" +
					"<li>click <a href='javascript:loadApp()'>here</a> to continue without java (midi data will not available!)</li></ul></div>" + 
					"<div><a href='http://java.com/en/download/index.jsp' target='_blank'><img src='/flashmusic/images/java_icon.jpg' width='75' height='65' border='0' /></a></div>";
					return s;}
var updateJava		= "<div ><p>Please update your Java Plugin!<br>" +
					"<ul><li>click <a href='http://java.com/en/download/index.jsp' target='_blank'>here</a> to update.</li>" +
					"<li>click <a href='javascript:loadJava()'>here</a> if your Java Plugin version is 1.3 or higher</li>" +
					"<li>click <a href='javascript:loadApp()'>here</a> to continue without java (midi data will not available!)</li></ul></div>" + 
					"<div><a href='http://java.com/en/download/index.jsp' target='_blank'><img src='/flashmusic/images/java_icon.jpg' width='97' height='80' border='0' /></a></div>";
var doubtJava		= "<div >Could not detect your Java Plugin version." +
					"<ul><li>click <a href='http://java.com/en/download/index.jsp' target='_blank'>here</a> to update to the newest version</a>.</li>" +
					"<li>click <a href='javascript:loadJava()'>here</a> if your Java Plugin version is 1.3 or higher</li>" +
					"<li>click <a href='javascript:loadApp()'>here</a> to continue without java (midi data will not available!)</li></ul></div>" + 
					"<div><a href='http://java.com/en/download/index.jsp' target='_blank'><img src='/flashmusic/images/java_icon.jpg' width='97' height='80' border='0' /></a></div>";
var hasJava			= "Java plugin detected: ";
var match			= "Java(TM)"; 
var javaMsgFrame;
var contentFrame;
var javaVersion;
var flashVersion;


function trace(args)
{
	alert(args);
}

function start()
{
	 checkFlash();
}

function checkFlash()
{
	contentFrame = document.getElementById(appElementId);

	if(swfobject.hasFlashPlayerVersion(minFlashVer))
	{
		var player   			= swfobject.getFlashPlayerVersion()
		flashVersion			= player.major + "." + player.minor + "." + player.release;
		//contentFrame.innerHTML 	= hasFlash + flashVersion;
		checkJava();
	}
	else
	{
		contentFrame.innerHTML 	= noFlash();
		contentFrame.innerHTML += noJava();
	}
}

function checkJava()
{
	//trace(navigator.userAgent);
	if(navigator.userAgent.indexOf('Mac') != -1)
	{
		jarUrl		= jarUrlOsx;
		appletCode 	= appletCodeOsx;
		
		if(navigator.userAgent.indexOf('Safari') == -1) 
		{
			jarUrl		= jarUrlOsxff;
			appletCode 	= appletCodeOsxff;
		}
	}
	if(navigator.userAgent.indexOf('MSIE') != -1) 
	{
		minJavaVer = "1.6";
	}
	
	//trace("browser:" + navigator.userAgent + "\njar:" + jarUrl + " \napplet:" + appletCode);

	if(promptForJava)
	{
		contentFrame.innerHTML = noJava();
	}
	else
	{
		loadJava();
	}
/*	
	var javaEnabled;
	
	if((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) 
	{
		//createVBScript();
		//trace(detectActiveXControl('Sun.Java'));
		//trace(PluginDetect.getVersion('Java', net.abumarkub.midi.MidiCaptureApplet));
		plugin		= "Java MSIE on Win";
		javaEnabled = navigator.javaEnabled();
		javaVersion	= "1.3";
		contentFrame.innerHTML = javaEnabled ? doubtJava : noJava;
		return;
	}
	else
	{	
		var numPlugins  = navigator.plugins.length;
		var fileName; 
		var plugin; 
		for (var i = 0; i < numPlugins; i++) 
		{
			plugin 		= navigator.plugins[i].name;
			fileName 	= navigator.plugins[i].filename
			//document.write(navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>");
			if(plugin.indexOf(match) == 0)
			{
				javaEnabled = true;
				break;
			}
		}
		try
		{
			javaVersion = java.lang.System.getProperty('java.version');
		}
		catch(e)
		{
			contentFrame.innerHTML = javaEnabled ? doubtJava : noJava;			
		}
	}
	
	if(!javaEnabled)
	{
		contentFrame.innerHTML = noJava;
		return;
	}
	//trace(javaVersion.substring(2,3));
	
	if(javaVersion.substring(2,3) < 3)
	{
		contentFrame.innerHTML = updateJava;
		return;
	}
	loadJava();	
 */
}
	
function loadJava()
{
	javaMsgFrame  = document.getElementById("javamessage");
	var content   = "<p style='text-align:center;'>Initializing Java....";
	content 	 += "<br>Please be patient, this can take up to 30 seconds.";
	content		 += "<br>Check your Java plugin settings if you see this message longer that a minute.";
	content		 += "<br>Additionally, you might want to read <a href='http://www.abumarkub.net/abublog/?p=97' target='blank'>this</a>.</p>";
	javaMsgFrame.innerHTML = content;
	 
	var fc 		 = document.getElementById(javaElementId);
	var html 	 = "<div id='java'><applet name='midiApplet' code='net.abumarkub.midi." + appletCode + "'";
    html		+= "archive='" + jarUrl + "' width='1' height='1' MAYSCRIPT>";
    html		+= "</applet></div>";
    fc.innerHTML = html;
}

function loadApp()
{	
	if(preloaderUrl != "")
	{	
		swfobject.embedSWF(preloaderUrl, appElementId, appWidth, appHeight, '9.0.124.0', 
				'swf/expressinstall.swf', {url:appUrl,midiBridgeId:bridgeId}, 
				{bgcolor: bgColor, menu: 'false', allowFullscreen:appAllowFullscreen, allowScriptAccess: 'always',wmode:appWmode}, 
				{id: 'app'});
		return;
	}

	swfobject.embedSWF(appUrl, appElementId, appWidth, appHeight, '9.0.124.0', 
			'swf/expressinstall.swf', {midiBridgeId:bridgeId,defMidiInport:deviceId}, 
			{bgcolor: bgColor, menu: 'false', allowFullscreen:appAllowFullscreen, allowScriptAccess: 'always',wmode:appWmode}, 
			{id: 'app'});
}

function isReady() 
{
	//alert("isReady:" + jsReady);
   return jsReady && javaReady;
}

function pageInit() 
{
    jsReady = true;
}

function getObject(objectName) 
{
    if (navigator.appName.indexOf("Microsoft") != -1) 
    {
        return window[objectName];
    }
    else 
    {
        return document[objectName];
    }
}

function talkToFlash(method,value) 
{
	//alert(method + ":" + value);	 
    if(method == "appletInitialized")
    { 	
    	jsReady 		= true;
    	javaReady 		= true;
    	nextUrl			= 'javascript:loadApp()';
    	nextUrlWindow 	= '_parent';
    	
    	if(appUrl == "" || !appUrl || appUrl == null)
    	{
    		contentFrame.innerHTML = "";
    		nextUrl = "";
    	}
    	
    	deviceId = deviceId == "" ? -1 : deviceId;
    	
		swfobject.embedSWF(bridgeUrl, bridgeElementId, bridgeWidth, bridgeHeight, '9.0.124.0', 
			'swf/expressinstall.swf', {midiBridgeId:bridgeId,defMidiInport:deviceId,nextUrl:nextUrl,nextUrlWindow:nextUrlWindow}, 
			{bgcolor: '#FFFFFF', menu: 'false', allowFullscreen: 'false', allowScriptAccess: 'always'}, 
			{id: 'midiFlash'});

    	return;
    } 
    if(method == "getMidiConfiguration" && value != "")
    {
    	javaMsgFrame.innerHTML = "";
    }
    var flash = getObject("midiFlash"); 
    flash.executeASMethod(method,value);
}

function talkToJava(method,value) 
{
    var applet = getObject("midiApplet");
    //alert(method + "(" + value + ")" + applet);
    applet.executeJavaMethod(method,value);
}

/*
* All credits to: http://www.eggheadcafe.com/articles/20020107.asp
*/

function PageQuery(q) 
{
	if(q.length > 1) 
	{
		this.q = q.substring(1, q.length);
	}
	else
	{
		this.q = null;
	}
	
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() 
	{ 
		return this.keyValuePairs; 
	}
	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return "";
	}
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() 
	{ 
		return this.keyValuePairs.length; 
	} 

	function queryString(key)
	{
		var page = new PageQuery(window.location.search); 
		return unescape(page.getValue(key)); 
	}
	
	function displayItem(key)
	{
		if(queryString(key)=='false') 
		{
			document.write("you didn't enter a ?name=value querystring item.");
		}
		else
		{
			document.write(queryString(key));
		}
	}
}


/*
function createVBScript()
{
	document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');

	
	var vb 	 = document.getElementById("vbscript");
	var html;
    html += '<script language="VBscript">';

    html += '\'do a one-time test for a version of VBScript that can handle this code';
    html += 'detectableWithVB = False';
    html += 'If ScriptEngineMajorVersion >= 2 then';
    html += '  detectableWithVB = True';
    html += 'End If';

    html += '\'this next function will detect most plugins';
    html += 'Function detectActiveXControl(activeXControlName)';
    html += '  on error resume next';
    html += '  detectActiveXControl = False';
    html += '  If detectableWithVB Then';
    html += '     detectActiveXControl = IsObject(CreateObject(activeXControlName))';
    html += '  End If';
    html += 'End Function';

    html += '\'and the following function handles QuickTime';
    html += 'Function detectQuickTimeActiveXControl()';
    html += '  on error resume next';
    html += '  detectQuickTimeActiveXControl = False';
    html += '  If detectableWithVB Then';
    html += '    detectQuickTimeActiveXControl = False';
    html += '    hasQuickTimeChecker = false';
    html += '    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")';
    html += '    If IsObject(hasQuickTimeChecker) Then';
    html += '      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ';
    html += '        detectQuickTimeActiveXControl = True';
    html += '      End If';
    html += '    End If';
    html += '  End If';
    html += 'End Function';

    html += '</scr' + 'ipt>';
    
    vb.innerHTML = html;

}
	
function writeFlashCheck(flashCheckElementId)
{
	var fc 		= document.getElementById(flashCheckElementId);
	var html 	=  "<div id='flashcontent' style='margin:0; text-align:center; color: #000; width: 100%, height: 100%'>";
	html		+= "<div style='{}'>";
	html		+= "<p>Please upgrade your flashplayer! Click <a href='http://www.adobe.com/go/getflashplayer'>here</a>.</p>";	
	html		+= "</div>";
	html		+= "<div>";
	html		+= "<a href='http://www.adobe.com/go/getflashplayer'><img src='/flashmusic/images/flash_icon.jpg' width='60' height='61' border='0' /></a>";
	html		+= "</div>";
	html		+= "</div>";
	html		+= "<script language='javascript'>";
	html		+= "alert(\"beer\");";
	html		+= "</script>";
	alert(html);
	fc.innerHTML = html;
}

function performFlashCheck(flashcheckSwf,flashcheckSwfWidth,flashcheckSwfHeight,afterFlashCheckUrl,afterFlashCheckWindow,flashCheckElementId)
{
	var fc 		 = document.getElementById(flashCheckElementId);
	var swf	     = "'" + flashcheckSwf + "', 'flashcontent', '" + flashcheckSwfWidth + "', '" + flashcheckSwfHeight + "', '9.0.124.0',"; 
	swf		    += "'/flashmusic/swf/expressinstall.swf', {nextUrl:'" + afterFlashCheckUrl + "',nextUrlWindow:'" + afterFlashCheckWindow + "'},";
	swf		    += "{bgcolor: '#FFFFFF', menu: 'true', allowFullscreen: 'false'}, {id: 'flashcheck'}";
	html		 = swfobject.embedSWF(swf);
	alert(swf);
	fc.innerHTML = html;
}
*/

