//Constructor
var JSubRoute=function()
{
	//Field
	this.name="";
	this.description="";
	this.routePoints=new Array();
	this.isLine=false;
	this.color="#ffffff";
	this.width=1;
	this.range=null;
	this.linePoints;// use point from earth3D control
	
	//Method
	this.init=JSubRoute_init;
	this.getRoutePoint=JSubRoute_getRoutePoint;
	this.alloc=JSubRoute_alloc;
	this.setLength=JSubRoute_setLength;
	this.setAt=JSubRoute_setAt;
	this.addRoutePoint=JSubRoute_addRoutePoint;
	this.removeRoutePoint=JSubRoute_removeRoutePoint;
	this.modifyRoutePoint=JSubRoute_modifyRoutePoint;
	
	this.getRange=JSubRoute_getRange;
	this.renderToMap=JSubRoute_renderToMap;
	this.appendToMap=JSubRoute_appendToMap;
	this.clearEmpty=JSubRoute_clearEmpty;

	//Event
	
	//Display
	this.init();	
}

function JSubRoute_init()
{	
}

function JSubRoute_getRoutePoint(index)
{
	return this.routePoints[index];
}

function JSubRoute_setLength(length)
{
	this.routePoints[length-1]=new JRoutePoint();
}

function JSubRoute_alloc(length)
{
	for(var i=0;i<length;i++)
		this.routePoints[i]=new JRoutePoint();
}

function JSubRoute_setAt(index,routePoint)
{
	this.routePoints[index]=routePoint;
}

function JSubRoute_addRoutePoint(routePoint)
{
	this.routePoints[this.routePoints.length]=routePoint;
}

function JSubRoute_removeRoutePoint(index)
{	
	delete this.routePoints[index];
}

function JSubRoute_modifyRoutePoint(index, routePoints)
{	
	this.routePoints[index]=routePoint;
}

function JSubRoute_getRange()
{
	// for line subroute , we try to use cached range to enhance speed
	if(this.isLine && this.range!=null)
		return this.range;
		
	var line=new JLine();
	if(this.isLine)
	{
		if(this.linePoints!=null)
		{
			line.setLength(this.linePoints.GetSize());

			var point;
			
			for(var i=0;i<this.linePoints.GetSize();i++)
			{
				point=this.linePoints.GetPoint(i);
				line.setAt(i,new JPoint(point.GetX(),point.GetZ()));
			}
		}
	}
	else
	{
		line.setLength(this.routePoints.length);
		for(var i=0;i<this.routePoints.length;i++)
		{
			var routePoint=this.routePoints[i];
			if(!g_isNull(routePoint))
			{
				var x,y;
				x=degreeFromDMS(routePoint.xDegree,routePoint.xMinute,routePoint.xSecond);
				y=degreeFromDMS(routePoint.yDegree,routePoint.yMinute,routePoint.ySecond);
				line.setAt(i,new JPoint(x,y));
			}
		}
	}
	
	if(line.points.length>0)
		this.range=line.calcRange();
	
	return this.range;
}

function JSubRoute_renderToMap(map)
{
	var x, y;
	// add as line
	if(this.isLine )
	{
		g_addDynamicLineEntity(map,this.linePoints,this.name, this.color, this.width);
	}
	else
	{
		for(var i=0;i<this.routePoints.length;i++)
		{
			var routePoint=this.getRoutePoint(i);
			if(!g_isNull(routePoint))
			{
				try
				{
					x=degreeFromDMSHigh(routePoint.xDegree, routePoint.xMinute, routePoint.xSecond,routePoint.xMSecond);
					y=degreeFromDMSHigh(routePoint.yDegree, routePoint.yMinute, routePoint.ySecond, routePoint.yMSecond);
					g_addDynamicEntity(map, x,y,routePoint.annoText,routePoint.color,routePoint.radius,routePoint.legendName);
				}
				catch(error)
				{
				}
			}
		}
	}
}

function JSubRoute_appendToMap(map)
{
	if(this.isLine)
		return;
		
	if(this.routePoints.length<=0)
		return;
		
	var x, y;
	
	var routePoint=this.getRoutePoint(this.routePoints.length-1);
	if(!g_isNull(routePoint))
	{
		x=degreeFromDMSHigh(routePoint.xDegree, routePoint.xMinute, routePoint.xSecond,routePoint.xMSecond);
		y=degreeFromDMSHigh(routePoint.yDegree, routePoint.yMinute, routePoint.ySecond, routePoint.yMSecond);
		g_addDynamicEntity(map, x,y,routePoint.annoText,routePoint.color,routePoint.radius,routePoint.legendName);
	}
}
// throw away empty points
function JSubRoute_clearEmpty()
{
	var oldRoutePoints=this.routePoints;

	this.routePoints=new Array();
	for(var i=0;i<oldRoutePoints.length;i++)
	{
		var routePoint=oldRoutePoints[i];
		if(!g_isNull(routePoint))
			this.addRoutePoint(routePoint);
	}
}