var CONTEXT_WEB;
var PROPERTY_TYPE;
var map;
var gsmallZoomControl3D;
var darkLayer = null;
var mapWidth;
var mapHeight;
var resultMarkerList = new Array();
var searchParam = null;
var resultGMarkerOptions = new Array();

function initialized() {
	loadContextWeb();
	initializePinIcon();
}

function loadContextWeb() {
	var js = /GSmallMap\.js(\?.*)?$/;
	$$("head script[src]").findAll(function (s) {
		return s.src.match(js);
	}).each(function (s) {	
		var path = s.src.replace(js, "");		
		var paramContext = s.src.split("context=");
		paramContext = paramContext[1].split("&");
		CONTEXT_WEB = paramContext[0];
		var propertyType = s.src.split("propertyType=");
		PROPERTY_TYPE = propertyType[1];		
	});
		
}

function initializePinIcon() {

	hpinGIcon = new GIcon();
	hpinGIcon.image = CONTEXT_WEB+"/images/gmapcircle/smallhpin.png";
	hpinGIcon.iconSize = new GSize(22,25);
	hpinGIcon.shadowSize = new GSize(0,0);
	hpinGIcon.iconAnchor = new GPoint(11,25);
	hpinGIcon.infoWindowAnchor = new GPoint(16,8);
	
	resultGMarkerOptions[0] = {icon: hpinGIcon};
	
	for( i = 1 ; i <= 8 ; i++ ){
		hpinGIcon = new GIcon();
		hpinGIcon.image = CONTEXT_WEB+"/images/gmapcircle/smallpin_"+i+".png";		
		hpinGIcon.iconSize = new GSize(22,25);
		hpinGIcon.shadowSize = new GSize(0,0);
		hpinGIcon.iconAnchor = new GPoint(11,25);
		hpinGIcon.infoWindowAnchor = new GPoint(16,8); 
		
		resultGMarkerOptions[i] = {icon: hpinGIcon};
	}
}

function loadGMap() {

	initialized();

	if (GBrowserIsCompatible()) {
	
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(13.7645594, 100.5387212), 10); //vitory monument position
		gsmallZoomControl3D = new GSmallZoomControl3D();
		map.addControl(gsmallZoomControl3D);
		
		mapWidth = map.getSize().width;
		mapHeight = map.getSize().height;
		
		
		displayAdsenseOnMap();
		
		remoteQuery(PROPERTY_TYPE);
		
	}
	
}

function displayAdsenseOnMap() {
	var publisher_id = 'pub-1579519254457274';
	var adsManagerOptions = {
	  maxAdsOnMap : 2,
	  style: G_ADSMANAGER_STYLE_ADUNIT,
	  /* The channel field is optional - replace this field with a channel number */ 
	  /* for Google AdSense tracking */
	  channel: '5450456125'
	};

	adsManager = new GAdsManager(map, publisher_id, adsManagerOptions);
	adsManager.enable();
}


function constructParameter() {
		
	searchParam = {
	  lat: '',
	  lng: '',
	  radius: '',
	  propertyType: '' + PROPERTY_TYPE,
	  adsPrice: '',
	  adsSpace: '',
	  searchBack: '',
	  page: '' + 1
	};
}

function remoteQuery(propertyType) {

	PROPERTY_TYPE = propertyType;
	
	removeAllMarker();
	
	constructParameter();
	
	var callbackProxy = function(rtnMap) {
	
		searchParam = null;			
		enableGMap();
		
		var rtnPaging = rtnMap['paging'];
			
		if( rtnPaging.totalRecord > 0 ) {
		
			/* set current page no;*/
			/*$('page').value = rtnPaging.pageNo;*/
		
			var rtnList = rtnMap['list'];

			for( rtnIndex = 0 ; rtnIndex < rtnList.length ; rtnIndex++ ) {
				
				rtnMarkerBean = rtnList[rtnIndex];
				
				var marker = new GMarker( new GLatLng(rtnMarkerBean.lat, rtnMarkerBean.lng), resultGMarkerOptions[rtnMarkerBean.propertyTypeId]);
				
				map.addOverlay(marker);
				marker.show = false; /* show is my customise variable*/					
				marker.message = genMarkerHTML(rtnMarkerBean); /* message is my customise variable*/
				addClickToMarker(marker);
			
				resultMarkerList.push(marker);
								
				/*myMap.addControl(new TextualZoomControl());*/					
			}
	
		} else {
			/*not found any results*/
			alert('\u0e44\u0e21\u0e48\u0e1e\u0e1a\u0e23\u0e32\u0e22\u0e01\u0e32\u0e23\u0e04\u0e49\u0e19\u0e2b\u0e32');
		}
		
	};

	/* freez the map*/
	disableGMap();

	var callMetaData = { async: false, callback: callbackProxy };

	AjaxService.searchMapByType( searchParam, callMetaData );
}


function enableGMap() {
	map.enableDragging();
	if( gsmallZoomControl3D == null ) {
		gsmallZoomControl3D = new GSmallZoomControl3D();
	}
	map.addControl(gsmallZoomControl3D);	
	map.removeOverlay(darkLayer);

	$('pinAll').onclick = new Function( 'remoteQuery(0)' );
	$('pinHome').onclick = new Function( 'remoteQuery(1)' );
	$('pinCondo').onclick = new Function( 'remoteQuery(2)' );
	$('pinApartment').onclick = new Function( 'remoteQuery(3)' );
	$('pinTownHouse').onclick = new Function( 'remoteQuery(4)' );
	$('pinHomeOffice').onclick = new Function( 'remoteQuery(5)' );
	$('pinBuilding').onclick = new Function( 'remoteQuery(6)' );
	$('pinLand').onclick = new Function( 'remoteQuery(7)' );

}

function disableGMap() {

	$('pinAll').onclick = function() {};
	$('pinHome').onclick = function() {};
	$('pinCondo').onclick = function() {};
	$('pinApartment').onclick = function() {};
	$('pinTownHouse').onclick = function() {};
	$('pinHomeOffice').onclick = function() {};
	$('pinBuilding').onclick = function() {};
	$('pinLand').onclick = function() {};

	map.disableDragging();
	map.removeControl(gsmallZoomControl3D);
	
	bounds = [];
	bounds.push(map.fromContainerPixelToLatLng(new GPoint(0,0)));
	bounds.push(map.fromContainerPixelToLatLng(new GPoint(mapWidth,0)));		
	bounds.push(map.fromContainerPixelToLatLng(new GPoint(mapWidth,mapHeight)));
	bounds.push(map.fromContainerPixelToLatLng(new GPoint(0,mapHeight)));
	if( darkLayer == null ) {		
		darkLayer = new GPolygon(bounds, '#000000', 1, 1, '#000000', 0.1);
	}
	map.addOverlay(darkLayer);
	
	/* create center progress*/
	/*var sw = myMap.fromContainerPixelToLatLng(new GPoint(250,240));*/
	/*var ne = myMap.fromContainerPixelToLatLng(new GPoint(520,220));*/
	/*progressBar = new GGroundOverlay(CONTEXT_WEB+'/img/ajax-loader2.gif', new GLatLngBounds(sw, ne));*/	
	/*myMap.addOverlay(progressBar);*/	
	
}

function removeAllMarker() {
	
	if( resultMarkerList != null ) {	
		for(mIndex = 0 ; mIndex < resultMarkerList.length ; mIndex++) {
			resultMarkerList[mIndex].remove();
		}
	}
	resultMarkerList = null;
	resultMarkerList = new Array();
	
}

function addClickToMarker(marker) {
	
	GEvent.addListener(marker, "click", function () {
	
		if (!marker.show) {
			marker.openInfoWindowHtml( marker.message );
			
		} else {			
			marker.closeInfoWindow();
		}
	});
	
	GEvent.addListener(marker, "infowindowopen", function() { marker.show = true; });
	GEvent.addListener(marker, "infowindowclose", function() { marker.show = false; });
}

function genMarkerHTML(marker) {
		
	var html = '<div style="width:320px; height: 100%;">';
	
	//html += '<p>';
	var url = marker.externalUrl;
	url = CONTEXT_WEB + '/viewads?id='+marker.id+'&amp;desc='+marker.seoTitle;
	var title = unescape(escape(marker.title));
	var seoTitle = marker.seoTitle;
	if( title != null && title != '' ) {
		title.truncate(20, '...');
	} else {
		title = '';
	}

	var imgSrc = marker.imgThumbnail;
	if( marker.imgSrc != '' ) {
		html += '<div style="float:right; margin: 5px;">';
		html += '<a href="'+url+'" title="'+seoTitle+'" target="_blank" ><img src="'+imgSrc+'"></a>';
		html += '</div>';
	}

	if( title != '' ) {
		html += '<div style="font-size:13px;">';
		html += '<a href="'+url+'" title="'+seoTitle+'" target="_blank" >'+title+'</a>';
		html += '</div>';
	}
	var detail = '';
	if( detail != '' ) {
		var descript = ''+detail;			
		html += ''+descript.truncate(30, '...');
	}
	html += '<br/>';
	html += '<font style="font-size: 13px; color: #000000;">';
	html += '\u0e1b\u0e23\u0e30\u0e01\u0e32\u0e28 ' + marker.adsType + '<br/>';
	html += '\u0e1b\u0e23\u0e30\u0e40\u0e20\u0e17 ' + marker.propertyType + '<br/>';
	if( marker.price != '' ) {
		html += '\u0e23\u0e32\u0e04\u0e32  ' + marker.price + '\u0e1a\u0e32\u0e17 <br/>';
	} else {
		html += '\u0e23\u0e32\u0e04\u0e32  \u0e44\u0e21\u0e48\u0e23\u0e30\u0e1a\u0e38 <br/>';
	}
	html += '</font>';
	html += '<a href="'+url+'" title="'+seoTitle+'" target="_blank" ><font style="font-size: 12px; text-decoration: underline;">\u0e14\u0e39\u0e23\u0e32\u0e22\u0e25\u0e30\u0e40\u0e2d\u0e35\u0e22\u0e14</font></a>';
	html += '<br/>';
	//html += '</p>';
	html += '</div>';
	
	return html;
}

