<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Vladislav Prikhodko(vlados.vp1@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/



$key = 'key='.$os_cck_configuration->get("google_map_key",'');
//echo '{|script-cck-gmap|}'; //add javascript with help System Plugin
includeJavaScriptCCK('//maps.googleapis.com/maps/api/js?'.$key);


$fName = $field->db_field_name;
$clasterer = isset($layout_fields['clasterer_'.$field->db_field_name]) ? $layout_fields['clasterer_'.$field->db_field_name] : '';
$field_styling = get_field_styles($field, $layout);
$field_styling = substr_replace($field_styling, ' display:block;"', strlen($field_styling)-1, strlen($field_styling));
$hover_styling = get_hover_css_style($field, $layout_params);
echo $hover_styling;
$custom_class = get_field_custom_class($field, $layout);
$offset_animation = get_field_offset_animation($field, $layout);
if(isset($layout_fields) && $layout_fields[$fName]['options']['width'] && $layout_fields[$fName]['options']['width']){
  $map_style = ' width:'.$layout_fields[$fName]['options']['width'].
                'px;height:'.$layout_fields[$fName]['options']['width'].'px;" ';
}else{
  $map_style = ' height: 300px;" ';
}

if(isset($layout_params['fields'][$field->db_field_name.'_tooltip']) && isset($layout_params['fields']['description_'.$field->db_field_name]) && $layout_params['fields']['description_'.$field->db_field_name]=='on'){
  $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title="'.$layout_params['fields'][$field->db_field_name.'_tooltip'].'"';           
}else{
  $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title=""';
}


$field_styling = substr_replace($field_styling, $map_style, strlen($field_styling)-1, strlen($field_styling));


//add maps markers for instances to map
$tempEntity = new os_cckEntity($db->_db);  
if(!empty($instancies)){
$tempEntity->load($instancies[0]->fk_eid); 

$tempFields = $entityFieldLists[$tempEntity->eid] ;
$for_map = '';
foreach ($tempFields as $tempField) {
    if( $tempField->field_type == "locationfield" ) {

      foreach ($instancies as $instancie) {
        $tempValue = $instancie->getFieldValue( $tempField ) ;
        
        if(!empty($tempValue)  && $tempValue[0]->vlat != '' && $tempValue[0]->vlong != ''){
            $for_map = 1;
        }


      }
      break ;
    }
  }

  if ($for_map != 1){

      $layout_html = str_replace("{|f-$fName|}", '', $layout_html);

  } else {

?>

<script language="JavaScript" type="text/javascript">
var items<?php echo $fName; ?> = new Array();

if(typeof(items<?php echo $fName; ?>) !== undefined){
<?php    
  foreach ($tempFields as $tempField) {
    if( $tempField->field_type == "locationfield" ) {
      // var_dump($layout_type); exit;
      foreach ($instancies as $instancie) {
          $link = '';
          $info_layout = '';
        if(isset($layout_fields[$fName.'_instance_layout']) && $layout_fields[$fName.'_instance_layout'] != -1){
            $modId = ($moduleId) ? '&moduleId=' . $moduleId : '';
              $layout_for_link = $layouts_type[$layout_fields[$fName.'_instance_layout']];
//              $layout_for_link->load($layout_fields[$fName.'_instance_layout']);
//              $layout_type 
//              var_dump($layout_type);
              $cat_id = (isset($category->cid))?'&amp;catid='.$category->cid : '&amp;catid=0';
              $unrote_link = 'index.php?option=com_os_cck&amp;view='.$layout_for_link.'&amp;eiid[]='
              . $instancie->eiid .'&amp;lid='.$layout_fields[$fName.'_instance_layout']
              . $cat_id . '&amp;Itemid=' . $Itemid . $modId;
              $link = JRoute::_($unrote_link);
              //var_dump($link); exit;
        }
        if(isset($layout_fields[$fName.'_info_layout']) && $layout_fields[$fName.'_info_layout'] != -1){
            $temp_instancies = array();
            $temp_instancies[] = $instancie;
          ob_start();
          Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name . '_info_layout'], $instancie->eiid, $layout_params, 0, '', 0, $temp_instancies);
            $info_layout .= ob_get_contents();
            ob_end_clean();
        }
        $tempValue = $instancie->getFieldValue( $tempField ) ;
        if(!empty($tempValue)){
?>
        if('<?php echo (isset($tempValue[0])) ? $tempValue[0]->vlat : 0; ?>' != 0 && '<?php echo (isset($tempValue[0])) ? $tempValue[0]->vlong : 0; ?>' != 0){


              items<?php echo $fName; ?>.push({vlat: '<?php echo $tempValue[0]->vlat; ?>',
              vlong: '<?php echo $tempValue[0]->vlong; ?>',
              adress:<?php echo $db->quote(ltrim( $tempValue[0]->country." ".$tempValue[0]->region." ".$tempValue[0]->city." ".$tempValue[0]->zipcode." ".$tempValue[0]->address ));?>,
              link: '<?php echo $link; ?>',
              info_layout: <?php echo json_encode($info_layout); ?>,});
        }  
<?php    
        }
      }
      break ;
    }
  }
?>

}

</script>

<?php   



ob_start();?>
<div <?php echo $tooltip; ?> class="<?php echo $custom_class . ' ' . $field->db_field_name; ?>" <?php echo $offset_animation; ?> <?php echo $field_styling; ?> id="map_canvas-<?php echo $os_cck_rand; ?>"></div>
<script type="text/javascript">
  function initialize() {
      var lastmarker = null;
      var clasterer = '<?php echo $clasterer; ?>';
      var image = {url: '<?php echo JURI::root()?>/components/com_os_cck/images/marker.png'};
      var mapOptions = {
          scrollwheel: false,
          zoomControlOptions: {
              style: google.maps.ZoomControlStyle.LARGE
          },
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map_canvas-<?php echo $os_cck_rand; ?>"),
          mapOptions);
      var marker = new Array();
      var bounds = new google.maps.LatLngBounds();

      for (var i = 0; i < items<?php echo $fName; ?>.length; i++){
        bounds.extend(new google.maps.LatLng(items<?php echo $fName; ?>[i]['vlat'], items<?php echo $fName; ?>[i]['vlong']));
        marker.push(new google.maps.Marker({
          position: new google.maps.LatLng(items<?php echo $fName; ?>[i]['vlat'], items<?php echo $fName; ?>[i]['vlong']),
          map: map,
          icon: image,
          animation: google.maps.Animation.DROP,
          id: i,
          title: items<?php echo $fName; ?>[i]['adress']
        }));
        
        if('<?php echo $layout_fields[$fName.'_info_layout']; ?>' != '-1'){
            var infowindow  = new google.maps.InfoWindow({});
            info_wind_<?php echo $fName; ?> = function() {
              var id =  jQuerOs(this)[0].id;
              infowindow.setContent(items<?php echo $fName; ?>[id]['info_layout']);
              infowindow.open(map,marker[id]);
            }
            google.maps.event.addListener(marker[i], 'mouseover', info_wind_<?php echo $fName; ?> );
            //mousedown - need because mouseover not work for mobile
            google.maps.event.addListener(marker[i], 'mousedown', info_wind_<?php echo $fName; ?> );
        }
        // google.maps.event.addListener(marker[i], 'mouseover', info_wind_<?php echo $fName; ?> );
        // //mousedown - need because mouseover not work for mobile
        // google.maps.event.addListener(marker[i], 'mousedown', info_wind_<?php echo $fName; ?> );
        
        google.maps.event.addListener(marker[i], 'click', clickMarker);
      }
      map.fitBounds(bounds);
      if(clasterer == 'on'){
          var markerCluster = new MarkerClusterer(map, marker,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      function clickMarker(){
        var id = jQuerOs(this)[0].id;
        var link = items<?php echo $fName; ?>[id]['link'];
        if(link != ''){
            window.location.href = link;
        }
        //console.log('11111111111111', link)
      }
      function updateCoordinates(latlng) {
          if (latlng) {
              document.getElementById('vlatitude').value = latlng.lat();
              document.getElementById('vlongitude').value = latlng.lng();
          }
      }
      function toggleBounce() {
          if (marker.getAnimation() != null) {
              marker.setAnimation(null);
          } else {
              marker.setAnimation(google.maps.Animation.BOUNCE);
          }
      }
      function updateCoordinates(latlng) {
          if (latlng) {
              document.getElementById('vlatitude').value = latlng.lat();
              document.getElementById('vlongitude').value = latlng.lng();
          }
      }
      function codeAddress() {
          var geocoder = new google.maps.Geocoder();
          myOptions = {
              zoom: 14,
              scrollwheel: false,
              zoomControlOptions: {
                  style: google.maps.ZoomControlStyle.LARGE
              },
              mapTypeId: google.maps.MapTypeId.ROADMAP
          }
          map = new google.maps.Map(document.getElementById("map_canvas-<?php echo $os_cck_rand; ?>"), myOptions);
          var address = document.getElementById('vlocation').value +
                          " " + document.getElementById('country').value +
                          " " + document.getElementById('city').value +
                          " " + document.getElementById('vlatitude').value +
                          " " + document.getElementById('vlongitude').value;
          geocoder.geocode({
              'address': address
          }, function (results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  map.setCenter(results[0].geometry.location);
                  updateCoordinates(results[0].geometry.location);
                  if (marker) marker.setMap(null);
                  marker = new google.maps.Marker({
                      map: map,
                      position: results[0].geometry.location,
                      draggable: true,
                      icon: image,
                      animation: google.maps.Animation.DROP
                  });
                  google.maps.event.addListener(marker, 'click', toggleBounce);
                  google.maps.event.addListener(marker, "dragend", function () {
                      updateCoordinates(marker.getPosition());
                  });
              } else {
                  alert("Please check the accuracy of Address");
              }
          });
      }
      initialize.codeAddress = codeAddress;
  } // end of intialize
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
<?php 
if($clasterer == 'on'){
  //echo '{|script-cck-markerclusterer|}'; //include javascript with help system plugin
  includeJavaScriptCCK('https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js');
}
$layout_html = str_replace("{|f-$fName|}", ob_get_contents(), $layout_html);
ob_end_clean();
  }
} else {
    $layout_html = str_replace("{|f-$fName|}", '', $layout_html);
}
