// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
var marker = [];

// load สำหรับสร้างแผนที่บนหน้าเว็บไซต์
function buildMap(lat, lng, zoom) {
	if (GBrowserIsCompatible()) {
		/*
		var map = new GMap2(document.getElementById("map_div"));
		// ---------- //
		map.setCenter(new GLatLng(lat, lng), zoom);
		// ---------- //
		var customUI = map.getDefaultUI();
		customUI.maptypes.physical = false;
		map.setUI(customUI);
		*/
		map=new GMap(document.getElementById("map_div"));
		var center=new GLatLng(lat,lng);
		map.setCenter(center,zoom);
		controlPanelHeight=($('map_div').getHeight()-10);
		ddmapcontrol=new ddMapControl();
		ddmapcontrol.availableControlType=[5,1,2,4];
		map.addControl(ddmapcontrol);
		var customUI = map.getDefaultUI();
		customUI.maptypes.physical = false;
		map.setUI(customUI);
		map.disableDoubleClickZoom();
		GEvent.addListener(map,"zoom",getBlockAttributes);
	}
}

// ---------- //
function buildMapSearch(propertys) {
	if (GBrowserIsCompatible()) {
		/*
		var map = new GMap2(document.getElementById("map_div"));
		map.setCenter(new GLatLng(18.788180, 98.985872), 14);
		var customUI = map.getDefaultUI();
		customUI.maptypes.physical = false;
		map.setUI(customUI);
		*/
		map=new GMap(document.getElementById("map_div"));
		var center=new GLatLng(centerLatitude,centerLongitude);
		map.setCenter(center,startZoom);
		controlPanelHeight=($('map_div').getHeight()-10);
		ddmapcontrol=new ddMapControl();
		ddmapcontrol.availableControlType=[5,1,2,4];
		map.addControl(ddmapcontrol);
		var customUI = map.getDefaultUI();
		customUI.maptypes.physical = false;
		map.setUI(customUI);
		map.disableDoubleClickZoom();
		GEvent.addListener(map,"zoom",getBlockAttributes);
		// ---------- //
		var bounds = new GLatLngBounds(); 
		// ---------- //
		var reverseLoop = propertys.length; //alert(propertys.length);
		reverseLoop--; //alert(reverseLoop);
		// ---------- //
		for (var i = reverseLoop; i >= 0; i--) { 
			var point = new GLatLng(propertys[i]['property']['lat'], propertys[i]['property']['lng']); 
			//////////////////////////////////////////////////
			if(propertys[i]['property']['type'] == "red") {
				var markerIcon = buildRedIcon();
			} else if(propertys[i]['property']['type'] == "blue") {
				var markerIcon = buildBlueIcon();
			}
			//////////////////////////////////////////////////
			marker[i] = new GMarker(point, {icon: markerIcon});
			addEventInfo(marker[i], propertys[i]['property']['topic'], propertys[i]['property']['image'], propertys[i]['property']['price'], propertys[i]['property']['id_p'], propertys[i]['property']['tooltip']);
			addEvenClick(marker[i], propertys[i]['property']['id_p']);
			map.addOverlay(marker[i]); 
			bounds.extend(point); 
		} 
		// ---------- //
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
	}
}

// ---------- //
function buildRedIcon() {
	var myIcon = new GIcon();
	myIcon.image = "index/image/red.png";
	// myIcon.shadow = "index/image/shadow.png";
	// myIcon.shadowSize = new GSize(32, 32);
	myIcon.iconSize = new GSize(32, 32);
	myIcon.iconAnchor = new GPoint(16, 32);
	myIcon.infoWindowAnchor = new GPoint(32, 1);
	myIcon.transparent =  "index/image/trans.png";
	myIcon.imageMap=[9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0];
	// myIcon.printImage = ""; 
	// myIcon.mozPrintImage = ""; 
	// myIcon.printShadow = "";
	return myIcon;
}

// ---------- //
function buildBlueIcon() {
	var myIcon = new GIcon();
	myIcon.image = "index/image/blue.png";
	// myIcon.shadow = "index/image/shadow.png";
	// myIcon.shadowSize = new GSize(32, 32);
	myIcon.iconSize = new GSize(32, 32);
	myIcon.iconAnchor = new GPoint(16, 32);
	myIcon.infoWindowAnchor = new GPoint(32, 1);
	myIcon.transparent =  "index/image/trans.png";
	myIcon.imageMap=[9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0];
	// myIcon.printImage = ""; 
	// myIcon.mozPrintImage = ""; 
	// myIcon.printShadow = "";
	return myIcon;
}

// ---------- //
function addEventInfo(marker, topic, image, price, id_p, tooltip) {
	GEvent.addListener(marker, "mouseover", function() {
		var html = makeHTML(topic, image, price, id_p, tooltip);
		marker.openInfoWindowHtml(html);
	});	
}

// ---------- //
function makeHTML(topic, image, price, id_p, tooltip) {
	var html = "<table width=\"0%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\"><tr>";
	html += "<td align=\"left\" valign=\"top\"><a href=\"detail_property/index.php?id_p=" + id_p + "\" target=\"_blank\"><img src=\"images/" + image + "\" width=\"100\" height=\"75\" /></a></td>";
	html += "<td align=\"left\" valign=\"top\"><table width=\"0%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	html += "<td align=\"left\" valign=\"top\"><a href=\"detail_property/index.php?id_p=" + id_p + "\" target=\"_blank\" title=\"" + tooltip + "\"><span class=\"sublink\">" + topic + "</span></a></td></tr><tr>";
	html += "<td align=\"left\" valign=\"top\">" +  price+ "</td></tr></table></td></tr></table>";
	return html;
}

// ---------- //
function addEvenClick(marker, id_p) {
	GEvent.addListener(marker, "click", function() {
		open("detail_property/index.php?id_p=" + id_p);
	});	
}