var gmap_mini;
var gmap_big;

var hotelMarkers = [];
var markerManagers = [];
var hotels = new Array();
var geocoder;
var currentHotelId;

function init_big_map(container, x, y, id)
{
	if (currentHotelId == id) return;
	currentHotelId = id;
	
		gmap_big = new GMap2( container );
		geocoder = new GClientGeocoder();

		if ((x!=0 && y!=0))
		{
			var center = new GLatLng(x, y);
		}
		else
		{
			var center = new GLatLng(47.0255863, 28.83059263);
		}
		gmap_big.setCenter(center, 13);
		gmap_big.setUIToDefault();
		
		gmap_big.removeMapType(G_PHYSICAL_MAP);
		
		jQuery.ajax({
			type: "POST",
			url: "/ajax.html?task=getHotels",
			async: false,
			dataType: "xml",
			success: loadHotels
		});
		
		markerManagers['current'] = new MarkerManager(gmap_big, { borderPadding: 50, minZoom: 8, trackMarkers: true, show: false });
		
		markerManagers['other'] = new MarkerManager(gmap_big, { borderPadding: 50, minZoom: 8, trackMarkers: true, show: false });
		
		setupOtherHotels();
		
		if (hotels[ id ])
		{
			var center = new GLatLng(hotels[ id ].gmap_lat, hotels[ id ].gmap_lng);	
			gmap_big.setCenter(center, 13);		
		}		
}

function init_mini_map(container, x, y)
{
	gmap_mini = new GMap2( container );
		//geocoder = new GClientGeocoder();

		if (!(x!=0 && y!=0))
		{
			var center = new GLatLng(x, y);
		}
		else
		{
			var center = new GLatLng(47.0255863, 28.83059263);
		}
		gmap_mini.setCenter(center, 16);

		gmap_mini.setUIToDefault();
}

function saveHotelViewport(hotelId)
{
	if (confirm("Sure to save viewport for this hotel?"))
	{
		jQuery.ajax({
			type: "POST",
			url: "/ajax.html",
			async: false,
			data: "task=saveHotelViewport&HotelId=" + hotelId + "&gmap_lat=" + gmap_mini.getCenter().lat() + "&gmap_lng=" + gmap_mini.getCenter().lng() + "&zoom=" + gmap_mini.getZoom(),
			//		error: writeLog('Error', "Failed to load regions!"),
			success: function(data) {
						alert('Viewport saved!');
						}}
		);
	}
}

function saveHotel(hotelId, point)
{
	//if (confirm("Sure to save viewport for this hotel?"))
	{
		jQuery.ajax({
			type: "POST",
			url: "/ajax.html",
			async: false,
			data: "task=saveHotelViewport&HotelId=" + hotelId + "&gmap_lat=" + point.lat() + "&gmap_lng=" + point.lng() + "&zoom=" + gmap_big.getZoom(),
			//		error: writeLog('Error', "Failed to load regions!"),
			success: function(data) {
						alert('Viewport saved!');
						}}
		);
	}
}

function createHotelMarker(point,  id)
{
	var newIcon = new GIcon(G_DEFAULT_ICON);
	
	if (id == currentHotelId)
	{
		newIcon.image = "/images/icons/marker_hotel_008800.png";
		newIcon.iconSize = new GSize(37, 37);
		newIcon.shadowSize = new GSize(37, 37);
		newIcon.iconAnchor = new GPoint(15, 36);
		newIcon.infoWindowAnchor = new GPoint(17, 2);
	}
	else
	{
		newIcon.image = "/images/icons/markerS_hotel_AA0000.png";
		newIcon.iconSize = new GSize(25, 25);
		newIcon.shadowSize = new GSize(25, 25);
		newIcon.iconAnchor = new GPoint(10, 25);
		newIcon.infoWindowAnchor = new GPoint(13, 2);
	}

	newIcon.shadow = "/images/icons/shadow.png";

	var dragMarkers = false;
	if (editHotels)
	{
		dragMarkers = true;
	}
	if (id == currentHotelId)
	{
		markerOptions = { icon: newIcon, draggable: dragMarkers, zIndexProcess: function() { return 5;} };
	}
	else
	{
		markerOptions = { icon: newIcon, draggable: dragMarkers };		
	}
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "mouseover", function() {
		hotelInfoWindow(marker, id);
	});
	GEvent.addListener(marker, "click", function() {
		document.location.href = hotels[id].PageURL;
	});
	
	GEvent.addListener(marker, "dragstart", function()
	{
		gmap_big.closeInfoWindow();
	});

	GEvent.addListener(marker, "dragend", function()
	{
		if (confirm("OK to save new position for "+hotels[id].HotelName + "?"))
		{
			var newPosition = marker.getLatLng();
			hotels[id].gmap_lat = newPosition.lat();
			hotels[id].gmap_lng = newPosition.lng();

			marker.openInfoWindowHtml("New position will be here...");

			saveHotel(id, newPosition);
		}
		else
		{
			marker.setLatLng( new GLatLng(hotels[id].gmap_lat, hotels[id].gmap_lng) );
			marker.openInfoWindowHtml("New position will be here...");
		}
	});

	return marker;
}


function hotelInfoWindow(marker, id)
{
	//var infoContent = "<div class='infoWindow'><h3>" + hotels[id].HotelName + "</h3>";
	var infoContent = " <div class='infoWindow'><div class='hotelsDescription'>	<div class='hotelBlock relative'> <h2> 	<span>" + hotels[id].HotelName + "</span>";
	infoContent += "<span>";
	for(var i=0 ; i < hotels[id].HotelStars ; i++)
	{
		infoContent += "<img src='/images/star.gif' alt='star'>";
	}
	infoContent += " </span><br><span>Location: " + hotels[id].HotelLocation + "</span>";
	infoContent += "<a href='" + hotels[id].PageURL + "'><div class='startPrice'>"
	if (hotels[id].RegularStartPrice)
	{
		infoContent += "<p id='discountPrice'>" + hotels[id].RegularStartPrice + " EUR</p><br>";
	}
	if (hotels[id].StartPrice)
		infoContent += "" + hotels[id].StartPrice + " EUR";
	infoContent += "</div></a></h2></div></div>";
	
	infoContent += "<div class='thumbContainer'><img src='/images/icons/thumb_128_" + hotels[id].PictureURL + "'></div>";
	infoContent += "<p>" + hotels[id].HotelBrief + "</p>";
	infoContent += "</div>";
		
	marker.openInfoWindowHtml(infoContent);
	
	return "";
	var client = clients[pm_ads[id].clientId].name;
	var constructionType = constructionTypes[pm_ads[id].constructionTypeId].name;
	var address = pm_ads[id].address;
	var imagePath = pm_ads[id].imagePath;
	var regionId = pm_ads[id].regionId;
	var region = '';
	if (regions[ regionId ] )
	{
		region = regions[ regionId ].name
	}

	var infoContent = "<div class='infoWindow'><h3>" + client + ' - ' + constructionType+ "<h3>";
	infoContent += '<ul>';
	if (address != '')
	{
		infoContent += "<li>" + address + "</li>";
	}
	else
	{
		infoContent += "<li>" + "No address specified" + "</li>";
	}
	infoContent += "</ul>";

	if (imagePath != '')
	{
		infoContent += "<a title='Address: " + region + ". " + address + "' class='photo_thumb' href='/images/photos/" + imagePath + "'><div align='center' style='height:100px;width:100%px;'><img  src='/images/photos/thumb/" + imagePath + "'></div></a>";
	}
	infoContent += "</div><br><a href='javascript:alert(\"To remove - right click on the marker\")'>Remove construction</a>&nbsp;";


	if (firstMarkerLoaded == 0 )
	{
		setTimeout("applyPreviewHover()", 650);
		firstMarkerLoaded = 1;
	}

	applyPreviewHover();
}

function loadHotels(xml)
{	
	jQuery(xml).find("Hotel").each( function()
	{
		var id = jQuery(this).children("id").text();
		var gmap_lat = jQuery(this).children("gmap_lat").text();
		var gmap_lng = jQuery(this).children("gmap_lng").text();
		var HotelName = jQuery(this).children("HotelName").text();
		hotels[ id ] = { "id": id ,"PageURL":  jQuery(this).children("PageURL").text() ,"StartPrice":  jQuery(this).children("StartPrice").text() ,"RegularStartPrice":  jQuery(this).children("RegularStartPrice").text() ,"CityName":  jQuery(this).children("CityName").text() , "HotelStars":  jQuery(this).children("HotelStars").text() ,"HotelBrief":  jQuery(this).children("HotelBrief").text() ,"HotelTypeName":  jQuery(this).children("HotelTypeName").text() ,"HotelName":  HotelName , "HotelLocation": jQuery(this).children("HotelLocation").text(), "PictureURL": jQuery(this).children("PictureURL").text(), "gmap_lat":gmap_lat, "gmap_lng":gmap_lng, "zoom":jQuery(this).children("zoom").text() } ;
		
		if (hotels[id].gmap_lat>0)
		{
			
		}
		else
		{
			return;
			if (geocoder) 
			{
		        geocoder.getLatLng(
		          hotels[ id ].CityName + " " + hotels[ id ].HotelName,
		          function(point) 
		          {
		            if (!point) 
		            {
		            	return;
		            } 
		          }
		        );
	      }
		}
	});
		
}

function setupOtherHotels()
{
	hotelMarkers [ 'other' ] = new Array();
	hotelMarkers [ 'current' ] = new Array();
	
	for(id in hotels)
	{
		if ((hotels[id].gmap_lat > 0) && (id == currentHotelId ))
		{			
			var point = new GLatLng(hotels[id].gmap_lat, hotels[id].gmap_lng);
			hotelMarkers [ 'current' ].push( createHotelMarker(point,  id) );			
		}
	}
	for(id in hotels)
	{
		if ((hotels[id].gmap_lat > 0) && (id != currentHotelId))
		{		
			var point = new GLatLng(hotels[id].gmap_lat, hotels[id].gmap_lng);
			hotelMarkers [ 'other' ].push( createHotelMarker(point,  id) );
		}
	}
	
	markerManagers['other'].addMarkers( hotelMarkers [ 'other' ] , 8 );	
	markerManagers['other'].refresh();
		
	markerManagers['current'].addMarkers( hotelMarkers [ 'current' ] , 8 );	
	markerManagers['current'].refresh();
	
	//toggleOther(false);
}

function zIndexProcess()
{
		
}

function toggleOther(value)
{

	if (value)
	{
		for(var hotelMarker in hotelMarkers['other'])
		{
			if (hotelMarkers['other'][hotelMarker].show)
			hotelMarkers['other'][hotelMarker].show();
		}
	}
	else
	{
		for(var hotelMarker in hotelMarkers['other'])
		{
			if (hotelMarkers['other'][hotelMarker].hide)
			hotelMarkers['other'][hotelMarker].hide();
		}
	}
}



GMarker.prototype.hide = function()
{
	if (this.getPoint().lat() < 90)
	{
		try
		{
			this.savePoint = this.getPoint();

			this.setPoint(new GLatLng(90, 0));
		}
		catch (e) { }
	}
}

GMarker.prototype.show = function()
{
	if (this.getPoint().lat() == 90)
	{
		if (this.savePoint)
		{
			try
			{
				this.setPoint(this.savePoint);
				this.savePoint = null;
			}
			catch (e) { }
		}
	}
}

GMarker.prototype.isHidden = function()
{
	if (this.getPoint().lat() == 90)
	{
		return true;
	}
	else
	{
		return false;
	}
}
