<?php
/**
 *
 * @package RealEstateManager
 * @copyright 2012 by Ordasoft
 * @author Andrey Kvasnevskiy - OrdaSoft (akbet@mail.ru); Rob de Cleen (rob@decleen.com);
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * Homepage: https://ordasoft.com/
 *
**/


if (!defined('_VALID_MOS') && !defined('_JEXEC')) die('Direct Access to ' . basename(__FILE__) . ' is not allowed.');

/*That is OLD code in any case:
global $realestatemanager_configuration, $doc, $mosConfig_live_site, $database, $Itemid;*/
/*// That is last version of the code. If global will be need, uncomment this piece of code:
global $realestatemanager_configuration, $mosConfig_live_site, $database;*/



    /*$api_key = "key=" . $realestatemanager_configuration['api_key'] ;
    $doc->addScript("//maps.googleapis.com/maps/api/js?$api_key");*/
    // rem_GoogleMap_KeyCheck_AddScript();

    $doc = JFactory::getDocument();
    if ( $realestatemanager_configuration['location_map'] == '1' && $realestatemanager_configuration['google_openmap']['show'] == '1' ) {
        if ( isset($realestatemanager_configuration['api_key']) && $realestatemanager_configuration['api_key'] ) {
            $api_key = "key=" . $realestatemanager_configuration['api_key'];
        } else {
          $api_key = JFactory::getApplication()->enqueueMessage("<a target='_blank' href='//developers.google.com/maps/documentation/geocoding/get-api-key'>" . _REALESTATE_MANAGER_GOOGLEMAP_API_KEY_LINK_MESSAGE . "</a>", _REALESTATE_MANAGER_GOOGLEMAP_API_KEY_ERROR);
        }
        if ( checkJavaScriptIncludedRE("maps.googleapis.com") === false ) {
            // $doc->addScript("//maps.googleapis.com/maps/api/js?$api_key&libraries=places");
            $doc->addScript("//maps.googleapis.com/maps/api/js?$api_key&libraries=places",'text/javascript', true,true);
        }
    }

?>

<script type="text/javascript">

    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190510, START:
    // Instead of two function below (window.addEvent and window.onload) we use the function rem_loc_map_addLoadEvent in the end of script. That is optimal solution in many case: when we use a few modules RealEstateManager Location Map on one page; when we display tab Location map on view my_houses...
    /*window.addEvent('domready', function() {
        initialize2<?php // echo $map_uniq_id; ?>();
    });*/
    /*window.onload = function() {
        initialize2<?php // echo $map_uniq_id; ?>();
    };*/

    // initialize2<?php //echo $map_uniq_id; ?>();
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190510, END.

    function initialize2<?php echo $map_uniq_id; ?>(){
        var map;
        var marker = new Array();

        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190327, START:
        var myOptions = {
            // scrollwheel: true,
            <?php
            if ( isset($params) && $params->get('scroll_wheel') == '1' ) echo "scrollwheel: true,";
            else echo "scrollwheel: false,";?>
            <?php if ( isset($params) && $params->get('menu_map') == '0' ) echo "
            mapTypeControl: false,";
            else echo "
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                position: google.maps.ControlPosition.TOP_CENTER
            }, "; ?>
            <?php if ( isset($params) && $params->get('control_map') == '0' ) echo "
            zoomControl: false,
            panControl: false,
            streetViewControl: false";
            else echo "
            zoomControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.DEFAULT
            },
            panControl: true,
            streetViewControl: true,
            streetViewControlOptions: {
                position: google.maps.ControlPosition.LEFT_BOTTOM
            } "; ?>

            // mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190327, END.

        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190327, START:
        // var imgCatalogPath = "<?php // echo $mosConfig_live_site; ?>components/com_realestatemanager/";
        var imgCatalogPath = "<?php echo $mosConfig_live_site; ?>components/com_realestatemanager";
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190327, END.

        var map = new google.maps.Map(document.getElementById("map_canvas<?php echo $map_uniq_id; ?>"), myOptions);
        var bounds = new google.maps.LatLngBounds ();

        var remove_id_list = [];
        <?php
        //$newArr = explode(",", _REALESTATE_MANAGER_HOUSE_MARKER);
        $j = 0;
        for ($i = 0;$i < count($rows);$i++) {
            if ($rows[$i]->hlatitude && $rows[$i]->hlongitude) {
                if( is_file($mosConfig_live_site.'components/com_realestatemanager/images/marker-'.$rows[$i]->property_type.'.png') ){

                     $numPick = '/images/marker-'.$rows[$i]->property_type.'.png';
                }
                else  $numPick = '/images/marker-2.png';   
        ?>


                var srcForPic = "<?php echo $numPick; ?>";
                var image = '';
                if(srcForPic.length){
                    var image = new google.maps.MarkerImage(imgCatalogPath + srcForPic,
                        new google.maps.Size(32, 32),
                        new google.maps.Point(0,0),
                        new google.maps.Point(16, 32));
                }


                marker.push(new google.maps.Marker({
                    icon: image,
                    position: new google.maps.LatLng(<?php echo $rows[$i]->hlatitude; ?>,
                     <?php echo $rows[$i]->hlongitude; ?>),
                    map: map,
                    title: "<?php echo $database->Quote($rows[$i]->htitle); ?>"
                }));

                bounds.extend(new google.maps.LatLng(<?php echo $rows[$i]->hlatitude; ?>,
                    <?php echo $rows[$i]->hlongitude; ?>));


                var infowindow  = new google.maps.InfoWindow({});

                info_wind_<?php echo $map_uniq_id; ?> = function() {
                    <?php
                    if (strlen($rows[$i]->htitle) > 45)
                        $htitle = mb_substr($rows[$i]->htitle, 0, 25) . '...';
                    else {
                        $htitle = $rows[$i]->htitle;
                    }
                    ?>
                    var title =  "<?php echo  $database->Quote( $htitle ); ?>";
                    <?php
                        //for local images
                        $imageURL = ($rows[$i]->image_link);

                        if ($imageURL == '') $imageURL = _REALESTATE_MANAGER_NO_PICTURE_BIG;

                        $watermark_path = ($realestatemanager_configuration['watermark']['show'] == 1) ? 'watermark/' : '';
                        $watermark = ($realestatemanager_configuration['watermark']['show'] == 1) ? true : false;
                        $file_name = rem_picture_thumbnail($imageURL,
                            $realestatemanager_configuration['fotogal']['width'],
                            $realestatemanager_configuration['fotogal']['high'], $watermark);

                        $file = $mosConfig_live_site . 'components/com_realestatemanager/photos/' . $file_name;
                    ?>
                    var imgUrl =  "<?php echo $file; ?>";

                    <?php if(!incorrect_price($rows[$i]->price)):?>
                        var price =  "<?php echo $rows[$i]->price; ?>";
                        var priceunit =  "<?php echo $rows[$i]->priceunit; ?>";
                    <?php else:?>
                        var price =  "<?php echo $rows[$i]->price; ?>";
                        var priceunit="";
                    <?php endif;?>


                    <?php
                    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190327, START:
                    // For mod_realestatemanager_location_map:
                    if ( !isset($new_target) ) $new_target = '_blank';

                    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190327, END.
                    ?>
                    var contentStr = '<div>'+
                        '<a onclick=window.open("<?php echo JRoute::_("index.php?option=com_realestatemanager&task=view&id={$rows[$i]->id}&catid={$rows[$i]->idcat}&Itemid={$Itemid}");?>",target="<?php echo $new_target; ?>") >'+
                       '<img width = "100%" src = "'+imgUrl+'">'+
                       '</a>' +
                          '</div>'+
                          '<div id="marker_link">'+
                              '<a onclick=window.open("<?php echo JRoute::_("index.php?option=com_realestatemanager&task=view&id={$rows[$i]->id}&catid={$rows[$i]->idcat}&Itemid={$Itemid}");?>",target="<?php echo $new_target; ?>") >' + title + '</a>'+
                          '</div>'+
                          '<div id="marker_price">'+
                              '<a onclick=window.open("<?php echo JRoute::_("index.php?option=com_realestatemanager&task=view&id={$rows[$i]->id}&catid={$rows[$i]->idcat}&Itemid={$Itemid}");?>",target="<?php echo $new_target; ?>") >' + price +' ' + priceunit + '</a>'+
                      '</div>';

                           infowindow.setContent(contentStr);
                           infowindow.setOptions( { maxWidth: <?php echo $realestatemanager_configuration['fotogal']['width'] ; ?> });
                           infowindow.open(map,marker[<?php echo $j; ?>]);
                };




                google.maps.event.addListener(marker[<?php echo $j; ?>], 'mouseover', info_wind_<?php echo $map_uniq_id; ?> );
                //mousedown - need because mouseover not work for mobile
                google.maps.event.addListener(marker[<?php echo $j; ?>], 'mousedown', info_wind_<?php echo $map_uniq_id; ?> );




                var myLatlng = new google.maps.LatLng(<?php echo $rows[$i]->hlatitude;
                 ?>,<?php echo $rows[$i]->hlongitude; ?>);
                var myZoom = <?php echo $rows[$i]->map_zoom; ?>;


                <?php
                $j++;
            }
        }
        ?>
        if (<?php echo $j; ?>>1) map.fitBounds(bounds);
        else if (<?php echo $j; ?>==1) {map.setCenter(myLatlng);map.setZoom(myZoom)}
        else {map.setCenter(new google.maps.LatLng(0,0));map.setZoom(0);}

    }

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190401, START:
    function rem_loc_map_addLoadEvent(func) {
     var oldonload = window.onload;
     if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        }
      }
    }
  rem_loc_map_addLoadEvent(initialize2<?php echo $map_uniq_id; ?>);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Vitaliy, 190401, END.

</script>
