// JScript source code
var wb=null;		      // Main workbench object
var edApplet = null;    // Engg Data applet object
var eds = null;         // Engg Data script engine
var eda = null;         // Engg Data namespace object (main.js)

function edPropGraphScope ()
{
   this.timeInterval = 0;

   this.GraphCtrl = null;

}
var edpg = new edPropGraphScope();

function window_unload()
{
	if (edApplet && edApplet.GUIState) {
		edApplet.GUIState.GraphMode = 0;
		edApplet.GUIState.UpdateEditToolbar();
	}
   eda.PropGraphScript = null;
   edpg.window = null;
   edpg.document = null;
}

function window_onload()
{
//debugger;
   // Go get the WB, Engg Data Applet; Script; and namespace
	wb			=  window.external;
	edApplet =  wb.AppletList.Applet( "EDApplet").App;
	if (edApplet.Script.eda.ProjectViewObj.EnggData == "kProjMaterial")
		edApplet.GUIState.GraphMode = -1;
	edApplet.GUIState.UpdateEditToolbar();
	eds = edApplet.Script;
   eda = eds.eda;

   eda.OverloadDefaultEvents(document);

   eda.PropGraphScript = edpg;
   
   edpg.window = window;
   edpg.document = document;
   // This determines Internet Explorer's major version number,
   // or 0 for others. It works by finding the "MSIE " string and
   // extracting the version number following the space, up to the decimal
   // point, ignoring the minor version number
   var ieVersion = 0;
   var ua = window.navigator.userAgent;
   var msie = ua.indexOf ( "MSIE " );

   if ( msie > 0 )      // The version for Internet Explorer is available
      ieVersion = parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
      
   if(ieVersion < 6)
   {
      edpg.timeInterval = window.setInterval("edpg.PageLoaded()", 250);
	}
	else
	{
	   edpg.PageLoaded();
	}

}

function graphsummary_onload()
{
   try
   {
	   wb			=  window.external;
	   edApplet =  wb.AppletList.Applet( "EDApplet").App;
	   eds = edApplet.Script;
	   eds.GraphSummaryActive = true;
   }
   catch(error)
   {
   }
}
function graphsummary_onunload()
{
   try
   {
      wb = null;
	   edApplet =  null;
	   eds = null;
	   eds.GraphSummaryActive = false;
   }
   catch(error)
   {
   }
}

edPropGraphScope.prototype.PageLoaded = function ()
{
	try
	{
		if( !isNaN(edpg.timeInterval) )
			window.clearInterval(edpg.timeInterval);
		edpg.timeInterval = 0;
      if ( EDAPropertyGraph ) {
         // The page is loaded so initialize all of the information
         edpg.GraphCtrl = EDA_GRAPH;
         edpg.Init();
         
        edpg.GetGraphData( );
        
        try
        {
            var ds = getAppletInMemory("DSApplet");
            ds.Script.ReportCallbackEDPageLoaded("PropertyGraph", document);
        }
        catch(e)
        {
        }   
      }
	}
	catch (e)
	{
//debugger;
		if( !isNaN(edpg.timeInterval))
			window.clearInterval(edpg.timeInterval);
		edpg.timeInterval = window.setInterval("edpg.PageLoaded()", 250);
	}
}

edPropGraphScope.prototype.Init = function ()
{
	try
	{
	   this.GraphCtrl.Datasets.Clear();
	   this.GraphCtrl.BackColor = wb.GetWindowColor();
	   this.GraphCtrl.Legend.Visible = false;
	   this.GraphCtrl.TitleClear();	//clear the graph titles
	   this.GraphCtrl.Type = 4;		//2d line graph
	   this.GraphCtrl.Line2DType = 2;	//line with points
       this.GraphCtrl.ZoomPanCtrlEnable = false;
      this.GraphCtrl.PadFull2DLineGraph = true;
	}
	catch(error)
	{
	   debugger;
	}
}

edPropGraphScope.prototype.GetGraphData = function (prop)
{
  //debugger;
	try
	{
	   if (!prop) {
	   // Plot the active property
	      var prop = eda.VarDataScript.getCurrentProperty();
	   }
		if(prop == null) return;
	   
	   if (edApplet.Script.isPSDLoad(prop))
	   {	   
		eda.PSDScript.GetGraphData(prop);
		return;
	   }
	   
	   var yItem = 0;
	   this.GraphCtrl.Datasets.Clear();

	   prop = prop.GraphObject;
	   // TODO determine the range from the Unique Set
	   var begin = 1;
      var end = prop.NumData;
      var uniqSets = prop.UniqueSets;
      if (uniqSets.Count)
      {
         for (var i = 1; i <= uniqSets.Count; i++)
         {
            var uniqSet = uniqSets.Item(i);
            begin = uniqSet.begin;
            end = uniqSet.end;
			   edApplet.Script.GetOneGraphData(this.GraphCtrl, prop, begin, end, false);
         }
      }
      else
      {
			edApplet.Script.GetOneGraphData(this.GraphCtrl, prop, begin, end, false);
      }
	  this.DrawGraph(0,prop);
	}
	catch(error)
	{
	   //debugger;
	}
}

edPropGraphScope.prototype.OrthoGetGraphData = function (prop)
{
	try
	{
	  if (!prop) 
	  {
	      var prop = eda.VarDataScript.getCurrentProperty();
	  }
	  if(prop == null) 
		return;
	  var yItem = 0;
	  
	  this.GraphCtrl.Datasets.Clear();
	   
	  prop = prop.GraphObject;	   
	   
	  var begin = 1;
	  var end = prop.NumData;
	  
      edApplet.Script.GetOneGraphData(this.GraphCtrl, prop, begin, end, false);
      
	  this.DrawGraph(0,prop);
	}
	catch(error)
	{
	   //debugger;
	}
}

edPropGraphScope.prototype.AddGraphData = function (DataSetIdx, xval, yval)
{
	try
	{
      edApplet.Script.AddGraphData(this.GraphCtrl, DataSetIdx, xval, yval);
	}
	catch(error)
	{
	   //debugger;
	}
}

edPropGraphScope.prototype.RemoveGraphData = function ( DataSetIdx, xval, yval)
{
	try
	{
      edApplet.Script.RemoveGraphData(this.GraphCtrl, DataSetIdx, xval, yval);
	}
	catch(error)
	{
	   //debugger;
	}
}

edPropGraphScope.prototype.DrawGraph = function ( DataSetIdx, VarData )
{
	try
	{
      edApplet.Script.DrawGraph(this.GraphCtrl, DataSetIdx, VarData);
  		if (eda.PropImgScript)
		   edpg.window.setTimeout(eda.PropImgScript.UpdateImages,250);
	}
	catch(error)
	{
	   //debugger;
	}
}


