var gmap, mmgr, zoom, lat, lon, fade;

var defaultWidth = 720;

var hotel = new GIcon();
hotel.image = "/images/markers/marker-red.png";
// hotel.shadow = "/images/markers/marker-shadow.png";
hotel.iconSize = new GSize(20,22);
// hotel.shadowSize = new GSize(22,25);
hotel.iconAnchor = new GPoint(11,12);
hotel.infoWindowAnchor = new GPoint(10,7);

/*
var hotel2 = new GIcon();
hotel2.image = "/images/markers/marker-blue.png";
hotel2.shadow = "/images/markers/marker-shadow.png";
hotel2.iconSize = new GSize(20,22);
hotel2.shadowSize = new GSize(22,25);
hotel2.iconAnchor = new GPoint(11,12);
hotel2.infoWindowAnchor = new GPoint(10,7);
*/

$(document).ready(function() {

	if (GBrowserIsCompatible()) {

		gmap = new GMap2(document.getElementById("gmap"));
		gmap.setCenter(new GLatLng(lat, lon), zoom, G_NORMAL_MAP);
		
		G_NORMAL_MAP.getMinimumResolution = function () { return 6 };
		G_SATELLITE_MAP.getMinimumResolution = function () { return 6 };
		G_HYBRID_MAP.getMinimumResolution = function () { return 6 };

		gmap.addControl(new GLargeMapControl());
		gmap.addControl(new GScaleControl());
		gmap.addControl(new GMapTypeControl());
		
		GEvent.addListener(gmap, "moveend", mapMove);
		GEvent.addListener(gmap, "click", getMapMarker);
		
		mmgr = new MarkerManager(gmap);
		
	}
	
});

function getMapWidth() {
	var width = $("#gmap").width();
	if(width < defaultWidth) width = defaultWidth;
	
	return width;
}

function getMapHeight() {
	return $("#gmap").height();
}

/* add marker */
function createMarker(lat, lon, id, country, tooltip) {

	var point = new GLatLng(parseFloat(lat), parseFloat(lon));

	var marker = new PdMarker(point, hotel);
	marker.id = id;

	GEvent.addListener(marker, "click", getMapMarker);
	
	GEvent.addListener(marker, "mouseover", function() {
		marker.topMarkerZIndex();
	}); 

	GEvent.addListener(marker, "mouseout", function() {
		marker.restoreMarkerZIndex();
	});
	
	marker.setTooltip(tooltip + "<br/><img src=\"/" + country + "/thumbnails/" + id + ".jpg\" alt=\"" + name + "\" />");
	marker.setOpacity(100);

	return marker;

};

function getMapMarker(overlay, point) {

	if(overlay) {

		if(this.id == undefined) {
			return;
		}

		showLodging(this.id)
		setReload();
		
	} else if(point) {
		gmap.panTo(new GLatLng(point.y, point.x), gmap.getZoom());
	}
	
}

/* clear markers on map */
function clear() {
	mmgr.clearMarkers();
};

/* on gmap move */
var mapMoveTimer;
function mapMove() {

	if(mapMoveTimer != null) {
		mapMoveTimer.stop();
	}
	
	/* hide see also/show more */
	$("#show-more-results, #see-also").hide();
	
	/* reset current map list */
	$("#map-list").html('<p id="loading-search-area"><img src="/images/loading/loading.gif" alt="Loading Search Area" /> loading search area</p>');
	$("#map-status").empty();
    
	mapMoveTimer = $.timer(2000, function (timer) {

		var centerPoint = gmap.getCenter();

		$.post("/page/fetch/", "lat=" + centerPoint.lat() + "&lon=" + centerPoint.lng() + "&zoom=" + gmap.getZoom() + "&height=" + getMapHeight() + "&width=" + getMapWidth() + "&" + $("#selectGroup").serialize(), function(data) {

			/* update place into */
			$("#page-details").html(data.pageHeader);
			
			/* page title */
			document.title = data.title;
			
			/* country */
			$('#page-logo span').html(data.country);

			/* update status */
			$("#map-status").text(data.status);
			
			/* show more */
			$("#show-more-results").replaceWith(data.showMore);
			
			/* update see also */
			$("#see-also ul").html(data.seeAlso);
			$("#see-also").show();
			
			/* update adverts */
			$("#gmap-ad").html(data.centerAdvert);
			$("#right-ad").html(data.rightAdvert);

			/* add markers */
			var resultArray = new Array();
			$.each(data.entries, function(i, result) {
				resultArray[i] = result;
			});
			
			$("#map-list").html(resultArray.join(''));
			
			eval(data.javascript);

		}, "json");

		setReload();
	
		timer.stop();
		
	});

}

$(document).ready(function() {

	$("#show-more-results a").livequery("click", function(event) {

		/* add loading */
		var old = $("#show-more-results").html();
		$("#show-more-results").html('<p id="loading-search-area"><img src="/images/loading/loading.gif" alt="Fetching more results" /> fetching more results</p>');
	
		var centerPoint = gmap.getCenter();
		
		$.post("/page/fetch/", "lat=" + centerPoint.lat() + "&lon=" + centerPoint.lng() + "&zoom=" + gmap.getZoom() + "&height=" + getMapHeight() + "&width=" + getMapWidth() + "&" + $("#selectGroup").serialize() + "&start=" + $("#map-list div.entry").length, function(data) {

			var resultArray = new Array();
			$.each(data.entries, function(i, result) {
				resultArray[i] = result;
			});
			
			$("#map-list").append(resultArray.join(''));
			
			/* remove loading */
			$("#show-more-results").html(old);
			
			/* update counter */
			$("#show-more-results strong").html($("#map-list div.entry").length);
			
			/* no more lodgings to come */
			if(data.count == $("#map-list div.entry").length) {
				$("#show-more-results").hide();
			}
			
		}, "json");
        
		setReload();
		
		event.preventDefault();
		
	});
	
});