<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

defined('_JEXEC') or die();
 
jimport( 'joomla.application.component.model' );

class rseventsModelLocations extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
	
		global $mainframe;
		$params = &$mainframe->getParams();
		$db = JFactory::getDBO();
		$filter=JRequest::getVar('rs_filter');
		$ordering = $params->get('order','DESC');
		
		$this->_query="SELECT IdLocation as RecordId, LocationName , LocationDescription,  published  FROM #__rsevents_locations WHERE LocationName LIKE '%".$db->getEscaped($filter)."%' ORDER BY LocationName $ordering";
		//echo $this->_query;
	}
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe, $option;

		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');

		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
		

		//echo $this->_db->_errorMsg;
	}
	
	function getData()
	{
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}
	
	function getTotal()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$this->_total = $this->_getListCount($this->_query);	
		}
		return $this->_total;
	}
	
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}
	
	function getLocation()
	{
		$cid= JRequest::getVar('cid',0,'request');
		$row= & JTable::getInstance('Rsevents_Locations','Table');
		$row->load($cid);
		return $row;
	}
	
	function getLocationById($cid)
	{
		$row= & JTable::getInstance('Rsevents_Locations','Table');
		$row->load($cid);
		return $row;
	}
	
	function getLocationName($IdLocation)
	{
		$IdLocation = intval($IdLocation);
		$db = & JFactory::getDBO();
		$db->setQuery("SELECT LocationName FROM #__rsevents_locations WHERE IdLocation='".$IdLocation."'");
		$row=$db->loadResult();
		return $row;
	}
	
	//get permissions for adding a new location
	
	function getLocationPermission()
	{
		$db = & JFactory::getDBO();
		$user =& JFactory::getUser();
		$gid = $user->gid;
		$db->setQuery("SELECT gp.PermissionValue FROM #__rsevents_groups g LEFT JOIN #__rsevents_groups_acl ga ON g.IdGroup = ga.IdGroup "
					."LEFT JOIN #__rsevents_group_permissions gp ON g.IdGroup = gp.IdGroup WHERE ga.GroupAcl = '".$gid."' AND gp.PermissionName = 'allowLocations'");
		$row = $db->loadResult();
		return $row;
	}
	
	function getMap($num = '') 
{		
			global $mainframe;
			$rseventsConfig = $mainframe->getUserState('rseventsConfig');
			$model = $this->getModel('locations');
			$row= & JTable::getInstance('Rsevents_Locations','Table');
			$row->load($num);

if (!empty($row->LocationLat) && !empty($row->LocationLon)) 
{		
			if (empty($num)) { $num = ''; }
		
$map ='
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$rseventsConfig['locations.gMaps.key'].'" type="text/javascript"></script>
<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key='.$rseventsConfig['locations.gMaps.key'].'" type="text/javascript"></script>
<script src="http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js" type="text/javascript"></script>

<style type="text/css">
	@import url("http://www.google.com/uds/css/gsearch.css");
	@import url("http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css");
</style>
<script type="text/javascript">

	function addListener(element, baseName, handler) {
		if (element.addEventListener)
			element.addEventListener(baseName, handler, false);
		else if (element.attachEvent)
			element.attachEvent("on"+baseName,handler);
	}
	
	
	function initialize'.$num.'() 
	{
	
		if (GBrowserIsCompatible()) 
		{
			var map'.$num.' = new GMap2(document.getElementById("google_map'.$num.'"),{mapTypes:[G_HYBRID_MAP]});
			var point = new GLatLng('.$row->LocationLat.','.$row->LocationLon.');
			map'.$num.'.setCenter(new GLatLng('.$row->LocationLat.','.$row->LocationLon.'),4);
			var mapControl = new GMapTypeControl();
			map'.$num.'.addControl(mapControl);
			map'.$num.'.addControl(new GLargeMapControl());
			map'.$num.'.addControl(new GOverviewMapControl());
			map'.$num.'.enableDoubleClickZoom();
			map'.$num.'.enableScrollWheelZoom();
			var bounds = new GLatLngBounds;
			
			var myIcon = new GIcon();
			myIcon.image = "http://www.google.com/mapfiles/marker.png";
			myIcon.iconAnchor = new GPoint((markerImage1.width/2),markerImage1.height);
			
			
			
			bounds.extend(point);
			setBounds(map'.$num.',bounds);				
			
			function createMarker(point) 
			{
				var marker = new GMarker(point);
				GEvent.addListener(marker,"click", function() {
				var myHtml = "<b>'.addslashes($row->LocationName).'</b><br/>'.addslashes($row->LocationAddress).' , '.addslashes($row->LocationCity).' - '.addslashes($row->LocationCountry).'";
				map'.$num.'.openInfoWindowHtml(point, myHtml);
				});
				return marker;
			}
			map'.$num.'.addOverlay(createMarker(point));
		}
	}
				
	function setBounds(map'.$num.',bounds) 
	{
		map'.$num.'.setZoom(15);
		map'.$num.'.setCenter(bounds.getCenter());
	}					

	function chargement() 
	{	
		markerImage1 = new Image(); 
		markerImage1.src = "http://www.google.com/mapfiles/marker.png";
		setTimeout("initialize'.$num.'()", 500); 
	}

	addListener(window, "load", chargement);
</script>
<div style="width: 100%;">
	<div id="google_map'.$num.'" style="width: 100%; height: 300px; overflow:hidden;"></div>
</div>';
} 	else $map = '';
		return $map;
}
}