$(function()
{
  // COUNTRIES
	$("select#ctlCountry").change(function(){
		$.getJSON("scripts/getState.php",{id: $(this).val(), ajax: 'true'}, function(j){
			var options = '<option value="" selected="selected">Select a State or Region</option>';
			for (i = 0; i < j.length; i++) {
				// onclick is not supported in non-mozilla browsers, instead we use the onchange event handler on the select tag
				//options += '<option onclick="findLocationState(\'' + j[i].optionState + ',' + j[i].optionCountry + '\',\'<strong>' +j[i].optionState + ', ' + j[i].optionCountry + '</strong>\')" value="' + j[i].optionState + '">' + j[i].optionState + '</option>';
				options += '<option value="' + j[i].optionState + '">' + j[i].optionState + '</option>';
				desc = '<strong>' + j[i].optionCountry + '</strong><br /><br />' + j[i].optionCountryDescription;
			}
			$("#ctlState").html(options);
			$('#ctlState option:first').attr('selected', 'selected');
			$("#ctlDescription").html(desc);
			$("#ctlAVA").html('<option value="" selected="selected">- - -</option>');
		
		})
		
		$.getJSON("scripts/getWineriesCountry.php",{id: $(this).val(), ajax: 'true'}, function(j){
			var options = '<option value="" selected="selected">Select a Winery</option>';
			for (var i = 0; i < j.length; i++) {
				//options += '<option onclick="findLocationWinery(\'' + j[i].optionWineryAddress + '\',\'' + j[i].optionWineryLong + '\',\'' + j[i].optionWineryLat + '\')" value="' + j[i].optionWineryValue + '">' + j[i].optionWineryDisplay + '</option>';
			  options += '<option value="' + j[i].optionWineryValue + '">' + j[i].optionWineryDisplay + '</option>';
			}
			$("#ctlWineries").html(options);
			$('#ctlWineries option:first').attr('selected', 'selected');
			
		})
	})
	//$("select#ctlCountry").attr("value","1"); //auto selects first sub upon page opening
	//$("select#ctlCountry").trigger("change");
}
)

$(function()
{
  // STATES
	$("select#ctlState").change(function(){
		$.getJSON("scripts/getAVA.php",{id: $(this).val(), ajax: 'true'}, function(j){
			var options = '<option value="" selected="selected">Select an AVA or Subregion</option>';
			var g = 0;
			for (var i = 0; i < j.length; i++) {
				//options += '<option onclick="findLocationState(\'' + j[i].optionAVA + ',' + j[i].optionState +',' + j[i].optionCountry + '\',\'<strong>' +j[i].optionState + ', ' + j[i].optionCountry + '</strong><br />' + j[i].optionAVA + '\')" value="' + j[i].optionAVA + '">' + j[i].optionAVA + '</option>';
				options += '<option value="' + j[i].optionAVA + '" id="' + j[i].optionAVA + ',' + j[i].optionState +',' + j[i].optionCountry + '">' + j[i].optionAVA + '</option>';
				if (j[i].optionAVA != '') { g++; }
				desc = '<strong>' + j[i].optionState + '</strong><br /><br />' + j[i].optionRegionDescription;
			}
			$("#ctlAVA").html(options);
			$('#ctlAVA option:first').attr('selected', 'selected');
			$("#ctlDescription").html(desc);
			if(g == 0) {
				$("#ctlAVA").html('<option value="" selected="selected">- - -</option>');
			}
		})
		$.getJSON("scripts/getWineriesState.php",{id: $(this).val(), ajax: 'true'}, function(j){
			var options = '<option value="">Select a Winery</option>';
			for (var i = 0; i < j.length; i++) {
				//options += '<option onclick="findLocationWinery(\'' + j[i].optionWineryAddress + '\',\'' + j[i].optionWineryLong + '\',\'' + j[i].optionWineryLat + '\')" value="' + j[i].optionWineryValue + '">' + j[i].optionWineryDisplay + '</option>';
			  options += '<option value="' + j[i].optionWineryValue + '" id="' + j[i].optionWineryAddress + ', ' + j[i].optionWineryLong + ', ' + j[i].optionWineryLat + '">' + j[i].optionWineryDisplay + '</option>';
			}
			$("#ctlWineries").html(options);
			$('#ctlWineries option:first').attr('selected', 'selected');
		})
	})
	//$("select#ctlState").attr("value","1"); //auto selects first sub upon page opening
	//$("select#ctlState").trigger("change");
}
)

$(function()
{
  // AVA
	$("select#ctlAVA").change(function(){
		$.getJSON("scripts/getWineriesAVA.php",{id: $(this).val(), ajax: 'true'}, function(j){
			var options = '<option value="" selected="selected">Select a Winery</option>';
			for (var i = 0; i < j.length; i++) {
				//options += '<option onclick="findLocationWinery(\'' + j[i].optionWineryAddress + '\',\'' + j[i].optionWineryLong + '\',\'' + j[i].optionWineryLat + '\')" value="' + j[i].optionWineryValue + '">' + j[i].optionWineryDisplay + '</option>';
				options += '<option value="' + j[i].optionWineryValue + '" id="' +  j[i].optionWineryAddress + ',' + j[i].optionWineryLong + ',' + j[i].optionWineryLat + '">' + j[i].optionWineryDisplay + '</option>';
				desc = '<strong>' + j[i].optionAVA + '</strong><br /><br />' + j[i].optionAVADescription;
			}
			$("#ctlWineries").html(options);
			$('#ctlWineries option:first').attr('selected', 'selected');
			$("#ctlDescription").html(desc);
		})
	})
	//$("select#ctlAVA").attr("value","1"); //auto selects first sub upon page opening
	//$("select#ctlAVA").trigger("change");
}
)

$(function()
{
  // WINERIES
	$("select#ctlWineries").change(function(){
		$.getJSON("scripts/getWinery.php",{id: $(this).val(), ajax: 'true'}, function(j){
				smDesc = j[0].optionWineryDescription;
				//alert(smDesc);
			$("#ctlDescription").html(smDesc);
			$('#ctlWineries').attr('selected', 'selected');

		})
	})
}
)

var map;
var geocoder;
zoom = 1;
var baseIcon = new GIcon();
		baseIcon.iconSize = new GSize(42, 47);
		//baseIcon.iconSize = new GSize(0, 0);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(15, 15);
		baseIcon.infoShadowAnchor = new GPoint(15, 25);
var icon = new GIcon(baseIcon);
		icon.image = "images/grapes2.png";
		
function initialize() {
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(34, -40), zoom, G_HYBRID_MAP);
	geocoder = new GClientGeocoder();
}

// countryToMap()
function addAddressToMap(response) {
  map.clearOverlays();
  if (response) {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    marker = new GMarker(point,icon);
    map.setZoom(zoom);
    map.addOverlay(marker);
    map.panTo(new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]));
  }
}

// showLocation() geocodes the address
function showLocation(address) {
  geocoder.getLocations(address, addAddressToMap);
}

//find the country
function findLocationCountry(address) {
  zoom = 4;
  htmlMessage = '<strong>' + address + '</strong>';
  showLocation(address);
}

//find the state or region
function findLocationState(address) {
  zoom = 6;
  htmlMessage = '<strong>' + address + '</strong>';
  showLocation(address);
}

//find the AVA
function findLocationAVA(address) {
  zoom = 7;
  htmlMessage = '<strong>' + address + '</strong>';
  showLocation(address);
}

//find the Winery
function findLocationWinery(id) {
  // split the id
  var tempArray = id.split(',');
  zoom = 16;
  longitude = tempArray[tempArray.length - 2];
  latitude = tempArray[tempArray.length - 1];
  updateMap();
}

// update the map
function updateMap() 
{
  map.clearOverlays();
  if (latitude == 0 && longitude == 0) {
    alert("Sorry, we are unable to process this location at this time.");
  } else {
    point = new GLatLng(latitude, longitude);
    marker = new GMarker(point,icon);
    map.setZoom(zoom);
    map.addOverlay(marker);
    map.panTo(new GLatLng(latitude,longitude));
	}
}