var gmap_on = false;
var gmap_in_admin = false;
var gmap_x = 0;
var gmap_y = 0;
var gmap_zoom = 12;

var map;

var icons = new Array();
var i_types = ['house', 'flat', 'land', 'comm', 'vip'];
for(var i in i_types){
	icons[i_types[i]] = new GIcon();
	icons[i_types[i]].image = "/img/gmap_" + i_types[i] + ".gif";
	icons[i_types[i]].shadow = "/img/blank.gif";
	icons[i_types[i]].iconSize = new GSize(20, 20);
	icons[i_types[i]].shadowSize = new GSize(20, 20);
	icons[i_types[i]].iconAnchor = new GPoint(10, 10);
	icons[i_types[i]].infoWindowAnchor = new GPoint(10, 10);

	icons[i_types[i] + '_sel'] = new GIcon();
	icons[i_types[i] + '_sel'].image = "/img/gmap_" + i_types[i] + "_sel.gif";
	icons[i_types[i] + '_sel'].shadow = "/img/blank.gif";
	icons[i_types[i] + '_sel'].iconSize = new GSize(20, 20);
	icons[i_types[i] + '_sel'].shadowSize = new GSize(26, 26);
	icons[i_types[i] + '_sel'].iconAnchor = new GPoint(13, 13);
	icons[i_types[i] + '_sel'].infoWindowAnchor = new GPoint(13, 13);
}

// маркер для админа
icons['admin'] = new GIcon();
icons['admin'].image = "/img/gmap_admin.gif";
icons['admin'].shadow = "/img/blank.gif";
icons['admin'].iconSize = new GSize(9, 9);
icons['admin'].shadowSize = new GSize(9, 9);
icons['admin'].iconAnchor = new GPoint(4, 4);

var admin_marker = new GMarker(new GLatLng(0, 0), icons['admin']);
var admin_marker_active = false;
var admin_y = 0;
var admin_x = 0;

function gmap_init(){
	if(GBrowserIsCompatible()){
		map = new GMap2(document.getElementById('map_canvas'));

        /* ТИПЫ КАРТ */
		// G_NORMAL_MAP – вид по умолчанию.
		// G_SATELLITE_MAP – снимки, сделанные со спутника, для Google Планета Земля.
		// G_HYBRID_MAP – комбинация обычных изображений и режима просмотра со спутника.
		// G_DEFAULT_MAP_TYPES – набор этих трех типов, удобен для повторной обработки.
		// G_PHYSICAL_MAP – физическая карта, основанная на информации о ландшафте.
		map.setMapType(G_HYBRID_MAP);
		map.setCenter(new GLatLng(gmap_y, gmap_x), gmap_zoom);
		map.setUIToDefault();
		map.disableScrollWheelZoom();

		if(gmap_in_admin){
			if(admin_y || admin_x) gmap_set_admin_point(new GLatLng(admin_y, admin_x));

			GEvent.addListener(map, 'click', function(overlay, latlng){
				if(latlng){
					document.getElementById('coord_y').value = latlng.lat();
					document.getElementById('coord_x').value = latlng.lng();
					gmap_set_admin_point(latlng);
				}
			});
		}
	}
}

function gmap_set_admin_point(latlng){
	admin_marker.setLatLng(latlng);

	if(!admin_marker_active){
		map.addOverlay(admin_marker);
		admin_marker_active = true;
	}
}




function gmap_goto(y, x, zoom){
	if(!zoom) zoom = 12;
	map.panTo(new GLatLng(y, x));
	map.setZoom(zoom);
}

function gmap_select_region(coords){
	var c = coords.split(',');
	gmap_goto(c[0], c[1]);
}

function gmap_select_region2(sel){
	if(!document.getElementById('map_regions')) return;

    var opts = sel.getElementsByTagName('OPTION');
    var num = 0;
    for(var i = 0, l = opts.length; i < l; i++)
    	if(opts[i].value == sel.value){
    		num = i;
    		break;
    	}
	if(!num) return;

	var opts = document.getElementById('map_regions').getElementsByTagName('OPTION');
	if(!opts[num - 1]) return;

	gmap_select_region(opts[num - 1].value);
}



var items_on_map = new Array();
var cur_x = 0, cur_y = 0;
function gmap_draw_items(){
	for(var i= 0, l = items_on_map.length; i < l; i++){
		var sel = cur_x == items_on_map[i].x && cur_y == items_on_map[i].y;

		var icon_name = items_on_map[i].icon; if(sel) icon_name += '_sel';
		var coord = new GLatLng(items_on_map[i].y, items_on_map[i].x);
		var marker = new GMarker(coord, {icon: icons[icon_name], title: decodeURIComponent(items_on_map[i].name)});


		marker.name = items_on_map[i].name;
		marker.items = items_on_map[i].items;

		GEvent.addListener(marker, 'click', function(){
			var html = '';
			for(var i = 0, l = this.items.length; i < l; i++){
				var item = this.items[i];
				if(html != '') html += '<hr>';

				html += '<a href="' + item.url + '"><img src="/photos/' + item.photo + '.jpg?w=50&h=50" alt=""></a>' +
				'<a href="' + item.url + '">ID: ' + item.outer_id + '</a><br>';
				if(item.square) html += 'Площадь: ' + item.square + ' м&sup2;<br>';
				if(item.square_land) html += 'Участок: <span class="square_land"><span class="display_val">' + item.square_land + ' м&sup2;</span><span class="real_val">' + item.square_land + '</span></span><br>';
				if(item.floor) html += 'Этаж: ' + item.floor + '<br>';
				if(item.sea_distance) html += 'До моря: ' + item.sea_distance + ' м<br>';
				if(item.cost_rent) html += 'Аренда: <b class="red"><nobr><span class="cost_block"><span class="display_val">' + item.cost_rent + ' THB</span><span class="real_val">' + item.cost_rent + '</span></span></nobr></b><br>';
				if(item.cost_buy) html += 'Продажа: <b class="red"><nobr><span class="cost_block"><span class="display_val">' + item.cost_buy + ' THB</span><span class="real_val">' + item.cost_buy + '</span></span></nobr></b>';
				html += '<div class="cl"></div>';
			}
            html = '<b class="red">' + this.name + '</b><div class="items_on_gmap"' + (this.items.length > 2 ? ' style="height: 250px"' : '') + '>' + html + '</div>';

			this.openInfoWindowHtml(html);
		});

	    GEvent.addListener(marker, 'infowindowopen', function(){
			set_display_cur();
		});

		map.addOverlay(marker);
	}
}



add_handler(window, 'load', function(){
	if(gmap_on){
		gmap_init();
		gmap_draw_items();
	}
});