var map = null;
var markers = {};
//взаимодействие с картой
function initializeMap() {
	
	map = new google.maps.Map2(document.getElementById("map"));
	
	setupBalakovoMap(map, true);

	// координаты фирм
	$(document).ready(function(){
		
		if (window.firms !== undefined)
		{
			
			var o = 0;
			for (var firmID in firms ) {
				var point = new google.maps.LatLng(firms[firmID]['coordY'], firms[firmID]['coordX']);

				if (o == 0) {
					map.setCenter(new google.maps.LatLng(firms[firmID]['coordY'], firms[firmID]['coordX']), defaultZoom);
				}
				
				map.addOverlay(createMarker(point, firms[firmID]));
				
				o++;
			}
			
		}		
		if (window.cities !== undefined)
		{
			
			var o = 0;
			for (var cityID in cities ) {
				var point = new google.maps.LatLng(cities[cityID]['coordY'], cities[cityID]['coordX']);

				if (o == 0) {
					map.setCenter(new google.maps.LatLng(cities[cityID]['coordY'], cities[cityID]['coordX']), defaultZoom);
				}
				
				map.addOverlay(createMarkerCity(point, cities[cityID]));
				
				o++;
			}
			
		}
		/*
		if (window.streets !== undefined)
		{
			var o = 0;
			for ( streetID in streets ) {
				var point = new google.maps.LatLng(streets[streetID]['coordY'], streets[streetID]['coordX']);

				if (o == 0) {
					map.setCenter(new google.maps.LatLng(streets[streetID]['coordY'], streets[streetID]['coordX']), defaultZoom);
				}
				
				map.addOverlay(createStreetMarker(point, streets[streetID]));
				
				o++;
			}
		}
		*/
		if (window.houses !== undefined)
		{
			var o = 0;
			for ( var houseID in houses ) {
				var point = new google.maps.LatLng(houses[houseID]['coordY'], houses[houseID]['coordX']);

				if (o == 0) {
					map.setCenter(new google.maps.LatLng(houses[houseID]['coordY'], houses[houseID]['coordX']), defaultZoom);
				}
				
				map.addOverlay(createHouseMarker(point, houses[houseID]));
				
				o++;
			}
		}
		
	});
}

function createMarker(point, firm) {
	var marker = new google.maps.Marker(point);
	  
	google.maps.Event.addListener(marker, "click", function() {
	    map.setCenter(new google.maps.LatLng(firm['coordY'], firm['coordX']));
	    //map.openInfoWindowHtml(point, firmInfo(firm));
	    
	    marker.openExtInfoWindow(
	            map,
	            "custom_info_window_bubble",
	            firmInfo(firm),
	            {beakOffset: 2}
	          ); 
	});
	
	markers['f' + firm['firmID']] = marker;
	
	return marker;
}
function createMarkerCity(point, city) {
	var marker = new google.maps.Marker(point);
	  
	google.maps.Event.addListener(marker, "click", function() {
	    map.setCenter(new google.maps.LatLng(city['coordY'], city['coordX']));
	    
	    marker.openExtInfoWindow(
	            map,
	            "custom_info_window_bubble",
	            cityInfo(city),
	            {beakOffset: 2}
	          ); 
	});
	
	markers['c' + city['cityID']] = marker;
	
	return marker;
}

function createStreetMarker(point, street) {
	var marker = new google.maps.Marker(point);
	  
	google.maps.Event.addListener(marker, "click", function() {
	    map.setCenter(new google.maps.LatLng(street['coordY'], street['coordX']));
	    //map.openInfoWindowHtml(point, firmInfo(firm));
	    marker.openExtInfoWindow(
	            map,
	            "custom_info_window_bubble",
	            streetInfo(street),
	            {beakOffset: 2}
	          ); 
	});
	
	markers['s' + street['streetID']] = marker;
	
	return marker;
}

function createHouseMarker(point, house) {

	// Create a lettered icon for this point using our icon class
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = "http://balakovomaps.ru/img/icons/map/shadow.png";
	baseIcon.iconSize = new GSize(49, 56);
	baseIcon.shadowSize = new GSize(75, 56);
	baseIcon.iconAnchor = new GPoint(14, 56);
	baseIcon.infoWindowAnchor = new GPoint(14, 24);
	baseIcon.imageMap = [[1,1],[1,30],[45,30],[45,1]];
	
	var number = house['number'];
	
	number = number.replace("/", "_");
	number = number.replace("а", "a");
	number = number.replace("б", "b");
	number = number.replace("в", "v");
	number = number.replace("г", "g");
	number = number.replace("д", "d");
	
	var letteredIcon = new GIcon(baseIcon);
	letteredIcon.image = "http://balakovomaps.ru/img/map1/markers/" + number + ".png";

	// Set up our GMarkerOptions object
	markerOptions = { icon:letteredIcon };
	var marker = new google.maps.Marker(point, markerOptions);

	  
	google.maps.Event.addListener(marker, "click", function() {
	    map.setCenter(new google.maps.LatLng(house['coordY'], house['coordX']));
	    //map.openInfoWindowHtml(point, firmInfo(firm));
	    marker.openExtInfoWindow(
	            map,
	            "custom_info_window_bubble",
	            houseInfo(house),
	            {beakOffset: 2}
	          ); 
	});
	
	markers['h' + house['houseID']] = marker;
	
	return marker;
}

function firmInfo(firm)
{
	var myHtml = '<div class="company-map-preview">';
    
    if (firm['logo'] != '')
		if (firm['blocked'] != '1')
		{
		myHtml += "<div class='maplogo' align='center'><a href='" + firm['url'] + "'>" + firm['logo'] + "</a></div>";
    	}
    myHtml += "<div class='mapinfo' style='text-align:center !important;'><h3>";
		if (firm['blocked'] != '1')
			{
			myHtml += "<a href='" + firm['url'] + "'>";
			}
    myHtml += "<div style='align:center !important;color:#0095da !important;'>" + firm['name'] + "</div>";
		if (firm['blocked'] != '1')
			{
			myHtml += "</a>";
			}
    myHtml += "</h3><p>" + firm['address'] + ", " + firm['phone'] + "</p>";
    myHtml += "</div></div>";
    
    return myHtml;
}

function cityInfo(firm)
{
	var myHtml = '<div class="company-map-preview">';
    
    if (firm['logo'] != '')
		if (firm['blocked'] != '1')
		{
		myHtml += "<div class='maplogo' align='center'><a href='" + firm['url'] + "'>" + firm['logo'] + "</a></div>";
    	}
    myHtml += "<div class='mapinfo' style='text-align:center !important;'><h3>";
		if (firm['blocked'] != '1')
			{
			myHtml += "<a href='" + firm['url'] + "'>";
			}
    myHtml += "<div style='align:center !important;color:#0095da !important;'>" + firm['name'] + "</div>";
		if (firm['blocked'] != '1')
			{
			myHtml += "</a>";
			}
    myHtml += "</h3>";
    myHtml += "</div></div>";
    
    return myHtml;
}

function streetInfo(street)
{
	var myHtml = '<div class="company-map-preview">';
    
    myHtml += "<h3>" + street['nameprefix'] + ' ' + street['name'] + "</h3>";
    
    myHtml += "</div>";
    
    return myHtml;
}

function houseInfo(house)
{
	var myHtml = '<div class="company-map-preview">';

	/*if (house['photofile'] != '')
    	myHtml +=  house['photofile'];*/
	
    myHtml += "<h3>" + house['street'] + ' д.' + house['number'] + "</h3>";
    myHtml += "<hr>";
    
    if (house['level'] != '' && house['type'] != '')
    	myHtml += "<p>" + house['level'] + ', ' + house['type'] + "</p>";
    
    if (house['firmName'] != '' && house['firmPhone'] != '')
    {
    	myHtml += "<p>Управляющая компания: " + house['firmName'] + "</p>";
    	myHtml += "<p>Телефон: " + house['firmPhone'] + "</p>";
    }
    
    if (house['url'] != '')
    	myHtml += "<a class='more' href='" + house['url'] + "'></a>";
    
    myHtml += "</div>";
    
    return myHtml;
}

function showMap()
{
	$("#jsHideMap").removeClass('hidden');
	$("#jsShowMap").addClass('hidden');
	$("div.map").removeClass('hidden');
	$("div.filter").removeClass('hidden');
	initializeMap();
}

function hideMap()
{
	$("div.map").addClass('hidden');
	$("div.filter").addClass('hidden');
	$("#jsHideMap").addClass('hidden');
	$("#jsShowMap").removeClass('hidden');
}

function showCompany(firm) {

	var point = new google.maps.LatLng(firm['coordY'], firm['coordX']);

	map.setCenter(point);
    //map.openInfoWindowHtml(point, firmInfo(firm));

	markers['f' + firm['firmID']].openExtInfoWindow(
            map,
            "custom_info_window_bubble",
            firmInfo(firm),
            {beakOffset: 2}
          ); 

    
    location.hash="#map";   
}

function showStreet(street) {

	var point = new google.maps.LatLng(street['coordY'], street['coordX']);

	map.setCenter(point);
    //map.openInfoWindowHtml(point, firmInfo(firm));

	markers['s' + street['streetID']].openExtInfoWindow(
            map,
            "custom_info_window_bubble",
            streetInfo(street),
            {beakOffset: 2}
          ); 

    
    location.hash="#map";   
}
function clearMap(){
        map.clearOverlays();
        polyPoints = [];
      }
function showHouse(house) {

	var point = new google.maps.LatLng(house['coordY'], house['coordX']);

	map.setCenter(point);
    //map.openInfoWindowHtml(point, firmInfo(firm));

	markers['h' + house['houseID']].openExtInfoWindow(
            map,
            "custom_info_window_bubble",
            houseInfo(house),
            {beakOffset: 2}
          ); 

    
    location.hash="#map";   
}
