/**
* UWAGA!!! Skrypt korzysta z pliku lokalizacja_miast.js gdzie znajdują sie współrzedne miast
*
* @author    Grzegorz Pawiński <grzegorz.pawinski@autentika.pl>
* @copyright Copyright (c) 2008, Autentika
*/

var googleBigMap = {
    /*Objects*/
    Icon:       null,
    //imgIcon:   null,    
    map:        null,
    /*arrays*/
    markers:    new Array(),
    images:     new Array(),
    htmls:      new Array(),
    /*geocoder*/
    geocoder:   null,
    address:    '',
    /*marker manager*/
    mgr:        null,    
    /*zoom*/
    IconZoom:   null,
    mmMaxZoom:  11,
    markersZoom: new Array(),
    htmlsZoom:   null,
    /*params*/    
    mapZoom:    6,
    mapType:    null,    
    action:     '',
    lat:        52.10650634765625000,
    lon:        18.76464843750000000,
    ids:        0,
    /**
        centerMode: 2-centruje mape i pobiera markery tylko po przesunieciu mapy
                    1-centruje mape i pobiera znaczniki takze przy klliknieciu na marker
                    0-nie centruje mapy i zawsze pobiera znaczniki
    **/
    centerMode: 2,
    frameFirst: false,
    photoView:  0,         
    init: function()
    {
        try{
            googleBigMap.Icon = new GIcon();
            googleBigMap.Icon.image = Config.app.siteurl + "/wp-content/themes/default/images/g_s.png";            
            googleBigMap.Icon.iconSize = new GSize(18, 20);            
            googleBigMap.Icon.iconAnchor = new GPoint(9, 20);
            googleBigMap.Icon.infoWindowAnchor = new GPoint(9, 20);
        
            if (googleBigMap.mmMaxZoom) {
                googleBigMap.IconZoom = new GIcon();
                googleBigMap.IconZoom.image = Config.app.siteurl + "/wp-content/themes/default/images/g_l.png"               
                googleBigMap.IconZoom.iconSize = new GSize(24, 24);                
                googleBigMap.IconZoom.iconAnchor = new GPoint(12, 24);
                googleBigMap.IconZoom.infoWindowAnchor = new GPoint(12, 24);
            }
            
        	if(!googleBigMap.mapType) {
                googleBigMap.mapType = G_NORMAL_MAP;
            }
                                    
            googleBigMap.load();            
        } catch(ex){if (Config.debug) alert('googleBigMap->init: '+ex);}
    },
    load: function()
    {       
        try{
            if (GBrowserIsCompatible() && document.getElementById("googleMap"))
            {
                googleBigMap.map = new GMap2($("googleMap"));

                if(navigator.userAgent.match("MSIE"))
                {
                    GEvent.addDomListener($("googleMap"), 'mousewheel', googleBigMap.wheelZoom);
                }
                else
                {
                    GEvent.addDomListener($("googleMap"), 'DOMMouseScroll', googleBigMap.wheelZoom); // Firefox
                }
                GEvent.addListener(googleBigMap.map, "moveend", googleBigMap.onmoveend);
                                
                
                googleBigMap.map.enableContinuousZoom();
                googleBigMap.map.enableDoubleClickZoom();                
                googleBigMap.map.addControl(new GLargeMapControl());
                                                
                googleBigMap.map.addMapType(G_PHYSICAL_MAP);
                googleBigMap.map.addControl(new GMapTypeControl());
                googleBigMap.map.addControl(new GOverviewMapControl());
                
                                
                googleBigMap.map.setCenter(new GLatLng(googleBigMap.lat, googleBigMap.lon), googleBigMap.mapZoom);
        		googleBigMap.map.setMapType(googleBigMap.mapType);
                		    
        		
        		if (googleBigMap.mmMaxZoom) {
        		    googleBigMap.loadZoomMarkers(0);            
        		    //window.setTimeout(googleBigMap.zoomAllowed, 0);
        		}
        		         
                switch(googleBigMap.action)
                {
                    case 'points':	 googleBigMap.markPoints();break;
                    case 'wyszukaj': googleBigMap.wyszukaj();break;                     
                    default:     //if (Config.debug)  alert('Nie wybrano akcji');
                }

            }
        } catch(ex){if (Config.debug) alert('googleBigMap->load: '+ex);}
    },
    loadZoomMarkers: function(j)
    {
        try
        {
            //var j = 0;
            
            //if (pointsZoom.length == 0) return;
            
            //googleBigMap.markersZoom = new Array();
            googleBigMap.htmlsZoom = new Array();
            
            for(var i =0; i < pointsZoom.length; i++)
            {
                if(pointsZoom[i].lat != 0 && pointsZoom[i].lon != 0)
                {
                    var mb  = googleBigMap.map.getBounds();                                                                 
                   // if (pointsZoom[i].lon <= mb.getNorthEast().x && pointsZoom[i].lon >= mb.getSouthWest().x && pointsZoom[i].lat >= mb.getSouthWest().y && pointsZoom[i].lat <= mb.getNorthEast().y){      
                        if (document.getElementById("googleMap"))
                        {
                            var html = '<div class="gmap_fotocloud"><table celpadding="0" celspacing="0" width="200"><tr><td class="gmap_desc">' + pointsZoom[i].name  + '</td></tr></table></div>';                        
                        }                    
                        googleBigMap.addMarkerZoom(pointsZoom[i].lat, pointsZoom[i].lon, '', j);                          
                        j++;
                    //}  
                    
                }
            }
            
            
            /*     gabinety w centrum       */
            for(var i =0; i < gabinetyCentrum.length; i++)
            {
                if(gabinetyCentrum[i].lat != 0 && gabinetyCentrum[i].lon != 0)
                {
                    var mb  = googleBigMap.map.getBounds();                                                                                    
                        if (document.getElementById("googleMap"))
                        {
                            var html = '<div class="gmap_fotocloud"><table celpadding="0" celspacing="0" width="200"><tr><td class="gmap_desc"><strong>dr ' + gabinetyCentrum[i].name  + '</strong></td></tr>'+(gabinetyCentrum[i].ginweb_url ? '<tr><td><a target="_blank" href="' + gabinetyCentrum[i].ginweb_url + '">strona internetowa gabinetu</a></td></tr>' : '')+'<tr><td><strong>Adres:</strong></td></tr><tr><td>' + gabinetyCentrum[i].miasto  + ', ' + gabinetyCentrum[i].adres  + '</td></tr><tr><td><strong>Telefon:</strong></td></tr><tr><td>' + gabinetyCentrum[i].telefon  + '</td></tr></table></div>';
                        }                    
                        googleBigMap.addMarkerZoom(gabinetyCentrum[i].lat, gabinetyCentrum[i].lon, html, j);                          
                        j++;                    
                    
                }
            }
            /*     gabinety w centrum       */
                        
            
        }catch(ex){if (Config.debug) alert('googleBigMap->loadZoomMarkers: '+ex);}
    },
    addMarkerZoom: function(lat, lon, html, id)
    {
        try{     
            if(html.length > 0)      
                var marker = new GMarker(new GLatLng(lat, lon), {icon:googleBigMap.Icon});
            else 
                var marker = new GMarker(new GLatLng(lat, lon), {icon:googleBigMap.IconZoom});
                
            googleBigMap.markersZoom[id] = marker;
            googleBigMap.htmlsZoom[id] = html;

            if (document.getElementById("googleMap"))
            {
                if(html.length > 0)
                {
                    GEvent.addListener
                    (
                    marker, "click", function()
                    {
                        googleBigMap.ids = id;
                        googleBigMap.markersZoom[id].openInfoWindowHtml(googleBigMap.htmlsZoom[id]);
                        Event.observe($("cloudImage"), "load", googleBigMap.refreshCloudZoom);                       
                    }
                    );
                }else
                {
                    GEvent.addListener
                    (
                    marker, "click", function()
                    {   
                        googleBigMap.map.setCenter(new GLatLng(lat, lon), googleBigMap.map.getZoom()+1);                                               
                    }
                    );
                }
            }
          googleBigMap.map.addOverlay(marker);
        } catch(ex){if (Config.debug) alert('googleBigMap->addMarkerZoom: '+ex);}
    },
    markPoints: function()
    {
        try
        {
            var bounds = new GLatLngBounds;
            var icons = new Array();
            var j = 0;
            
            //googleBigMap.markers = new Array();
            googleBigMap.htmls   = new Array();
             
            if (points.length == 0) return;
            
            for(var i =0; i < points.length; i++)
            {
                if(points[i].lat != 0 && points[i].lon != 0)
                {
                    if (document.getElementById("googleMap"))
                    {
                        var html = '<div class="gmap_fotocloud"><table celpadding="0" celspacing="0" width="200"><tr><td class="gmap_desc"><strong>dr ' + points[i].name  + '</strong></td></tr>'+(points[i].ginweb_url ? '<tr><td><a target="_blank" href="' + points[i].ginweb_url + '">strona internetowa gabinetu</a></td></tr>' : '')+'<tr><td><strong>Adres:</strong></td></tr><tr><td>' + points[i].miasto  + ', ' + points[i].adres  + '</td></tr><tr><td><strong>Telefon:</strong></td></tr><tr><td>' + points[i].telefon  + '</td></tr></table></div>';                       
                        var img = points[i].img;
                    }
                    bounds.extend(new GLatLng(points[i].lat, points[i].lon));

                    googleBigMap.addMarker(points[i].lat, points[i].lon, html, null, j);
                    j++;
                }
            }
            
            if(googleBigMap.photoView == 1 && $('showAll').checked == false)
            {
                googleBigMap.pictureDisplay(false);
            }
            else if (googleBigMap.centerMode > 0)
            {
                if(points.length > 0)
                {
                    googleBigMap.map.setCenter(bounds.getCenter());
                }
                if(points.length == 1)
                {
                    googleBigMap.map.setZoom(3);
                }
                else
                {
                    googleBigMap.map.setZoom(googleBigMap.map.getBoundsZoomLevel(bounds));
                }
            }
        } catch(ex){if (Config.debug) alert('googleBigMap->markPoints: '+ex);}
    },
    addMarker: function(lat, lon, html, img, id)
    {
        try{
            if (img)
            {
                var imgIcon = new GIcon();
                googleBigMap.images[id] = img;
                imgIcon.image = img;
            	imgIcon.iconSize = new GSize(32, 32);
            	imgIcon.shadowSize = new GSize(32, 32);
            	imgIcon.iconAnchor = new GPoint(9, 27);
            	imgIcon.infoWindowAnchor = new GPoint(5, 2);
            	if(id == 0 && googleBigMap.frameFirst)
            	{
            	    imgIcon.shadow = googleBigMap.shadow;
                    imgIcon.shadowSize = new GSize(36, 36);
                    imgIcon.shadowAnchor = new GPoint(9, 27);
            	}
    
                var marker = new GMarker(new GLatLng(lat, lon), {icon:imgIcon});
            }else
            {
                var marker = new GMarker(new GLatLng(lat, lon), {icon:googleBigMap.Icon});
            }
            googleBigMap.markers[id] = marker;
            googleBigMap.htmls[id] = html;

            if (document.getElementById("googleMap"))
            {
                if(html.length > 0)
                {
                    GEvent.addListener
                    (
                    marker, "click", function()
                    {                        
                        googleBigMap.centerMode = 1;
                        googleBigMap.ids = id;
                        googleBigMap.markers[id].openInfoWindowHtml(googleBigMap.htmls[id]);
                        Event.observe($("cloudImage"), "load", googleBigMap.refreshCloud);                       
                    }
                    );
                }
            }
           //if (!googleBigMap.mmMaxZoom) googleBigMap.map.addOverlay(marker);
           googleBigMap.map.addOverlay(marker);
        } catch(ex){if (Config.debug) alert('googleBigMap->addMarker: '+ex);}
    },
    prevPicture: function(id)
    {
        try{
            if(id == 0)
            {
                id = googleBigMap.markers.length - 1;
            }
            else
            {
                id = id - 1;
            }

            $('hiddenPhoto').src = googleBigMap.images[id];
            googleBigMap.ids = id;
            googleBigMap.markers[id].openInfoWindowHtml(googleBigMap.htmls[id]);
            Event.observe($("cloudImage"), "load", googleBigMap.refreshCloud);            
        } catch(ex){if (Config.debug) alert('googleBigMap->prevPicture: '+ex);}
    },
    nextPicture: function(id)
    {
        try{
            if(id == googleBigMap.markers.length - 1)
            {
                id = 0;
            }
            else
            {
                id = id + 1;
            }

            $('hiddenPhoto').src = googleBigMap.images[id];
            googleBigMap.ids = id;
            googleBigMap.markers[id].openInfoWindowHtml(googleBigMap.htmls[id]);
            Event.observe($("cloudImage"), "load", googleBigMap.refreshCloud);            
        } catch(ex){if (Config.debug) alert('googleBigMap->nextPicture: '+ex);}
    },
    refreshCloud: function()
    {
        try{
            googleBigMap.markers[googleBigMap.ids].openInfoWindowHtml(googleBigMap.htmls[googleBigMap.ids]);
        } catch(ex){if (Config.debug) alert('googleBigMap->refreshCloud: '+ex);}
    },
    refreshCloudZoom: function()
    {
        try{
            googleBigMap.markersZoom[googleBigMap.ids].openInfoWindowHtml(googleBigMap.htmlsZoom[googleBigMap.ids]);
        } catch(ex){if (Config.debug) alert('googleBigMap->refreshCloudZoom: '+ex);}
    },
    wyszukaj:   function()
    {
        try{
            $('error').innerHTML = '';
            if ($('miejscowosc').value == '' || $('miejscowosc').value == 'wpisz miejscowość'){
                $('error').innerHTML = 'Przybliż mapę lub kliknij ikonę G/g aby zobaczyć gabinety ginekologiczne w tej okolicy.';
                return false;
            }            
            googleBigMap.showAddress('Polska, ' +$('miejscowosc').value, false);
            return false;
        } catch(ex){if (Config.debug) alert('googleBigMap->wyszukaj: '+ex);}
    },                                     
    pictureDisplay: function(show)
    {
        try{
            var bounds = new GLatLngBounds;
            googleBigMap.map.clearOverlays();
            googleBigMap.map.addOverlay(googleBigMap.markers[0]);
            bounds.extend(googleBigMap.markers[0].getPoint());
            if(show)
            {
                for(var i = 1; i < googleBigMap.markers.length; i++)
                {
                    bounds.extend(googleBigMap.markers[i].getPoint());
                    googleBigMap.map.addOverlay(googleBigMap.markers[i]);
                }
            }
            if(points.length > 0)
            {
                //googleBigMap.map.setCenter(bounds.getCenter());
            }
            if(points.length == 1)
            {
                googleBigMap.map.setZoom(3);
            }
            else
            {
                googleBigMap.map.setZoom(googleBigMap.map.getBoundsZoomLevel(bounds));
            }
        } catch(ex){if (Config.debug) alert('googleBigMap->pictureDisplay: '+ex);}
    },
    markAlbums: function()
    {
        try{
            var bounds = new GLatLngBounds;
            for(var i =0; i < points.length; i++)
            {
                if(points[i].loc_gps_x != 0 && points[i].loc_gps_y != 0)
                {
                    var html = '<div class="gmap_cloud">' +
                    '<img src="' + points[i].img + '" />' +
                    '<a href="' + points[i].url + '">' + points[i].title  + '</a>' +
                    '<p>' + points[i].desc + '</p>' +
                    '<a href="' + points[i].user_url + '">' + points[i].user_name  + '</a>' +
                    '<p>Liczba zdjęć: ' + points[i].count + '</p>' +
                    '</div>';
                    googleBigMap.addAlbumMarker(points[i].loc_gps_x, points[i].loc_gps_y, html);
                    point = new GLatLng(points[i].loc_gps_x, points[i].loc_gps_y);
                    bounds.extend(point);
                }
            }
            
            if(points.length > 0)
                {
                    //googleBigMap.map.setCenter(bounds.getCenter());
                }
                if(points.length == 1)
                {
                    googleBigMap.map.setZoom(3);
                }
                else
                {
                    googleBigMap.map.setZoom(googleBigMap.map.getBoundsZoomLevel(bounds));
                }
            
        } catch(ex){if (Config.debug) alert('googleBigMap->markAlbums: '+ex);}
    },
    addAlbumMarker: function(lat, lon, html)
    {
        try{
            var marker = new GMarker(new GLatLng(lat, lon), {icon:googleBigMap.Icon});
            GEvent.addListener
            (                
                marker, "click", function()
                {
                    marker.openInfoWindowHtml(html);
                }
            );
            googleBigMap.map.addOverlay(marker);
        } catch(ex){if (Config.debug) alert('googleBigMap->addAlbumMarker: '+ex);}
    },
    showAddress: function(address, show)
    {
         try{
          geocoder = new GClientGeocoder();           
          if (geocoder) {
            geocoder.getLatLng(
              address,
              function(point) {
                if (point) {                
                    googleBigMap.map.setCenter(point, 12);                  
                      if (show != 'undefined' && show)
                      {                          
                          var marker = new GMarker(point); 
                          googleBigMap.map.addOverlay(marker);
                          marker.openInfoWindowHtml(address);                  
                      }              
                     var mb  = googleBigMap.map.getBounds();
                     pobierz_widoczne_gabinety(mb.getNorthEast().x, mb.getNorthEast().y, mb.getSouthWest().x, mb.getSouthWest().y);
                }else {
                    $('error').innerHTML = 'Nie znaleziono miejscowości';
                }                
              }
            );
          }          
      } catch(ex){if (Config.debug) alert('googleBigMap->showAddress: '+ex);}
    },
    wheelZoom: function(event)
    {
        try{
            if (event.detail){
                if (event.detail < 0){
                        googleBigMap.map.zoomIn();                    
                }
                if (event.detail > 0){
                        googleBigMap.map.zoomOut();
                }
            } else if (event.wheelDelta) {
                if (event.wheelDelta > 0) {
                        googleBigMap.map.zoomIn();
                }
                if (event.wheelDelta < 0) {
                        googleBigMap.map.zoomOut();
                }
            }
            if (event.preventDefault) {
                event.preventDefault();
                event.stopPropagation();
            } else {
                event.returnValue = false;
                event.cancelBubble = true;
            }           
        } catch(ex){if (Config.debug) alert(ex);}
    },
    onmoveend: function(){
        
        if (googleBigMap.centerMode > 0) {
            googleBigMap.centerMode--;
            return;
        }
        
        if (googleBigMap.map.getZoom() >= 11) {
            $('error').innerHTML  = ''; 
            var mb  = googleBigMap.map.getBounds();
            pobierz_widoczne_gabinety(mb.getNorthEast().x, mb.getNorthEast().y, mb.getSouthWest().x, mb.getSouthWest().y);
            
        }else
        {
            $('error').innerHTML  = 'Przybliż mapę lub kliknij ikonę <img src="'+Config.app.siteurl+'/wp-content/themes/default/images/g_l.gif"/> / <img src="'+Config.app.siteurl+'/wp-content/themes/default/images/g_s.gif"/> aby zobaczyć gabinety ginekologiczne w tej okolicy.'; 
            googleBigMap.redraw('big'); 
        }
        
    },
    zoomAllowed: function() {
        try{
              var mgrOptions = { borderPadding: 50, maxZoom: 12, trackMarkers: false };  
              mgr = new MarkerManager(googleBigMap.map, mgrOptions);              
              mgr.addMarkers(googleBigMap.markers, googleBigMap.mmMaxZoom);              
              mgr.addMarkers(googleBigMap.markersZoom, 4, googleBigMap.mmMaxZoom-1);      
              mgr.refresh();
       } catch(ex){if (Config.debug) alert('googleBigMap->zoomAllowed: '+ex);}
    },
    redraw:     function(mode){
        try{
              for(var i=0; i<googleBigMap.markers.length;i++)
              {
                  googleBigMap.map.removeOverlay(googleBigMap.markers[i]);
              }
              googleBigMap.markers = new Array();
               if (googleBigMap.mmMaxZoom && mode == 'big' && googleBigMap.map.getZoom() < googleBigMap.mmMaxZoom) 
               {
                   if (googleBigMap.markersZoom.length == 0) googleBigMap.loadZoomMarkers(0);                   
               }
               else
               {                   
                   if (googleBigMap.markersZoom.length > 0)
                   {                       
                       for(var i=0; i<googleBigMap.markersZoom.length;i++)
                       {
                          googleBigMap.map.removeOverlay(googleBigMap.markersZoom[i]);
                       }
                       googleBigMap.markersZoom = new Array();  
                   }
                                      
                   googleBigMap.markPoints();
               }
              
              //mgr.addMarkers(googleBigMap.markers, googleBigMap.mmMaxZoom);    
              //mgr.refresh();              
        } catch(ex){if (Config.debug) alert('googleBigMap->redraw: '+ex);}
    }

};
//Event.observe(window, 'load', googleBigMap.init, false);

