var map;
var geocoder;

$(document).ready(function(){

	$('#find').click(function(){
		find();
	});
});

var bounds = new google.maps.LatLngBounds ();

function initialize() {
	$('#searchingNow').html('Searching Now...');
	$('#searchResultsContent').html('');

	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	var myOptions = {
		zoom: 9,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("mapCanvas"),myOptions);
	bounds = new google.maps.LatLngBounds ();
}

function find(){
	
	initialize();
	$('#mapCanvas').css('display','block');
	$('#searchResults').css('display','block');

	var brand		=	$('#brand').val();
	var wine		=	$('#wine').val();
	var zip			=	$('#zip').val();
	var location	=	$('#location').val();	
	
	geocoder.geocode( { 'address': zip}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			getResults(results[0].geometry.location);
		} else {
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}


function getResults(center){
	var miles		=	$('#miles').val();
	var brand		=	$('#brand').val();
	var wine			=	$('#wine').val();
	var wine			=	$('#wine').val();
	var location	= $('input[name=location]:checked').val();
	var searchUrl 	= 	'location-list/';
	var post		= { 
		lat: center.lat(),
		lng: center.lng(),
		radius: miles,
		brand: brand,
		wine: wine,
		location: location
	}
	
	jQuery.post(searchUrl, post, function(data) {
		a=0;
		jQuery(data).find("marker").each(function() {
			a++;
			var markerInfo = jQuery(this);
			var latlng = new google.maps.LatLng(parseFloat(markerInfo.attr("lat")),
				parseFloat(markerInfo.attr("lng")));
			
			var info=new Array();
			info['name']=markerInfo.attr("name");
			info['address']=markerInfo.attr("address");
			info['city']=markerInfo.attr("city");
			info['state']=markerInfo.attr("state");
			info['zip']=markerInfo.attr("zip");
			info['phone']=markerInfo.attr("phone");
			createMarker(info, latlng);
			bounds.extend(latlng);			
		});
		$('#searchingNow').html('');
		
		if (a == 0){
			//no items found.
			$('#searchingNow').html('No results found.');
		}
		//map.setCenter(center);
		
		map.fitBounds(bounds);
		
    });
}


var infowindow;

function createMarker(info, latlng){
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map
	});
	
	searchContent = '<li>'+info['name']+'<br />'+info['address']+'<br />'+info['city']+' '+info['state']+', '+info['zip']+'</li>';
	$('#searchResultsContent').append(searchContent);
	
	google.maps.event.addListener(marker, 'click', function() {
		if (infowindow){ infowindow.close() }
		contentVar= info['name']+'<br/>'+info['address']+','+info['city']+' '+info['state']+', '+info['zip']+'<br/><a target="_blank" href="http://maps.google.com/maps?daddr='+info['address']+'">Get Directions</a>';
		
		infowindow = new google.maps.InfoWindow({
			content: contentVar
		});
		infowindow.open(map,marker);
	});
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function changeWineList(brandId) {
	$("#wine").html($("#wineListBrand"+brandId).html());
}
