//alert("enter indicator.js");


function showObject(obj)
{
		var description="";
		for (var i in obj){
			var property=obj[i];
			description+=i+" = "+property+"\n";
		}
		return description;
}

//var URL_GET_USER_POSITION = "http://www.gpslook.net/gps_get_coord.php";
var IndicatorArray = new Array();
var IndicatorTrackArray = new Array();
var IndicatorTrackDataArray = new Array();

function createIndicator(indId,token)
{


	for(i=0;i<IndicatorArray.length;i++)
	{
		if (IndicatorArray[i].getAttribute("uid")==indId)
		{
			alert("createIndicator exist "+indId);
			return ;
			
		}
	}
	

	var objSrc="<div id=\""+indId+"\"  style=\"position:absolute; left:20px; top:20px;z-index:9\"></div>";
	//alert(objSrc);
	//log.value+=objSrc;
	obj=document.createElement(objSrc);
	//alert(objSrc);
	
	//obj.setAttribute("mapx",0);
	//obj.setAttribute("mapy",0);
	obj.setAttribute("lon",0);
	obj.setAttribute("lat",0);
	
	obj.setAttribute("is_track","false");
	obj.setAttribute("is_bind","false");
	
	
	obj.setAttribute("uid",indId);
	obj.setAttribute("token",token);
	document.body.appendChild(obj);
	
	obj.innerHTML="<span name='info' style='color:#ffffff;background-color:green'>"+indId+" <img src='images/close.gif' onclick='deleteIndicator(this.parentElement.parentElement)'></span><br><img src='images/arrow.gif'>";
	IndicatorArray.push(obj);
	IndicatorTrackArray.push(null);
	IndicatorTrackDataArray.push(new Array());
	
	//alert("create finish " + indId+" has "+IndicatorArray.length+" indicators "+IndicatorArray);
	updateIndicator();
}

function moveIndicator(indId,lon,lat)
{
	var obj=null;
	var i=0;
	for(i=0;i<IndicatorArray.length;i++)
	{
		if (IndicatorArray[i].getAttribute("uid")==indId)
		{
			obj=IndicatorArray[i];
			break;
		}
	}
	
	//var obj=document.getElementById(indId);
	if (obj==null)
	{
		//alert("Error!" +indId);
		//createIndicator(indId,posx,posy);
	}
	else
	{
		obj.setAttribute("lon",lon);
		obj.setAttribute("lat",lat);
		//obj.innerHTML=showObject(obj);
		//win = window.open("", "js");
		//win.document.write(showObject(obj.children[0].children[0]));
 		//obj.children[0].children[0].href=getGoogleMapUrl(lon,lat);
 		
 		is_track=obj.getAttribute("is_track");
		is_bind=obj.getAttribute("is_bind");
		if (is_bind=="true")
		{
			var point=new LTPoint(lon,lat);
			map.moveToCenter(point);	
		}
		
		if(is_track=="true")
		{
			trackData=IndicatorTrackDataArray[i];
			len=trackData.length;
			if (len==0)
			{
				trackData.push(new LTPoint( lon , lat ));
			}
			else{
				oldPt=trackData[len-1];
				if (Math.abs(oldPt.getLatitude()-lat)+Math.abs(oldPt.getLongitude()-lon)>100 ){
					trackData.push(new LTPoint( lon , lat ));
					if (IndicatorTrackArray[i]!=null)
					{
						map.removeOverLay(IndicatorTrackArray[i],true);
						IndicatorTrackArray[i]=null;
					}
					var polyLine = new LTPolyLine( trackData );
					map.addOverLay( polyLine );
					IndicatorTrackArray[i]=polyLine;
				}
			}
		}
		else if (IndicatorTrackArray[i]!=null)
		{
				map.removeOverLay(trackArray[i],true);
				IndicatorTrackArray[i]=null;
		}
		
 		coord=getCoord(lon,lat);
		obj.style.left=coord[0]-12;
		obj.style.top=coord[1]-25;
		
		//if(g_bShowNavPane)
		//	obj.style.top=coord[1]-25;
		//else
		//	obj.style.top=coord[1]-25+25;
		
		//updateIndicator();
	}
	
}


function updateIndicator()
{
	for(i=0;i<IndicatorArray.length;i++)
	{
		obj=IndicatorArray[i];
		lat=parseInt(obj.getAttribute("lat"));
		lon=parseInt(obj.getAttribute("lon"));
		
		
		
		
		
		//alert(Math.round(parseFloat(lon)*10000)+" "+Math.round(parseFloat(lat)*10000));
		//coord=getCoord(Math.round(parseFloat(lon)*100000),Math.round(parseFloat(lat)*100000));
		coord=getCoord(lon,lat);
		//alert(coord[0]+" "+coord[1]);
		//alert((coord[0]-12)+ " " + (coord[1]-58));
		obj.style.left=coord[0]-12;
		obj.style.top=coord[1]-25;
		//if(g_bShowNavPane)
		//	obj.style.top=coord[1]-25;
		//else
		//obj.style.top=coord[1]-25+25;
		
		
	}
	
}

function deleteIndicator(IndObj)
{

	for(i=0;i<IndicatorArray.length;i++)
	{
		if (IndicatorArray[i]==IndObj)
			{
				IndicatorArray.splice(i,1);	
				if (IndicatorTrackArray[i]!=null)
					map.removeOverLay(IndicatorTrackArray[i],true);
				IndicatorTrackArray.splice(i,1);		
				IndicatorTrackDataArray[i].length=0;
				IndicatorTrackDataArray.splice(i,1);
				break;
			}
	}
	
	IndObj.removeNode(true);
}

function deleteIndicatorById(uid)
{
	//alert("deleteIndicatorById ");
	var IndObj;
	for(i=0;i<IndicatorArray.length;i++)
	{
		if (IndicatorArray[i].getAttribute("uid")==uid)
			{
				//alert("asdfa");
				IndObj=IndicatorArray[i];
				IndicatorArray.splice(i,1);	
				if (IndicatorTrackArray[i]!=null)
					map.removeOverLay(IndicatorTrackArray[i],true);
				IndicatorTrackArray.splice(i,1);		
				IndicatorTrackDataArray[i].length=0;
				IndicatorTrackDataArray.splice(i,1);
				IndObj.removeNode(true);
				break;
			}
	}
	
	
}


//ajax function
var req = null;

function sendAjaxRequest(url) {
	if (window.XMLHttpRequest) 
	 {
    	req = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) 
   {
    	req = new ActiveXObject("Microsoft.XMLHTTP");   
	 }
	 req.onreadystatechange = processAjaxResponse;
  req.open("GET", url, true);
  req.send(null);
}

var requestArray=new Array();

function freshIndicator() {
	/*
	for(i=0;i<IndicatorArray.length;i++)
	{
		
		var url = URL_GET_USER_POSITION +"?uid="+IndicatorArray[i].getAttribute("uid")+"&token="+IndicatorArray[i].getAttribute("token");
		//alert(url);
		for(i=0;i<requestArray.length;i++)
		{
			if (requestArray==url) 
				continue;
		}
		requestArray.push(url);
		
	}
	*/
	if (IndicatorArray.length>0){
		//var sUrl = "http://www.gpslook.net/api/rpc.php?rs=getMutiCoords&rsargs[]=";
		var ids="";
		for(i=0;i<IndicatorArray.length;i++)
		{
			ids+=IndicatorArray[i].getAttribute("uid");		
			if (i!=IndicatorArray.length-1)
				ids+=",";
		}
		ajax_call("/api/rpc.php","getMutiCoords",new Array(ids),ongetMutiCoordsResult);
		//alert("test");
		//sendAjaxRequest(sUrl+"&test="+Math.random());
	}
}

function ongetMutiCoordsResult(rt){
	
	if (rt[0]>=0)
	{
		//alert(rt);
		for(var n=0;(n<rt.length-1)/3;n++){
			//var coords=rt[n].split(",");
			//var lon=Math.round(parseFloat(coords[1]));
    	//var lat=Math.round(parseFloat(coords[2]))    	
    	//moveIndicator(coords[0],lon,lat);			
    	moveIndicator(rt[1+n*3],rt[1+n*3+1],rt[1+n*3+2]);			
		}
	}
	//else
	//	alert(rt[1]);
}

function getIndicators(){
	subList=new Array();
	//alert("getIndicators "+IndicatorArray.length+" "+IndicatorArray);
	for(i=0;i<IndicatorArray.length;i++)
	{
		//
		desc=new Array();
		desc.push(IndicatorArray[i].getAttribute("uid"));
		desc.push(IndicatorArray[i].getAttribute("is_bind"));
		desc.push(IndicatorArray[i].getAttribute("is_track"));
		subList.push(desc);
	}
	return subList;
}

function bindToIndicator(uid,bFlag){
	
	for(i=0;i<IndicatorArray.length;i++)
	{
		IndicatorArray[i].setAttribute("is_bind","false");
		if (IndicatorArray[i].getAttribute("uid")==uid){
				if (bFlag)
					IndicatorArray[i].setAttribute("is_bind","true");
				//else
					//IndicatorArray[i].setAttribute("is_bind","false");
		}
		
	}
};

function drawTrackForIndicator(uid,bFlag){
	for(i=0;i<IndicatorArray.length;i++)
	{
		if (IndicatorArray[i].getAttribute("uid")==uid){
				if (bFlag)
					IndicatorArray[i].setAttribute("is_track","true");
				else
					IndicatorArray[i].setAttribute("is_track","false");
		}
		
	}
}

function sendRequest()
{
	if (req==null && requestArray.length>0)
	{
		sendAjaxRequest(requestArray.pop()+"&test="+Math.random());
	}
}

function processAjaxResponse() {
  if (req.readyState == 4 && req.status == 200) {
  	//alert(req.responseText);
    var rt=req.responseText.split("|");
    if(rt[0]=="indicator")
    {
    	//alert(rt[1]+" "+rt[2]+" "+rt[3]);
    	lon=Math.round(parseFloat(rt[2]));
    	lat=Math.round(parseFloat(rt[3]))
    	
    	moveIndicator(rt[1],lon,lat);
    }
    req=null;
  }
  else
  	;//alert("other");
}
//alert("leave indicator.js");
/*
function AJAXInteraction(url, callback) {    var req = init();    req.onreadystatechange = processRequest;            function init() {      if (window.XMLHttpRequest) {        return new XMLHttpRequest();      } else if (window.ActiveXObject) {        return new ActiveXObject("Microsoft.XMLHTTP");      }    }        function processRequest () {      if (req.readyState == 4) {        if (req.status == 200) {          if (callback) callback(req.responseXML);        }      }    }    this.doGet = function() {      req.open("GET", url, true);      req.send(null);    }        this.doPost = function(body) {      req.open("POST", url, true);      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");      req.send(body);    }}function makeRequest() {  var ai = new AJAXInteraction("processme", function() { alert("Doing Post Process");});  ai.doGet();}
*/
