function TimeDiffSec(ts,te){
	var totTime = (te - ts)/1000;
	return totTime;
}
function GetGmtTime(){
	var time = new Date();
	var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000);
	var gmtTime =  new Date(gmtMS);
	return gmtTime.getTime();
}

function Time2MysqlForm(gmtTimeMilliSec){
	
	var gmtTime =  new Date(gmtTimeMilliSec);
	
	var time_year = gmtTime.getFullYear();
	var time_mon = gmtTime.getMonth();
	var time_date = gmtTime.getDate();
	var time_hr = gmtTime.getHours();
	var time_min = gmtTime.getMinutes();
	var time_sec = gmtTime.getSeconds();

	time_mon = time_mon + 1;
	if(time_mon < 10){
		time_mon = "0" + time_mon;
	}
	if(time_date < 10){
		time_date = "0" + time_date;
	} 
	if(time_hr < 10){
	 time_hr = "0" + time_hr;
	 }
	if(time_min < 10){
	 time_min = "0" + time_min;
	 }
	if(time_sec < 10){
	 time_sec = "0" + time_sec;
	 } 
	 formatted_time= time_year+"-"+time_mon+"-"+time_date+" "+time_hr + ":" + time_min + ":" + time_sec;
	 
	 return formatted_time;
}
function TrackUserAction(enterTime,ClientId,MapId)
{

	var startTime= Math.ceil(enterTime/1000);	        // change to seconds
	var exitDate = new Date();
	var exitTime = Math.ceil(exitDate.getTime()/1000); 

	var totalTime = exitTime - startTime;

	// get time from browser side
	//var tend = GetGmtTime();
	//endtime= Time2MysqlForm(tend);
	//var totTime = TimeDiffSec(tstart,tend);
	
	var lat=GetLat();
	var lon=GetLon();
	var zoom=GetZoom();
	var TrackInfo=GetTrackProduct();
	var TrackProduct=GetTrackProduct();
	var TrackZoomCenter=GetTrackZoomCenter();	
	var NumsZoom=GetNumsZoom();
	var NumsZoomIn=GetNumsZoomIn();
	var NumsZoomOut=GetNumsZoomOut();
	var NumsMapMove=GetNumsMapMove();
	var TotalTimeOnMap=GetNetTotalTimeOnMap();
	var FocusInOut=GetTrackFocusInOut();
	var timesPlayClicked=GetTimesPlayClicked();
	var timesStopClicked=GetTimesStopClicked();
	
	try{
		console.log("NumsZoom %d NumsMapMove %d TotalTimeonMap %d FocusInOut %s",NumsZoom,NumsMapMove,TotalTimeOnMap,FocusInOut);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}	
	
	msg = "ClientId="+ClientId+
		"&MapId="+MapId+
		"&totaltime="+totalTime+
		'&enterTime='+startTime+
		//'&endtime='+endtime+
		'&exitTime='+exitTime+
		'&lat='+lat+
		'&lon='+lon+
		'&TrackProduct='+TrackProduct+
		'&TrackZoomCenter='+TrackZoomCenter+	
		'&NumsZoomIn='+NumsZoomIn+
		'&NumsZoomOut='+NumsZoomOut+		
//		'&NumsZoom='+NumsZoom+
//		'&NumsMapMove='+NumsMapMove;		
		'&NumsMapMove='+NumsMapMove+
		'&netTotalTimeOnMap='+TotalTimeOnMap+
		'&mapFocusInOut='+FocusInOut+
		'&timesPlayClicked='+timesPlayClicked+
		'&timesStopClicked='+timesStopClicked;
	sendRequest("ImapMetrix.php",msg);
}

function createXMLHTTPObject(){
	var xmlHttp;
	try{  // Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}catch (e){  // Internet Explorer  
		try{    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}catch (e){    
			try{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}catch (e){      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}
	
	return xmlHttp;
}

function sendRequest(url,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;

	var method = (postData) ? "POST" : "GET";
	req.open(method,url,false);	
	/* in IE, should call onreadystatechange after XMLHttpObject.open
	req.onreadystatechange = function () {
	}
	*/
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');

	req.send(postData);
	return;
}

// FABridge related functions
var myCallback = function() {
	console.log("[my callback]");
}

var initialCallback = function() {
	try{
		 console.log("[init start from initialCallback]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}

	flexApp = FABridge.flex.root();
	
	try{
		 console.log("[init end from initialCallback]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}

  //flexApp.addEventListener("change", myCallback );			
	return;
}

function SetData(){
	
	try{
		 console.log("[reset center]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}				

  //flexApp.getMap().setZoom(6);

  //var center = flexApp.getMap().getCenter();
  //flexApp.getMap().setCenter(new LatLng(38.4,95.9));
  flexApp.getMap().setCenterFromPrimitives(38.4,-95.9,4);

}

function ReceiveDataFromFlashMovie(){
	//console.log("[here]");

	//console.log("flex app: " + flexApp);
  var zoom = flexApp.getMap().getZoom();
	//console.log("zoom: " + zoom);

  var center = flexApp.getMap().getCenter();
  var TrackProduct=GetTrackProduct();
  var TrackZoomCenter=GetTrackZoomCenter();
	try{
		 console.log("lat:" + center.lat() + " lng:" + center.lng() + " zoom:" + zoom);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}

	 document.getElementById("lat").value = center.lat();
	 document.getElementById("lon").value = center.lng();
	 document.getElementById("zoom").value = zoom;
	 document.getElementById("TrackInfo").value = TrackProduct;
	 document.getElementById("TrackZoom").value = TrackZoomCenter;
	
}

function initBridge (){
	try{
		console.log("[init start]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}

	flexApp = FABridge.flex.root();
	
	try{
		console.log("[init end]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	//var btn = flexApp.getBtn();
	//btn.addEventListener('click', onClick);
	document.getElementById("htmlContent").removeChild(document.getElementById("init_btn"));
}
function GetZoom(){
	//console.log("[here]");

	//console.log("flex app: " + flexApp);
	var zoom = flexApp.getMap().getZoom();
  	try{
		console.log("[get zoom]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
  return zoom;	
}			
function GetTrackProduct(){
	//console.log("[here]");

	//console.log("flex app: " + flexApp);
	var TrackProduct = flexApp.getMap().getTrackProduct();
  	try{
		console.log("[get TrackProduct]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
  return TrackProduct;	
}

function GetTrackZoomCenter(){
	//console.log("[here]");

	//console.log("flex app: " + flexApp);
	var TrackZoomCenter = flexApp.getMap().getTrackZoomCenter();
  	try{
		console.log("[get TrackZoom]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
  return TrackZoomCenter;	
}
function GetLat(){	
	var center = flexApp.getMap().getCenter();
	var lat = center.lat();

	try{
		console.log("[get lat]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return lat;	
}			

function GetLon(){	
	var center = flexApp.getMap().getCenter();
	var lon = center.lng();

	try{
		console.log("[get lon]");
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return lon;	
}			
function GetNumsZoom(){	
	var NumsZoom = flexApp.getMap().getNumsZoom();

	try{
		console.log("[get Nums of Zoom] %s",NumsZoom);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return NumsZoom;	
}	
function GetNumsZoomIn(){	
	var NumsZoomIn = flexApp.getMap().getNumsZoomIn();

	try{
		console.log("[get Nums of Zoom In] %s", NumsZoomIn);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return NumsZoomIn;	
}	
function GetNumsZoomOut(){	
	var NumsZoomOut = flexApp.getMap().getNumsZoomOut();

	try{
		console.log("[get Nums of Zoom Out] %s", NumsZoomOut);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return NumsZoomOut;	
}	
function GetNumsMapMove(){	
	var NumsMapMove = flexApp.getMap().getNumsMapMove();

	try{
		console.log("[get Nums of Map Move] %s",NumsMapMove);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return NumsMapMove;	
}	
function GetNetTotalTimeOnMap(){	
	var netTotalTimeOnMap = flexApp.getMap().getNetTotalTimeOnMap();

	try{
		console.log("[get net Total Time on Map] %d",netTotalTimeOnMap);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return netTotalTimeOnMap;	
}	
function GetTrackFocusInOut(){	
	var FocusInOut = flexApp.getMap().getTrackFocusInOut();

	try{
		console.log("[get FocusInOut] %s",FocusInOut);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return FocusInOut;	
}	
function GetTimesPlayClicked(){	
	var timesPlayClicked = flexApp.getMap().getTimesPlayClicked();

	try{
		console.log("[get Times PlayClicked] %s",timesPlayClicked);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return timesPlayClicked;	
}	
function GetTimesStopClicked(){	
	var timesStopClicked = flexApp.getMap().getTimesStopClicked();

	try{
		console.log("[get Times StopClicked] %s",timesStopClicked);
	}catch(e){ 
		console = { 
			log: function() {} 
		} 
	}
	return timesStopClicked;	
}	

