var map;
var gr;
// var geocoder;
var blueparkingicon;
var orangeparkingicon;
var parkingiconshadow;
var infowindow = new google.maps.InfoWindow({
	maxWidth: 204,
	pixelOffset: new google.maps.Size(5, 1)
});

function createMap() {

	gr = new google.maps.LatLng(42.963958,-85.669714);
    var myOptions = {
		zoom: 15,
		center: gr,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL
		}
    };	
	// biz & awards page hides the map type control
	if(
		window.location.pathname=='/biz.php'
		|| window.location.pathname=='/awards.php'
	)
		myOptions.mapTypeControl= false;

    map = new google.maps.Map(document.getElementById("google_map"), myOptions);

	// geocoder = new GClientGeocoder();

	// Create our tiny parking marker icons
	blueparkingicon = new google.maps.MarkerImage('layout_imgs/marker_small_p_blue.png',
		// This marker is 12 pixels wide by 20 pixels tall.
		new google.maps.Size(12, 20),
		// The origin for this image is 0,0.
		new google.maps.Point(0,0),
		// The anchor for this image is the point at 6,20.
		new google.maps.Point(6, 20)
	);
	
	// everything is the same as the blue except the image
	orangeparkingicon = new google.maps.MarkerImage('layout_imgs/marker_small_p_orange.png',
		new google.maps.Size(12, 20), // size
		new google.maps.Point(0,0), // origin
		new google.maps.Point(6, 20) // anchor
	);

	// shadow for the icons
	parkingiconshadow = new google.maps.MarkerImage('layout_imgs/marker_small_shadow.png',
		// size is wider, origin and anchor point is the same as above
		new google.maps.Size(22,20),
		new google.maps.Point(0,0),
		new google.maps.Point(6,20)
	);

	if(typeof(nom_map_info)!='undefined'){ mapNominees(nom_map_info); }
}

var last_marker = null;
function mapLocation(coordinates, address, name, other_link) {
	
	if (last_marker)
		last_marker.setMap(null);
	infowindow.close();

	if(coordinates==false){
		map.panTo(gr);
		return true;
	}


	var full_address = address + ' Grand Rapids, MI';
	var latlong = coordinates.split(",");
	var point = new google.maps.LatLng(latlong[0], latlong[1], 0);
	
	var marker = new google.maps.Marker({
        position: point,
        map: map,
        title: name
    });
	last_marker = marker;
	map.panTo(point);

	google.maps.event.addListener(marker, "click", function() {
		infowindow.setContent('<div class="google_info_content">'+'<h3>'+name+'</h3><p>'+address+'</p><p><a target="_blank"  href="http://maps.google.com/maps?oi=map&daddr='+escape(full_address)+'&f=d">[ get directions ]</a> '+(other_link? other_link : '')+'</p>'+'</div>');
		infowindow.open(map,marker);
	});
	return true;
}

//FUNCTION TO MAP AWARD NOMINEES
var award_marker = Array();
//used to center and zoom the map to fit all the nominees in the viewpoint
var bounds = new google.maps.LatLngBounds();
function mapNominees(info){
	//console.log(info);
	//loop through and add the icons for the nominee
	for(i=0; i<info.length; i++){
		var point = new google.maps.LatLng(info[i]['lat'], info[i]['lng'], 0);
		//add this point to the bounds
		bounds.extend(point);
		var icon = createIcon(info[i]['map_marker']);		
		opts = {
			"icon": icon,
			"clickable": true,
			"title": info[i]['project_name'],
			"entry_id": info[i]['entry_id'],
			"project_name":info[i]['project_name']
		};
		var marker = new NomineeMarker(point, opts);
		google.maps.event.addListener(marker, "click", function() {
			launchAwardDetails(this.entry_id, false);
		});		
	}
}

function createIcon(marker){
	if(award_marker[marker]){
		return award_marker[marker];
	} else {		
		nom_icon = new google.maps.MarkerImage(marker,
			// This marker is 18 pixels wide by 18 pixels tall.
			new google.maps.Size(18, 18),
			// The origin for this image is 0,0.
			new google.maps.Point(0,0),
			// The anchor for this image is the point at 6,20.
			new google.maps.Point(9, 9)
		);
		award_marker[marker] = nom_icon;
		return award_marker[marker];
	}
}

function launchAwardDetails(entry_id, include_vote_link){
	$.facebox(function($) {
		$.get(
			'awards-entry_details.php',
			{'entry_id':entry_id, 'include_vote_link':include_vote_link},
			function(data){
				$('#facebox').addClass('entry_details')
				$('#facebox .content').append(data);
				$('#facebox .loading').remove();
				$('#facebox .body > *').fadeIn('normal');
			}
		)
	});
	return false;
}

function loadRegularParking() {
	load('parking_get_info.php', mapRegularParking, ''); 
}
function mapRegularParking(serialized_parking, tmp) {
	var parking = PHP_Unserialize(serialized_parking);
	var regularparking = parking['regular'];
	specialparking = parking['special'];
	
	for (lot in regularparking) {
		mapParkingLot(regularparking[lot]['coordinates'], regularparking[lot]['Address'], regularparking[lot]['Company'], regularparking[lot]['Costs'], 0);
	}
}
function loadSpecialParking() {
	load('parking_get_info.php', mapSpecialParking, ''); 
}
function mapSpecialParking() {
	for (lot in specialparking) {
		mapParkingLot(specialparking[lot]['coordinates'], specialparking[lot]['Address'], specialparking[lot]['Company'], specialparking[lot]['Costs'], 1);
	}
}
function mapParkingLot(coordinates, address, name, cost, special) {
	var latlong = coordinates.split(",");
	var point = new google.maps.LatLng(latlong[0], latlong[1], 0);
	var marker = new google.maps.Marker({
        position: point,
        map: map,
        shadow: parkingiconshadow,
        icon: special ? orangeparkingicon : blueparkingicon,
        title: name+' Parking'
    });

	google.maps.event.addListener(marker, "click", function() {
		infowindow.setContent('<div class="google_info_content"><h3>'+name+' Parking</h3><p>'+cost+'</p><p>'+address+'</p><p><a target="_blank"  href="http://maps.google.com/maps?oi=map&daddr='+escape(address+' Grand Rapids, MI 49503')+'&f=d">[ get directions ]</a></p></div>');
		infowindow.open(map,marker);
	});
	
	return true;
}

