	
	
	
/*--------------------------------------------------------------------------------
	Search panel
--------------------------------------------------------------------------------*/

	document.observe("dom:loaded", function() {
	
		if($('propertymap'))
		{
			initMap();
		}
		
		var container = $$('div.property div.search_panel')[0];
		
		// Hide headings
		container.select('h2').each(function(element){ element.hide(); });
		
		// Show the prebuilt tab links
		var tabs_ul = container.select('ul.tabs')[0];
		tabs_ul.show();
		
		tabs_ul.select('li').each(function(li){
			li.observe('click', function(event){
				event.stop();
				
				// Get the previously selected tab and its class name
				var selected_li = tabs_ul.select('li.selected')[0];
				var selected_class = $w(selected_li.className)[0];
				
				// Unselect the previous tab and hide its form
				selected_li.removeClassName('selected');
				container.select('div.' + selected_class)[0].hide();
				
				// Select the selected tab and show its form
				li.addClassName('selected');
				var class_name = $w(li.className)[0];
				container.select('div.' + class_name)[0].show();
				
			}.bindAsEventListener());
		});
		
		
		// Hide the form divs
		container.select('div.sales')[0].hide();
		container.select('div.lettings')[0].hide();
		
		// Show the selected tab's form
		var selected_tab = tabs_ul.select('li.selected')[0];
		var class_name = $w(selected_tab.className)[0];
		container.select('div.' + class_name)[0].show();
		
		
		// Select STC when "Plot of land" is the selected property type.
		$('propertyType').observe('change', function(){
			if (this.value == 'land'){
				$('showSold').setAttribute('checked', true);
			}
		});
		
		new Carousel('.carousel_inner');
		
		
		
		
	});
	
	
/*--------------------------------------------------------------------------------
	Autocomplete
--------------------------------------------------------------------------------*/

	document.observe("dom:loaded", function() {
		if($('townPostcode'))
		{
			new Autocomplete('townPostcode', { serviceUrl:'/assets/modules/property/ajax_includes/autocomplete.php' });
		}
	});	
	
	
/*--------------------------------------------------------------------------------
	Carousel
--------------------------------------------------------------------------------*/

	document.observe("dom:loaded", function() {
		autoScroll();
	});
	
	var scrollerEl;
	var scrollerRowEl;
	var currentPos = 0;
	var itemWidth = 202;
	var speed = 40;
	var currentItem = 1;
	var items;
	var timer;
	var currentRequest = false;
	
	function autoScroll()
	{
		scrollerEl = $('scrollerTable');
		scrollerRowEl = $('scrollerRow');
		
		if(!scrollerEl || !scrollerRowEl)
		{
			return false;
		}
		
		var container = scrollerEl.up();
		container.observe('mouseover', function(){ playPause('pause'); });
		container.observe('mouseout', function(){ playPause('play'); });
		
		items = scrollerRowEl.select('td').length;
		
		itemWidth = parseInt('-' + itemWidth);
		
		playPause('play');
	}
	
	function moveScroller()
	{
		newPos = currentPos - 1;
		
		if (newPos == itemWidth){
			moved = document.getElementById('scrollerItem' + currentItem);
			scrollerRowEl.removeChild(moved);
			scrollerRowEl.appendChild(moved);
		
			if(currentItem == items)
				currentItem = 1;
			else
				currentItem++;
			
			currentPos = 0;
			newPos = -1;
		} else {
			currentPos = newPos;
		}
		newPosValue = newPos + 'px';
		
		if (scrollerEl){
			scrollerEl.style.left = newPosValue;
		}
	}

	function playPause(action)
	{
		switch (action)
		{
			case 'play':
				window.clearInterval(timer);
				timer = window.setInterval(moveScroller, speed);
				break;
			case 'pause':
				window.clearInterval(timer);
				break;
		}
	}
	
	
	
/*--------------------------------------------------------------------------------
	Property details thumbnails
--------------------------------------------------------------------------------*/

	document.observe("dom:loaded", function() {
		
		var main_image = $$('div.property div.details div.mainImage img')[0];
		var main_image_src = main_image.getAttribute('src');
		
		$$('div.property div.details div.thumbnails img').each(function(element, key){
			
			var src = element.getAttribute('data-hoverimage');
			
			element.observe('mouseover', function(){
				//element.default_img = main_image.getAttribute('src');
				main_image.setAttribute('src', src);
			});
			
			element.observe('mouseout', function(){
				main_image.setAttribute('src', main_image_src);
			});
			
		});
	});
		
/*--------------------------------------------------------------------------------
	
	Property map functions, these need to be added to a class and called 
	statically
	
--------------------------------------------------------------------------------*/


	var requestURL = '/cms/modules/property/ajax_handler.php';
	
	function reloadMap()
	{
	//	alert(map.getZoom());
		document.getElementById('map_status').style.display = 'block';
		document.getElementById('map_status').innerHTML = 'Loading properties...';
		var bounds = map.getBounds();
		
		GDownloadUrl(requestURL+'?mode=update_map&ne='+bounds.getNorthEast().toUrlValue()+'&sw='+bounds.getSouthWest().toUrlValue()+'&z='+map.getZoom(),  function(responseText, httpCode){ 
			if(httpCode == 200)
			{
				document.getElementById('map_status').innerHTML = 'adding markers...';
				map.clearOverlays();
				try
				{
					var points = eval('(' + responseText + ')');
					
					for(i = 0; i < points.length; i++)
					{
						var thispoint = points[i];
						
						if(thispoint.totalproperties > 4)
						{
							var popup_contents = '<p>'+thispoint.totalproperties+' properties in this area.<br /><a href="#" onclick="map.setCenter(new GLatLng('+thispoint.latitude+', '+thispoint.longitude+'),  map.getZoom()+3); return false;">Zoom in</a> to see them all.</p>';
						}
						else
						{
							if(thispoint.totalproperties == 1)
							{
								var popup_contents = propertyHTML(thispoint.properties[0]);
							}
							else
							{
								var popup_contents = new Object;
								for(j = 0; j < thispoint.properties.length; j++)
								{
									popup_contents[(j+1)] = propertyHTML(thispoint.properties[j]);
								}
								
							}
						}
						
						var marker = createMarker(new GLatLng(thispoint.latitude, thispoint.longitude), popup_contents, icons[thispoint.icon], thispoint.tooltip);
						map.addOverlay(marker);
						
						
					}
					
					document.getElementById('map_status').innerHTML = '&nbsp;';
					document.getElementById('map_status').style.display = 'none';

				}
				catch(err)
				{
					document.getElementById('map_status').innerHTML = 'Error loading properties - ' + err.description ;
				}
			}
		});
		
	}
	
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
	
	function propertyHTML(property)
	{
		
		var property_bedrooms = (property.bedrooms > 0) ? property.bedrooms+' Bedroom' : 'Studio';
		
		// this element has to have its style set inline to stop a bug in IE6.
		var display = '<div class="infoWindowHolder" style="width: 400px; height: 165px; zoom: 1; _margin-bottom: -45px;">';
		
			if(property.type == 'development')
			{
				display += '<h4>'+property.streetName+', '+property.area_district+'</h4>';
			}
			else
			{
				display += '<h4>'+property_bedrooms+' '+property.type+' in '+property.streetName+', '+property.area_district+'</h4>';
			}
		
			if(property.mainImage)
			{
				display += '<img src="http://'+window.location.host+'/assets/file_uploads/m_property_properties_mainImage/'+property.mainImage+'" class="propertyImage" alt="Image of property" width="180" height="135" />';
			}
			else
			{
				display += '<img src="http://'+window.location.host+'/assets/templates/mishonmackay/images/placeholder.gif" class="propertyImage" alt="Image of property" width="180" height="135" />';
			}
			
			display += '<div class="details">';
			display += '<div class="summary">'+property.briefDescription+'</div>';	
				display += '<div class="price">&pound;'+property.salePrice+' | '+property.saleStatus+'</div>';
				display += '<div class="options">';
					display += '<a href="../'+property.id+'/">View full details</a> | ';
					display += '<a href="#" onclick="addToShortlist(this, \''+property.id+'\', \''+property.streetName+', '+property.area_district+'\'); return false">Add to shortlist</a>';
					
					/*
					$html.= '<form action="http://maps.google.com/maps" method="get" target="_blank">';
					$html.= '<div class="getdirections">Get directions to this property</div>';
					$html.= '<input type="text" class="txtField" name="saddr" style="width: 135px" onfocus="if(this.value ==\\\'Enter postcode\\\') this.value = \\\'\\\';" value="Enter postcode" />';
					$html.= '<input value="Go" type="submit">';
					$html.= '<input type="hidden" name="daddr" value="'.$property['postCode'].'">';
					$html.= '</form>';
					*/
				
				display += '</div>';
			display += '</div>';
			display += '<div class="clear"></div>';
			
		display += '</div>';
		
		
		return display;
	}
	
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
	
	function reloadMapClearMarkers()
	{
		map.clearOverlays();
		reloadMap();
	}

/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/

	function addToShortlist(obj, propid, title)
	{
		GDownloadUrl(requestURL+'?mode=addToShortlist&id='+propid+'&title='+title,  function(responseText, httpCode)
		{ 
			if(httpCode == 200)
			{
				obj.innerHTML = 'View shortlist';
				obj.onclick = function() { window.location = '/property-search/shortlist/'; };
			}
		});
	}
	
	function removeFromShortlist(obj, propid)
	{
		GDownloadUrl(requestURL+'?mode=removeFromShortlist&id='+propid,  function(responseText, httpCode)
		{ 
			if(httpCode == 200)
			{
				obj.innerHTML = 'Add to shortlist';
				obj.onclick = function() { addToShortlist(this, ''+propid+''); return false; };
			}
		});
	}

/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/



