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

// Are we already in a full window full mode?
var view_mode = param("viewMode") || "normal";
var full_mode=0;
if (view_mode.indexOf("full") != -1) {
	full_mode=1;
}

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

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

// Set up a default width.
var default_scorch_width = 637;
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);
	
}




// ========================
// functions for doing stuff
// ========================

// en/disable the number of systems option in the score view table
function redraw_table() {
	
	var split_mode_selected = "normal";
	if (document.getElementById('viewMode').selectedIndex > -1) {
		split_mode_selected = document.getElementById('viewMode').options[document.getElementById('viewMode').selectedIndex].value || "normal";
	}

	if (split_mode_selected.indexOf("split") != -1) {
		document.getElementById('numberOfSystems').style.visibility="visible";
		document.getElementById('numberOfSystemsText').style.visibility="visible";
		
		if (document.all) {
			document.getElementById('zoomrow').style.display="inline";
		} else {
			document.getElementById('zoomrow').style.display="table-row";
		}
		
	} else {
		document.getElementById('zoomrow').style.display="none";
		document.getElementById('numberOfSystems').style.visibility="hidden";
		// document.getElementById('numberOfSystemsText').style.visibility="hidden";
	}
}
	
	

function update_score() {
	// Change the way that the score is displayed on this page
	// this might mean refreshing the page completely or just recreating one table cell.
	// time passes..
	// actually it turns out that Scorch can get a bit upset if we don't reload the
	// entire page, so we will reload everything
	
	var agent=navigator.userAgent.toLowerCase();
	if ((agent.indexOf("opera") != -1) && (agent.indexOf("6") != -1))	{
		alert("Sorry - this feature is not available in Opera 6");
		return 0;
	}
	
	var split_mode_selected = document.getElementById('viewMode').options[document.getElementById('viewMode').selectedIndex].value;
	var zoom = document.getElementById('zoom').options[document.getElementById('zoom').selectedIndex].value;
	//if ((split_mode_selected.indexOf("full") != -1) || full_mode) {
		// load a different HTML page
		var systems="";
		if (split_mode_selected.indexOf("split") != -1) {
			systems = "&systems=" + document.getElementById('numberOfSystems').value;
		}

		window.location="index.php?sm=home.score&scoreID=" + scoreid + "&storeid=-1&z=" + zoom + "&viewMode=" + split_mode_selected + systems + "#scorchIsHere";
	//} else {
	//	view_mode = split_mode_selected;
	//	size_init();
	//	write_scorch(1);
	//}
}

	
function write_scorch(jumpToScorch) {
	// alert("Writing Scorch");
	// 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";
//	if (document.getElementById('viewMode').selectedIndex > -1) {
//		split_mode_selected = document.getElementById('viewMode').options[document.getElementById('viewMode').selectedIndex].value || "normal";
//	}
	
	
//	var zoom = 100;
//	if (document.getElementById('zoom').selectedIndex > -1) {
//		zoom = document.getElementById('zoom').options[document.getElementById('zoom').selectedIndex].value;
//	}
//	
//	if (split_mode_selected.indexOf("split") != -1) {
//		split_params=	'<param name="SplitView" value="1">' +
//						'<param name="SplitPlayback" value="1">' + 
//						'<param name="SplitNumSystems" value="' + document.getElementById('numberOfSystems').value + '">\n';
//		split_options=	'SplitView="1"' +
//						'SplitPlayback="1"' +
//						'SplitNumSystems="' + document.getElementById('numberOfSystems').value + '"\n';
//	}

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

	// alert("To Print ID = "+toprintid);

	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="http://www.sibeliusmusic.com/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="' + toprintid + '">\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="' + toprintid + '"\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>';

	// alert(make_sco_path(scoreid));
//	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="http://www.sibeliusmusic.com/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";
	// alert(path);
	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]);
		}
	}
}

