
var S, D, Sys;
useSVG=true;//you can also change this by hand

function show3D()
{
   new Ajax.Request(url + "?request=show3D",
   {
   		method:'post',
   		Accept: 'text/html',
   		onSuccess: handleShow3D,
   		onFailure: function(){alert('somethind went wrong ...');}
	});
	
}

function handleShow3D( transport )
{
  try{
		var json = eval( '(' + transport.responseText + ')' );	
	    if( json.alert != null )
	    {
	       alert( json.alert);
	       return;
	    }  	
	    else if( json.showRechteck != null )
	    {
	    	showRechteck(json.a, json.b, json.t);
	    }
	    else if( json.showEllipse != null )
	    {
	    	showEllipse(json.ha, json.hb, json.t);
	    }
    }
    catch(e)
    {
    	alert( e );
    }
}

function drawSystem()
{
   Sys = new CoordSys(S, "#000000" );

}

function showRechteck( a,b,t )
{
  var max = Math.max(a,b);
  max = Math.max(max,t);
  
  S.Delete();
  initCAD();
  drawSystem();  
  
  D = new Rechteck(a/max,b/max,t/max, S, "#00800ff", "#0000ff", "#ffffff", 1);
  D.Zoom(0.9);
  
  refresh();

}

function showEllipse( ha, hb, t )
{
 var max = Math.max(ha,hb);
 max = Math.max(max, t);
 max /= 100;
  S.Delete();
  initCAD();
  // drawSystem();
  D = new Circle(ha/max,hb/max,t/max, S, "#00800ff", "#0000ff", "#ffffff", 1);
  // D.Zoom(0.9);
 refresh();
}

function initCAD()
{ if ( true || useSVG)
  { if (! SVGObjects[0])
    { setTimeout("Init()",100);
      return;
    }
    S=new Scene3D(SVGObjects[0],0,500,500);
  }
  else S=new Scene3D(document.getElementById("Scene1"),1);


/*
  C=new Cube(S, "#0080ff", "#0000ff", "#000000", 1);
  C.Zoom(0.9);
  C.Shift(1,-1,-1);
  C.SetId(2);
  C.SetEventAction("click", parent.ClickMsg);

  I=new Icosahedron(S, "#0080ff", "#0000ff", "#000000", 1);
  I.Shift(-1,-1,1);
  I.SetId(3);
  I.SetEventAction("click",parent.ClickMsg);

  D=new Dodecahedron(S, "#0080ff", "#0000ff", "#000000", 1);
  D.SetId(4);
  D.SetEventAction("click", parent.ClickMsg);
*/
}
function refresh()
{
  S.AutoCenter();
  S.Center.Zoom(0.0);  
  S.ZoomAll*=1.4;
  S.ChangeViewer(-15,0);
  S.ChangeLight(-20,-30);
  S.Sort();
  S.Draw();
}
function ClickMsg(evt)
{ var oo=new Array("tetrahedron","octahedron","cube","icosahedron","dodecahedron");
  if (evt) alert("You clicked on the "+oo[evt.target.id]+"."); //SVG
  else alert("You clicked on the "+oo[this.id]+"."); //VML
}
function Rotate()
{ if (! isRotating) return;
  S.ChangeViewer(0,-5);
  S.ChangeLight(0,-5);
  S.Sort();
  S.Draw();
  setTimeout("Rotate()",100);
}
function ChangeViewer(vv)
{ S.ChangeViewer(vv, 0);
  if (! isRotating)
  { S.Sort();
    S.Draw();
  }
}
function ChangeLight(ttheta, ffi)
{ S.ChangeLight(ttheta, ffi);
  if (! isRotating) S.Draw();
}
var viewerzoomed=0;
function ZoomViewer(vv)
{ if ((viewerzoomed+vv>5)||(viewerzoomed+vv<-5)) return;
  viewerzoomed+=vv;
  if (vv>0) S.Dist*=0.8;
  else S.Dist/=0.8;
  if (! isRotating)
  { S.Sort();
    S.Draw();
  }
}
function Shift(hh, vv)
{ S.XM+=hh;
  S.YM+=vv;
  if (! isRotating) S.Draw();
}
var picturezoomed=0;
function ZoomPicture(vv)
{ if ((picturezoomed+vv>5)||(picturezoomed+vv<-5)) return;
  picturezoomed+=vv;
  if (vv>0) S.ZoomAll*=1.1;
  else S.ZoomAll/=1.1;
  if (! isRotating) S.Draw();
}
var isRotating=false;
function StartStop()
{ if (isRotating)
  { isRotating=false;
    document.getElementById("StartStop").value="rotate";
  }
  else
  { isRotating=true;
    document.getElementById("StartStop").value="stop";
    Rotate();
  }
}

