var address = false;
var points = new Hash();
var map = false;
var thumb = false;

window.onload = function() {
	loadMap();	
	loadSlideshow();
}

window.onunload = function() { GUnload() };

function loadMap() {
	if (GBrowserIsCompatible()) {
	       	map = new GMap2(document.getElementById('map'));
			geocoder = new GClientGeocoder();
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			if (address == false) {
				var point = new GLatLng(points.lat,points.lng);
				plot(point);
			}else{
				geocoder.getLatLng(address,plot);
			}
		}
}

function plot(point) {
	if(point) {
		map.setCenter(point, 13);
		map.addOverlay(new GMarker(point));
	}else{
		$('map').update('<div id="noMap">We were unable to find the listed address.</div>');
	}

}

function loadSlideshow() {
	
	var thumbs = $$('a.thumb');;

	for (var i = 0; i < thumbs.length; i++) {
		
		Event.observe(thumbs[i], 'click', function(event) {
			
			thumb = this;
			
			thumb.immediateDescendants()[0].style.display = 'block';

			event.stop();

			new Ajax.Request('properties/update_photo/',
				  {
				    method:'post',
					parameters: {image: this.id.split('_')[1]},
				    onSuccess: function(transport){
						var json = transport.responseText.evalJSON();
						if (json.status == 1) {
							$('mainPhoto').update(json.img);
							$('mainPhoto').immediateDescendants()[0].toggle();
							Event.observe($('mainPhoto').immediateDescendants()[0],'load',function(event) { this.toggle(); thumb.immediateDescendants()[0].style.display = 'none'; });
						}else{
							thumb.immediateDescendants()[0].style.display = 'none';
							alert('There was a problem loading the next image.  Please refresh the page and try again.  If the problem resists please let us know.');
						}
				    },
				    onFailure: function(){ 
						thumb.immediateDescendants()[0].style.display = 'none';
						alert('There was a problem loading the next image.  Please refresh the page and try again.  If the problem resists please let us know.');
						}
				  });
			
		});
		
	}
	
}
