
function DetectFinale()
{
	// Check for plugin
	if (!detectFinaleViewer(null, false)) 
	{
		//alert("Not detected");
		var div1 = document.getElementById('fvDownload');
		if (div1) div1.style.display = '';

		return;
	}

	//alert("detected");
	var div2 = document.getElementById('fvWrapper');
	if (div2) div2.style.display = '';
}

function ViewerLoadedSmall() 
{	
	//alert("Plugin called ViewerLoadedSmall");
 
	var finaleSWF = "http://www.finalemusic.com/viewer/controls/FinaleViewerS-2.0.swf";
	var finaleSWFID = "FlashViewer";  // ControllerLoaded() must also change if this changes
	var finaleWidth = 440;
	var finaleHeight = 45;
	var host = location.host;
	
	var flashTag = new String();

	flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
	flashTag += 'id="'+finaleSWFID+'" ';
	flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
	flashTag += 'width="'+finaleWidth+'" ';
	flashTag += 'height="'+finaleHeight+'">';
	flashTag += '<param name="movie" value="'+finaleSWF+'"/>';
	flashTag += '<param name="quality" value="high"/>';
	flashTag += '<param name="menu" value="false"/>';
	flashTag += '<param name="bgcolor" value="#ffffff"/>';
	flashTag += '<param name="scale" value="noscale"/>'; 
	flashTag += '<param name="allowScriptAccess" value="always"/>'; 
	flashTag += '<param name="FlashVars" value="remoteAllow='+host+'"/>';
	flashTag += '<embed src="'+finaleSWF+'" ';
	flashTag += 'quality="high" '; 
	flashTag += 'menu="false" '; 
	flashTag += 'bgcolor="#ffffff" ';
	flashTag += 'width="'+finaleWidth+'" ';
	flashTag += 'height="'+finaleHeight+'" ';
	flashTag += 'type="application/x-shockwave-flash" ';
	flashTag += 'scale="noscale" ';  
	flashTag += 'name="'+finaleSWFID+'" ';
	flashTag += 'id="'+finaleSWFID+'" ';
	flashTag += 'allowScriptAccess="always" ';
	flashTag += 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
	flashTag += 'FlashVars="remoteAllow='+host+'">';
	flashTag += '</embed>';
	flashTag += '</object>';
	//alert("flashTag:"+flashTag);

	document.getElementById('interfaceDiv').innerHTML = flashTag.toString();

}


function ViewerLoadedMedium() 
{	
	//alert("Plugin called ViewerLoadedMedium");
 
	var finaleSWF = "http://www.finalemusic.com/viewer/controls/FinaleViewerM-2.0.swf";
	var finaleSWFID = "FlashViewer";  // ControllerLoaded() must also change if this changes
	var finaleWidth = 586;
	var finaleHeight = 60;
	var host = location.host;
	
	var flashTag = new String();

	flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
	flashTag += 'id="'+finaleSWFID+'" ';
	flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
	flashTag += 'width="'+finaleWidth+'" ';
	flashTag += 'height="'+finaleHeight+'">';
	flashTag += '<param name="movie" value="'+finaleSWF+'"/>';
	flashTag += '<param name="quality" value="high"/>';
	flashTag += '<param name="menu" value="false"/>';
	flashTag += '<param name="bgcolor" value="#ffffff"/>';
	flashTag += '<param name="scale" value="noscale"/>'; 
	flashTag += '<param name="allowScriptAccess" value="always"/>'; 
	flashTag += '<param name="FlashVars" value="remoteAllow='+host+'"/>';
	flashTag += '<embed src="'+finaleSWF+'" ';
	flashTag += 'quality="high" '; 
	flashTag += 'menu="false" '; 
	flashTag += 'bgcolor="#ffffff" ';
	flashTag += 'width="'+finaleWidth+'" ';
	flashTag += 'height="'+finaleHeight+'" ';
	flashTag += 'type="application/x-shockwave-flash" ';
	flashTag += 'scale="noscale" ';  
	flashTag += 'name="'+finaleSWFID+'" ';
	flashTag += 'id="'+finaleSWFID+'" ';
	flashTag += 'allowScriptAccess="always" ';
	flashTag += 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
	flashTag += 'FlashVars="remoteAllow='+host+'">';
	flashTag += '</embed>';
	flashTag += '</object>';
	//alert("flashTag:"+flashTag);

	document.getElementById('interfaceDiv').innerHTML = flashTag.toString();

}


function ViewerLoadedLarge() 
{	
	//alert("Plugin called ViewerLoaded");
 
	var finaleSWF = "http://www.finalemusic.com/viewer/controls/FinaleViewerL-2.0.swf";
	var finaleSWFID = "FlashViewer";  // ControllerLoaded() must also change if this changes
	var finaleWidth = 800;
	var finaleHeight = 81;
	var host = location.host;
	
	var flashTag = new String();

	flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
	flashTag += 'id="'+finaleSWFID+'" ';
	flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
	flashTag += 'width="'+finaleWidth+'" ';
	flashTag += 'height="'+finaleHeight+'">';
	flashTag += '<param name="movie" value="'+finaleSWF+'"/>';
	flashTag += '<param name="quality" value="high"/>';
	flashTag += '<param name="menu" value="false"/>';
	flashTag += '<param name="bgcolor" value="#ffffff"/>';
	//flashTag += '<param name="scale" value="noscale"/>'; 
	flashTag += '<param name="allowScriptAccess" value="always"/>'; 
	flashTag += '<param name="FlashVars" value="remoteAllow='+host+'"/>';
	flashTag += '<embed src="'+finaleSWF+'" ';
	flashTag += 'quality="high" '; 
	flashTag += 'menu="false" '; 
	flashTag += 'bgcolor="#ffffff" ';
	flashTag += 'width="'+finaleWidth+'" ';
	flashTag += 'height="'+finaleHeight+'" ';
	flashTag += 'type="application/x-shockwave-flash" ';
	//flashTag += 'scale="noscale" ';  
	flashTag += 'name="'+finaleSWFID+'" ';
	flashTag += 'id="'+finaleSWFID+'" ';
	flashTag += 'allowScriptAccess="always" ';
	flashTag += 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
	flashTag += 'FlashVars="remoteAllow='+host+'">';
	flashTag += '</embed>';
	flashTag += '</object>';
	//alert("flashTag:"+flashTag);

	document.getElementById('interfaceDiv').innerHTML = flashTag.toString();

}

function getFlashMovieObject(movieName)
{
	if (window.document[movieName])
	{
		//alert("Flash Object window.document["+movieName+"]:"+window.document[movieName]);
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) 
	{
		// Not IE
		if (document.embeds && document.embeds[movieName])
		{
			//alert("Flash Object document.embeds["+movieName+"]:"+document.embeds[movieName]);
			return document.embeds[movieName];
		}
	}
	else
	{
		//alert("Flash Object document.getElementById("+movieName+"):"+document.getElementById(movieName));
		return document.getElementById(movieName);
	}
}

function ControllerLoaded()
{
	//alert("Flash (and the plugin) called ControllerLoaded");
	
	// MZ 
	// For Flash 8 ExternalInterface, when we call Actionscript functions
	// inside flash, we need to reference the id of the OBJECT tag that
	// loaded the plugin.  We set that up here.
	flashRef = getFlashMovieObject('FlashViewer');

	//if (navigator.appName.indexOf("Microsoft") != -1) 
	//{
		//flashRef =  window['FlashViewer'];
	//}
	//else
	//{
		//flashRef = document['FlashViewer'];
	//}

	//alert("flashRef:"+flashRef);

	//alert("minMeasure:"+document.finaleViewer.minMeasure+" Measure:"+document.finaleViewer.Measure+" maxMeasure:"+document.finaleViewer.maxMeasure);
	//alert("setup Volume:"+document.finaleViewer.Volume);
	flashRef.setup(document.finaleViewer.Volume,		// Volume
		100,					// Tempo
		document.finaleViewer.minMeasure,	// Minimum Measure
		document.finaleViewer.maxMeasure,	// Maximum Measure
		document.finaleViewer.Measure		// Measure
	);
	//alert("after ControllerLoaded");
}

function StopPlayBack()
{
	if (!document.finaleViewer)
		return;

	if (document.finaleViewer.isPlaying == 1)
		document.finaleViewer.PlayPause();
} 

// Call from plugin to Flash every few tenths of second
// to update measure count and play/stop button
function PlayTick()
{
	//alert("PlayTick isPlaying:"+document.finaleViewer.isPlaying+" Measure:"+document.finaleViewer.Measure);
	flashRef.playTick(document.finaleViewer.isPlaying,	// Is Playing?
		document.finaleViewer.Measure		// Measure
	);	
}

function FitWidth()
{
	//alert("Flash called FitWidth");
	document.finaleViewer.SetViewAsPage(true);
}

function FitHeight()
{
	//alert("Flash called FitHeight");
	document.finaleViewer.SetViewAsPage(false);
}

// Call from Flash to the plugin every time the play/stop button is pressed
function PlayPause()
{
	document.finaleViewer.PlayPause();	
}

function ChangeKey()
{
	//alert("Flash called ChangeKey");
	document.finaleViewer.ChangeKey();
}

function AboutBox()
{
	//alert("Flash called AboutBox");
	document.finaleViewer.AboutBox();
}

function PrintView()
{
	//alert("Flash called PrintView");
	document.finaleViewer.PrintView('TEST', true);
	//SetFlashFocus()
	//setTimeout('SetFlashFocus()', 2000);
}

//function SetFlashFocus()
//{
	// MZ This was an attempt to correct an issue on Mac where
	// after you do a print or change key, you have to click
	// in flash twice to get a button to work.  The first click
	// was just giving back focus.  So the setFlashFocus()
	// call into Flash just sets the focus on the print button,
	// but does not get rid of the extra click.  And on the Win
	// side (which does not have this issue), setting the focus
	// also causes it to click the button again, and again, sometimes???
	//alert("Now call Flash to set focus");
	//flashRef.setFlashFocus();
	// This is meant to make sure that the whole Flash control has the
	// focus of the browser.  It says it only works on Windows, where I don't
	// need it.
	//alert("focus:"+window.document.FlashViewer);
	//window.document.FlashViewer.focus();
//}

function SetVolume(val)
{
	//alert("Flash called SetVolume:"+val);
	document.finaleViewer.Volume = val;
}

function SetTempo(val)
{
	//alert("Flash called SetTempo:"+val);
	document.finaleViewer.Tempo = val;
}

function SetMeasure(val)
{
	//alert("Flash called SetMeasure = "+val);
	document.finaleViewer.Measure = val;
}

function LoadSettings()
{
	//alert('LoadSettings');
	//alert(document.finaleViewer.action);
	var productCode = getParam("itemId");
	//alert("ProductCode:"+productCode);
	var cookieval = readCookie("productSettings_" + productCode);
	//alert(cookieval);

	if (cookieval != null)
	{
		var start = cookieval.indexOf(",");
		var soloIndex = cookieval.substr(1, start-1);
		cookieval = cookieval.substr(start+1);

		start = cookieval.indexOf(",");
		var octaveIndex = cookieval.substr(1, start-1);
		cookieval = cookieval.substr(start+1);	
		
		var transposeIndex = cookieval.substr(1, cookieval.length);
		
		//alert("Call plugin SetChangeKey("+soloIndex+","+octaveIndex+","+transposeIndex+")");
		document.finaleViewer.SetChangeKey(parseInt(soloIndex), parseInt(octaveIndex), parseInt(transposeIndex));
	}
}

function SaveSettings()
{
	//alert("SaveSettings");
	var productCode = getParam("itemId");
	//alert('productCode:'+productCode);
	var cookieval = "S" + document.finaleViewer.soloIndex;
	cookieval = cookieval + ",O" + document.finaleViewer.octaveIndex;
	cookieval = cookieval + ",T" + document.finaleViewer.transposeIndex;
	//alert("cookieval:"+cookieval);
	createCookie("productSettings_" + productCode, cookieval, 30);
}

// This gets called when the plugin finishes printing
function PrintAllCompleted()
{
	if (document.finaleViewer.PrintQtyAvail > 0)
	{
		alert("You have "+document.finaleViewer.PrintQtyAvail+" print(s) still available.");
	}
	else
	{
		alert("You have printed all the copies you have purchased.");
		if (document.finaleViewer.redirecturl)
			window.location.replace(document.finaleViewer.redirecturl);
	}
}

// QUERY STRING

function getParam(name)
{
  var start=location.search.indexOf("?"+name+"=");
  if (start<0) start=location.search.indexOf("&"+name+"=");
  if (start<0) return '';
  start += name.length+2;
  var end=location.search.indexOf("&",start)-1;
  if (end<0) end=location.search.length;
  var result=location.search.substring(start,end);
  var result='';
  for(var i=start;i<=end;i++) {
    var c=location.search.charAt(i);
    result=result+(c=='+'?' ':c);
  }
  return unescape(result);
}

// COOKIE MANAGEMENT

function createCookie(name,value,days)
{
	if (days)
	{
	  var date = new Date();
	  date.setTime(date.getTime()+(days*24*60*60*1000));
	  var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	//alert("Before setting cookie to:"+name+"="+value+expires+"; path=/");
	document.cookie = name+"="+value+expires+"; path=/";
	//alert("document.cookie:"+document.cookie);
	//alert("Read cookie:"+readCookie("productSettings_R123"));
}

function readCookie(name)
{
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++)
 {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}

function eraseCookie(name)
{
 createCookie(name,"",-1);
}

// VIEWPORT

function getViewPortWidth()
{
	return (window.innerWidth || document.body.clientWidth || documemt.documentElement.offsetWidth || 0);
}

function getViewPortHeight()
{
	return (window.innerHeight || document.body.clientHeight || documemt.documentElement.offsetHeight || 0);
}


// PLUGIN DETECTION
//
// Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
// initialize global variables
var detectableWithVB = false;
var pluginFound = false;


function goURL(daURL) {
    // if the browser can do it, use replace to preserve back button
    window.location.replace(daURL);
    return;
}

function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    // check for redirection
    if( redirectURL && ((pluginFound && redirectIfFound) || 
	(!pluginFound && !redirectIfFound)) ) {
	// go away
	goURL(redirectURL);
	return pluginFound;
    } else {
	// stay here and return result of plugin detection
	return pluginFound;
    }	
}

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
    } else {
	return false;
    }
}

function detectFinaleViewer(redirectURL, redirectIfFound) { 
    pluginFound = detectPlugin('Finale Viewer'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound)
    {
        pluginFound = detectGeckoActiveXControl('FINALEVIEWER.FinaleViewerCtrl.1');
    }
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('FINALEVIEWER.FinaleViewerCtrl.1');
    }
    // check for redirection
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectGeckoActiveXControl(controlName)
{
    try
    {
        var o = new GeckoActiveXObject('FINALEVIEWER.FinaleViewerCtrl.1');
	if (o)
            return true;
    }
    catch (e) { }

    return false;
}

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;
		// for each plugin...
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
			// loop through all desired names and check each against the current plugin name
			var numFound = 0;
			for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
				// if desired plugin name is found in either plugin name or description
				//alert("name:"+navigator.plugins[pluginsArrayCounter].name);
				//alert("desc:"+navigator.plugins[pluginsArrayCounter].description);
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
					(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
					// this name was found
					numFound++;
				}   
			}
			// now that we have checked all the required names against this one plugin,
			// if the number we found matches the total number provided then we were successful
			if(numFound == daPlugins.length) {
				pluginFound = true;
				// if we've found the plugin, we can stop looking through at the rest of the plugins
				break;
			}
		}
    }
	//alert("Pluginfound:"+pluginFound);
    return pluginFound;
} // detectPlugin


// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    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>');
}

