
var googleMap = new GoogleMap();
var localDataInfo = new databaseInfo();
var tempGoogleMap;
var g_init = false;
var g_init_imei ="";


var g_timer = null;
var g_currentImei = '';
var g_currentAssetName = '';
var g_monitor = false;
var g_tracker = false;
var g_monitor_devices = [];

//-----------this for tracking-----------
var g_playIndex = 0;
var g_currentVehicleInfo = null;
var g_interval = 30;


function initialize()
{
	globalFunc_SelectMap(g_mapType);
	globalFunc_setTimerValue(g_interval);
	if(g_init){
		g_init = false;
		localDataInfo.checkboxClick(true, g_init_imei);
		g_monitor_devices.push(g_init_imei);
		localDataInfo.getData_FromSQL();
		localDataInfo.setCenterPoint();
	}
	
}
function initAllMap()
{
	googleMap.map = null;
}

function globalFunc_SelectMap(index)
{
	var divObj = document.getElementById("divMap");
	divObj.innerHTML = "";
	initAllMap();
	g_mapType = index;
	localDataInfo.getData_FromSQL();	
	switch(g_mapType)
	{
		case 0:
			googleMap.initMap(divObj);
			tempGoogleMap = googleMap.map;
			break;	
		
	}
	
}
function globalFunc_AddMarker(info, index, action)
{
        console.log(info+index+action);
	switch(g_mapType)
	{
		case 0:
			googleMap.addMarker(info, index, action);	
			break;	
		
	}
}

function globalFunc_setCenterZoom(lat, lng, zoom)
{
	switch(g_mapType)
	{
		case 0:
			googleMap.setCenterPos(lat, lng, zoom);	
			break;	
		
	}
}


function globalFunc_removeAllMarker()
{
	switch(g_mapType)
	{
		case 0:
			googleMap.removeAllMarker();	
			break;	
		
	}
}

function timerFunc()
{
	globalFunc_removeAllMarker();
	localDataInfo.getData_FromSQL();
	//localDataInfo.setAlertBox("", "");
}
function globalFunc_setTimerValue(interval)
{
	if(g_timer != null)
	{
		clearInterval(g_timer);
		g_timer = null;
	}
	g_interval = interval;
	g_timer = setInterval(timerFunc, interval * 1000);
}

google.maps.event.addDomListener(window, 'load', initialize);// JavaScript Document

//-----------add by vuxuantuyen.mta@gmail.com 11/01/2014-----------------------

function chooseVehicle(obj, imei){
	localDataInfo.checkboxClick(obj.checked, imei);
}
function statusDetail(imei, assetname){
	g_currentImei = imei;
	g_currentAssetName = assetname;
	localDataInfo.setDivContent(imei, assetname);
	//localDataInfo.setAlertBox(imei, assetname);
//	if($("#"+id).css("display")=='none'){
//		$("#"+id).show("slow");
//	}else{
//		$("#"+id).hide("slow");
//	}
	
}

//------------------this for tracking--------------------------------------------
function globalSelectedVehicleInformation(imei)
{
	
	
	removeAllObject();
	localDataInfo.getVehicleInfo(imei);
	
}
function globalPlay()
{

	if(g_timer != null)
	{
		clearInterval(g_timer);
		//g_playIndex = 0;
	}
	trackingTimerFunc();
	g_timer = setInterval(trackingTimerFunc, g_interval * 1000);
}	

function globalPause()
{
	g_showInfo = true;
	clearInterval(g_timer);
}
function globalDrawPolyLineOnMap()
{
	
	switch(g_mapType)
	{
		case 0:
			googleMap.drawPolyLine();	
			break;	
		
	}
}


function removeAllObject()
{
	switch(g_mapType)
	{
		case 0:
			googleMap.removeMarker();
			googleMap.removePolyLine();
			break;	
		
		
	}
}

function globaldrawMarker(index)
{
	
	g_playIndex = index;
	switch(g_mapType)
	{
		case 0:
			googleMap.drawMarker(index);	
			break;	
		
	}
}
function trackingTimerFunc()
{
	
	if(g_playIndex > g_currentVehicleInfo.length - 1)
	{
		globalPause();
		return;
	}
	
	globaldrawMarker(g_playIndex);
	g_playIndex++;
}

//------------this for replay--------------------

function globalRePlay()
{
	if(g_timer != null)
	{
		clearInterval(g_timer);		
	}
	g_timer = setInterval(timerReplay, 300);
}

function timerReplay()
{
	
	if(g_playIndex > g_currentVehicleInfo.length - 1)
	{
		globalPause();
		g_playIndex = 0;
		g_showInfo = true;
		return;
	}
	g_showInfo = false;
	setSlider(g_playIndex);
	globaldrawMarker(g_playIndex);
	g_playIndex++;
}