var v_distance_min = 0;
var v_distance_max = 50;
// var v_avg_price_min = 0;
// var v_avg_price_max = 1000;
var v_holes_number_min = 9;
var v_holes_number_max = 90;
/*
var lat = 43.670233;
var lng = -79.386755;
*/
var lat = 52.940694;
var lng = -92.490238;

var overlay;
var geocoder;
var map;
var markers = new Array();
var zoom = 8;
var defaultZoom = 3;
var map_type_id = 0;

function slideshow(f, a, l) {
	$('slide' + f).fade({ duration: 3.0, from: 1, to: 0 });
	$('slide' + a).appear({ duration: 3.0, from: 0, to: 1 });
	$('slide' + a).className = 'slide';
	if (a == l) window.setTimeout(function() {slideshow(a, 0, l)}, 6000);
	else window.setTimeout(function() {slideshow(a, a+1, l)}, 6000);
}

function dropdowns_init() {
	var handles;
	
	var distance = $('distance');
	new Control.Slider(distance.select('.handle'), distance, {
		range: $R(0, 250),
		sliderValue: [v_distance_min, v_distance_max],
		onSlide: function(values) {
			$('distance_min').innerHTML = Math.round((values[0] < values[1] ? values[0] : values[1]));
			$('distance_max').innerHTML = Math.round((values[0] > values[1] ? values[0] : values[1]));
		},
		onChange: function(values) {
			v_distance_min = values[0] < values[1] ? values[0] : values[1];
			v_distance_max = values[0] > values[1] ? values[0] : values[1];
			refreshMap();
		}
	});
	
	/*
	var avg_price = $('avg_price');
	new Control.Slider(avg_price.select('.handle'), avg_price, {
		range: $R(0, 1500),
		sliderValue: [v_avg_price_min, v_avg_price_max],
		onSlide: function(values) {
			var avg_min = Math.round((values[0] < values[1] ? values[0] : values[1]))
			$('avg_price_min').innerHTML = avg_min ? '$' + avg_min : 'N/A';
			$('avg_price_max').innerHTML = '$' + Math.round((values[0] > values[1] ? values[0] : values[1]));
		},
		onChange: function(values) { 
			v_avg_price_min = values[0] < values[1] ? values[0] : values[1];
			v_avg_price_max = values[0] > values[1] ? values[0] : values[1];
			refreshMap();
		}
	});
	*/
	
	var holes_number = $('holes_number');
	new Control.Slider(holes_number.select('.handle'), holes_number, {
		range: $R(9, 90),
		sliderValue: [v_holes_number_min, v_holes_number_max],
		onSlide: function(values) {
			$('holes_number_min').innerHTML = Math.round((values[0] < values[1] ? values[0] : values[1]));
			$('holes_number_max').innerHTML = Math.round((values[0] > values[1] ? values[0] : values[1]));
		},
		onChange: function(values) { 
			v_holes_number_min = values[0] < values[1] ? values[0] : values[1];
			v_holes_number_max = values[0] > values[1] ? values[0] : values[1];
			refreshMap();
		}
	});
}

var goto_destination = '';
function auto_destination() {
	$('city_name').value = goto_destination;
	geocoder.geocode( { address: goto_destination}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				lat = results[0].geometry.location.lat();
				lng = results[0].geometry.location.lng();
				var myLatlng = new google.maps.LatLng(lat, lng);
				var myOptions = {
					zoom: zoom,
					center: myLatlng,
					mapTypeId: map_type_id
				};
				map = new google.maps.Map($('map_canvas'), myOptions);
				refreshMap();
			}
		}
	});
	goto_destination = '';
}

function googlemap_init() {
	if (map_type_id == 0) map_type_id = google.maps.MapTypeId.ROADMAP;
	geocoder = new google.maps.Geocoder();
	
	if (goto_destination != '') auto_destination();
	else {
		var myLatlng = new google.maps.LatLng(lat, lng);
		var myOptions = {
			zoom: defaultZoom,
			center: myLatlng,
			mapTypeId: map_type_id
		};
		map = new google.maps.Map($('map_canvas'), myOptions);	
	}
}

function show_suggest_box() {
	var address = $('city_name').value;
	geocoder.geocode( { address: address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				$('suggest_box').style.display = 'block';
				$('suggest_box').innerHTML = '';
				for (var i = 0; i < results.length; i++) {
					$('suggest_box').innerHTML += '<a href="/" onclick="$(\'city_name\').value=this.innerHTML; city_changed(); refreshMap(); return false;">' + results[i].formatted_address + '</a><br />';
				}
//				map.setCenter(results[0].geometry.location);
//				lat = results[0].geometry.location.lat();
//				lng = results[0].geometry.location.lng();
//				refreshMap();
				
				/*
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					map: map
				});
				*/
			}
		} else {
//			alert("Geocode was unsuccessful due to: " + status);
		}
	});
}

function city_changed() {
	var address = $('city_name').value;
	geocoder.geocode( { address: address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				map.setCenter(results[0].geometry.location);
				lat = results[0].geometry.location.lat();
				lng = results[0].geometry.location.lng();
				map.setZoom(8);
				refreshMap();
				
				/*
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					map: map
				});
				*/
			}
		} else {
//			alert("Geocode was unsuccessful due to: " + status);
		}
	});
}

function refreshMap() {
	new Ajax.Request('ajax',{
		method: 'post',
		parameters: {
			lat: lat,
			lng: lng,
			distance_min:		v_distance_min,
			distance_max:		v_distance_max,
//			avg_price_min:		v_avg_price_min,
//			avg_price_max:		v_avg_price_max,
			holes_number_min:	v_holes_number_min,
			holes_number_max:	v_holes_number_max,
			public_courses:		$('public_courses').checked,
			private_courses:	$('private_courses').checked,
			semi_private:		$('semi_private').checked,
			miniature:			$('miniature').checked
//			power_cart:			$('power_cart').checked
		},
		onSuccess: function(transport){
			$('suggest_box').style.display = 'none';
			$('suggest_box').innerHTML = '';
			$('result').update(transport.responseText);
		},
		onLoading: function(transport){
			// loading
		}
	});
}

myOverlay.prototype = new google.maps.OverlayView(); 

function overlay_init(position, m)
{
	var swBound = new google.maps.LatLng(position.Df + 0.10, position.Ff + 0.10);
	var neBound = new google.maps.LatLng(position.Df + 0.20, position.Ff + 0.20);
	var bounds = new google.maps.LatLngBounds(swBound, neBound);
	var srcImage = '';
	overlay = new myOverlay(bounds, srcImage, m);
}

function myOverlay(bounds, image, map)
{
	this.bounds_ = bounds;
	this.image_ = image;
	this.map_ = map;
	
	this.div_ = null;
	
	this.setMap(map);
}

myOverlay.prototype.onAdd = function() {
	var div = document.createElement('div');
	div.style.border = "1px solid none";
	div.style.position = "absolute";
	
	var image = document.createElement("img");
	img.src = this.image_;
	img.style.width = "100%";
	img.style.height = "100%";
	div.appendChild(img);
	
	this.div_ = div;
	
	var panes = this.getPanes();
	panes.overlayImage.appendChild(div);
}

myOverlay.prototype.draw = function() {
	var overlayProjection = this.getProjection();
	
	var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
	var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
	var div = this.div_;
	div.style.left = sw.x + 'px';
	div.style.top = ne.y + 'px';
	div.style.width = (ne.x - sw.x) + 'px';
	div.style.height = (sw.y - ne.y) + 'px';
}

myOverlay.prototype.onRemove = function() {
	this.div_.parentNode.removeChild(this.div_);
	this.div_=null;
}
/*
function getDirection(point)
{
  directionPanel = $('#directionInfo');
  directions = new GDirections(map, directionsPanel);
  from = $('#saddr').val();  
  to = new Array(2);
  to[0] = point.lat;
  to[1] = point.lng;
  directions.loadFromWayPoints("from: " + from, "to : " + to);
}
*/


