<?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('Restricted access');
if($this->rseventsConfig['locations.gmaps'] == 1) {
	JHTML::script('maps?file=api&amp;v=2&amp;key='.$this->rseventsConfig['locations.gMaps.key'],'http://maps.google.com/',false);
	JHTML::script('api?file=uds.js&v=1.0&key='.$this->rseventsConfig['locations.gMaps.key'],'http://www.google.com/uds/',false);
	JHTML::script('gmlocalsearch.js','http://www.google.com/uds/solutions/localsearch/',false);
	JHTML::stylesheet('gsearch.css','http://www.google.com/uds/css/');
	JHTML::stylesheet('gmlocalsearch.css','http://www.google.com/uds/solutions/localsearch/');
}
?>
<script language="javascript" type="text/javascript">

function validation()
{
	var form = document.adminForm;
	var ret = true;
	
	if(form.LocationName.value=='') { form.LocationName.className = 'rserror'; ret=false; } else { form.LocationName.className = '';  }	
	if(form.LocationAddress.value=='') { form.LocationAddress.className = 'rserror'; ret=false; } else { form.LocationAddress.className = '';  }	
	return ret;
	 
}
</script>
<style>
.rserror 
{
	border:1px solid red;
}
</style>
<?php if($this->rseventsConfig['locations.gmaps'] == 1) { ?>
<script type="text/javascript">

	var app;
	function OnLoad() {
		app = new App();
	}
	
	function App() {
	
		this.myMap = null;
		
		this.myMap = new GMap2(document.getElementById("google_map"),{mapTypes:[<?php echo $this->rseventsConfig['google.map'] ?>]});
		
		this.myMap.addControl(new GLargeMapControl());
		
		<?php
			if(!empty($this->data->LocationLat) && !empty($this->data->LocationLon)) {
				echo 'var point = new GLatLng('.$this->data->LocationLat.', '.$this->data->LocationLon.');';
				echo 'this.myMap.setCenter(new GLatLng('.$this->data->LocationLat.', '.$this->data->LocationLon.'), 10);';
			} else {
				echo 'var point = new GLatLng('.$this->rseventsConfig['locations.gMaps.center'].');';
				echo 'this.myMap.setCenter(new GLatLng('.$this->rseventsConfig['locations.gMaps.center'].'), 10);';
			}
			
		?>
		
		

		var image = new Image();
		
		image.src = "http://www.google.com/mapfiles/marker.png";


		var myIcon = new GIcon();
		myIcon.iconAnchor = new GPoint((image.width/2), image.height);
		myIcon.image = image.src;
		
		var markerOptions = { icon:myIcon, draggable: true, "title":"<?php echo JFilterOutput:: stringURLSafe($this->data->LocationName); ?>" };
		
		var gmarker = new GMarker(point, markerOptions);
			
														
		this.myMap.addOverlay(gmarker);		
		
		GEvent.addListener(gmarker, "dragend", function() {
			document.getElementById("lat").value = this.getLatLng().lat();
			document.getElementById("lon").value = this.getLatLng().lng();
		});
		
		GEvent.addListener(this.myMap, "singlerightclick", function(point) {
			point = this.fromContainerPixelToLatLng(point);
			gmarker.setLatLng(point);
			document.getElementById("lat").value = gmarker.getLatLng().lat();
			document.getElementById("lon").value = gmarker.getLatLng().lng();
		});				
		
		var searchControl = new GSearchControl();
		
		var localSearch = new GlocalSearch();
		
		var options = new GsearcherOptions();
		options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);
		searchControl.addSearcher(new GlocalSearch(), options);
		
		localSearch.setCenterPoint(this.myMap);
		
		searchControl.draw(document.getElementById("searchcontrol"));
		
		searchControl.setSearchCompleteCallback(this, App.prototype.OnSearchComplete);
		searchControl.setSearchStartingCallback(this, App.prototype.OnSearchStarting);
		
		<?php
			if (empty($this->data->LocationLat) && empty($this->data->LocationLon)) {
				echo 'searchControl.execute();';
			}
		?>
	}
	
	App.prototype.OnSearchComplete = function(sc, searcher) {
		if ( searcher.results && searcher.results.length > 0) {
			this.myMap.clearOverlays();
			for (var i = 0; i < searcher.results.length; i++) {
				var result = searcher.results[i];
				
				if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS ) {
					var markerObject = new Object();
					markerObject.result = result;
					markerObject.latLng = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));

					var image = new Image();
					
					image.src = "http://www.google.com/mapfiles/marker.png";


					var myIcon = new GIcon();
					myIcon.iconAnchor = new GPoint((image.width/2), image.height);
					myIcon.image = image.src;
					
					markerObject.markerOptions = { icon:myIcon, draggable: true, "title":"<?php echo $this->data->LocationName; ?>" };
					
					markerObject.gmarker = new GMarker(markerObject.latLng, markerObject.markerOptions);
					var clickHandler = method_closure(this, App.prototype.OnMarkerClick, [markerObject]);
					GEvent.bind(markerObject.gmarker, "click", this, clickHandler);

					this.myMap.addOverlay(markerObject.gmarker);
					result.__markerObject__ = markerObject;
					
					this.myMap.setCenter(markerObject.latLng);
					
					document.getElementById("lat").value = markerObject.latLng.lat();
					document.getElementById("lon").value = markerObject.latLng.lng();
					
					GEvent.addListener(markerObject.gmarker, "dragend", function() {
						document.getElementById("lat").value = this.getLatLng().lat();
						document.getElementById("lon").value = this.getLatLng().lng();
					});
					
					GEvent.addListener(this.myMap, "singlerightclick", function(point) {
						point = this.fromContainerPixelToLatLng(point);
						markerObject.gmarker.setLatLng(point);
						document.getElementById("lat").value = markerObject.gmarker.getLatLng().lat();
						document.getElementById("lon").value = markerObject.gmarker.getLatLng().lng();
					});
				}
			}
		}
	}
	
	App.prototype.OnSearchStarting = function(sc, searcher, query) {
		//
	}
	
	
	function method_closure(object, method, opt_argArray) {
		return function() {
			return method.apply(object, opt_argArray);
		}
	}
	
	GSearch.setOnLoadCallback(OnLoad);	

</script>
<?php } ?>

<form action="index.php" method="post" name="adminForm" id="adminForm">
	<table cellspacing="0" cellpadding="0" border="0" width="100%">
		<tr>
			<td valign="top">
				<table  class="adminform">
					<tr>
						<td>&nbsp;</td>
						<td>						
							<?php if (isset($_GET['tmpl'])) { ?>
							<button onclick="window.parent.document.getElementById('sbox-window').close();" style="float:right;"><?php echo JText::_('RSE_CANCEL_BTN'); ?></button>
							<?php } else { ?>
							<button onclick="document.location = '<?php echo html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events'.$this->itemId)); ?>';" style="float:right;"><?php echo JText::_('RSE_CANCEL_BTN'); ?></button>
							<?php } ?>
							<?php if (isset($_GET['tmpl'])) { ?>
							<button type="submit" onclick="if (validation()) { window.top.setTimeout('window.parent.rse_refresh_locations(\'<?php echo JURI::root(); ?>\',\'IdLocation\');window.parent.document.getElementById(\'sbox-window\').close()',100); } else return false;" style="float:right;"><?php echo JText::_('RSE_SAVE_BTN'); ?></button>
							<?php } else { ?>
							<button type="submit" onclick="return validation();" style="float:right;"><?php echo JText::_('RSE_SAVE_BTN'); ?></button> 
							<?php } ?>
						</td>
					</tr>
					<tr>
						<td>
							<label for="name">
								<?php echo JText::_( 'RSE_LOCATION_NAME' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationName" value="<?php echo $this->data->LocationName; ?>" size="50" maxlength="100" /> (*)
						</td>
					</tr>
					<tr>
						<td>
							<label for="weburl">
								<?php echo JText::_( 'RSE_LOCATION_URL' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationURL" value="<?php echo $this->data->LocationURL; ?>" size="50" maxlength="100" />
						</td>
					</tr>
					<tr>
						<td>
							<label for="address">
								<?php echo JText::_( 'RSE_LOCATION_ADDRESS' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationAddress" value="<?php echo $this->data->LocationAddress; ?>" size="50" /> (*)
						</td>
					</tr>
					<tr>
						<td>
							<label for="zip">
								<?php echo JText::_( 'RSE_LOCATION_ZIP' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationZip" value="<?php echo $this->data->LocationZip; ?>" size="50" maxlength="100" />
						</td>
					</tr>
					<tr>
						<td>
							<label for="city">
								<?php echo JText::_( 'RSE_LOCATION_CITY' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationCity" value="<?php echo $this->data->LocationCity; ?>" size="50" maxlength="100" />
						</td>
					</tr>
					<tr>
						<td>
							<label for="state">
								<?php echo JText::_( 'RSE_LOCATION_STATE' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationState" value="<?php echo $this->data->LocationState; ?>" size="50" maxlength="100" />
						</td>
					</tr>
					<tr>
						<td>
							<label for="country">
								<?php echo JText::_( 'RSE_LOCATION_COUNTRY' ).':'; ?>
							</label>
						</td>
						<td>						
							<input name="LocationCountry" value="<?php echo $this->data->LocationCountry; ?>" size="50" maxlength="100" />
						</td>
					</tr>
					<tr>
						<td>
							<label for="description">
								<?php echo JText::_( 'RSE_LOCATION_DESCRIPTION' ).':'; ?>
							</label>
						</td>
						<td>
							<?php $editor=& JFactory::getEditor();
							echo $editor->display('LocationDescription',$this->data->LocationDescription,550,400,70,10); ?>						
						</td>
					</tr>
					<?php if($this->rseventsConfig['locations.gmaps'] == 1) { ?>
					<tr>
						<td>
							<label for="map">
								<?php echo JText::_( 'RSE_LOCATION_MAP' ).':'; ?>
							</label>
						</td>
						<td>
							<div id="google_map" style="width: 400px; height: 300px; overflow:hidden;"></div>
							<p>		
							<?php echo JText::_( 'RSE_LOCATION_LATITUDE' ); ?> <input class="text_area" type="text" name="LocationLat" id="lat" size="20" value="<?php echo @$this->data->LocationLat;?>" /> <?php echo JText::_( 'RSE_LOCATION_LONGITUDE' ); ?> <input class="text_area" type="text" name="LocationLon" id="lon" size="20" value="<?php echo @$this->data->LocationLon;?>" />
					</p>				
						</td>
					</tr>
					<?php } ?>
				</table>
			</td>
		</tr>
	</table>
<?php echo JHTML::_( 'form.token' ); ?>
<input type="hidden" name="option" value="com_rsevents" />
<input type="hidden" name="cid" value="<?php echo $this->data->IdLocation; ?>" />
<input type="hidden" name="IdLocation" value="<?php echo $this->data->IdLocation; ?>" />
<input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
<input type="hidden" name="controller" value="locations" />
<input type="hidden" name="view" value="locations" />
<input type="hidden" name="layout" value="addeditlocation" />
<input type="hidden" name="task" value="save" />
</form>

<?php
//keep session alive while editing
JHTML::_('behavior.keepalive');
?>