/*****************************\
*       Global variables      *
\*****************************/

// list all properties in view port
var TRMListPropURL = "/PropertyController/searchProperty";

// default GMap2 instance
var TRMMapInstance = null;

// mouse over cheker
var TRMMouseOver = false;

// define properties array
var TRMProperties = new Array();

// define NEW Properties Index
var TRMFDirectionPropIndex = 99991;
var TRMSDirectionPropIndex = 99992;
var TRMAddNewPropIndex = 99999;
// define Search Properties Index
var TRMSearchPropIndex = 0;


// define dummy marker
var TRMTmpMarker = null;

// define default position and zoom
var TRMDefaultLat = 13.7534;
var TRMDefaultLng = 100.4991;
var TRMDefaultZoom = 13;

// define user position and zoom
var TRMLat = 0
var TRMLng = 0
var TRMZoom = 0

// define default control
var TRMDefaultControl = new TRMControl();

// define marker draggable default value
var TRMMarkerDraggable = false;

// define default zoom level where search
var TRMSearchZoom = 15

// define marker index
var TRMSearchMarker = 0;
var TRMFDirectionMarker = 91;
var TRMSDirectionMarker = 92;
var TRMAddNewMarker = 99;

// define EWindow instance
var TRMEw = null;

// define GDirection instance
var TRMDirectionInstance = null;
var TRMPolyline = null;
var TRMDirectionVertexIndex = 0;

// define marker templates
var TRMMarkers = new Array();

// 0 - search marker
	TRMMarkers[TRMSearchMarker] = new GIcon();
	TRMMarkers[TRMSearchMarker].image = "http://www.thairemap.com/Img/icon/green-house.png";
	TRMMarkers[TRMSearchMarker].shadow = "http://www.thairemap.com/Img/icon/shadow.png";
	TRMMarkers[TRMSearchMarker].iconSize = new GSize(20, 20);
	TRMMarkers[TRMSearchMarker].shadowSize = new GSize(30, 20);
	TRMMarkers[TRMSearchMarker].iconAnchor = new GPoint(9, 20);
	TRMMarkers[TRMSearchMarker].infoWindowAnchor = new GPoint(18, -5);
	TRMMarkers[TRMSearchMarker].infoShadowAnchor = new GPoint(17, 22);

// 1 - apartment/horpak marker
	TRMMarkers[1] = new GIcon();
	TRMMarkers[1].image = "http://www.thairemap.com/Img/icon/icon_apartment.png";
	TRMMarkers[1].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_apartment.png";
	TRMMarkers[1].iconSize = new GSize(28.0, 20.0);
	TRMMarkers[1].shadowSize = new GSize(39.0, 20.0);
	TRMMarkers[1].iconAnchor = new GPoint(14.0, 10.0);
	TRMMarkers[1].infoWindowAnchor = new GPoint(14.0, 10.0);

// 2 - condo marker
	TRMMarkers[2] = new GIcon();
	TRMMarkers[2].image = "http://www.thairemap.com/Img/icon/icon_condominium.png";
	TRMMarkers[2].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_condominium.png";
	TRMMarkers[2].iconSize = new GSize(20.0, 23.0);
	TRMMarkers[2].shadowSize = new GSize(32.0, 23.0);
	TRMMarkers[2].iconAnchor = new GPoint(10.0, 11.0);
	TRMMarkers[2].infoWindowAnchor = new GPoint(10.0, 11.0);

// 3 - village marker
	TRMMarkers[3] = new GIcon();
	TRMMarkers[3].image = "http://www.thairemap.com/Img/icon/icon_village.png";
	TRMMarkers[3].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_village.png";
	TRMMarkers[3].iconSize = new GSize(28.0, 20.0);
	TRMMarkers[3].shadowSize = new GSize(39.0, 20.0);
	TRMMarkers[3].iconAnchor = new GPoint(14.0, 10.0);
	TRMMarkers[3].infoWindowAnchor = new GPoint(14.0, 10.0);

// 4 - hotel marker
	TRMMarkers[4] = new GIcon();
	TRMMarkers[4].image = "http://www.thairemap.com/Img/icon/icon_hotel.png";
	TRMMarkers[4].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_hotel.png";
	TRMMarkers[4].iconSize = new GSize(20.0, 20.0);
	TRMMarkers[4].shadowSize = new GSize(31.0, 20.0);
	TRMMarkers[4].iconAnchor = new GPoint(10.0, 10.0);
	TRMMarkers[4].infoWindowAnchor = new GPoint(10.0, 10.0);

// 5 - room in condo marker
	TRMMarkers[5] = new GIcon();
	TRMMarkers[5].image = "http://www.thairemap.com/Img/icon/icon_roomcondo.png";
	TRMMarkers[5].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_roomcondo.png";
	TRMMarkers[5].iconSize = new GSize(20.0, 22.0);
	TRMMarkers[5].shadowSize = new GSize(32.0, 22.0);
	TRMMarkers[5].iconAnchor = new GPoint(10.0, 11.0);
	TRMMarkers[5].infoWindowAnchor = new GPoint(10.0, 11.0);
	
// 6 - teedin marker
	TRMMarkers[6] = new GIcon();
	TRMMarkers[6].image = "http://www.thairemap.com/Img/icon/icon_teedin.png";
	TRMMarkers[6].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_teedin.png";
	TRMMarkers[6].iconSize = new GSize(21.0, 20.0);
	TRMMarkers[6].shadowSize = new GSize(32.0, 20.0);
	TRMMarkers[6].iconAnchor = new GPoint(10.0, 10.0);
	TRMMarkers[6].infoWindowAnchor = new GPoint(10.0, 10.0);


// 7 - home marker
	TRMMarkers[7] = new GIcon();
	TRMMarkers[7].image = "http://www.thairemap.com/Img/icon/icon_home.png";
	TRMMarkers[7].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_home.png";
	TRMMarkers[7].iconSize = new GSize(22.0, 20.0);
	TRMMarkers[7].shadowSize = new GSize(33.0, 20.0);
	TRMMarkers[7].iconAnchor = new GPoint(11.0, 10.0);
	TRMMarkers[7].infoWindowAnchor = new GPoint(11.0, 10.0);
	

// 8 - twin home marker
	TRMMarkers[8] = new GIcon();
	TRMMarkers[8].image = "http://www.thairemap.com/Img/icon/icon_twinhome.png";
	TRMMarkers[8].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_twinhome.png";
	TRMMarkers[8].iconSize = new GSize(35.0, 20.0);
	TRMMarkers[8].shadowSize = new GSize(46.0, 20.0);
	TRMMarkers[8].iconAnchor = new GPoint(17.0, 10.0);
	TRMMarkers[8].infoWindowAnchor = new GPoint(17.0, 10.0);


// 9 - townhouse marker
	TRMMarkers[9] = new GIcon();
	TRMMarkers[9].image = "http://www.thairemap.com/Img/icon/icon_townhouse.png";
	TRMMarkers[9].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_townhouse.png";
	TRMMarkers[9].iconSize = new GSize(20.0, 20.0);
	TRMMarkers[9].shadowSize = new GSize(31.0, 20.0);
	TRMMarkers[9].iconAnchor = new GPoint(10.0, 10.0);
	TRMMarkers[9].infoWindowAnchor = new GPoint(10.0, 10.0);
	

// 10 - townhome marker
	TRMMarkers[10] = new GIcon();
	TRMMarkers[10].image = "http://www.thairemap.com/Img/icon/icon_townhome.png";
	TRMMarkers[10].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_townhome.png";
	TRMMarkers[10].iconSize = new GSize(21.0, 20.0);
	TRMMarkers[10].shadowSize = new GSize(32.0, 20.0);
	TRMMarkers[10].iconAnchor = new GPoint(10.0, 10.0);
	TRMMarkers[10].infoWindowAnchor = new GPoint(10.0, 10.0);
	

// 11 - building marker
	TRMMarkers[11] = new GIcon();
	TRMMarkers[11].image = "http://www.thairemap.com/Img/icon/icon_building.png";
	TRMMarkers[11].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_building.png";
	TRMMarkers[11].iconSize = new GSize(21.0, 20.0);
	TRMMarkers[11].shadowSize = new GSize(32.0, 20.0);
	TRMMarkers[11].iconAnchor = new GPoint(10.0, 10.0);
	TRMMarkers[11].infoWindowAnchor = new GPoint(10.0, 10.0);
	

// 12 - office marker
	TRMMarkers[12] = new GIcon();
	TRMMarkers[12].image = "http://www.thairemap.com/Img/icon/icon_office.png";
	TRMMarkers[12].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_office.png";
	TRMMarkers[12].iconSize = new GSize(20.0, 23.0);
	TRMMarkers[12].shadowSize = new GSize(32.0, 23.0);
	TRMMarkers[12].iconAnchor = new GPoint(10.0, 11.0);
	TRMMarkers[12].infoWindowAnchor = new GPoint(10.0, 11.0);
	

// 13 - warehouse marker
	TRMMarkers[13] = new GIcon();
	TRMMarkers[13].image = "http://www.thairemap.com/Img/icon/icon_warehouse.png";
	TRMMarkers[13].shadow = "http://www.thairemap.com/Img/icon/shadow-icon_warehouse.png";
	TRMMarkers[13].iconSize = new GSize(25.0, 20.0);
	TRMMarkers[13].shadowSize = new GSize(36.0, 20.0);
	TRMMarkers[13].iconAnchor = new GPoint(12.0, 10.0);
	TRMMarkers[13].infoWindowAnchor = new GPoint(12.0, 10.0);
	

// 91 - first direction marker
	TRMMarkers[TRMFDirectionMarker] = new GIcon();
	TRMMarkers[TRMFDirectionMarker].image = "http://www.thairemap.com/images/red-star.png";
	TRMMarkers[TRMFDirectionMarker].shadow = "http://www.thairemap.com/images/star-shadow.png";
	TRMMarkers[TRMFDirectionMarker].iconSize = new GSize(20, 20);
	TRMMarkers[TRMFDirectionMarker].shadowSize = new GSize(30, 20);
	TRMMarkers[TRMFDirectionMarker].iconAnchor = new GPoint(9, 20);
	TRMMarkers[TRMFDirectionMarker].infoWindowAnchor = new GPoint(18, -5);
	TRMMarkers[TRMFDirectionMarker].infoShadowAnchor = new GPoint(16, 22);

// 92 - secont direction marker
	TRMMarkers[TRMSDirectionMarker] = new GIcon();
	TRMMarkers[TRMSDirectionMarker].image = "http://www.thairemap.com/images/red-star.png";
	TRMMarkers[TRMSDirectionMarker].shadow = "http://www.thairemap.com/images/star-shadow.png";
	TRMMarkers[TRMSDirectionMarker].iconSize = new GSize(20, 20);
	TRMMarkers[TRMSDirectionMarker].shadowSize = new GSize(30, 20);
	TRMMarkers[TRMSDirectionMarker].iconAnchor = new GPoint(9, 20);
	TRMMarkers[TRMSDirectionMarker].infoWindowAnchor = new GPoint(18, -5);
	TRMMarkers[TRMSDirectionMarker].infoShadowAnchor = new GPoint(16, 22);

// 99 - new property marker
	TRMMarkers[TRMAddNewMarker] = new GIcon();
	TRMMarkers[TRMAddNewMarker].image = "http://www.thairemap.com/Img/icon/green-house.png";
	TRMMarkers[TRMAddNewMarker].shadow = "http://www.thairemap.com/Img/icon/shadow.png";
	TRMMarkers[TRMAddNewMarker].iconSize = new GSize(20, 20);
	TRMMarkers[TRMAddNewMarker].shadowSize = new GSize(30, 20);
	TRMMarkers[TRMAddNewMarker].iconAnchor = new GPoint(9, 20);
	TRMMarkers[TRMAddNewMarker].infoWindowAnchor = new GPoint(18, -5);
	TRMMarkers[TRMAddNewMarker].infoShadowAnchor = new GPoint(16, 22);

	//function
	function showNESW(ne, sw) {
		$('ne').value = ne;
		$('sw').value = sw;
	}
	
	function showLatLng (lat, lng) {
		$('lat').value = lat;
		$('lng').value = lng;
	}
	
	function calDistance(d) {
		var td = d.meters;
		var dd = d.html;
		$('distance').setHTML("ระยะทาง : " + dd + " ("+td+" เมตร)");
	}
	
	function genDirection(f, s) {
		var p = new Array();
	
		p[1] = {
			'lat' : 13.7628,
			'lng' : 100.4874
		};
		p[2] = {
			'lat' : 13.9055,
			'lng' : 100.5067
		};
		p[3] = {
			'lat' : 13.7148,
			'lng' : 100.4680
		};
		
		TRMCreateDirection(p[f], p[s], $('direction'), calDistance);
		
	}	
	
	//property detail popup window
	function openPropertyDetail(property_id){
	     var pop = window.open ("/PropertyController/propertyDetail/"+property_id,"detail","location=1, status=1,toolbar=1, height=1000,with=1000,resizable=1, scrollbars=1");
	     pop.moveTo(0, 0);
	}
	
	
/*****************************\
*          Functions          *
\*****************************/

/*
function TRMLoad
	info	:	load google maps to an element 
	input	:	ele			- an html element
				_lat		- latitude
				_lng		- longitude
				_zoom		- zoom level
				_control	- and GControl Object
				_callback	- callback function
	output	:	GMap2 Object

*/
var TRMLoad = function (ele, _lat, _lng, _zoom, _control, _callback) {
	if (GBrowserIsCompatible()) {
	    TRMMapInstance = new GMap2(ele);
		if (_control == null) {
			TRMMapInstance.addControl(TRMDefaultControl);
		} else {
			TRMMapInstance.addControl(_control);
		}
		
		//TRMMapInstance.enableScrollWheelZoom();
		TRMMapInstance.enableDoubleClickZoom();

		var lat = (_lat == null) ? TRMDefaultLat : _lat;
		var lng = (_lng == null) ? TRMDefaultLng : _lng;
		var z = (_zoom == null) ? TRMDefaultZoom : _zoom;
		TRMLat = lat;
		TRMLng = lng;
		TRMZoom = z;

		var c = new GLatLng(lat, lng);

		TRMMapInstance.setCenter(c, z);

		var hierarchy = new GHierarchicalMapTypeControl();
		hierarchy.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, null, true);
		TRMMapInstance.addControl(hierarchy);


		if (_callback != null) {
			var b = TRMMapInstance.getBounds();
			_callback(b.getNorthEast().toString(), b.getSouthWest().toString());
		}

		TRMEw = new EWindow(TRMMapInstance, E_STYLE_7);
		TRMMapInstance.addOverlay(TRMEw);

		//GEvent.addListener(TRMMapInstance, "moveend", getPropJson);
		GEvent.addListener(TRMMapInstance, "click", function(marker,point) {
			if (point) {
				TRMEw.hide();
			}
		});

		/*
		// mousewheel handler

		GEvent.addListener(TRMMapInstance, "mouseover", function () {
			TRMMouseOver = true;
		});
		GEvent.addListener(TRMMapInstance, "mouseout", function () {
			if (TRMMouseOver) {
				TRMMouseOver = false;
			}
		});

		document.addEvent('mousewheel', function(e) {
			if (TRMMouseOver) {
				var e = new Event(e);
				e.stopPropagation();
				e.preventDefault();
			}
		});
		*/
		//getPropJson();
	}


	return (TRMMapInstance);
}

/*
function TRMAddMarker
	info	:	add marker to map
	input	:	id			- marker id
				lat			- latitude
				lng			- longitude
				type		- type of marker
				_draggable	- set this marker dragable or not
				_txt		- text for info window
				_callback	- callback function
	output	:	GMarker Object

*/
var TRMAddMarker = function (id, lat, lng, type, _txt, _draggable, _callback) {
	var p = new GLatLng(lat, lng);
	var d = (_draggable == null) ? TRMMarkerDraggable : _draggable;
	var mIcon = new GIcon(TRMMarkers[type]);

	var mOptions =  { 
			'draggable':d,
			'icon':mIcon
		};
	if (TRMProperties[id] == null) {
		TRMProperties[id] = new GMarker(p, mOptions);
		GEvent.addListener(TRMProperties[id], "dragstart", function() {
			//TRMMapInstance.closeInfoWindow();
			TRMEw.hide();
		});

		GEvent.addListener(TRMProperties[id], "dragend", function() {
			if (_callback != null) {
				var pos = TRMProperties[id].getLatLng();
				_callback(pos.lat(), pos.lng());
			}
		});

		TRMMapInstance.addOverlay(TRMProperties[id]);

		if (_callback != null) {
			var pos = TRMProperties[id].getLatLng();
			_callback(pos.lat(), pos.lng());
		}


		if (_txt != null) {
			GEvent.addListener(TRMProperties[id], "click", function() {
				//TRMProperties[id].openInfoWindow(_txt);
				TRMEw.openOnMarker(TRMProperties[id], TRMEWindowWarper('Information', _txt));
				if (TRMEw.isHidden()) {
					TRMEw.show();
				}
			});
		}
	} 

	return (TRMProperties[id]);
}

/*
function TRMAddNewProperty
	info	:	add new property
	input	:	_txt		- text for info window
				_callback	- callback function
	output	:	GMarker Object
	
*/
var TRMAddNewProperty = function (_txt, _callback) {
	var c = TRMMapInstance.getCenter();
	lat = c.lat();
	lng = c.lng();
	var _ap = TRMAddMarker (TRMAddNewPropIndex, lat, lng, TRMAddNewMarker, _txt, true, _callback);
	if (_txt) {
		TRMEw.openOnMarker(_ap, TRMEWindowWarper('Information', _txt));
	}
	return (_ap);
}

/*
function TRMLoadMarkers
	info	:	load property list and generate their markers to map
	input	:	k			- text (json format)
	output	:	bool
	
*/
var TRMLoadMarkers = function (j) {
    var res = eval(j);
    for (i=0; i<result.root.length; i++) {
		TRMAddMarker(result.root[i].propertyId, 
		             result.root[i].propertyLatitude, 
		             result.root[i].propertyLongitude, 
		             result.root[i].propertyType, 
		             '<a href="javascript:void(0)" onclick="openPropertyDetail('+result.root[i].propertyId+')">'+result.root[i].propertyNameThai+'</a>', 
		             null, 
		             null);
	}

	if (i == 0) {
		return (false);
	} else {
		return (true);
	}
}

/*
function TRMLoadMarkers
	info	:	reset map to first position
	input	:	void
	output	:	void
	
*/
var TRMMapReset = function () {
	TRMMapInstance.clearOverlays();
	TRMProperties = new Array();
	TRMEw = new EWindow(TRMMapInstance, E_STYLE_7);
	TRMMapInstance.addOverlay(TRMEw);
	TRMDirectionInstance = null;
	var c = new GLatLng(TRMLat, TRMLng);
	TRMMapInstance.setCenter(c, TRMZoom);
	//getPropJson();
}


/*
function TRMCenterAt
	info	:	center at specify lat, lng
	input	:	lat			- latitude
				lng			- longitude
				_callback	- callback function
	output	:	void
*/
var TRMCenterAt = function (lat, lng, _callback) {
	var c = new GLatLng(lat, lng);
	TRMMapInstance.setCenter(c, TRMSearchZoom);
	if (_callback != null) {
		var b = TRMMapInstance.getBounds();
		_callback(b.getNorthEast().toString(), b.getSouthWest().toString());
	}
}

//
// Added on 22/01/2008
//
// BEGIN
//

/*
function TRMCenterAtMarker
	info	:	center at specify property id
	input	:	id			- property id
				_callback	- callback function
	output	:	void
*/
var TRMCenterAtProperty = function (id, _callback) {
	var c = TRMProperties[id].getLatLng();
	TRMMapInstance.setCenter(c, TRMSearchZoom);
	if (_callback != null) {
		var b = TRMMapInstance.getBounds();
		_callback(b.getNorthEast().toString(), b.getSouthWest().toString());
	}
}

/*
function TRMOpenInfoWindow
	info	:	center at specify property id
	input	:	id			- property id
				txt			- window head, can be html
				txt			- window content, can be html 
				_callback	- callback function
	output	:	void
*/
var TRMOpenInfoWindow = function (id, head, txt, _callback) {
	TRMEw.openOnMarker(TRMProperties[id], TRMEWindowWarper(head, txt));
	if (_callback != null) {
		_callback(TRMProperties[id]);
	}
}

//
// END
//

/*
function TRMSearchAt
	info	:	create search marker and center at specify lat, lng
	input	:	lat			- latitude
				lng			- longitude
				_callback	- callback function
	output	:	void
*/
var TRMSearchAt = function (lat, lng, _callback) {
	var _ap = TRMAddMarker (TRMSearchPropIndex, lat, lng, TRMSearchMarker);
	var c = _ap.getLatLng();
	TRMMapInstance.setCenter(c, TRMSearchZoom);
	if (_callback != null) {
		var b = TRMMapInstance.getBounds();
		_callback(b.getNorthEast().toString(), b.getSouthWest().toString());
	}
}

/*
function TRMEWindowWraper
	info	:	create content for ewindow
	input	:	h			-	ewindow header
				c			-	ewindow content
	output	:	text		-	html format
*/
var TRMEWindowWarper = function (h, c) {
	return	'<table cellpadding="0" cellspacing="0" border="0" ><tr><td style="BACKGROUND-REPEAT: no-repeat;" background="/Img/infowindow/top_left.png"><img src="/Img/infowindow/blank.gif" border="0" width="11" height="1" /></td>		<td background="/Img/infowindow/top_bg.png" style="background-repeat: repeat-x;background-position: top;color:white;font-weight:bold;font-size:1.2em;" valign="middle" align="center" nowrap="nowrap"><div style="width:190px">'+h+
			'</div><a href="javascript:TRMEw.hide()"><img width="14" height="13" title="Close" src="/_property/Img/eclose.gif" border="0" style="position:absolute;right:7px;top:4px" /></a></td><td><img src="/Img/infowindow/top_right.png" border="0" alt="top_right" /></td></tr><tr><td background="/Img/infowindow/left_bg.png"  ><img src="/Img/infowindow/blank.gif" border="0" width="1" height="1" /></td><td background="/Img/infowindow/mid_bg.png"  >' + c +
			'</td><td background="/Img/infowindow/right_bg.png" style="background-repeat: repeat-y;background-position: left;" ><img src="/Img/infowindow/blank.gif" border="0" width="1" height="1" /></td></tr><tr><td style="BACKGROUND-REPEAT: no-repeat;" background="/Img/infowindow/bottom_left.png"><img src="/Img/infowindow/blank.gif" border="0" width="11" height="1" /></td>	<td background="/Img/infowindow/bottom_bg.png" valign="bottom" style="background-repeat: repeat-x;background-position: bottom;" ><img src="/Img/infowindow/blank.gif" border="0" width="1" height="1" /></td><td><img src="/Img/infowindow/bottom_right.png" border="0" alt="bottom_right" /></td></tr></table>';
}

/*
function TRMCreateDirection
	info	:	create direction between 2 point
	input	:	p1			-	first point (object)
				p2			-	second point (object)
				_panel		-	panel (div element)
				_callback	-	callback function
	output	:	void
*/
var TRMCreateDirection = function (p1, p2, _panel, _callback) {
	var P1 = new GLatLng(p1.lat, p1.lng);
	var P2 = new GLatLng(p2.lat, p2.lng);
	if (TRMDirectionInstance == null) {
		if (_panel != null) {
			TRMDirectionInstance = new GDirections(TRMMapInstance, _panel);
		} else {
			TRMDirectionInstance = new GDirections(TRMMapInstance);
		}
	} 

	TRMDirectionInstance.loadFromWaypoints([P1.toUrlValue(6), P2.toUrlValue(6)], {locale:'th_TH', getPolyline:true, preserveViewport:true});

	GEvent.addListener(TRMDirectionInstance,"load", function() {
		if (_callback != null) {
			var d = TRMDirectionInstance.getDistance();
			_callback(d);
		}
	});

	GEvent.addListener(TRMDirectionInstance,"error", function() {
		alert("Failed: "+TRMDirectionInstance.getStatus().code);
	});
}

/*
function getPropJson
	info	:	get all properties in viewport (JSON)
	input	:	void
	output	:	void
*/
var getPropJson = function() {
	var bounds = TRMMapInstance.getBounds();
	ne = bounds.getNorthEast().toString();
	sw = bounds.getSouthWest().toString();
		q = Object.toQueryString({
		'ne' : ne,
		'sw' : sw
	});
	var loadPropAjax = new Ajax (TRMListPropURL, {
		data:q,
		onComplete:TRMLoadMarkers
	}).request(); 
}

/*
function TRMCreateDirection2
	info	:	create direction between 2 point (custom icom and info window)
	input	:	p1			-	first point (object)
				p2			-	second point (object)
				_panel		-	panel (div element)
				_callback	-	callback function
	output	:	void
*/
var TRMCreateDirection2 = function (p1, p2, _panel, _callback) {
	var P1 = new GLatLng(p1.lat, p1.lng);
	var P2 = new GLatLng(p2.lat, p2.lng);
	if (TRMDirectionInstance == null) {
		if (_panel != null) {
			TRMDirectionInstance = new GDirections(null, _panel);
		} else {
			TRMDirectionInstance = new GDirections();
		}
	} else {
	}

	TRMDirectionInstance.loadFromWaypoints([P1.toUrlValue(6), P2.toUrlValue(6)], {locale:'th_TH', getPolyline:true, preserveViewport:true});

	GEvent.addListener(TRMDirectionInstance,"load", function() {
		//var n = TRMDirectionInstance.getPolyline().getVertexCount();
		//var v1 = TRMDirectionInstance.getPolyline().getVertex(0);
		//var v2 = TRMDirectionInstance.getPolyline().getVertex(n-1);
		//TRMDirectionVertexIndex = (n-1);
		var v1Lat = p1.lat;
		var v1Lng = p1.lng;
		var v2Lat = p2.lat;
		var v2Lng = p2.lng;
		var v1Txt = (p1.txt == undefined) ? null : p1.txt;
		var v2Txt = (p2.txt == undefined) ? null : p2.txt;
		var v1CallBack = (p1.callBack == undefined) ? null : p1.callBack;
		var v2CallBack = (p2.callBack == undefined) ? null : p2.callBack;
		
		if (TRMProperties[TRMFDirectionPropIndex] != null) {
			TRMMapInstance.removeOverlay(TRMProperties[TRMFDirectionPropIndex]);
			TRMProperties[TRMFDirectionPropIndex] = null;
			TRMMapInstance.removeOverlay(TRMProperties[TRMSDirectionPropIndex]);
			TRMProperties[TRMSDirectionPropIndex] = null;
		}

		TRMAddMarker (TRMFDirectionPropIndex, v1Lat, v1Lng, TRMFDirectionMarker, v1Txt, false, v1CallBack);
		TRMAddMarker (TRMSDirectionPropIndex, v2Lat, v2Lng, TRMSDirectionMarker, v2Txt, false, v2CallBack);
		if (TRMPolyline != null) {
			TRMMapInstance.removeOverlay(TRMPolyline);
		}
		TRMPolyline = TRMDirectionInstance.getPolyline();
		TRMMapInstance.addOverlay(TRMPolyline);
		/*
		*/
		if (_callback != null) {
			var d = TRMDirectionInstance.getDistance();
			_callback(d);
		}
	});

	GEvent.addListener(TRMDirectionInstance,"error", function() {
		alert("Failed: "+TRMDirectionInstance.getStatus().code);
	});
}


