
// ========================
// Initalisation stuff
// ========================

// Are we already in a full window full mode?
var view_mode = "normal";
var full_mode=0;

// Which score ID are we looking at?
// scoreid = param("scoreid") || backup_scoreid;
 scoreid = param("scoreid");

// are we in a store?
 storeid = param("storeid") || -1;

// Set up a default width.
var default_scorch_width = 617;
var default_scorch_height = 873;
var full_scorch_width = default_scorch_width;
var full_scorch_height = default_scorch_height;
var scorch_width = default_scorch_width;
var scorch_height = default_scorch_height;



var version=navigator.appVersion;
var agent=navigator.userAgent.toLowerCase();


// Set a margin and percentage size
var width_percent = 0.95;
var height_percent = 1;
var width_margin = 10;
var height_margin = 10;


// Browser is MSIE
var isIE = (navigator.appName.indexOf("Microsoft") != -1);
// Browser is  Netscape (or Opera, for that matter)
var isNav = (navigator.appName.indexOf("Netscape") != -1);


size_init();

function size_init() {
	// Set width on browsers that'll let us.
	if (isIE) {
		full_width = (document.body.offsetWidth * width_percent) - width_margin;
		full_height = (document.body.offsetHeight * height_percent) - height_margin;
		//alert(full_height);
	} else if (isNav){
		full_width = (window.innerWidth * width_percent) - width_margin;
		full_height = (window.innerHeight * height_percent) - height_margin;
	} else {
		full_width = 800;
		full_height = 600;
	}
	
	
	// Set the height and width to what we want for scorch in this window in this viewMode
	if (view_mode.indexOf('width') != -1) {
		// A Full width mode
		// Set the height, and make them integers.
		var ratio = (full_width / default_scorch_width);
		scorch_height = scorch_height * ratio;
		scorch_width = full_width;
	} 
	if (view_mode.indexOf('height') != -1) {
		// a full height mode
		var ratio = (full_height / default_scorch_height);
		scorch_height = full_height;
		scorch_width = default_scorch_width * ratio;
		
	}
	
	
	if (view_mode.indexOf('split') != -1) {
		scorch_height = full_height;
		if (view_mode.indexOf('full') != -1) {
			// leave a bit of space for the options at the top...
			scorch_height -=100;
		}
	}
	
	
	scorch_width = Math.round(scorch_width);
	scorch_height = Math.round(scorch_height);
	
}

	
function write_scorch(jumpToScorch) {
	// Browser is MSIE
	
	opHack=arguments[1] || 0;
	var tmp = agent.indexOf("opera");
	//alert(tmp);
	
	if ( ((agent.indexOf("opera") == -1) && (agent.indexOf("6") == -1)) && opHack) {
		// don't do anything if this is Opera 6 and we are also passing the opera hack flag
		return 0;
	}
	
	
	var isIE = (navigator.appName.indexOf("Microsoft") != -1);
	// Browser is  Netscape (or Opera, for that matter)
	var isNav = (navigator.appName.indexOf("Netscape") != -1);
	
	var split_params="";
	var split_options="";
	var num_staves_params="";
	var num_staves_options="";
	var split_mode_selected = "normal";
	
	
	var zoom = 100;


    var preferred_version = 6010;
    var min_version = 6010; 
    var IsMac = (navigator.platform.indexOf("MacPPC") != -1);
    
    if (!IsMac) {
       min_version = 6010;
    }

	var scorch = '<object id="ScorchPlugin"' +
	'classid="clsid:A8F2B9BD-A6A0-486A-9744-18920D898429"' +
	'width="' + scorch_width + '"\n' +
	'height="' + scorch_height + '"\n' +
	'codebase="http://www.sibelius.com/download/software/win/ActiveXPlugin.cab#version=6,0,1,2">\n' +
				'<param name="src" value="' + make_sco_path(scoreid) + '">\n' +
				'<param name="type" value="application/x-sibelius-score">\n' +
				'<param name="scorch_minimum_version" value="' + min_version + '">\n' +
				'<param name="scorch_preferred_version" value="' + preferred_version + '">\n' +
				'<param name="scorch_base_url" value="/cgi-bin/">\n' +
				'<param name="scorch_security" value="challengeResponse">\n' +
				'<param name="scorch_command" value="' + command + '">\n' +
				'<param name="scorch_num_copies" value="1">\n' +
				'<param name="scorch_arg_1" value="">\n' +
				'<param name="scorch_arg_2" value="' + scoreid + '">\n' +
				'<param name="scorch_arg_3" value="">\n' +
				'<param name="scorch_arg_4" value="1">\n' +
				'<param name="scorch_arg_5" value="' + command + '">\n' +
				'<param name="scorch_arg_9" value="beta">\n' +
				'<param name="scorch_transposition" value="' + transpose + '">\n' +
				'<param name="scorch_instrument" value="' + top_instrument + '">\n' +
				'<param name="scorch_shrink_limit" value="100">\n' +
				'<param name="ShrinkWindow" value="0">\n' +
				'<param name="AutoZoomPercent" value="' + zoom + '">\n' +
				split_params +
				'<embed src="' + make_sco_path(scoreid) + '"\n' +
   'scorch_base_url="/cgi-bin/"\n' +
   'scorch_security="challengeResponse"\n' +
   'scorch_command="' + command + '"\n' +
   'scorch_num_copies="1"\n' +
   'scorch_arg_1=""\n' +
   'scorch_arg_2="' + scoreid + '"\n' +
   'scorch_arg_3=""\n' +
   'scorch_arg_4="1"\n' +
   'scorch_arg_5="' + command + '"\n' +
   'scorch_transposition="' + transpose + '"\n' +
   'scorch_instrument="' + top_instrument + '"\n' +
   'scorch_minimum_version="' + min_version + '"\n' +
   'scorch_preferred_version="' + preferred_version + '"\n' +
   'ShrinkWindow="0"\n' +
   'scorch_shrink_limit="100"\n' +
   'AutoZoomPercent="' + zoom + '"\n' +
   'ShrinkWindow="0"\n' +
   split_options +

   'width="' + scorch_width + '"\n' +
   'height="' + scorch_height + '"\n' +
   'type="application/x-sibelius-score"\n' +
   'pluginspage="http://www.sibelius.com/cgi/plugin.pl"> </embed>\n' +
			'</object>';

	
//	var txtNode = document.createTextNode(scorch);	

	
//	alert(scorch);
//	if ((agent.indexOf("opera") != -1) && (agent.indexOf("6") != -1)) {
//		if (opHack) {	// Only if we are using Opera6 do we actually write scorch directly into the HTML, 'cos it doesn't understand innerHTML
//			document.write(scorch);
//		}
//	} else {
//		document.getElementById('scorchCell').innerHTML=scorch;
//	}
	
	document.write(scorch);
	
	
	if (jumpToScorch) {
		location.hash="scorchIsHere";
	}
}



// ========================
// Generic functions
// ========================

function make_sco_path(id) {
	var id = id.toString();
	var orig_id = id;
	
	// pad left with zeros
	var zeros_to_add = 3 - (id.length);
	if (id.length < 3) {
		for (var loop=0; loop<zeros_to_add;loop++) {
			id = "0" + id;
		}
	}
	
	var nums = id.split("");
	var path="/sco/";
	for (var loop=0; loop<3;loop++) {
		var this_num=0;
		if (loop < nums.length) {
			this_num = nums[loop]
		}
		path = path + this_num + "/";
	}
	path = path + orig_id + ".sco";	
	return path;
}

function param(name) {
	// get a value from the query string
	var query = location.search;
	query=query.replace(/^\?/,"");
	var pairs = query.split("&");
	for (var i =0; i < pairs.length; i++) {
		var bits = pairs[i].split("=");
		if (bits[0] == name) {
			return unescape(bits[1]);
		}
	}
}

