var Dooley = new Class( 
{ 
	initialize: function() 
	{
		this.initGallery();
		this.initSearch();
		this.initAdvancedSearch();
		this.initPropertyMap();
	},
	
	initGallery: function()
	{
		var galleryContainer = $('gallerycontainer');
		if($chk(galleryContainer))
		{
			var galleryThumbs = $$('img.gallery_thumb_image');
			for(var i=0; i<galleryThumbs.length; i++)
			{
				galleryThumbs[i].addEvent('click', this.galleryThumbClick.bind(this));
			}
			
			var gallery_left = $('gallery_left');
			gallery_left.addEvent('click', this.galleryMoveLeft.bind(this));
			
			var gallery_right = $('gallery_right');
			gallery_right.addEvent('click', this.galleryMoveRight.bind(this));
		}
	},
	
	galleryThumbClick: function(event)
	{
		event = new Event(event);
		event.stop();
		var galleryThumbImage = $(event.target);
		if($chk(galleryThumbImage.getParent()) && galleryThumbImage.getParent().hasClass('gallery_thumb'))
		{
			var galleryThumb = $(galleryThumbImage.getParent());
			var thumbID = parseInt(galleryThumb.id.toString().split('_')[2]);
			
			var scrollGallery = new Fx.Scroll('galleryfilmstrip', {
				wait: false,
				duration: 500,
				transition: Fx.Transitions.Quad.easeInOut
			});
			
			var thumbWidth = galleryThumbImage.getSize().x;
			var stripWidth = $('galleryfilmstrip').getSize().x;
			
			scrollGallery.start(((thumbID - 1) * thumbWidth) - (stripWidth / 2) + (thumbWidth / 2), 0);
			
			var galleryImage = $('galleryimage');
			galleryImageSrc = $('gallery_image_' + thumbID).value;
			
			new Asset.image(galleryImageSrc, {onload: function()
			{
				var myFx = new Fx.Tween(galleryImage);
				myFx.start('opacity', '1', '0').chain(function(){
					galleryImage.setStyles({'background':'url(' + galleryImageSrc + ') center center no-repeat'});
					this.start('opacity', '0', '1');
				});
			}});
		}
	},
	
	galleryMoveLeft: function(event)
	{
		event = new Event(event);
		event.stop();
		
		var scrollGallery = new Fx.Scroll('galleryfilmstrip', {
			wait: false,
			duration: 500,
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		var stripWidth = $('galleryfilmstrip').getScroll().x;
		
		scrollGallery.start(stripWidth - 57, 0);
	},
	
	galleryMoveRight: function(event)
	{
		event = new Event(event);
		event.stop();
		
		var scrollGallery = new Fx.Scroll('galleryfilmstrip', {
			wait: false,
			duration: 500,
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		var stripWidth = $('galleryfilmstrip').getScroll().x;
		
		scrollGallery.start(stripWidth + 57, 0);
	},
	
	initSearch: function()
	{
		if($chk($('for_sale')))
		{
			$('for_sale').addEvent('change', this.updateSearch.bind(this));
		}
		if($chk($('for_let')))
		{
			$('for_let').addEvent('change', this.updateSearch.bind(this));
		}
		if($chk($('property_type')))
		{
			$('property_type').addEvent('change', this.updateSearch.bind(this));
		}
		if($chk($('property_locality')))
		{
			$('property_locality').addEvent('change', this.updateSearch.bind(this));
		}
	},
	
	updateSearch: function(event)
	{
		event = new Event(event).stop();
		
		if($chk($('for_sale')))
		{
			if($('for_sale').checked == true)
			{
				forSale = 1
			}
			else
			{
				forSale = 0
			}
		}
		else
		{
			forSale = -1;
		}
		
		selectedIndex = $('property_type').selectedIndex;
		if(selectedIndex > 0)
		{
			selected_properties_type = $('property_type').options[selectedIndex].value;
		}
		else
		{
			selected_properties_type = false;
		}
		
		var jsonRequest = new Request.JSON(
		{
			url: "remote.php",
			onComplete: this.searchUpdated.bind(this)
		}).get(
		{
			'action': 'update-search',
			'for_sale': forSale,
			'property_type': selected_properties_type
		});	
	},
	
	searchUpdated: function(data)
	{
		if($chk(data))
		{
			if($chk(data['properties']) && $chk($('property_type')))
			{
				selectedIndex = $('property_type').selectedIndex;
				if(selectedIndex > 0)
				{
					selected_properties_type = $('property_type').options[selectedIndex].value;
				}
				else
				{
					selected_properties_type = false;
				}
				
				$('property_type').set('html', '');
				for(i=0; i<data['properties'].length; i++)
				{
					option = new Element('option');
					option.value = data['properties'][i]['id'];
					option.set('text', data['properties'][i]['text']);
					
					if(selected_properties_type == data['properties'][i]['id'])
					{
						option.selected = "selected";
					}
					
					option.injectInside($('property_type'));
				}
			}
			if($chk(data['localities']) && $chk($('property_locality')))
			{
				selectedIndex = $('property_locality').selectedIndex;
				if(selectedIndex > 0)
				{
					selected_properties_locality = $('property_locality').options[selectedIndex].value;
				}
				else
				{
					selected_properties_locality = false;
				}
				
				$('property_locality').set('html', '');
				for(i=0; i<data['localities'].length; i++)
				{
					option = new Element('option');
					option.value = data['localities'][i]['id'];
					option.set('text', data['localities'][i]['text']);
					
					if(selected_properties_locality == data['localities'][i]['id'])
					{
						option.selected = "selected";
					}
					
					option.injectInside($('property_locality'));
				}
			}
		}
	},
	
	initAdvancedSearch: function()
	{
		if($chk($('property_county')) && $chk($('property_locality')))
		{
			property_county = $('property_county');
			property_locality = $('property_locality');
			
			property_county.addEvent('change', this.propertyCountry.bind(this));
		}
	},
	
	propertyCountry: function(event)
	{
		var property_county = $(event.target);
		
		var jsonRequest = new Request.JSON({url: "remote.php", onComplete: function(localities){
			if($chk(localities))
			{
			    var property_locality = $('property_locality');
			    if($chk(property_locality))
			    {
			    	while (property_locality.hasChildNodes())
			    	{
						property_locality.removeChild(property_locality.childNodes[0]);
					}
					
					var i=0;
					for(i=0; localities.length; i++)
					{
						var locality = localities[i];
						
						property_locality.options[i] = new Option(locality.text, locality.id);
					}
			    }
			}
		}}).get({'action': 'getlocations', 'county': property_county.value});	
	},
	
	initPropertyMap: function(event)
	{
		if($chk($('property_map')))
		{
			if (GBrowserIsCompatible()) 
			{
				$('property_map').setStyles({'height':'500px', 'width':'100%'});
				
				var map = new GMap2($("property_map"));
				map.addControl(new GSmallMapControl());
        		map.addControl(new GMapTypeControl());
				
				var property_lat = parseFloat($('property_location_lat').value);
				var property_long = parseFloat($('property_location_long').value);
				
				var point = new GLatLng(property_lat,  property_long);
				
				map.setCenter(point, 14);
				
				//creates a marker and adds it to the map
				var dooleyIcon = new GIcon();
				dooleyIcon.shadow = "styles/images/shadow.png";
				dooleyIcon.image = "styles/images/Teacht.png";
				dooleyIcon.iconSize = new GSize(40, 40);
		        dooleyIcon.shadowSize = new GSize(40, 40);
		        dooleyIcon.iconAnchor = new GPoint(15, 40);
				
				
				var markerOptions = { icon:dooleyIcon };
				
				marker = new GMarker(point, markerOptions);
        		map.addOverlay(marker);
			}
		}
	}
});

window.addEvent('load', function() 
{ 
	var dooley = new Dooley(); 
});