var isMapInit=false;
var currentRoute=null;			


////////////////////////////////////////////////////////////////
// display service
function getMap()
{
	return oEarthViewer;
}

function flyingToRange(range)
{
	var map=getMap();
	if(g_isNull(map))
		return;
	var lastX,lastY;
	if(!g_isNull(range))
	{
		lastX=(range.left()+range.right())/2;
		lastY=(range.top()+range.bottom())/2;
		
		// the height is related to line range
		var height;
		
		height= Math.max(range.width,range.height)*100000;
		
		if(height<10000)
			height=10000;
			
		map.FlyingTo(lastX,height,lastY,45);
	}	
}

function showRoute(route)
{
	try
	{
		var map=getMap();
		// loop to add entity
		if(g_isNull(route))
			return;
		if(g_isNull(map))
			return;
		var range=route.getRange();
		flyingToRange(range);
	}
	catch(error)
	{
	}
}

function showLineRoute(subRoute)
{
	try
	{
		if(g_isNull(subRoute))
		return;
		var map=getMap();

		var range=subRoute.getRange();
		flyingToRange(range);
	}
	catch(error)
	{
	}
}

function showRoutePoint(routePoint)
{
	try
	{
		if(g_isNull(routePoint))
			return;
		var map=getMap();
		var lastX,lastY;

		lastX=degreeFromDMS(routePoint.xDegree,routePoint.xMinute,routePoint.xSecond);
		lastY=degreeFromDMS(routePoint.yDegree,routePoint.yMinute,routePoint.ySecond);
		map.FlyingTo(lastX,10000,lastY,45);
	}
	catch(error)
	{
	}
}

////////////////////////////////////////////////////////////////
function showMyInfo()
{
mapPanelSpan.showInfo();
}

function showPoint(x,y)
{
mapPanelSpan.showPoint(x,y);
}


function showLocateTool()
{
if(!isMapPanel())
showMapPanel();
mapPanelSpan.showLocateTool();
}


/////////////////////////////////////////////////////////
// focus route and item manage methods

var systemTopic='系统建设的相关问题';
var focusRouteID=g_SystemID;
var focusRouteName=systemTopic;
var mapFocusRouteID=0;
var detailFocusRouteID=0;
var talkFocusRouteID=0;
var historyFocusRouteID=0;

function setFocusRoute(id, route)
{
setFocusRouteEx(id,route,false);
}

function setFocusRouteEx(id, route, isForce)
{
if(id!=focusRouteID || isForce)
{
currentRoute=route;
/*
// focus changed , we do something
if(isTalkPanel())
changeTalkFocus(id);
else
if(isHistoryPanel())
changeHistoryFocus(id);
else
if(isMapPanel())
changeMapFocus(id);
else
if(isDetailPanel())
changeDetailFocus(id);

*/
focusRouteID=id;
if(!g_isNull(route))
focusRouteName=route.name;
}

// special treatment for homepage
if(id==g_SystemID)
{
focusRouteName=systemTopic;
mapOperationPanelSpan.focusDetailImage();
mapOperationPanelSpan.showMapToolbars(false);
showDetailPanel();
}
}

function setFocusItem(itemType, itemIndex)
{
if(g_isNull(currentRoute))
return;
if(itemType==g_LineItem)
{
var lineSubRoute=currentRoute.getSubRoute(itemIndex);
showLineRoute(lineSubRoute);
}
else 
if(itemType==g_PointItem)
{
var pointSubRoute=currentRoute.getSubRoute(0);
if(!g_isNull(pointSubRoute))
{
var routePoint=pointSubRoute.getRoutePoint(itemIndex);
showRoutePoint(routePoint);
}
}
}

function previewRoute(route)
{
//if(!isDetailPanel())
//	return;
routeDetailSpan.previewRoute(route);
showRoute(route);
}
/////////////////////////////////////////////////////////
// panel change methods

function hideAllPanels()
{
mapPanelSpanDiv.style.visibility="hidden";
homePageSpanDiv.style.visibility="hidden";
routeDetailSpanDiv.style.visibility="hidden";
talkPanelSpanDiv.style.visibility="hidden";
historyPanelSpanDiv.style.visibility="hidden";
}

function showMapPanel()
{
hideAllPanels();
mapPanelSpanDiv.style.visibility="visible";
if(!isMapInit)
{
mapPanelSpan.navigate("MapPanel.aspx");
isMapInit=true;
}
else
{
changeMapFocus(focusRouteID);
}
}

function changeMapFocus(id)
{
if(id<=0)
return;
if(g_isNull(currentRoute))
return;

if(id!=mapFocusRouteID)
{
showRoute(currentRoute);
mapFocusRouteID=id;
}
}

function showDetailPanel()
{
hideAllPanels();

// change 
routeDetailSpanDiv.style.visibility="visible";
changeDetailFocus(focusRouteID);
}

function changeDetailFocus(id)
{
if(id<=0)
return;
if(id==g_SystemID)
{
homePageSpanDiv.style.visibility="visible";
routeDetailSpanDiv.style.visibility="hidden";
}
else
{
homePageSpanDiv.style.visibility="hidden";
routeDetailSpanDiv.style.visibility="visible";
}

if(id!=detailFocusRouteID)
{
var url;
if(id!=g_SystemID)
{
url="RouteDetailPage.aspx?id="+id.toString();
routeDetailSpan.navigate(url);
}
detailFocusRouteID=id;
}
}

function showTalkPanel()
{
// should first init focus
if(focusRouteID==0)
{
parent.indexFrame.treeIndexPanel().setFocusNode();
}

hideAllPanels();
talkPanelSpanDiv.style.visibility="visible";

// change 
changeTalkFocus(focusRouteID);
}

function changeTalkFocus(id)
{
if(id<=0)
return;
if(id!=talkFocusRouteID)
{
var url="TalkBrowsePage.aspx?topicid="+id.toString()+"&"+"pageid=1";
talkPanelSpan.navigate(url);
talkFocusRouteID=id;
}
}


function showHistoryPanel()
{
// should first init focus
if(focusRouteID==0)
{
parent.indexFrame.treeIndexPanel().setFocusNode();
}

hideAllPanels();
historyPanelSpanDiv.style.visibility="visible";

// change 
changeHistoryFocus(focusRouteID);
}

function changeHistoryFocus(id)
{
if(id<=0)
return;
if(id!=historyFocusRouteID)
{
var url="HistoryBrowsePage.aspx?topicid="+id.toString();
historyPanelSpan.navigate(url);
historyFocusRouteID=id;
}
}

/////////////////////////////////////////////////////////
// judge status of panel
function isMapPanel()
{
return mapPanelSpanDiv.style.visibility=="visible";
}

function isDetailPanel()
{
return routeDetailSpanDiv.style.visibility=="visible" || homePageSpanDiv.style.visibility=="visible";
}

function isTalkPanel()
{
return talkPanelSpanDiv.style.visibility=="visible";
}

function isHistoryPanel()
{
return historyPanelSpanDiv.style.visibility=="visible";
}