﻿/**
 * @(#)7x_cubeplayer.js  1.0  2008-08-27
 *
 * Copyright (c) 2008-2009 
 * Werner & Walter Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Werner Randelshofer. For details see accompanying license terms. 
 */
 
 
 
// Variables
// ---------

function insertCubePlayer(cube, script, id) {
	// Cube-specific attributes and parameters
	var cubeSpecific = {
		"vcube7" : {
			"attributes" : {
				"code":"Cube7Player.class", 
				"archive":"lib/Cube7Player.jar" 
			},
			"parameters" : {
				"resourceFile":"../notations/superset_eng_7x7.xml",
				"faceList":"5,0,1,2,3,4"
			}
		},
		
		
		// Geo Cubes
		
		
		// Clock Cubes
		
		
		// Clock & Calendar Cubes
		
		
		// Orbit Cubes
		"7x_orbit_cube" : {
			"attributes" : {
				"code":"Cube7Player.class", 
				"archive":"lib/Cube7Player.jar", 
				"width":"700",
				"height":"580"
			},
			"parameters" : {
				"resourceFile":"../notations/superset_eng_7x7.xml",
				"stickersImage":"images/7x_OrbitCube_2048.gif",
				"scaleFactor":"1.05",
				"twistDuration":"400",
				"displayLines":"3",
				"showRear":"true",
				"showInfo":"false"
			}
		},
		
		
		// Games
		"7x_ninemensmorris" : {
			"attributes" : {
				"code":"Cube7Player.class", 
				"archive":"lib/Cube7Player.jar", 
				"width":"470",
				"height":"360"
			},
			"parameters" : {
				"resourceFile":"../notations/superset_eng_7x7.xml",
				"stickersImage":"images/7x_NineMensMorrisCube_1024.gif",
				"beta":"25",
				"scaleFactor":"1.1",
				"twistDuration":"400",
				"displayLines":"6",
				"showRear":"true",
				"showInfo":"false"
			}
		}
		
		
		
		
		
	};


	// Applet attributes
	var appletAttributes = {
		"codebase" : "..",
		"width" : "700",
		"height" : "460"
	}
	// Set cube-specific applet attributes
	for (var key in cubeSpecific[cube]["attributes"]) {
		appletAttributes[key] = cubeSpecific[cube]["attributes"][key];
	}
	// Set id if defined
	if (id != null) {
		appletAttributes["id"] = id;
	}
	
	
	// Applet parameters
	var appletParameters = {
		"showInfo" : "true",    
		"showRear" : "true",     
		"twistDuration" : "550",     
	
		// Parameters for Sun Java Plugin:
		"codebase_lookup" : "false",   
		"classloader_cache" : "false",    
		"java_arguments" : "-Djnlp.packEnabled=true",     
		"image" : "Splash.gif",     
		"centerimage" : "true",     
		"boxborder" : "false"     
	}; 
	// Set cube-specific applet parameters
	for (var key in cubeSpecific[cube]["parameters"]) {
		appletParameters[key] = cubeSpecific[cube]["parameters"][key];
	}
	
	
	appletParameters["script"] = toSanitizedScript(script);

	// Write applet
	// ------------
	document.write("<applet ");
	for (var key in appletAttributes) {
		document.write(" "+key+"=\""+appletAttributes[key]+"\"");
	}
	document.writeln(">");
	for (var name in appletParameters) {
		document.write("  <param name=\""+name+"\" value=\"" + appletParameters[name] + "\">");
	}
	document.writeln("<p><b>To see the cube applet you have to install the <a href=\"http://java.sun.com/getjava/\" target=\"_blank\">Java Plug-in</a>!</b></p>");
	document.write("</applet>");
}

function toSanitizedScript(script) {
	// Sanitize the script parameter.
	sanitizedScript = "";
	if(script != null) { sanitizedScript = script; }
	
	sanitizedScript = sanitizedScript.replace(/\"/g,"\\\"");  // replaces " with \"
	sanitizedScript = sanitizedScript.replace(/\./g," ·");  // replaces . with a blank followed by ·
	sanitizedScript = sanitizedScript.replace(/([RUFLDB]2?|\))-/g,"$1\'");    // replaces - with ' if it appears after RUFLDB a 2 or )
	sanitizedScript = sanitizedScript.replace(/(([CNMSTVW][1-9]*(-[1-9]+)?)?[RUFLDB]'?)/g," $1");     // inserts a blank before a statement. 
	// A statement optionally starts with one of the letters CMNSTVW, optionally followed by the number 1, 
	// and always contains one of the letters RUFLDB, and optionally finishes with an apostrophe
	sanitizedScript = sanitizedScript.replace(/(?:(\(|<))/g," $1");     // inserts a whitespace before an opening brackets: ( <
	sanitizedScript = sanitizedScript.replace(/(?:(\(|<) )/g,"$1");     // removes whitespace after opening brackets: ( <
	sanitizedScript = sanitizedScript.replace(/(?:(>) )/g,"$1");     // removes whitespace after closing bracket: >
	sanitizedScript = sanitizedScript.replace(/  /g," ");     // removes duplicate whitespaces
	sanitizedScript = sanitizedScript.replace(/^ +/g,"");     // removes leading whitespace
	return sanitizedScript;
}
