﻿function RealtyItemManager(items, enableSearch, servicePath)
{
	this._minZoom = 10;
	this._items = items;
	this._servicePath = servicePath;
	
	if (g_map.IsEnabled)
		this.MinZoom = Math.min(this._minZoom, g_map.GetZoom());

	if (g_map.GetZoom() > this._minZoom)
		g_map.SetZoom(this._minZoom);
	
	this.EnableSearch = enableSearch && g_map.IsEnabled;
  
	this.ShowOnMap = function()
	{
		for (var i=0; i<this._items.length; i++)
		{
			var item = this._items[i];			
			item._realtyItemManager = this;
			item.ShowOnMap();
		}
						
		var instance = this;
		
		if (this.EnableSearch)
		{
			g_map.OnMapMoved = function(considerable) { if (considerable) instance.MapMoved(); };
			g_map.AttachZoomHandler(function(oldLevel, newLevel) { instance.MapZoomed(oldLevel, newLevel); });
		}
	}
	
	this.DoSearchOnMap = function()
	{
		var b = g_map.GetBounds();	
		
		var boundsField = $get('MapBounds');
		boundsField.value = b.swLat + ',' + b.swLng + ',' + b.neLat + ',' + b.neLng;
		
		g_map.ClearMarkers();
				
		if (typeof this.SearchOnMapHandler == 'function')
			this.SearchOnMapHandler();
	}
	
	this.MapMoved = function()
	{
		this.DoSearchOnMap();
	}
	
	this.MapZoomed = function(oldLevel, newLevel)
	{	
		if (newLevel < this.MinZoom)
		{
			g_map.SetZoom(this.MinZoom);
			return;
		}
							
		if (oldLevel > newLevel)
		{
			this.DoSearchOnMap();
		}		
	}

	this.FindItem = function(id)
	{
		for (var i = 0; i < this._items.length; i++)
		{
			var item = this._items[i];

			if (item.Items)
			{
				for (var j = 0; j < item.Items.length; j++)
					if (item.Items[j].Id == id)
					return item;
			}
			else if (item.Id == id)
				return item;
		}

		return null;
	}
	
	this.FindOnMap = function(id)
	{
		var items = this.FindItem(id);
		if (items != null)
		{
			items.FindOnMap();
		}		
	}
}

// ======== map item ========= //
function MapItem(lat, lng)
{
	if (arguments.length > 0)
		this.Init(lat, lng);
}

MapItem.prototype.Init = function(lat, lng)
{
	this.Lat = lat;
	this.Lng = lng;
	this._realtyItemManager = null;
}

MapItem.prototype.ShowInfoPanel = function()
{
	if (!this._realtyItemManager)
		return;

	if (this.Result)
	{
		this.ShowInfoPanelCallBack(this.Result, null);
	}
	else
	{
		var params = this.CreateInfoPanelParameters();

		Sys.Net.WebServiceProxy.invoke(this._realtyItemManager._servicePath, this.ShowInfoPanelMethod, false, params,
			Function.createDelegate(this, this.ShowInfoPanelCallBack));
	}
}

MapItem.prototype.ShowInfoPanelCallBack = function(result, ctx)
{
	this.Result = result;

	var div = document.createElement('div');

	div.align = "left";
	div.innerHTML = this.CreateInfoWindowHtml(result);

	g_map.ShowInfoWindow(this.Lat, this.Lng, div);
}

MapItem.prototype.CreateInfoWindowHtml = function()
{
}

MapItem.prototype.HideInfoPanel = function()
{
	g_map.HideInfoWindow();
}

MapItem.prototype.ShowOnMap = function()
{
	if (!g_map.IsEnabled)
		return;

	var instance = this;
	var args = { 'mouseover': function() { instance.ShowInfoPanel(); } };
	
	// add marker
	g_map.AddMarker(this.Lat, this.Lng, g_ResHost + 'Images/Realty/' + this.GroupId + '.png', args);
}

MapItem.prototype.FindOnMap = function()
{
	if (!g_map.IsEnabled)
		return;

	g_map.SetCenter(this.Lat, this.Lng);

	this.ShowInfoPanel();
}


RealtyItems.prototype = new MapItem();
RealtyItems.prototype.constructor = RealtyItems;
RealtyItems.base = MapItem.prototype;

function RealtyItems(lat, lng, realtyItems) 
{
	this.Items = realtyItems;
	this.ShowInfoPanelMethod = "GetRealtyObjects";
	this.GroupId = 0;
	
	RealtyItems.base.Init.call(this, lat, lng);
}

RealtyItems.prototype.CreateInfoPanelParameters = function()
{
	var idList = [];

	for (var i = 0; i < this.Items.length; i++)
		idList.push(this.Items[i].Id);

	return { idList: idList };
}

RealtyItems.prototype.CreateInfoWindowHtml = function(result)
{
	var html = "<div class='realtyMapItemsRegion'>" + result[0].Region + "</div>";
	
	html += "<table cellpadding='0' cellspacing='0' class='realtyMapItems'>";
	html += "<tr><th>Тип</th><th>Цена</th><th>Площадь</th><th></th></tr>";
	for (var i = 0; i < result.length; i++)
	{
		var item = result[i];

		html += i % 2 == 1 ? "<tr class='altRow'>" : "<tr>";
		html += "<td align='center'><img src='" + item.IconUrl + "' /></td>";
		html += "<td>" + item.Price + "</td><td>" + item.Area + "</td>";
		html += "<td><a href='" + item.DetailsUrl + "'>подробнее</a></td>";
		html += "</tr>";
	}

	html += "</table>";

	return html;
}


RealtyItem.prototype = new MapItem();
RealtyItem.prototype.constructor = RealtyItem;
RealtyItem.base = MapItem.prototype;

function RealtyItem(id, lat, lng, groupId)
{
  this.Id = id;
  this.GroupId = groupId;
  this.ShowInfoPanelMethod = "GetRealtyObject";

  RealtyItem.base.Init.call(this, lat, lng);
}

RealtyItem.prototype.CreateInfoPanelParameters = function()
{
	return { id: this.Id };
}

RealtyItem.prototype.CreateInfoWindowHtml = function(result)
{	
	var html = "<table cellpadding='0' cellspacing='0' class='realtyMapItem'>";

	html += "<tr><td colspan='2'><img src='Images/0.gif' height='0' width='216' /></td></tr>";

	html += "<tr><td rowspan='4' class='thumb' valign='top'><img src='" + result.ThumbUrl + "' /></td>";
	html += "<td style='width:168px'><b>" + result.Category + "</b></td></tr>";
	html += "<tr><td class='price'>" + result.Price + "</td></tr>";
	html += "<tr><td>" + result.Region + "</td></tr>";
	html += "<tr><td>" + result.Area + "</td></tr>";

	var detailsHtml = "<a href=\"" + result.DetailsUrl + "\">подробнее</a>";
	var contactHtml = ""; //(result.ContactPage) ? "&nbsp;&nbsp;<a href=\"" + result.ContactPage + "\">contact</a>" : "";

	html += "<tr><td colspan='2'><hr style='width:100%'/></td></tr>";
	html += "<tr><td colspan='2' align='right' style='padding:0px 7px;'>" + detailsHtml + contactHtml + "</td></tr>";

	html += "</table>";

	return html;
}
