var map;
var restaurants;
var checkWindow;
var curPos;
var latitude, longitude;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();

jQuery(document).ready(function($) {

if($("#maps").length > 0) {
	
	la = $("#maps").attr("latitude");
	lo = $("#maps").attr("longitude");

	if(la && lo) {
		latitude = la;
		longitude = lo;
	}
	if(latitude && longitude) {
		var latlng = new google.maps.LatLng(latitude, longitude);
	} else if(google.loader.ClientLocation) {
		var latt = google.loader.ClientLocation.latitude;
		var longg = google.loader.ClientLocation.longitude;
		var latlng = new google.maps.LatLng(latt, longg);
	} else {
		var latlng = new google.maps.LatLng("40.41153868","-3.70362707");
	}
	curPos = latlng;
	
	var myOptions = {
		zoom: 15,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true
	};
	
	var map = new google.maps.Map(document.getElementById("maps"), myOptions);
	
	if($("#directions").length > 0) {
		directionsDisplay = new google.maps.DirectionsRenderer();
		directionsDisplay.setMap(map);
		directionsDisplay.setPanel(document.getElementById("directions"));
	}
	

	if(latitude && longitude) {
		latlng = new google.maps.LatLng(latitude,longitude);
		marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: ""
		});
	} else if(restaurants) {
		var bounds = new google.maps.LatLngBounds();
		l = restaurants.length;

		var marker = [];
		var infowindow = new google.maps.InfoWindow({
			content: "holding..."
		});
		for(i = 0 ; i < l ; i++) {
			latlng = new google.maps.LatLng(restaurants[i][1],restaurants[i][2]);
			
			if(restaurants[i][3]) {
				ico = new google.maps.MarkerImage(restaurants[i][3], new google.maps.Size(32, 37), new google.maps.Point(0,0), new google.maps.Point(0, 32));
			} else {
				ico = new google.maps.MarkerImage("/embed/icons/restaurant.png", new google.maps.Size(32, 37), new google.maps.Point(0,0), new google.maps.Point(0, 16));
			}
			marker[i] = new google.maps.Marker({
				position: latlng,
				map: map,
				icon: ico,
				title: restaurants[i][0]
			});
			marker[i].html = restaurants[i][4];
			marker[i].postid = restaurants[i][3];

			google.maps.event.addListener(marker[i], 'click', function() {
				curPos = this.getPosition();
				infowindow.setContent(this.html);
				infowindow.open(map, this);
			});

			//infowindow.setContent(marker[i].html);
			//infowindow.open(map, marker[i]);
			bounds.extend(latlng);
		}

		if(i < 2) {
			curPos = latlng;
			map.setCenter(latlng);
		} else {
			map.fitBounds(bounds);
		}
	}
	
	$(".directionform").submit(function() {
		var from = curPos;
		var to = document.getElementById("directions_to").value;
		var request = {
			origin:from,
			destination:to,
			travelMode: google.maps.DirectionsTravelMode.DRIVING,
			unitSystem: google.maps.DirectionsUnitSystem.METRIC
		};
		directionsService.route(request, function(result, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(result);
			}
		});
		return false;
	});
	}
});

var openInfoWindow;
function infoCallback(infowindow, marker) {
	if(openInfoWindow) {
		openInfoWindow.close();
	}
	infowindow.open(map, marker);
	openInfoWindow = infowindow;
}


