﻿var _map;
var _geocoder;
var _maxZoomLevel = 20;
var _edcounter = 0;
var _qps = 1;
var _qpsIndex = 0;
var _bounds;
var _totalTime;

function GetLocations()
{
	$("#spMapComplete").html(parseInt(_totalTime / 5));
	_totalTime--;

	var start = _qpsIndex * _qps;
	_qpsIndex++;

	var done = true;

	for (var i = start; i < start + _qps && i < _estateData.length; i++)
	{
		done = false;

		_geocoder.getLocations(_estateData[i].location, function (result)
		{
			if (result.Status.code == G_GEO_SUCCESS)
			{
				_bounds.extend(new google.maps.LatLng(result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0]));
				var zoom = _map.getBoundsZoomLevel(_bounds);
				if (_mobileRequest)
				{
					zoom -= 4;
				}
				_map.setCenter(_bounds.getCenter(), zoom);

				addAddressToMap(result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0], result.Placemark[0], _estateData[_edcounter]);
			}

			_edcounter++;
		});
	}

	if (done)
	{
		$("#divMapComplateContainer").fadeOut("slow");

		HideLoading();
	}
	else
		setTimeout('GetLocations()', 200);
}

function DisplayLoading()
{
	var scrollTop = document.all ? document.documentElement.scrollTop : window.pageYOffset;
	var ajaxLoader = document.getElementById('LoadingMap');
	var bounds = $('#mapLayer').bounds();
	ajaxLoader.style.position = 'absolute';
	ajaxLoader.style.left = (bounds.x + (bounds.width / 2) - (ajaxLoader.offsetWidth / 2)) + 'px';
	ajaxLoader.style.top = scrollTop + (screen.height / 2 - 200) - (ajaxLoader.offsetHeight / 2) + 'px';
	ajaxLoader.style.display = document.all ? 'inline' : ajaxLoader.tagName.toLowerCase() == 'table' ? 'table' : 'block';
}

function HideLoading()
{
	document.getElementById('LoadingMap').style.display = 'none';
}

function getViewportSize()
{
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number')
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return { width: myWidth, height: myHeight };
}

function pageOnLoad()
{
	// check to see if the map is bigger than the screen
	var mapContainer = document.getElementById("map");
	var viewportSize = getViewportSize();
	if (parseInt(mapContainer.style.width) > viewportSize.width)
	{
		mapContainer.style.width = viewportSize.width + 'px';
	}
	if (parseInt(mapContainer.style.height) > viewportSize.height)
	{
		mapContainer.style.height = viewportSize.height + 'px';
	}

	_edcounter = 0;
	_map = null;

	_bounds = new google.maps.LatLngBounds();

	showMap();

	_geocoder = new google.maps.ClientGeocoder();
	_qpsIndex = 0;

	_totalTime = _estateData.length % _qps == 0 ? parseInt(_estateData.length / _qps) : parseInt(_estateData.length / _qps) + 1;

	if ($("#divMapComplateContainer"))
	{
		$("#divMapComplateContainer").css("display", "block");
	}

	GetLocations();
}

function showMap()
{
	if (!_map)
	{
		_map = new google.maps.Map2(document.getElementById("map"));
		_map.addControl(new google.maps.SmallMapControl());
		_map.addControl(new google.maps.MapTypeControl());
		_map.continuousZoomEnabled();
		_map.enableContinuousZoom();
		_map.enableDoubleClickZoom();
		GEvent.addListener(_map, "zoomend", function(ozoom, nzoom)
		{
			if (nzoom == _maxZoomLevel)
				_map.setZoom(_maxZoomLevel - 1);
		});
	}
}

function addAddressToMap(lat, lng, place, estateRow)
{
	var icon = new google.maps.Icon();
	icon.image = "/Pictures/icon-house.gif";
	icon.iconSize = new google.maps.Size(20, 20);
	icon.iconAnchor = new google.maps.Point(6, 20);
	icon.infoWindowAnchor = new google.maps.Point(5, 1);

	marker = new google.maps.Marker(new google.maps.LatLng(lat, lng), icon);
	_map.addOverlay(marker);

	marker.showInfos = function()
	{
		var html = '<div style="width: 250px;"><div style="float: right; margin-right: 10px;"><img src="' + estateRow.image + '" border="0" /></div><strong class="Text" style="font-size: 12px; color: #636363;">' + estateRow.type + '</strong>';
		if (estateRow.price.length > 0)
			html += '<br /><strong class="Text" style="color: #636363;">' + _priceTag + ' : </strong><span class="Text" style="color: #636363;">&euro; ' + estateRow.price + '</span>';
		if (estateRow.displayAddress)
			html += '<br /><strong class="Text" style="color: #636363;">' + _addressTag + ' : </strong><br /><span class="Text" style="color: #636363;">' + place.address.split(",").join("<br />") + '</span>';
		if (estateRow.message.length > 0)
			html += '<br /><span style="color: red; font-weight: bold;" class="Text">' + estateRow.message + '</span>';
		html += '</div>';

		this.openInfoWindowHtml(html);
	}

	marker.gotoDetails = function()
	{
		if (estateRow.linkToDetails)
			window.location.href = '/Web.mvc/' + _cultureTag + '/Detail/' + estateRow.estateID;
	}

	GEvent.addListener(marker, "mouseover", function()
	{
		this.showInfos();
	});

	GEvent.addListener(marker, "mouseout", function()
	{
		//this.closeInfoWindow();
	});

	GEvent.addListener(marker, "click", function()
	{
		this.gotoDetails();
	});
}
