try{
GeometryEditor.defineCommand( "Debug", [__Debug] );
GeometryEditor.defineCommand( "Example", [__Example] );
GeometryEditor.defineCommand( "ObjectsInBFS", [_ObjectInBFS] );
GeometryEditor.defineCommand( "SelectedObjectsInBFS", [_SelectedObjectInBFS] );
GeometryEditor.defineCommand( "Save Macro", [_SaveMacro] );
GeometryEditor.defineCommand( "Load Macro", [_LoadMacro] );
GeometryEditor.defineCommand( "Debug Macro", [_DebugMacro] );
GeometryEditor.defineCommand( "Save Data", [_SaveData] );
GeometryEditor.defineCommand( "Load Data Value", [_LoadDataValue] );
GeometryEditor.defineCommand( "Load Data URL", [_LoadDataURL] );
GeometryEditor.defineCommand( "AllSelected", [_AllSelected] );
GeometryEditor.defineCommand( "EquivSelect", [_EquivSelect] );
GeometryEditor.defineCommand( "ExpressionTest", [_ExpressionTest] );
GeometryEditor.defineCommand( "TypeLists", [_TypeLists] );
GeometryEditor.defineCommand( "ShowTypesSet", [_ShowTypesSet] );
GeometryEditor.defineCommand( "ObjectDetail", [_ObjectDetail] );
GeometryEditor.defineCommand( "TestDFS", [_TestDFS] );

}catch(e)
{alert( e.message + " " + e.name + " " + e.lineNumber + " " + e.fileName + " " + e.stack );}

var GeometryEditor_Macros = {
	 "Circle through three points":[
		{"CN":0,"OT2":"Point","isGiven":1},
		{"CN":1,"OT2":"Point","isGiven":1},
		{"CN":2,"OT2":"Point","isGiven":1},
		{"CN":3,"OT2":"LineWithTwoPoints","OT3":"Segment","visible":false,"nP":2,"ps":[0,1]},
		{"CN":4,"OT2":"LineWithTwoPoints","OT3":"Segment","visible":false,"nP":2,"ps":[1,2]},
		{"CN":5,"OT2":"MidPoint","visible":false,"nP":1,"ps":[3]},
		{"CN":6,"OT2":"PerpendicularLine","OT3":"Line","visible":false,"nP":2,"ps":[5,3]},
		{"CN":7,"OT2":"MidPoint","visible":false,"nP":1,"ps":[4]},
		{"CN":8,"OT2":"PerpendicularLine","OT3":"Line","visible":false,"nP":2,"ps":[7,4]},
		{"CN":9,"OT2":"ILL","nP":2,"ps":[8,6]},
		{"CN":10,"OT2":"IntersectLineLine","nP":1,"ps":[9]},
		{"CN":11,"OT2":"CircleWithCenterAndOnePoint","nP":2,"ps":[10,1]}],

	"Drawing a parallelogram":[
		{"CN":0,"OT2":"Point","isGiven":1},
		{"CN":1,"OT2":"Point","isGiven":1},
		{"CN":2,"OT2":"Point","isGiven":1},
		{"CN":3,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[0,1]},
		{"CN":4,"OT2":"ParallelogramPoint","nP":3,"ps":[0,1,2]},
		{"CN":5,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[0,4]},
		{"CN":6,"OT2":"BasicPolygon","nP":4,"ps":[0,1,2,4]},
		{"CN":7,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[1,2]},
		{"CN":8,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[4,2]},
		{"CN":9,"OT2":"Expr","OT3":"Calculation","psInfoStr":"Areable","nP":1,"ps":[6],"expression":"area([0])"}],
	
	"One intersection of two circles":[
		{"CN":0,"OT2":"Circle","isGiven":1},
		{"CN":1,"OT2":"Circle","isGiven":1},
		{"CN":2,"OT2":"ICC","nP":2,"ps":[1,0]},
		{"CN":3,"OT2":"IntersectCircleCircle","nP":1,"nE":1,"ps":[2],"whichOneInPair":0}],
	"Nested Triangles":[
		{"CN":0,"OT2":"Point","isGiven":1},
		{"CN":1,"OT2":"Point","isGiven":1},
		{"CN":2,"OT2":"Point","isGiven":1},
		{"CN":3,"OT2":"ScalarAuto","isGiven":2,"nE":1,"defVal":3},
		{"CN":4,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[0,1]},
		{"CN":5,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[2,0]},
		{"CN":6,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[1,2]},
		{"CN":7,"OT2":"MidPoint","nP":1,"ps":[4]},
		{"CN":8,"OT2":"MidPoint","nP":1,"ps":[5]},
		{"CN":9,"OT2":"MidPoint","nP":1,"ps":[6]},
		{"CN":10,"OT2":"Recursion","nP":10,"nE":4,"ps":[3,0,1,2,4,5,6,7,8,9],"nSrcObjs":3,"nInitObjs":6,"mappings":[5,4,3],"fullOrbit":true},
		{"CN":11,"OT2":"RecursiveObject","nP":2,"ps":[6,10]},
		{"CN":12,"OT2":"RecursiveObject","nP":2,"ps":[7,10]},
		{"CN":13,"OT2":"RecursiveObject","nP":2,"ps":[8,10]},
		{"CN":14,"OT2":"RecursiveObject","nP":2,"ps":[9,10]},
		{"CN":15,"OT2":"RecursiveObject","nP":2,"ps":[4,10]},
		{"CN":16,"OT2":"RecursiveObject","nP":2,"ps":[5,10]}],
	"Recursion":[
			{"CN":0,"OT2":"Point","isGiven":1},
			{"CN":1,"OT2":"Point","isGiven":1},
			{"CN":2,"OT2":"ScalarAuto","isGiven":2,"nE":1,"defVal":3},
			{"CN":3,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[0,1]},
			{"CN":4,"OT2":"MidPoint","nP":1,"ps":[3]},
			{"CN":5,"OT2":"Recursion","nP":4,"nE":4,"ps":[2,0,3,4],"nSrcObjs":1,"nInitObjs":2,"mappings":[1],"fullOrbit":true},
			{"CN":6,"OT2":"RecursiveObject","nP":2,"ps":[3,5]},
			{"CN":7,"OT2":"RecursiveObject","nP":2,"ps":[4,5]}],
	"SegmentLength":[
		{"CN":0,"OT2":"Lengthable","isGiven":1},
		{"CN":1,"OT2":"Expr","OT3":"Calculation","psInfoStr":"Lengthable","nP":1,"ps":[0],"expression":"len([0])"}],
	"SynCopy":[
		{"CN":0,"OT2":"Point","isGiven":1},
		{"CN":1,"OT2":"Point","isGiven":1},
		{"CN":2,"OT2":"Point","isGiven":1},
		{"CN":3,"OT2":"ScalarAuto","isGiven":2,"nE":1,"defVal":1},
		{"CN":4,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[0,1]},
		{"CN":5,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[2,0]},
		{"CN":6,"OT2":"BasicPolygon","nP":3,"ps":[0,1,2]},
		{"CN":7,"OT2":"SynCopy","nP":4,"nE":5,"ps":[3,0,2,1],"cosRotateAngle":0.8485397266147896,"sinRotateAngle":-0.5291316777102824,"nPoints":3,"pivotPreExist":1,"pivotIdx":0},
		{"CN":8,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[1,2]},
		{"CN":9,"OT2":"CopyPoint","nP":1,"nE":2,"ps":[7],"rotateAroundPivotAllowed":"1","copyPointIdx":"1"},
		{"CN":10,"OT2":"CopyPoint","nP":1,"nE":2,"ps":[7],"rotateAroundPivotAllowed":"1","copyPointIdx":"2"},
		{"CN":11,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[10,9]},
		{"CN":12,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[9,0]},
		{"CN":13,"OT2":"BasicPolygon","nP":3,"ps":[0,10,9]},
		{"CN":14,"OT2":"LineWithTwoPoints","OT3":"Segment","nP":2,"ps":[0,10]}]
};
function __Debug()
{
	var t = this.editorWin;
	t._debug();
}
function _TestDFS()
{
	var t = this.editorWin;
	t.gUtil.DFS( t.objectArray, 0, t.numOfObjects-1, t.objectsInOrder, 0 );
}
function _ObjectDetail()
{
//__debug( editor.editorWin.gOM.objectOT2Managers["IntersectCircleCircle"].nObjs );
	var tmp = prompt( "Please enter the gIdx:" );
	if ( !tmp || tmp == "" )
		return;
	var gOM = this.editorWin.gOM;
	var t = gOM.getObjByGIdx(parseInt(tmp));
	__debug( "seletable: " + t.selectable + "\n finalVisible: " + t.finalVisible + 
			"\n primitive: " + t.primitive + " flag: " + t.flag.toString(16) + 
			" \nlabelSet: " + t.labelSet + " label: " + t.label + " " + "sLabel: " + t.sLabel + 
			(t.el?("\n"+t.el.getAttributeNS(null,"style")):"") );
	
}
function _ShowTypesSet()
{
	var gEL = this.editorWin.gEL, i, j;
	__debug( gEL.nTypes );
	for ( i=0; i<gEL.types.length; i++ )
	{
		__debug( " [" );
		for ( j=0; j<gEL.nTypes[i]; j++ )
			__debug( gEL.types[i][j].name + "," );
		__debug( "]" );
	}
}
function _TypeLists()
{
	var arr = this.editorWin.ParentInfo.arr
	for ( var i in arr )
		if ( arr[i].onOrder>=0 )
			__debug( i + ": " + arr[i].nObjs + " [" + arr[i].objs + "]\n" );
}
function _ExpressionTest()
{
	var tmp = prompt( "Please enter the expression:" );
	if ( !tmp || tmp == "" )
		return;
	var gEP = this.editorWin.gEP;
	gEP.setVarAllowed( true );
	gEP.addVariable( "x" );
	gEP.setExpr( tmp );
try{
	gEP.findTokens();
	__debug( "nTokens: " + gEP.nTokens + " Tokens: [" + gEP.tokens + "]\n" );

	gEP.infixToPostfix();
	__debug( "nPTokens: " + gEP.nPTokens + " pTokens: [" + gEP.pTokens + "]\n" );
	
	gEP.evaluate();
	__debug( "value: " + gEP.result.toString1() + "\n" );

//gEP.postfixToMathML( document );

}catch(e)
{alert( "Error " + e  
+ "\n" + Util.err(e));
}
}

function _ObjectInBFS()
{
try{
	var objectsInOrder = this.editorWin.objectsInOrder, numOfObjects = this.editorWin.objectsInOrder.numOfObjects;
	for ( var i=0; i<numOfObjects; i++ )
		__debug( objectsInOrder[i].OT2 + " " + objectsInOrder[i].gIdx + " " + objectsInOrder[i].d + " " + objectsInOrder[i].group + " " + objectsInOrder[i].x + " " + objectsInOrder[i].y );
}catch(e)
{Util.err(e);}
}
	
function _SelectedObjectInBFS()
{
try{
	for ( var i=0; i<tmp.length; i++ )
		alert( tmp[i].OT2 + " " + tmp[i].gIdx + " " + tmp[i].d + " " + tmp[i].group );
}catch(e)
{Util.err(e);}		
}


function allStatusStackInfo()
{
try{
	var StatusSTACK = this.editorWin.StatusSTACK;
	for ( var k=0; k<StatusSTACK.size; k++ )
		__debug( StatusSTACK.stack[k] );
}catch(e)
{Util.err(e);}
}

function _SaveData()
{
try{
	__debug( this.editorWin.constructDataStr().replace( '{"CN":', '\n{"CN":', "gi" ) );
}catch(e)
{Util.err(e);}
}

function _LoadDataValue()
{
try{
    this.updateTool({
        data: document.getElementById( "debugg" ).value.replace( '\n', '', "gi" )
    });
}catch(e)
{Util.err(e);}
}
function _LoadDataURL()
{
try{
	var tmp = prompt( "Please enter the URL of the manipulative:" );
	if ( !tmp || tmp == "" )
		return;
    this.updateTool({
        url: tmp
    });
}catch(e)
{Util.err(e);}
}

function _AllSelected()
{
try{
	var tmp = document.getElementById("info2"), tmp2;
	tmp.value = " status: " + gEL.status + " " ;
	tmp.value += gOM.getNSels() + "\n";
	for ( var i=0; i<gOM.getNSels(); i++ )
	{
		tmp2 = gOM.sels[i];
		tmp.value += tmp2.OT2 + " " + tmp2.OT3 + " " + tmp2.gIdx + "\n";
	}	
}catch(e)
{Util.err(e);}
}

function _EquivSelect()
{
	var gOM=this.editorWin.gOM, gSL=this.editorWin.gSL, t;
	gSL.findEquivalentSelection( gOM.sels, gOM.getNSels() );
	for ( i=0; i<gSL.nUpdates; i++ )
	{
		t = gSL.updates[i];
		__debug( t.gIdx + " " + t.OT2 + " UT1=" + t.UT1 + " UT2=" + t.UT2 + " UT3=" + t.UT3 + " UT4=" + t.UT4 + " updType=" + t.updType + " pan=" + t.pan );
		if ( t.OT2 == "PlotPoint" )
			__debug( " panX=" + t.panX + " recalX=" + t.recalX + " panY=" + t.panY + " recalY=" + t.recalY );
		__debug( "\n" );
	}
}

function _SaveMacro()
{
try{
	var tmp = prompt( "which macro?" );
	if ( !tmp || tmp=="" || isNaN(parseInt(tmp)) )
		return;
	__debug( this.getMacroByIdx(parseInt(tmp)).replace( '{"CN":', '\n{"CN":', "gi" ) );
}catch(e)
{Util.err(e);}
}

function _LoadMacro()
{
try{
	this.addMacro( null, document.getElementById("debugg").value );
}catch(e)
{Util.err(e);}
}

function _DebugMacro()
{
try{
	var gML = this.editorWin.gML;
	var s = prompt( "which macro?" );
	if ( !s || s=="" || isNaN(parseInt(s)) )
		return;
	var obj = this.editorWin.gML.macros[parseInt(s)+lxG.nPreMacros];
	
	var k, tmp="Givens:\n";
	for ( k=0; k<obj.nGivens; k++ )
  		tmp+=k + " " + obj.givens[k].CN + " " + obj.givens[k].OT2 + " " + obj.givens[k].nC + " " + obj.givens[k].group + " " + obj.givens[k].lastAncestor.CN + " " + obj.givens[k].nP + "\n";
	tmp += "\n";
	__debug( tmp );
	
	tmp="Involved:\n";
	for ( k=0; k<obj.nInvolved; k++ )
  		tmp+=k + " " + obj.involved[k].CN + " " + obj.involved[k].OT2 + " " + obj.involved[k].nC + " " + obj.involved[k].group + " " + obj.involved[k].lastAncestor.CN + " " + obj.involved[k].nP + "\n";
	tmp += "\n";
	__debug( tmp );
}catch(e)
{Util.err(e);}
}
Util = {};
Util.err = function(e, t)
{
	var t1 = (t==null)?"":t;
	alert( t1 + " " + e.message + " " + e.name + " " + e.lineNumber + " " + e.fileName + " " + e.stack );
}

function __Example()
{
	var name = "";
	while ( name == "" )
	{
		name = prompt( "\nPlease enter the name of the new manipulative", "" );
		if ( !name )
			return;
	}
	_Examples( name, this );
}
function _Examples( whichExample, editor )
{
	var dataObj;
try{

	if ( whichExample == "Trapezoid" )
	{}	
	else
	{
		return;
	}
	editor.dataValue = dataObj;
	editor.update();
}catch(e)
{alert( e.message + " " + e.name + " " + e.lineNumber + " " + e.fileName + " " + e.stack );}	
}
function __debug(str)
{
	document.getElementById('debugg').value += str;
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           8