<?php 
// no direct access
defined('_JEXEC') or die('Restricted access');
?>

<div class="vehiclemanager_<?php if($moduleclass_sfx!='') echo $moduleclass_sfx; ?>">
<noscript>Javascript is required to use Vehicle Manager location map module<a 
href="http://ordasoft.com/vehicle-manager-joomla-automotive-extension"
>Vehicle Manager is automotive Joomla component for car/vehicle management</a>, <a
 href="http://ordasoft.com/vehicle-manager-joomla-automotive-extension"
>With Vehicle Manager component you can manage different types of vehicles: cars, trucks, trailers, vans, bikes, motorcycles, boats, yachts, campers ..</a></noscript>
  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=<?php echo $api_key; ?>" ></script>
  <script type="text/javascript">
    function vmFireEvent(el,event) {

        if ("createEvent" in document) {
            el.click();
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent(event, true, true);// event type,bubbling,cancelable
            return el.dispatchEvent(evt);
        }
        else{
          // dispatch for IE
          var evt = document.createEventObject();
          return el.fireEvent("on"+event,evt);              
       }
    }    
    
    function vmOpenItem() {
        var tmpLinks=document.getElementsByTagName('a');
        
        for(var i=0;i<tmpLinks.length;i++){
          if(tmpLinks[i].name=='vm_link_item'){
            setTimeout(function(){vmFireEvent(tmpLinks[i],'click')},50);
            return ;            
          }          
        }
    }    
    
    
    
    
    
    window.addEvent('domready', function() {
      var marker = new Array();
      var myOptions = {
        zoom:  1,
        scrollwheel: false,
        center: new google.maps.LatLng(<?php if (isset($rows[0]->vlatitude) && $rows[0]->vlatitude) echo $rows[0]->vlatitude; else echo 0; ?>,
        <?php if (isset($rows[0]->vlatitude) && $rows[0]->vlongitude) echo $rows[0]->vlongitude; else echo 0; ?>),
        <?php if ($params->get('menu_map') == 0) echo "mapTypeControl: false,"; else echo "mapTypeControl: true,";?>
        <?php if ($params->get('control_map') == 0) echo "zoomControl: false, panControl: false, streetViewControl: false,";
             else echo "zoomControl: true, panControl: true, streetViewControl: true,";?>
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var imgCatalogPath = "<?php 
              echo $mosConfig_live_site; ?>/components/com_vehiclemanager/";
      var map_mod_vehicle = new google.maps.Map(document.getElementById("vm_mod_map_canvas<?php echo $pr; ?>"), myOptions);
      var bounds = new google.maps.LatLngBounds ();
      <?php
      $newArr = explode(",", _VEHICLE_MANAGER_LOCATION_MARKER);
      $j=0;
      for ($i=0;$i < count($rows);$i++){
        if ($rows[$i]->vlatitude && $rows[$i]->vlongitude) {
          $numPick = '';
          if(isset($newArr[$rows[$i]->vtype])){
              $numPick = $newArr[$rows[$i]->vtype];
          } ?>
          var srcForPic = "<?php echo $numPick; ?>";
          var image = '';
          if(srcForPic.length){
            var image = new google.maps.MarkerImage(imgCatalogPath + srcForPic,
                new google.maps.Size(32, 39),
                new google.maps.Point(0,0),
                new google.maps.Point(16, 39));
          }
          marker.push(new google.maps.Marker({
              icon: image,
              position: new google.maps.LatLng(<?php echo $rows[$i]->vlatitude; ?>,
               <?php echo $rows[$i]->vlongitude; ?>),
              map: map_mod_vehicle,
              title: "<?php echo $database->Quote($rows[$i]->vtitle); ?>"
          }));
          bounds.extend(new google.maps.LatLng(<?php echo $rows[$i]->vlatitude; ?>,<?php
           echo $rows[$i]->vlongitude; ?>));
          var infowindow  = new google.maps.InfoWindow({});
          google.maps.event.addListener(marker[<?php echo $j; ?>], 'mouseover', function() {
          <?php
          if (strlen($rows[$i]->vtitle) > 45)
              $vtitle = substr($rows[$i]->vtitle, 0, 25) . '...';
          else {
              $vtitle = $rows[$i]->vtitle;
          }
          ?>     
          var title =  "<?php echo $vtitle ?>";
          <?php 
            //for local images
            $imageURL = ($rows[$i]->image_link);
            if ($imageURL == '') $imageURL = _VEHICLE_MANAGER_NO_PICTURE_BIG;
                $file_name = vm_picture_thumbnail($imageURL,150,150);
                $file = $mosConfig_live_site . '/components/com_vehiclemanager/photos/' . $file_name;
          ?>
          var imgUrl =  "<?php echo $file; ?>";
          var price =  "<?php echo $rows[$i]->price; ?>";
          var priceunit =  "<?php echo $rows[$i]->priceunit; ?>";
          var foto_width = 150;
          var foto_height = 150;
          var contentStr = '<div>'+
                              '<div>'+
                                  '<img width = "'+foto_width+'" height="'+foto_height+'" src = '+imgUrl+
                                      ' onclick="vmOpenItem();" >'+
                              '</div>'+
                              '<div id="marker_link">'+
                                  '<a name="vm_link_item" target="<?php echo $new_target; ?>" '+
                                    ' href="index.php?option=com_vehiclemanager&task=view_vehicle&id=<?php
                                     echo $rows[$i]->id; ?>&catid=<?php echo $rows[$i]->idcat 
                                     ?>&Itemid=<?php echo $Itemid;?>" >' +
                                     title + '</a>'+
                              '</div>'+
                              '<div id="marker_price">'+
                                  '<a target="<?php echo $new_target; ?>" '+
                                    ' href="index.php?option=com_vehiclemanager&task=view_vehicle&id=<?php
                                     echo $rows[$i]->id; ?>&catid=<?php echo $rows[$i]->idcat 
                                     ?>&Itemid=<?php echo $Itemid;?>" >' +
                                   price +' ' + priceunit + '</a>'+
                              '</div>'+
                          '</div>';
            infowindow.setContent(contentStr);
            infowindow.open(map_mod_vehicle,marker[<?php echo $j; ?>]);
          });
          var myLatlng = new google.maps.LatLng(<?php echo $rows[$i]->vlatitude; ?>,<?php
           echo $rows[$i]->vlongitude; ?>);
          var myZoom = <?php echo $rows[$i]->map_zoom; ?>;
          <?php
          $j++;
        }
      } ?>
      if (<?php echo $j; ?>>1) map_mod_vehicle.fitBounds(bounds);
      else if (<?php echo $j; ?>==1) {map_mod_vehicle.setCenter(myLatlng);map_mod_vehicle.setZoom(myZoom)}
      else {map_mod_vehicle.setCenter(new google.maps.LatLng(0,0));map_mod_vehicle.setZoom(0);}
    });
  </script>
  <div id="vm_mod_map_canvas<?php echo $pr; ?>" class="vm_mod_map_canvas" 
    style="width: <?php echo $params->get('map_width');
    ?>px; height: <?php echo $params->get('map_height'); ?>px; float: rigth;" >
  </div>
</div>
