﻿

// limit of objects of a given category to be displayed
var limit=140;

// zooms on which limit is applied
var zooms = [13,13,14];



// for loading objects on map
mapInstance.prototype.getObjects = function(name, callback)
{
    var mapka = hashMaps.get(this.mapKey).mapka;
    mapka.updater.show();
    this.showBounds(name, callback);
};


if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
};




// invokes async postback to server, asking for places
mapInstance.prototype.getPlaces = function(category, from, to)
{
    var that = this;
        PageMethods.GetPlacesInTown(this.TownId, category, this.NSCoords, this.WECoords, from, to, function(res){
            var xTo = that.putPlaces(res, category);
            if(xTo > 0)
            {
                that.getPlaces(category, xTo, xTo+1000);
            }
            else
                hashMaps.get(that.mapKey).mapka.updater.hide();
        });
};
    
    
// callback function for getPlaces
// parses returned xml and puts some markers on the map
mapInstance.prototype.putPlaces = function(res, category, limited)
{

    var mapka = hashMaps.get(this.mapKey).mapka;
    var xmlDoc = GXml.parse(res);
    var markers = xmlDoc.documentElement.getElementsByTagName("place");
    var goOn = xmlDoc.documentElement.getAttribute("lastPart") == 'true' ? false : true;
    var et = xmlDoc.documentElement.getAttribute("et") == 'true' ? true : false;
    
    // et holds information if there are any objects of a given category in town.
    // if not -> uncheck and disable proper checkbox in the legend
    if(et)
    {
        var ctrl = this.mapKey+"_"+category+'s';
        document.getElementById(ctrl).disabled=true;
        document.getElementById(ctrl).checked=false;
		chgImg(category, false);
		
        document.getElementById(this.mapKey+"_"+category+'s_leg').style.color='gray';
        mapka.zoom.showInfo('Some of the selected objects do not exist in the database and cannot be presented.', true);
        return null;
    }
    if(limited)
        goOn = false;
        
    // create icon of a given category
    this.tmpIcon = createIcon(category, 0);
    
    for (var i = 1; i < markers.length; i++) {
      // obtain the attribues of each marker
      var lat = parseFloat(markers[i].getAttribute("lat"));
      var lng = parseFloat(markers[i].getAttribute("lng"));
      var point = new GLatLng(lat,lng);
      var name = markers[i].getAttribute("name");
      var id = markers[i].getAttribute("id");
      var dsc = parseInt(markers[i].getAttribute("dsc"), 10);
      var address = markers[i].getAttribute('address');
      
      
      if(address!= '' && lat == 0 && lng == 0)
      {
            mapka.getCoords(address, name, id, category, this, this.putPlacesMaker);
      }
      else
      {
          this.putPlacesMaker(point, id, name, category, this, markers[i].getAttribute("photo"), dsc);
      }
        
    }
     this.tmpIcon = null;
    document.getElementById(this.mapKey + '_got_' + category).value='1';
    
   
    
    return goOn;
};

// that function actually creates and puts marker on the map
mapInstance.prototype.putPlacesMaker = function(point, id, name, category, mapInst, photo, dsc)
{
      var mapka = hashMaps.get(mapInst.mapKey).mapka;
      var html = "";
      if(photo && photo != '')
        html = "<img src=\"http://photos.staypoland.com" + photo + "35x35.jpg\" alt=\"img\" /><div><b>"+name+"</b></div>";
      else
        html = "<div><b>"+name+"</b></div>";
      
      // create the marker
      if(mapInst.tmpIcon == null)
        mapInst.tmpIcon = createIcon(category, 0);
        
      var marker = null;
      if(category[0] + id == this.selectedId)
      {
        mapka.map.setCenter(point);
        mapka.mapCenter = point;
        marker = createTabbedMarker(point, [html], ["general"], true, false, createIcon(category, 1), name);
        mapka.map.addOverlay(marker);
        mapka.map.setZoom(15);
      }
      else
        marker = createTabbedMarker(point, [html], ["general"], true, false, mapInst.tmpIcon, name);

                    
      // fill some marker's attributes
      marker.dsc = dsc;
      marker.photo = photo;
      marker.categ = category;
      marker.oId = id;
      mapInst.allmarkers.push(marker);
      mapka.map.addOverlay(marker);    
      
      var tmp = getMarkerImg(marker);
      if(tmp)
      {
        helpHandler(marker);
      }
};
        
function helpHandler(marker)
{
        GEvent.addListener(marker, "mouseover", function(){            
            getMarkerImg(marker).style.zIndex = 9999;
        });  
        
        GEvent.addListener(marker, "mouseout", function(){            
            getMarkerImg(marker).style.zIndex = 0;
        });  
};
        
// putHotels helper function - hides updater indicator
mapInstance.prototype.putSHotels = function(res, clickedHotelId)
{
    this.putHotels(res, clickedHotelId);
    hashMaps.get(this.mapKey).mapka.updater.hide();
};

// callback function for getHotels
// parses returned xml and puts markers on the map
mapInstance.prototype.putHotels = function(res, clickedHotelId)
{
        var mapka = hashMaps.get(this.mapKey).mapka;
        mapka.updater.show();
        res = res.replace(/&lt;/g,'<'); // in some browsers character '<' is passed as '&lt;'
        var xmlDoc = GXml.parse(res);
        var markers = xmlDoc.documentElement.getElementsByTagName("hotel");
        

        this.tmpIcon = createIcon('hotel', 0);
        var postback = xmlDoc.documentElement.getAttribute("postback");
        var goOn = xmlDoc.documentElement.getAttribute("lastPart") == 'true' ? false : true;
        var bounds = new GLatLngBounds();

        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          
          if(isNaN(lat) || isNaN(lng))
            continue;


          var point = new GLatLng(lat,lng);
          var name = markers[i].getAttribute("name");
          var id = markers[i].getAttribute("id");
          var stars = markers[i].getAttribute("stars");

		  
          // create the marker
          var marker = null;
          
          if(this.bigIcon != null && this.bigIcon.oId == id)
            continue;
            
          if('h' + id == this.selectedId)
          {
            mapka.map.setCenter(point);
            mapka.mapCenter = point;
            marker = createTabbedMarker(point, [""], [""], true, false, this.tmpIcon, name);
          }
          else
          {
            marker = createTabbedMarker(point, [""], [""], false, false, this.tmpIcon, name);
          }
          
          marker.photo = markers[i].getAttribute("photo");
          marker.categ = 'hotel';
          marker.oId = id;
          marker.stars = stars;
          this.allmarkers.push(marker);
          mapka.map.addOverlay(marker);
          
          var tmp = getMarkerImg(marker);
          if(tmp)
          {
            helpHandler(marker);
          }

          if(!document.getElementById(this.mapKey + '_hotels').checked)
            marker.hide();
        }
        
        
        document.getElementById(this.mapKey + '_got_hotel').value='1';
        
        
        var that = this;
        
        // if this is 'postback' and infoWindow is opened - update it
        if(postback == 'true' && infoWndVisible)
            PageMethods.GetHotelInfo(parseInt(document.getElementById(clickedHotelId).value), 
                function(res){
                    updateInfoWndTrigger(res, that.mapKey);
                }
            );
            
            
        return goOn;
};


// callback function for getObjects
// use it to put the most general object on the map
mapInstance.prototype.putObjects = function(res)
{
    var mapka = hashMaps.get(this.mapKey).mapka;
        mapka.updater.show();
        res = res.replace(/&lt;/g,'<');
        var xmlDoc = GXml.parse(res);
        var markers = xmlDoc.documentElement.getElementsByTagName("hotel");
        var bounds = new GLatLngBounds();

        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          
          if(isNaN(lat) || isNaN(lng))
            continue;


          var point = new GLatLng(lat,lng);
          var name = markers[i].getAttribute("name");
          var id = markers[i].getAttribute("id");
          var custId = markers[i].getAttribute("custId");
          var custName = markers[i].getAttribute("custName");
          var icon = markers[i].getAttribute("icon");
          
          
          var marker = null;
          if(icon == 'red')
          {
            marker = createTabbedMarker(point, [id], ["general"], false, false, hotelred, id + ' ' + name + '<br />' + custName, true,hotelred.toolTipColor);
          }
          else if(icon == 'green')
          {
            marker = createTabbedMarker(point, [""], ["general"], false, false, hotelgreen, id + ' ' + name + '<br />' + custName, true, hotelgreen.toolTipColor);
          }
          bounds.extend(point);

          marker.categ = 'hotel';
          marker.test = id + ' ' + name + '<br />' + custName;
          marker.oId = id;
          marker.custId = custId;
          this.allmarkers.push(marker);
          mapka.map.addOverlay(marker);
        }
};


    
// invokes async postback to server, asking for hotels
mapInstance.prototype.getHotels = function(from, to)
{
    var that = this;
    PageMethods.GetHotelsInTown(this.TownId, this.NSCoords, this.WECoords, from, to, function(res){
        if(that.putHotels(res, that.clickedHotelId))
        {
            that.getHotels(to, to+1000);
        }
        else
            hashMaps.get(that.mapKey).mapka.updater.hide();
    });
}; 

// invokes async postback to server, asking for monuments
mapInstance.prototype.getMonuments = function()
{
    var that = this;
        PageMethods.GetMonumentsInTown(this.TownId, this.NSCoords, this.WECoords, function(res){
        that.putMonuments(res);
        });
};

// method used in "see map" popup - there is only one instance of map and it creates big marker
mapInstance.prototype.reselect = function(newId, place)
{
	if(this.allmarkers.length == 0)
	;
    
    this.selectedId=newId;
    
    // there can be only one big icon on the map, so remove the previous one if it exists
    if(this.bigIcon)
        this.bigIcon.remove();
    
    for(var i=0; i<this.allmarkers.length; i++)
    {           
        if(document.getElementById(this.mapKey + '_hotels').checked && this.allmarkers[i].categ == 'hotel')
            this.allmarkers[i].show(); 
            
        if(this.allmarkers[i].oId == newId)
        {
            // druting -> check if we are looking for a place
            if(place && this.allmarkers[i].categ != 'hotel')
            {
                var marker = this.allmarkers[i];
                
                this.allmarkers[i].hide();
                this.mapka.mapCenter = this.mapka.map.getCenter();
                
                var html = '';
                
			      html = "<div class=\"dane\">";
		        if(marker.photo && marker.photo != '')
			        html += "<img src=\"http://photos.staypoland.com" + marker.photo + "35x35.jpg\" align=\"left\" class=\"himg\" />";
			      html += "<span class=\"hm\">" + marker.getTitle() + "</span><br />";
			      if(marker.dsc &&  (marker.categ == 'monument' || marker.categ == 'museum' || marker.categ == 'institution'))
			        html += "<a href=\"/Place.aspx?pid=" + marker.oId + "\" class=\"clickme\" target=\"_blank\">" + see_more + "</a>";
			      html += "</div>";
    	          
                this.bigIcon = createTabbedMarker(marker.getPoint(), [html], ["general"], true, false, createIcon(place, 1), marker.getTitle(), false, null, 1);
                this.bigIcon.oId = marker.oId;
				this.bigIcon.photo = marker.photo;
                this.bigIcon.categ = marker.categ;
                tmp = getMarkerImg(this.bigIcon);
                    
                break;
            }
            
            // or if a marker is from category 'hotel'...
            if(this.allmarkers[i].categ == 'hotel')
            {
                var marker = this.allmarkers[i];
                
                this.allmarkers[i].hide();
                this.mapka.mapCenter = this.mapka.map.getCenter();
                
                var html = '';
                
			      html = "<div class=\"dane\">";
		        if(marker.photo && marker.photo != '')
			        html += "<img src=\"http://photos.staypoland.com" + marker.photo + "35x35.jpg\" align=\"left\" class=\"himg\" />";
		        html += "<span class=\"hm\">" + marker.getTitle() + "</span><br />";
		        html += "<img src=\"/img/" + marker.stars + "star.gif\" class=\"sss\" /><br />";
		        html += "<a href=\"/HotelPage.aspx?hid=" + marker.oId + "\" class=\"clickme\" target=\"_blank\">" + see_more + "</a>";
		        html += "</div>";
    	          
                this.bigIcon = createTabbedMarker(marker.getPoint(), [html], ["general"], true, false, createIcon('hotel', 1), marker.getTitle(), false, null, 1);
                this.bigIcon.oId = marker.oId;
				this.bigIcon.photo = marker.photo;
				this.bigIcon.stars = marker.stars;
                this.bigIcon.categ = marker.categ;                
                break;
            }
        }
    }
    
    if(this.bigIcon)
    {
        this.mapka.map.addOverlay(this.bigIcon);
        tmp = getMarkerImg(this.bigIcon);
                if(tmp && tmp.style)
                    tmp.style.zIndex = 120;
    }   
        
    
    if(marker)
    {
        openWindow(this.mapka.map, marker.getPoint(), [html], ["general"]);
        if(marker.categ == 'parks_and_garden')
            document.getElementById('legPrk' + this.mapKey).click();
    }
};

// that method must be implemented in every mapInstanceExtender
mapInstance.prototype.bindEvents = function()
{
    var that = this;
    this.polygons = [];
    this.curBnd = this.mapka.map.getBounds();   
    
    try
    {
        loadBigMap(this);
    }
    catch(e)
    {}
    
//    GEvent.addListener(this.mapka.map, "moveend", function(){
//        console.log('moveend');
//        that.showBounds();
//    });        
    
    GEvent.addListener(this.mapka.map, "dragend", function(){            
        that.showBounds();
    });        
    
    GEvent.addListener(this.mapka.map, "zoomend", function(){            
        that.showBounds();
    });        
	
	GEvent.addListener(this.mapka.map, "click", function(overlay, point)
	{
		if(point != null)
			that.clickedPoint = point;
	});
};


var onePart = false;
mapInstance.prototype.showBounds = function(type, callback)
{
    if(type == null)
        type = '';
    var mapka = this.mapka;
    
    var isChecked = false;
    var coll = document.getElementById('panLegend_' + this.mapKey).getElementsByTagName('input');
    for(var i=0; i<coll.length; i++)
        if(coll[i].type == 'checkbox' && coll[i].checked)
        {
            isChecked = true;
            break;
        }

    
    if(mapka.map.getZoom() < 13 && isChecked)
    {
        this.curBnd = new GLatLngBounds(new GLatLng(0,0), new GLatLng(0,0));
        mapka.zoom.showInfo('Selected zoom is too small to present the icons<br />Please zoom in to see more detailed map.');
        var all = this.mapKey != 'googlemap1';
        for(var i=0; i<this.allmarkers.length; i++)
            if(all || this.allmarkers[i].categ != 'hotel')
                this.removeMarker(this.allmarkers[i], i--);
        this.zoomInfoVisible = true; 
        return;
    }
    else
    {
        mapka.zoom.hideInfo();
    }
    mapka.updater.show();
    
    
    var bsbnd = mapka.map.getBounds();
    
    xDst = Math.abs(bsbnd.getNorthEast().lng() - bsbnd.getSouthWest().lng());
    yDst = Math.abs(bsbnd.getNorthEast().lat() - bsbnd.getSouthWest().lat());
    
    var ind = 0.3;
    
    var bnd = new GLatLngBounds(new GLatLng(bsbnd.getSouthWest().lat()-ind*yDst, bsbnd.getSouthWest().lng()-ind*xDst), 
                                new GLatLng(bsbnd.getNorthEast().lat()+ind*yDst, bsbnd.getNorthEast().lng()+ind*xDst));
                      
      
    var that = this;
    
    if(type != '' || this.curBnd == null || this.zoomInfoVisible || onePart)
    {
        this.curBnd = new GLatLngBounds(new GLatLng(0,0), new GLatLng(0,0));
        this.zoomInfoVisible = false;
    }
        

    
    var hasPendingAjax = false;
    
    if(this.mapKey != 'googlemap1') //nie search map
    {
        
        this.removeInvisibleMarkers(bnd, '', type);
        
        if( type == 'hotel' || ((type == null || type == '' && document.getElementById(this.mapKey + '_hotels').checked)))
        {
            hasPendingAjax = true;
            var cnt = 10000;
            if(zooms.indexOf(this.mapka.map.getZoom()) > -1)
            {
                cnt = limit;
                onePart = true;
            }
            if(bnd.toString() == this.curBnd.toString())
                this.curBnd = new GLatLngBounds(new GLatLng(0,0), new GLatLng(0,0));
                
            PageMethods.GetHotelsInBounds(0,cnt, bnd.getSouthWest().lng(), bnd.getNorthEast().lng(), bnd.getSouthWest().lat(), bnd.getNorthEast().lat(), 
                                            this.curBnd.getSouthWest().lng(), this.curBnd.getNorthEast().lng(), this.curBnd.getSouthWest().lat(), this.curBnd.getNorthEast().lat(),        
            function(res){
                if(!that.putHotels(res, that.clickedHotelId) || onePart)
                {
                    if(callback != null)
                        setTimeout(callback, 1);
                        
                    that.mapka.updater.hide();                           
                }
            });
        }
    }
    else
    {
        this.removeInvisibleMarkers(bnd, 'hotel', type);        
        that.mapka.updater.hide();   
    }

    if(type == '')
    {
        var coll = document.getElementById('panLegend_' + this.mapKey).getElementsByTagName('input');
        for(var i=2; i<coll.length; i++)
            if(coll[i].type == 'checkbox' && coll[i].checked)
            {
                hasPendingAjax = true;
                this.getPlacesInBnd(bnd, coll[i].id.replace(this.mapKey+'_','').replace(/s$/,''), callback);
            }
    }
    else if(type != 'hotel')
    {
        hasPendingAjax = true;
        this.getPlacesInBnd(bnd, type, callback);
    }
    
    if(!hasPendingAjax)
        mapka.updater.hide();
    
    this.curBnd = bnd;
};

mapInstance.prototype.getPlacesInBnd = function(bnd, type, callback)
{
    var that = this;
    this.mapka.updater.show();
    var cnt = 10000;
    var limited = false;
    if(zooms.indexOf(this.mapka.map.getZoom()) > -1)
    {
        cnt = limit;
        limited = true;
    }
    
        this.mapka.updater.show();
      PageMethods.GetPlacesInBounds(3, type, 0, cnt, this.TownId, bnd.getSouthWest().lng(), bnd.getNorthEast().lng(), bnd.getSouthWest().lat(), bnd.getNorthEast().lat(), 
                                        this.curBnd.getSouthWest().lng(), this.curBnd.getNorthEast().lng(), this.curBnd.getSouthWest().lat(), this.curBnd.getNorthEast().lat(),        
        function(res){
            that.mapka.updater.show();
            if(!that.putPlaces(res, type, limited))
            {
                if(callback != null)
                    setTimeout(callback, 1);
                that.mapka.updater.hide();
            }
        });
};

mapInstance.prototype.removeInvisibleMarkers = function(newBounds, ommit, removeOnly)
{
    var x1 = newBounds.getSouthWest().lng();
    var y1 = newBounds.getSouthWest().lat();
    
    var x2 = newBounds.getNorthEast().lng();
    var y2 = newBounds.getNorthEast().lat();
    
    for(var i=0;i<this.allmarkers.length;i++)
            if((removeOnly != null && this.allmarkers[i].categ == removeOnly) || 
            (removeOnly == null || removeOnly == '' && (( (ommit == null || ommit.length == 0) || ommit != null && ommit.length > 0 && this.allmarkers[i].categ != ommit) ) && (this.allmarkers[i].getPoint().lat() < y1 || this.allmarkers[i].getPoint().lat() > y2 || this.allmarkers[i].getPoint().lng() < x1 || this.allmarkers[i].getPoint().lng() > x2)))
                this.removeMarker(this.allmarkers[i], i--);
};

mapInstance.prototype.removeMarker = function (marker, x)
{        
    if(x > -1)
    {
        this.allmarkers.splice(x, 1);
        this.mapka.map.removeOverlay(marker);
    }
};


// switches between "show" and "hide" mode of markers in category 'catName'
mapInstance.prototype.showHide = function(sender, catName)
{
    if(sender.checked)
    {
        //if(document.getElementById(this.mapKey + '_got_' + catName).value == '0')
        //    this.getObjects(catName);
        //this.showBounds(catName, '');
    }
    else
    {
        this.hideCat(catName);
    }
};


mapInstance.prototype.putPolygons = function(xml, type, mapKey)
{    
    var xmlDoc = GXml.parse(xml);
    var markers = xmlDoc.documentElement.getElementsByTagName("polygon");
    
    var points = [];
    var gmarkers = [];
    for (var i = 0; i < markers.length; i++) 
    {
      points = [];
      gmarkers = [];
      var tmp = markers[i].getElementsByTagName("coord");
      for(var j=0; j<tmp.length; j++)
      {
        // obtain the attribues of each marker
        var lat = parseFloat(tmp[j].getAttribute("lat"));
        var lng = parseFloat(tmp[j].getAttribute("lng"));
        points.push(new GLatLng(lat,lng));        
      }
      points.push(points[0]);
      var name = markers[i].getAttribute("locId");
      var objName= markers[i].getAttribute("objName");
      var color = markers[i].getAttribute("color");
    
      var tst = new CstPolygon(points,'#' + color, 2, 0.6,'#' + color,0.4);
      tst.oId = name;
      tst.type = type;
      tst.objName = objName;
      tst.id=objName;
      tst.domid = objName;
      try
      {
        this.mapka.map.addOverlay(tst);
      }
      catch(e){}
      this.polygons.push(tst);
      polyHandler(tst, mapKey);
    }
};


function showResult(str)
{
    alert(str);
};


function showMapPopup2(selectedId, ctrlId, hotel) {
    var modalPopupBehavior = $find('googlePopupExtender');
                   
    var mapka = hashMaps.get(ctrlId).mapka;
    
    mapka.map.checkResize();
    mapka.map.setCenter(new GLatLng(0,0));
    
    PageMethods.GetObjectLoc(selectedId, hotel ? 'hotel' :'monument', function(res){
        modalPopupBehavior.show(); 
        mapka.map.checkResize();
        
        mapka.map.setCenter(new GLatLng(parseFloat(res[0].replace(',','.')), parseFloat(res[1].replace(',','.'))));
        
        hashMaps.get(ctrlId).customzoom = true;
        mapka.map.setZoom(15); 
        
        PageMethods.GetBasicObjInfo(selectedId, hotel ? 'hotel' : 'museum', function(res){
			var marker ;
			if(hotel)
            {
				marker = createTabbedMarker(mapka.map.getCenter(), [""], [""], false, false, createIcon(res[1], 0), res[2]);
				
				marker.oId = selectedId;
				marker.categ = res[1];
				marker.photo = res[4];
				marker.stars = res[3];
		    }
		    else
		    {
		        marker = createTabbedMarker(mapka.map.getCenter(), [""], [""], false, false, createIcon(res[1], 0), res[0]);
				
				marker.oId = selectedId;
				marker.categ = res[1];
				marker.photo = res[2];
		    }
			
            // var marker = createTabbedMarker(mapka.map.getCenter(), [""], [""], false, false, createIcon(res[1], 0), res[0]);
            // marker.oId = selectedId;
            // marker.categ = res[1];
            // marker.photo = res[2];
			
            hashMaps.get(ctrlId).allmarkers.push(marker);
            
            mapka.map.addOverlay(marker);
                
            var callback = "hashMaps.get('" + ctrlId + "').reselect(" + selectedId + ", '" + res[1] + "');";
            hashMaps.get(ctrlId).showBounds('', null);
            
            setTimeout(callback, 1);
        });        
    });
};

function showMapBig(selectedId, ctrlId, hotel) {
    var mapka = hashMaps.get(ctrlId).mapka;
    
    PageMethods.GetObjectLoc(selectedId, hotel ? 'hotel' :'monument', function(res){
    mapka.map.checkResize();
    
    mapka.map.setCenter(new GLatLng(parseFloat(res[0].replace(',','.')), parseFloat(res[1].replace(',','.'))));
    
    hashMaps.get(ctrlId).customzoom = true;
    mapka.map.setZoom(15); 
    
    PageMethods.GetBasicObjInfo(selectedId, hotel ? 'hotel' : 'museum', function(res){
		var marker ;
		if(hotel)
        {
			marker = createTabbedMarker(mapka.map.getCenter(), [""], [""], false, false, createIcon(res[1], 0), res[2]);
			
			marker.oId = selectedId;
			marker.categ = res[1];
			marker.photo = res[4];
			marker.stars = res[3];
	    }
	    else
	    {
	        marker = createTabbedMarker(mapka.map.getCenter(), [""], [""], false, false, createIcon(res[1], 0), res[0]);
			
			marker.oId = selectedId;
			marker.categ = res[1];
			marker.photo = res[2];
	    }
		
        // var marker = createTabbedMarker(mapka.map.getCenter(), [""], [""], false, false, createIcon(res[1], 0), res[0]);
        // marker.oId = selectedId;
        // marker.categ = res[1];
        // marker.photo = res[2];
		
        hashMaps.get(ctrlId).allmarkers.push(marker);
        
        mapka.map.addOverlay(marker);
            
        var callback = "hashMaps.get('" + ctrlId + "').reselect(" + selectedId + ", '" + res[1] + "');";
        hashMaps.get(ctrlId).showBounds('', null);
        
        setTimeout(callback, 1);
        });        
    });      
};



function mapClicked(id, mapKey, callback)
{
    var mapinst = hashMaps.get(mapKey);
    document.getElementById(mapinst.clickedHotelId).value = id;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
    
    if(mapinst.HotelClickable)
    {
        if(!prm.get_isInAsyncPostBack())
        {
            if(mapinst.lastClickedMarker.categ=='hotel')
                PageMethods.GetHotelInfo(parseInt(id), function(res){
                    callback(res, mapKey);
                    var sht = 'hashMaps.get("' + mapKey + '").mapka.map.panTo(hashMaps.get("' + mapKey + '").lastClickedMarker.getPoint()' + ');';
                    setTimeout(sht, 300);
                });
            else
                divMapClicked(id, mapKey);                    
        }
    }
    
};

function divMapClicked(id, mapKey)
{
    /*var mapinst = hashMaps.get(mapKey);
    
    document.getElementById(mapinst.clickedHotelId).value = id;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
    if(mapinst.HotelClickable)
    {
        PageMethods.GetBasicObjInfo(parseInt(id), mapinst.lastClickedMarker.categ, function(res)
        {
            var html = '';
            var marker= mapinst.lastClickedMarker;
            if(mapinst.lastClickedMarker.categ == 'hotel')
            {
		        html = "<div class=\"dane\">";
		        if(res[4] && res[4] != '')
			        html += "<img src=\"http://photos.staypoland.com" + res[4] + "35x35.jpg\" align=\"left\" class=\"himg\" />";
		        html += "<span class=\"hm\">" + res[2] + "</span><br />";
		        html += "<img src=\"http://net2.europe-cities.com/img/" + res[3] + "star.gif\" class=\"sss\" /><br />";
		        html += "<a href=\"/Hotel.aspx?hid=" + id + "\" class=\"clickme\">see more details</a>";
		        html += "</div>";
		    }
		    else
		    {
		        html = "<div class=\"dane\">";
		        if(res[2] && res[2] != '')
			        html += "<img src=\"http://photos.staypoland.com" + res[2] + "35x35.jpg\" align=\"left\" class=\"himg\" />";
			      html += "<span class=\"hm\">" + res[0] + "</span><br />";
			      if(marker.categ == 'monument' || marker.categ == 'museum' || marker.categ == 'institution')
			        html += "<a href=\"/Place.aspx?pid=" + id + "\" class=\"clickme\">see more details</a>";
			      html += "</div>";
		    }
            updateWindow(mapinst.mapka.map, [html], ['all']);
        });
        
        var sht = 'hashMaps.get("' + mapKey + '").mapka.map.panTo(hashMaps.get("' + mapKey + '").lastClickedMarker.getPoint()' + ');';
        setTimeout(sht, 300);
        mapinst.showBounds();
    }
    */
   
    var mapinst = hashMaps.get(mapKey);
        
    document.getElementById(mapinst.clickedHotelId).value = id;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
    var html = [mapinst.lastClickedMarker.getTitle()];
    if(mapinst.lastClickedMarker.photo && mapinst.lastClickedMarker.photo != '')
        html[0] = "<img src=\"http://photos.staypoland.com" + mapinst.lastClickedMarker.photo + "35x35.jpg\" alt=\"img\" />" + html[0];
    
    switch(mapinst.lastClickedMarker.categ)
    {
        case 'hotel':
			html[0] = "<div class=\"dane\">";
			if(mapinst.lastClickedMarker.photo && mapinst.lastClickedMarker.photo != '')
				html[0] += "<img src=\"http://photos.staypoland.com" + mapinst.lastClickedMarker.photo + "35x35.jpg\" align=\"left\" class=\"himg\" />";
			html[0] += "<span class=\"hm\">" + mapinst.lastClickedMarker.getTitle() + "</span><br />";
			html[0] += "<img src=\"/img/" + mapinst.lastClickedMarker.stars + "star.gif\" class=\"sss\" /><br />";
			html[0] += "<a href=\"/HotelPage.aspx?hid=" + mapinst.lastClickedMarker.oId + "\" class=\"clickme\" target=\"_blank\">" + see_more + "</a>";
			html[0] += "</div>";
            // html[0] += '<br /><a href="/HotelPage.aspx?hid=' + id + '" target="_blank">See more</a>';
            break;
        case 'monument':
        case 'museum':
        case 'institution':
            if(mapinst.lastClickedMarker.dsc)
                html[0] += '<br /><a href="/Place.aspx?pid=' + id + '" target="_blank">' + see_more + '</a>';
            
            break;
    }
    
    if(mapinst.HotelClickable)
        updateWindow(mapinst.mapka.map, html, ['all']);       
};

var plgTown = -1;
function loadPlg2(mapKey, type, tid)
{
    var mapinst = hashMaps.get(mapKey);
    mapinst.mapka.updater.show();
    if(type != null)
    {
        if(plgTown != mapinst.TownId)
        {
            for(var i=0; i<mapinst.polygons.length; i++)
                mapinst.mapka.map.removeOverlay(mapinst.polygons[i]);
                
            mapinst.polygons = [];
        }
    


        var contained = false;
        for(var i=0; i<mapinst.polygons.length; i++)
        {
            if(mapinst.polygons[i].type == type)
            {
                contained = true;
                mapinst.polygons[i].show();
            }
            else
                mapinst.polygons[i].hide();
        }
            
        if(!contained)
        {   
            PageMethods.GetPolygonsXml(tid, type, function(xml){
                mapinst.putPolygons(xml, type, mapKey);
                mapinst.mapka.updater.hide();
            });
        }
        else
            mapinst.mapka.updater.hide();
    }
    else
    {
        for(var i=0; i<mapinst.polygons.length; i++)
            mapinst.polygons[i].hide();
            
        mapinst.mapka.updater.hide();			
    }
    
};

function polyHandler(poly, mapKey)
{
    GEvent.addListener(poly,"mouseover",function(){poly.setFillOpacity(0.6); showObjectName(mapKey, poly.objName);});
    GEvent.addListener(poly,"mouseout",function(){poly.setFillOpacity(0.4); showObjectName(mapKey, '');});
	GEvent.addListener(poly,"click",function()
	{
		//alert('z polygona: ' + hashMaps.get(mapKey).clickedPoint);
		var mapka = hashMaps.get(mapKey);
		mapka.mapka.map.openInfoWindow(mapka.clickedPoint, poly.objName + '<br /><a href="/CityLocation.aspx?lid=' + poly.oId + '&tid=' + mapka.TownId + '" target="_blank" >' + see_more + '</a>');
	});
};

function showObjectName(obj, txt)
{
    document.getElementById(obj + '_info').innerHTML = txt;
};

