﻿if (GWMap.prototype)
{
    GWMap.prototype.GPSGate = function(){};
}

// prototype1.6
Object.extend = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};


//**********************//
//GPSGate的擴充模組//
//**********************//
var ShowGPSTimeout;
var TrackGPSTimeout;
var GPSGateExtend =
{
    BoolShowGPS: false,
    BoolTrackGPS: false,
    //ShowGPSTimeout:0,
    //TrackGPSTimeout:0,
    GetGPS: function (callback)
    {
        GpsGate.Client.GetGPS(callback);
    },//GetGPS
    
    ShowGPS: function(tf, callback)
    {
        if(this.BoolShowGPS == tf){return;}
        if(tf == true){
            this.BoolShowGPS = true;
            this.ShowGPSTicker(callback);
        }else{
            clearTimeout(ShowGPSTimeout);
            this.BoolShowGPS = false;
        }
    },//ShowGPS
    ShowGPSTicker: function(callback)
    {
        GpsGate.Client.ShowGPS(callback)
        ShowGPSTimeout = setTimeout('GPSGateExtend.ShowGPSTicker('+callback+')',1000);
    },
    
    TrackGPS: function(tf,callback)
    {
        if(this.BoolTrackGPS == tf)return;
        if(tf==true)
        {
            this.BoolTrackGPS = true;
            this.TrackGPSTicker(callback);
        }else{
            clearTimeout(TrackGPSTimeout);
            this.BoolTrackGPS = false;
        }
    },//TrackGPS
    TrackGPSTicker: function(callback)
    {
        GpsGate.Client.TrackGPS(callback);
        TrackGPSTimeout = setTimeout('GPSGateExtend.TrackGPSTicker('+callback+')',1000);
    },
    
    StartRecordGPS: function (callback)
    {
        GpsGate.Client.StartRecordGPS(callback);
    },//StartRecordGPS
    
    StopRecordGPS: function (callback)
    {
        GpsGate.Client.StopRecordGPS(callback);
    },//StopRecordGPS
    
    GetGPSRecord: function(callback)
    {
        GpsGate.Client.GetGPSRecord(callback);
    }//GetGPSRecord
};//GPSGateExtend()


//新增GPSGateExtend至GWMap
Object.extend(GWMap.prototype, GPSGateExtend);

//************//
//GPSGate的XSS//
//************//
(function()
{
	if (typeof(GpsGate) == 'undefined')
	{
		GpsGate = {};
	}
	var _url = 'http://localhost:12345/gps/';
	var _scriptCounter = 0;

	function xssCall(methodName, callback)
	{
		var id = _scriptCounter++;
		var scriptNodeId = 'GpsGateXss_' + id;
		var poolName = methodName + '_' + id;

		GpsGate.Client._callback[poolName] = function(/*arguments*/)
		{
			var scriptNode = document.getElementById(scriptNodeId);
			scriptNode.parentNode.removeChild(scriptNode);
			delete GpsGate.Client._callback[poolName];

			callback.apply(this, arguments);
		};
        var src = _url+'?'+methodName+'='+'GpsGate.Client._callback.'+poolName;
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.id = scriptNodeId;
		script.charset = 'utf-8'; // necessary?
        script.src = src;
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	GpsGate.Client = {
		GetGPS: function(callback)
		{
			xssCall('GetGPS', callback);
		},
		ShowGPS: function(callback)
		{
		    xssCall('GetGPS', callback);
		},
		TrackGPS: function(callback)
		{
		    xssCall('GetGPS', callback);
		},
		StartRecordGPS: function(callback)
		{
		    xssCall('StartRecordGPS', callback);
		},
		StopRecordGPS: function(callback)
		{
		    xssCall('StopRecordGPS', callback);
		},
		GetGPSRecord: function(callback)
		{
		    xssCall('GetGPSRecord', callback);
		},
		GpsGateFalse: function()
		{
		    alert("GPSGate解析錯誤!!");
		},
		_callback: {}
	};

})();
