$(document).ready(function () {					
	$('.map_go').click(mapGo);
});
function autoChgGoSuperRegion(superregion_id) {
	$('#go_superregion').val(superregion_id);
	chgGoSuperRegion('autoLoad');
}
function chgGoSuperRegion() {
	var autoLoad = arguments.length > 0;
	
	var frm = typeof(document.map_nav) != 'undefined' ? document.map_nav : document.profile_form;
	
	// superregion value 
	var superregionId = frm.go_superregion.value;	
	
	if (!autoLoad) {		
		$('.location_feedback').css('visibility','visible');
		location.href = '/resorts.php?superregion='+superregionId;		
	} else {
		
		// load the subregion options for this country
		var subregionSelect = frm.go_region;
		subregionSelect.selectedIndex = 0;
		subregionSelect.length = 1; // remove existing ones	
		
		if (frm.go_superregion.value != 0) {
			$.ajax({
				url : "/aj/map_nav.php",
				data : { type: 'regions', id: frm.go_superregion.value },
				dataType : 'json',
				type: 'POST',
				beforeSend: function () {
					$('.location_feedback').css('visibility','visible');;
				},
				success: function (data) {
					$.each(data.regions, function (i,val) {
						subregionSelect.options[i+1] = new Option(val.name, val.id, false);
					});
					if (!autoLoad && typeof(map) != 'undefined') {
						if (map.getZoom != 4) {
							map.setZoom(4);
						}
						map.panTo(new GLatLng(data.superregion.latitude, data.superregion.longitude));				
						//map.setCenter(new GLatLng(data.superregion.latitude, data.superregion.longitude), 4);
					}
					$('.location_feedback').css('visibility','hidden');
				}
			});
		
			frm.go_region.disabled = false;	
			if (frm.go_resort) {
				frm.go_resort.disabled = true;
			}
		}
	}
}
function autoChgGoRegion(region_id) {
	if (!$('#go_region').attr('disabled')) {
		$('#go_region').val(region_id);
		chgGoRegion('autoLoad');
	}
}
function chgGoRegion() {
	var autoLoad = arguments.length > 0;
	// load the resort options for this subregion
	var subregionSelect = typeof(document.map_nav) != 'undefined' ? document.map_nav.go_region : document.profile_form.go_region;
	var resortSelect = typeof(document.map_nav) != 'undefined' ? document.map_nav.go_resort : document.profile_form.go_resort;
	resortSelect.selectedIndex = 0;
	resortSelect.length = 1; // remove existing ones	

	if (subregionSelect.value != 0) {
		$.ajax({
			url : "/aj/map_nav.php",
			data : { type: 'resorts', id: subregionSelect.value },
			dataType : 'json',
			type: 'POST',
			beforeSend: function () {
				$('.location_feedback').css('visibility','visible');
			},
			success: function (data) {
				$.each(data.resorts, function (i,val) {
					resortSelect.options[i+1] = new Option(val.name, val.id, false);
				})
				if (!autoLoad && typeof(map) != 'undefined') {
					if (map.getZoom != 6) {
						map.setZoom(6);
					}
					map.panTo(new GLatLng(data.region.latitude, data.region.longitude));				
					//map.setCenter(new GLatLng(data.region.latitude, data.region.longitude), 6);
				}
				$('.location_feedback').css('visibility','hidden');
			}
		});
		resortSelect.disabled = false;
	}
}

function mapGo () {
	var resortSelect = typeof(document.map_nav) != 'undefined' ? document.map_nav.go_resort : document.profile_form.go_resort;
	if (resortSelect.selectedIndex < 1) {
		alert('Please select a Resort');
		return false;
	} else {
		var resortId = resortSelect.value;
		pageTracker._trackEvent('resorts', 'go_to_resort_dropdown_nav', getOptValuesString('go_to_resort_dropdown_nav',resortId), 1);
		document.location.href = '/resort.php?rid='+resortId;
	}
}