loadLanguage();
	}
	public function _setPhocaMapsPluginNumber() {
		$this->_plgPhocaMapsNr = (int)$this->_plgPhocaMapsNr + 1;
	}
	public function _setPhocaMapsPluginLoadedBootstrap() {
		$this->_loadedBootstrap = (int)$this->_loadedBootstrap + 1;
	}
	public function onContentPrepare($context, &$article, &$params, $page = 0) {
		// Don't run this plugin when the content is being indexed
		if ($context == 'com_finder.indexer') {
			return true;
		}
		//$layout 			= new FileLayout('test', null, []);
		//$layout->addIncludePaths([JPATH_SITE . '/plugins/content/phocamaps/layouts']);
		$app 	= Factory::getApplication();
		$view	= $app->input->get('view');
		if ($view == 'tag') { return; }
		$param['display_map_description'] = $this->params->get('display_map_description', 0);
		$param['display_auto_location'] = $this->params->get('display_auto_location', 0);
		// Start Plugin
		$regex_one		= '/({phocamaps\s*)(.*?)(})/si';
		$regex_all		= '/{phocamaps\s*.*?}/si';
		$matches 		= array();
		$count_matches	= preg_match_all($regex_all,$article->text,$matches,PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
		$lang = Factory::getLanguage();
		$lang->load('com_phocamaps.sys');
		$lang->load('com_phocamaps');
		// Start if count_matches
		if ($count_matches != 0) {
			if (!ComponentHelper::isEnabled('com_phocamaps', true)) {
				Text::_('PLG_CONTENT_PHOCAMAPS_PLUGIN_REQUIRE_COMPONENT');
				return true;
			}
			$document		= Factory::getDocument();
			$db 			= Factory::getDBO();
			//$menu 			= &JSite::getMenu();
			//$plugin 		= &JPluginHelper::getPlugin('content', 'phocamaps');
			//$paramsP 		= new JParameter( $plugin->params );
			$paramsP		= $this->params;
			require_once( JPATH_ROOT.'/components/com_phocamaps/helpers/route.php' );
			require_once( JPATH_ADMINISTRATOR.'/components/com_phocamaps/helpers/phocamapspath.php' );
			require_once( JPATH_ADMINISTRATOR.'/components/com_phocamaps/helpers/phocamaps.php' );
			require_once( JPATH_ADMINISTRATOR.'/components/com_phocamaps/helpers/phocamapsmap.php' );
			require_once( JPATH_ADMINISTRATOR.'/components/com_phocamaps/helpers/phocamapsmaposm.php' );
			//$component 		= 'com_phocamaps';
			//$table 			=& JTable::getInstance('component');
			//$table->loadByOption( $component );
			//$paramsC	 	= new JParameter( $table->params );
			$component			=	'com_phocamaps';
			$paramsC			= ComponentHelper::getParams($component) ;
			$tmpl			= array();
			HTMLHelper::_('jquery.framework', false);
			$document->addStyleSheet(URI::base(true).'/media/com_phocamaps/css/phocamaps.css');
			$document->addStyleSheet(URI::base(true).'/media/plg_content_phocamaps/css/default.css');
			$allIds = array();
			for($i = 0; $i < $count_matches; $i++) {
				// MUST BE HERE - defined for each instance
				$tmpl['enable_kml']				= $paramsC->get( 'enable_kml', 0 );
				$tmpl['display_print_route']	= $paramsC->get( 'display_print_route', 1 );
				$tmpl['close_opened_window']	= $paramsC->get( 'close_opened_window', 0 );
				$tmpl['map_type']				= $paramsC->get( 'map_type', 2 );
				$tmpl['osm_map_type']			= $paramsC->get( 'osm_map_type', 'osm' );
				$tmpl['osm_search']				= $paramsC->get( 'osm_search', 0 );
				$tmpl['osm_easyprint'] 			= $paramsC->get( 'osm_easyprint', 0 );
				$this->_setPhocaMapsPluginNumber();
				// Only loaded when the type is really map not a link - see below view=map YES, view=link NO
				//$id	= 'PlgPM'.(int)$this->_plgPhocaMapsNr;
				//$allIds[] = $id;
				$view	= '';
				$idMap	= '';
				$text	= '';
				//$lang   = '';
				// Get plugin parameters
				$phocaMaps	= $matches[0][$i][0];
				preg_match($regex_one,$phocaMaps,$phocaMaps_parts);
				$parts			= explode("|", $phocaMaps_parts[2]);
				$values_replace = array ("/^'/", "/'$/", "/^'/", "/'$/", "/
/");
				foreach($parts as $key => $value) {
					$values = explode("=", $value, 2);
					foreach ($values_replace as $key2 => $values2) {
						$values = preg_replace($values2, '', $values);
					}
					// Get plugin parameters from article
						 if($values[0]=='view')				{$view							= $values[1];}
					else if($values[0]=='id')				{$idMap							= $values[1];}
					else if($values[0]=='text')				{$text							= $values[1];}
					//else if($values[0]=='lang')			{$lang							= $values[1];}
					else if($values[0]=='kmlfile')			{$tmpl['enable_kml']			= $values[1];}
					else if($values[0]=='printroute')		{$tmpl['display_print_route']	= $values[1];}
					else if($values[0]=='maptype')			{$tmpl['map_type']				= $values[1];}
					else if($values[0]=='osmmaptype')		{$tmpl['osm_map_type']			= $values[1];}
					else if($values[0]=='iframesrc')		{$tmpl['iframesrc']				= $values[1];}
					else if($values[0]=='iframewidth')		{$tmpl['iframewidth']			= $values[1];}
					else if($values[0]=='iframeheight')		{$tmpl['iframeheight']			= $values[1];}
				}
				$output = '';
				switch($view) {
					// - - - - - - - - - - - - - - - -
					// Map
					// - - - - - - - - - - - - - - - -
					case 'map':
						$id	= 'PlgPM' . (int)$this->_plgPhocaMapsNr;
						// Javascript for all Google Maps
						if ($tmpl['map_type'] == 1) {
							$allIds[] = $id;
						}
						$query = 'SELECT a.*'
							.' FROM #__phocamaps_map AS a'
							.' WHERE a.id = '.(int) $idMap;
						$db->setQuery($query);
						$mapp = $db->loadObject();
						if (empty($mapp)) {
							echo '
'. Text::_('PLG_CONTENT_PHOCAMAPS_PLUGIN_ERROR') . ' - '. Text::_('PLG_CONTENT_PHOCAMAPS_MAP_NOT_EXISTS') . ' (ID = '.$idMap.')
';
							return false;
						}
						$query = 'SELECT a.*, i.id as iconid, i.url as iurl, i.urls as iurls, i.object as iobject, i.objects as iobjects, i.objectshape as iobjectshape'
								.' FROM #__phocamaps_marker AS a'
								.' LEFT JOIN #__phocamaps_map AS c ON c.id = a.catid '
								.' LEFT JOIN #__phocamaps_icon AS i ON i.id = a.iconext '
								.' WHERE c.id = '.(int) $idMap
								.' AND a.published = 1'
								.' ORDER BY a.ordering ASC';
						$db->setQuery($query);
						$markerp = $db->loadObjectList();
						// Parameters
						$tmpl['apikey']					= $paramsC->get( 'google_maps_api_key', '' );
						$tmpl['displayphocainfo']		= $paramsC->get( 'display_phoca_info', 1 );
						$tmpl['displaymapdescription']	= $paramsP->get( 'display_map_description', 0 );
// - - - - - - - - - - - - - - -
// RENDER
// - - - - - - - - - - - - - - -
// Display Description
$tmpl['description'] = '';
if (isset($mapp->description) && $mapp->description != '' && $param['display_map_description'] == 1) {
	$tmpl['description'] = ''.$mapp->description.'
';
}
// Check Width and Height
$tmpl['fullwidth'] = 0;
if (!isset($mapp->width)) {
	$mapp->width = '100%';
}
if (isset($mapp->width) && (int)$mapp->width < 1) {
	$tmpl['fullwidth'] = 1;
}
if (!isset($mapp->height) || (isset($mapp->height) && (int)$mapp->height < 1)) {
	$mapp->height = '50vh';
}
if (!isset($mapp->zoom) || (isset($mapp->zoom) && (int)$mapp->zoom < 1)) {
	$mapp->zoom = 2;
}
if (is_numeric($mapp->width)) {
	$mapp->width = $mapp->width . 'px';
}
if (is_numeric($mapp->height)) {
	$mapp->height = $mapp->height . 'px';
}
// Map Langugage
$tmpl['params'] = '';
if (!isset($mapp->lang) || (isset($mapp->lang) && $mapp->lang == '')) {
	//$tmpl['params'] 		= '{other_params:"sensor=false"}';
	$tmpl['params'] 		= '';
	$tmpl['paramssearch'] 	= '';
	$tmpl['lang']			= '';
} else {
	//$tmpl['params'] 		= '{other_params:"sensor=false&language='.$mapp->lang.'"}';
	$tmpl['params'] 		= '{other_params:"language='.$mapp->lang.'"}';
	$tmpl['paramssearch'] 	= '{"language":"'.$mapp->lang.'"}';
	$tmpl['lang']			= $mapp->lang;
}
// Design
$tmpl['border'] = '';
if (isset($mapp->border)) {
	switch ($mapp->border) {
		case 1:
			$tmpl['border'] = '-grey';
		break;
		case 2:
			$tmpl['border'] = '-greywb';
		break;
		case 3:
			$tmpl['border'] = '-greyrc';
		break;
		case 4:
			$tmpl['border'] = '-black';
		break;
	}
}
// Plugin - no border
$tmpl['stylesite'] 	= 'margin:0;padding:0;margin-top:10px;';
$tmpl['stylesitewidth']	= '';
if ($tmpl['fullwidth'] == 1) {
	$tmpl['stylesitewidth'] = 'style="width:100%"';
}
// Parameters
if (isset($mapp->continuouszoom) && (int)$mapp->continuouszoom == 1) {
	$mapp->continuouszoom = 1;
} else {
	$mapp->continuouszoom = 0;
}
if (isset($mapp->doubleclickzoom) && (int)$mapp->doubleclickzoom == 1) {
	$mapp->disabledoubleclickzoom = 0;
} else {
	$mapp->disabledoubleclickzoom = 1;
}
if (isset($mapp->scrollwheelzoom) && (int)$mapp->scrollwheelzoom == 1) {
	$mapp->scrollwheelzoom = 1;
} else {
	$mapp->scrollwheelzoom = 0;
}
// Since 1.1.0 zoomcontrol is alias for navigationcontrol
if (empty($mapp->zoomcontrol)) {
	$mapp->zoomcontrol = 0;
}
if (empty($mapp->scalecontrol)) {
	$mapp->scalecontrol = 0;
}
if (empty($mapp->typecontrol)) {
	$mapp->typecontrol = 0;
}
if (empty($mapp->typecontrolposition)) {
	$mapp->typecontrolposition = 0;
}
if (empty($mapp->typeid)) {
	$mapp->typeid = 0;
}
// Display Direction
$tmpl['displaydir'] = 0;
if (isset($mapp->displayroute) && $mapp->displayroute == 1) {
	if (isset($markerp) && !empty($markerp)) {
		$tmpl['displaydir'] = 1;
	}
}
// KML Support
$tmpl['load_kml'] = FALSE;
if($tmpl['enable_kml'] == 1) {
	jimport( 'joomla.filesystem.folder' );
	jimport( 'joomla.filesystem.file' );
	$path = PhocaMapsPath::getPath();
	if (isset($mapp->kmlfile) && File::exists($path->kml_abs . $mapp->kmlfile)) {
		$tmpl['load_kml'] = $path->kml_rel_full . $mapp->kmlfile;
	}
}
$output .= '';
if ((!isset($mapp->longitude))
		|| (!isset($mapp->latitude))
		|| (isset($mapp->longitude) && $mapp->longitude == '')
		|| (isset($mapp->latitude) && $mapp->latitude == '')) {
	$output .= '
' . Text::_('COM_PHOCAMAPS_MAP_ERROR_FRONT') . '
';
} else {
	#########################
	# ALL MAPS - Google Maps, OpenStreetMap
	#########################
	$output .= $tmpl['description'];
	// Map Box
	if ($tmpl['border'] == '') {
		$output .= '
';
		if ($tmpl['fullwidth'] == 1) {
			$output .= '
';
		} else {
			$output .= '
';
		}
		$output .= '
';
	} else {
		$output .= '
';
		if ($tmpl['fullwidth'] == 1) {
			$output .= '
';
		} else {
			$output .= '
';
		}
		$output .= '
';
		//echo '
';
				if ($tmpl['display_print_route'] == 1) {
					$output .= '
';
				}
				$output .= '
';
				$output .= '
';
				$output .= '';
				if ($tmpl['display_print_route'] == 1) {
					$output .= '
';
				}
				$output .= '
' . addslashes($markerV->title) . '
';
						// template
						// $text = '' . addslashes($markerV->title) . '
';
						// Try to correct images in description
						$markerV->description = PhocaMapsHelper::fixImagePath($markerV->description);
						$markerV->description = str_replace('@', '@', $markerV->description);
						//$markerV->description = str_replace("/", '/', $markerV->description);
						$markerV->description = str_replace("'", ''', $markerV->description);
						//$markerV->description = str_replace('"', '"', $markerV->description);
						//$markerV->description = htmlentities($markerV->description);
						// template
						/*
						$text .= ''. PhocaMapsHelper::strTrimAll(addslashes($markerV->description)).'
';
						if ($markerV->displaygps == 1) {
							$text .= '| '. Text::_('PLG_CONTENT_PHOCAMAPS_GPS') . ':'
									.' | '.PhocaMapsHelper::strTrimAll(addslashes($markerV->gpslatitude)).' | 
'
									.'| '
									.' | '.PhocaMapsHelper::strTrimAll(addslashes($markerV->gpslongitude)).' | 
' . addslashes($markerV->title) . '
';
					// Try to correct images in description
					$markerV->description = PhocaMapsHelper::fixImagePath($markerV->description);
					$markerV->description = str_replace('@', '@', $markerV->description);
					$text .= ''. PhocaMapsHelper::strTrimAll(addslashes($markerV->description)).'
';
					if ($markerV->displaygps == 1) {
						$text .= '| '. Text::_('COM_PHOCAMAPS_GPS') . ':'
								.' | '.PhocaMapsHelper::strTrimAll(addslashes($markerV->gpslatitude)).' | 
'
								.'| '
								.' | '.PhocaMapsHelper::strTrimAll(addslashes($markerV->gpslongitude)).' | 
'. Text::_('PLG_CONTENT_PHOCAMAPS_PLUGIN_ERROR') . ' - '. Text::_('PLG_CONTENT_PHOCAMAPS_MAP_NOT_EXISTS') . ' (ID = '.$idMap.')
';
								return false;
							}
							$query = 'SELECT a.id'
								.' FROM #__phocamaps_marker AS a'
								.' LEFT JOIN #__phocamaps_map AS c ON c.id = a.catid '
								.' WHERE c.id = '.(int) $idMap
								.' AND a.published = 1';
							$db->setQuery($query);
							$markerp = $db->loadObjectList();
							$linkMap 		= PhocaMapsHelperRoute::getMapRoute( $mapp->id, $mapp->alias);
							if ($text =='') {
								$text = Text::_('PLG_CONTENT_PHOCAMAPS_LINK_TO_MAP');
							}
							// Parameters
							$tmpl['detailwindow']		= $paramsP->get( 'detail_window', 0 );
							$tmpl['mbbordercolor']		= $paramsP->get( 'modal_box_border_color', '#6b6b6b' );
							$tmpl['mbborderwidth']		= $paramsP->get( 'modal_box_border_width', 2 );
							$tmpl['mboverlaycolor']		= $paramsP->get( 'modal_box_overlay_color', '#000000' );
							$tmpl['mboverlayopacity']	= $paramsP->get( 'modal_box_overlay_opacity', 0.3 );
							if ($mapp->width > 0) {
								$tmpl['windowwidth'] = (int)$mapp->width + 20;
							} else {
								$tmpl['windowwidth'] = 640;
							}
							if ($mapp->width > 0) {
								$tmpl['windowheight'] = (int)$mapp->height + 20;
							} else {
								$tmpl['windowheight'] = 360;
							}
							//Route
							if (isset($mapp->displayroute) && $mapp->displayroute == 1) {
								if (isset($markerp) && !empty($markerp)) {
									$tmpl['windowheight'] = (int)$tmpl['windowheight'] + 40;
								}
							}
							if ($tmpl['detailwindow'] == 1) {
								$button = new CMSObject();
								$button->set('name', 'phocamaps');
								$button->set('methodname', 'js-button');
								$button->set('options', "window.open(this.href,'win2','width=".$tmpl['windowwidth'].",height=".$tmpl['windowheight'].",menubar=no,resizable=yes'); return false;");
								$output .= ''.$text.'';
							} else if ($tmpl['detailwindow'] == 0) {
								// Button
								HTMLHelper::_('behavior.modal', 'a.modal-button');
								$cssSbox = " #sbox-window {background-color:".$tmpl['mbbordercolor'].";padding:".$tmpl['mbborderwidth']."px} \n"
								." #sbox-overlay {background-color:".$tmpl['mboverlaycolor'].";} \n";
								$document->addCustomTag( "\n");
								$button = new CMSObject();
								$button->set('name', 'phocamaps');
								$button->set('modal', true);
								$button->set('methodname', 'modal-button');
								$button->set('options', "{handler: 'iframe', size: {x: ".$tmpl['windowwidth'].", y: ".$tmpl['windowheight']."}, overlayOpacity: ".$tmpl['mboverlayopacity'].", classWindow: 'phocamaps-plugin-window', classOverlay: 'phocamaps-plugin-overlay'}");
								$output .= ''.$text.'';
							} else if ($tmpl['detailwindow'] == 2) {
								// Bootstrap Modal
								$item 		= 'phPlgMapsModalDetail' . $this->_plgPhocaMapsNr;
								if($this->_loadedBootstrap == 0) {
									HTMLHelper::_('script', 'media/plg_content_phocamaps/js/main.js', array('version' => 'auto'));
									Factory::getApplication()
										->getDocument()
										->getWebAssetManager()
										->useScript('bootstrap.modal');
									$output .= '';
									$this->_setPhocaMapsPluginLoadedBootstrap();
								}
								$output .= ''.$text.'';
							}
						}
					break;
					case 'iframe':
						$output = '';
						$width = isset($tmpl['iframewidth']) && $tmpl['iframewidth'] != '' ? $tmpl['iframewidth'] : '100%';
						$height = isset($tmpl['iframeheight']) && $tmpl['iframeheight'] != '' ? $tmpl['iframeheight'] : '450';
						if (isset($tmpl['iframesrc']) && $tmpl['iframesrc'] != '') {
							$output = '';
						}
					break;
				}
				$article->text = preg_replace($regex_all, $output, $article->text, 1);
			} // end foreach
			// Add init for all Google Maps
			$iDi = '';
			if (!empty($allIds)) {
				$jsI = ''."\n";
				$mapA	= new PhocaMapsMap($iDi);
				$jsI	.= $mapA->loadAPI($iDi, $mapp->lang);
				$article->text = $article->text . $jsI;
			}
		}// end if count_matches
		return true;
	}
	public function getTemplateOutput($data, $tmpl = "default") {
		ob_start();
		$getTemplatePath = PluginHelper::getLayoutPath('content', $this->plg_name, $tmpl);
		include($getTemplatePath);
		return ob_get_clean();
	}
}
?>