var g_lat, g_lon, g_zoom, selected_marker, selected_icon, current_lable, points, map, baseIcon;

function g_load(){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("emap"));
						
		map.addControl(new GSmallZoomControl());
		map.addControl(new GMapTypeControl());

		map.setCenter(new GLatLng(g_lat, g_lon), g_zoom);
		
		baseIcon = new GIcon();
		baseIcon.iconSize = new GSize(17, 20);
		baseIcon.shadow = "";
		baseIcon.iconAnchor = new GPoint(8, 20);
		baseIcon.infoWindowAnchor = new GPoint(0, 0);
		baseIcon.infoShadowAnchor = new GPoint(0, 0);        
	}
}

function createCurrentMarker(map, point, options, name, description){
	marker = new GMarker(point, options);
	marker.value = name;

	var myHtml = "<b>" + name + "</b>" + "<br/>" + description;
	map.openInfoWindowHtml(point, myHtml);

	GEvent.trigger(marker, "click");
	
	return marker;		
}

function createMarker(map, point, options, name, description, link){
	marker = new GMarker(point, options);
	marker.value = name;
	GEvent.addListener(marker,"click", function() {
		if(link){
			window.location = link;
		}
		else{
			var myHtml = "<b>" + name + "</b>" + "<br/>" + description;
			map.openInfoWindowHtml(point, myHtml);
		}
	});
	
	GEvent.addListener(marker,"mouseover", function() {
		var myHtml = "<b>" + name + "</b>" + "<br/>" + description;
		selectMarker(this, map, myHtml);
	});

	GEvent.addListener(marker,"mouseout", function() {
		DeSelectMarker(map);
	});

	return marker;
}

function selectMarker(marker, map, hotel_info){
	DeSelectMarker(map);
	
	selected_marker = marker;
	icon = marker.getIcon();
	
	selected_icon = icon.image;
	marker.setImage("/templates/www/mini-oteli/images/marker-hotel-g.png");
	
	current_lable = new ELabel(marker.getLatLng(), hotel_info, "elabel", new GSize(20, 0), false, true);
	map.addOverlay(current_lable);
}

function DeSelectMarker(map){
	if(typeof(selected_marker) == 'undefined') return;
	
	selected_marker.setImage(selected_icon);
	
	selected_marker = undefined;
	
	current_lable.hide();
}

function G_SetPoints(){
	for(i=0;i<points.length;i++){
		if(points[i][11] == 0){
			hotelIcon = new GIcon(baseIcon);
			hotelIcon.image = GetHotelIcon(points[i][2]);
			markerOptions = { icon:hotelIcon };
			hotelmarker = createMarker(map, new GLatLng(points[i][0], points[i][1]), markerOptions, points[i][3], GetHotelDesc(points[i]), points[i][10]);
			map.addOverlay(hotelmarker);
		}
		else{
			hotelmarker = createCurrentMarker(map, new GLatLng(points[i][0], points[i][1]), null, points[i][3], GetHotelDesc(points[i]), points[i][10]);
			map.addOverlay(hotelmarker);
		}
	}
}

function GetHotelDesc(info){
	description = '<i>' + GetHotelCategory(info[2]) + '</i><br>';
	description += '<b>Номеров:</b> ' + info[5] + '<br>';
	description += '<b>Адрес:</b> ' + info[6] + '<br>';
	description += '<b>Метро:</b> ' + info[7] + '(' + info[8] + ')';
	
	if(info[9] != '' && info[9] != '0'){
		description += '<br><b>Цена от:</b> ' + info[9] + ' рублей';
	}
	
	return description;
}

function GetHotelCategory(category){
	switch(category){
		case 0:
			return 'Хостел';
		case 1:
			return 'Мини-отель';
	}
}

function GetHotelIcon(category){
	switch(category){
		case 0:
			return '/templates/www/mini-oteli/images/marker-hotel-b.png';
		case 1:
			return '/templates/www/mini-oteli/images/marker-hotel-o.png';
	}
}