var markersLoad = [];
var markerCollection = [];
var baseIcon = null;
var map;

function getBaseIcon()
{
    if (baseIcon === null) {
        baseIcon = new GIcon();
    }
    return baseIcon;
}

function createMarker(point, label, HTMLContent, hotelID) {
	var icon = new GIcon(getBaseIcon());
	icon.image = "http://www.dsbw.ru/google/icon/marker.png";
	var marker = new GMarker(point, icon);
	marker.label = label;
	marker.hotelID = hotelID;
	markerCollection.push(marker);
	
	GEvent.addListener(marker, "click", function() {
	     marker.openInfoWindowHtml(HTMLContent);
	});
	
	if($('hi' + hotelID)){
		Event.observe($('hi' + hotelID), 'click', function(event) {
			map.panTo(point);
			marker.openInfoWindowHtml(HTMLContent);
			Hotel.ShowMap();
		});
	}
	return marker;
}

function loadGoogleMap(){
	//alert(Hotel.GoogleMarkers.length);
	if(!Hotel.GoogleMarkers){
		return;
	}
	//var marker = [];
	var i = 1;
	baseIcon = getBaseIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);


	if (GBrowserIsCompatible()) {
		map = new GMap2($("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

		GEvent.addListener(map, "moveend", function() {
		});

		//alert(Hotel.GoogleMarkers);
		var xml = GXml.parse(Hotel.GoogleMarkers);
		var maplocate = xml.documentElement.getElementsByTagName("maplocate");
		maplocate = maplocate[0];
		//console.log("Print: "  + parseFloat(maplocate.getAttribute("Y").replace(",", ".")));
		//console.log("Print: "  + parseFloat(maplocate.getAttribute("X").replace(",", ".")));
		//console.log("Print: "  + parseFloat(maplocate.getAttribute("zoom").replace(",", ".")));
		map.setCenter(new GLatLng(parseFloat(maplocate.getAttribute("Y").replace(",", ".")), parseFloat(maplocate.getAttribute("X").replace(",", "."))), parseInt(maplocate.getAttribute("zoom").replace(",", ".")));

		//return;
		var markers =   xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) {
			var point = new GLatLng(parseFloat(markers[i].getAttribute('lat').replace(",", ".")), parseFloat(markers[i].getAttribute('log').replace(",", ".")));
			var HTMLContent = '';
			//HTMLContent += "<img src='http://www.dsbw.ru/data/hotels_photo/" + markers[i].getAttribute('hotelID') + "_small.jpg' border='1' id='hi" + markers[i].getAttribute('hotelID') + "'><br />";

			HTMLContent += "<br /><b>Название: </b>" + markers[i].getAttribute('HotelName') + " " + markers[i].getAttribute('HotelCategory') + "<br />";
			if(markers[i].getAttribute('HotelAddress').length > 2) {
				HTMLContent += "<b>Адрес: </b>" + markers[i].getAttribute('HotelAddress') + "<br />";
			}
			if(markers[i].getAttribute('HotelPhone').length > 2){
				HTMLContent += "<b>Телефон: </b>" + markers[i].getAttribute('HotelPhone') + "<br />";
			}
			if(markers[i].getAttribute('HotelEmail').length > 2){
				HTMLContent += "<b>Email: </b>" + markers[i].getAttribute('HotelEmail') + "<br />";
			}
			if(markers[i].getAttribute('HotelDescription').length > 2){
				HTMLContent += "<b>Описание: </b><br />" + markers[i].getAttribute('HotelDescription').replace("\n", "<br>") + "<br />";
			}
			HTMLContent += "<br /><a href='javascript:void(0);' onclick=\"$('hipnt" + markers[i].getAttribute('hotelID') + "').scrollTo();\">Перейти к отелю в результатах поиска</a>";
			HTMLContent += "<br />";
			HTMLContent = "<div style='width: 350px; text-align: left;'>" + HTMLContent + "</div>";
			map.addOverlay(createMarker(point, markers[i].getAttribute('label'), HTMLContent, markers[i].getAttribute('hotelID')));

		}


	}

}
