$(document).ready(function() {
	$("#mapdiv #map_control .close").click(function() {
		// hide map

		$("#mapdiv").fadeOut(250, function() {
			$("#map_container").fadeOut(250);
		});

	});
	
	$(window).resize(function() {
		position_map();
	});
	
	$(window).scroll(function() {
		position_map();
	});
});

function show_map_point(id) {

	zoom = 12;
	
	lat = office[id]['lat'];
	lng = office[id]['lng'];
	
	address = office[id]['address']+"<br />"+office[id]['city']+", "+office[id]['province']+"<br />"+office[id]['postcode'];
	contact = new Array();
	contact[0] = office[id]['phone'] ? "<strong>Phone:</strong> "+office[id]['phone'] : false;
	contact[1] = office[id]['fax'] ? "<strong>Fax:</strong> "+office[id]['fax'] : false;
	
	contact_info = "";
	for (i = 0; i < contact.length; i++) {
		if (!contact[i]) {
			continue;
		}
		
		contact_info += (i != contact.length - 1) ? contact[i]+"<br />" : contact[i];
	}
	contact_info = contact_info ? "<p>"+contact_info+"</p>" : "";
	
//	contact = "<strong>Phone:</strong> "+office[id]['phone']+"<br /><strong>Fax:</strong> "+office[id]['fax'];
	
	var gmap = new GMap2(document.getElementById("map"));
	
	gmap.addControl(new GSmallMapControl());
	gmap.addControl(new GMapTypeControl());
	
	gmap.setCenter(new GLatLng(lat,lng),zoom);
	gmap.checkResize();
	marker = new GMarker(new GLatLng(lat,lng));
	gmap.addOverlay(marker);
	info_html = "<h4>"+office[id]['name']+"</h4><p>"+address+"</p><p>"+contact_info+"</p><p><a href=\"http://maps.google.ca/maps/ms?ie=UTF8&hl=en&msa=0&msid=100814477951806619658.00044c959c43101875469&ll=43.464881,-79.887085&spn=0.69771,1.167297&z=9&source=embed\">Get Directions</a></p>";
	marker.openInfoWindowHtml(info_html);
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindow(info_html);
	});

	show_map(office[id]['name'], gmap, lat, lng);
}

function show_map(office, gmap, lat, lng) {
	
	position_map();
	
	$("#mapdiv #map_control .titlebar").html(office);

	// prevent scrolling
//	$("body").css({overflow:"hidden"});

	// show map

	// reset map container so it's not expanding the document size
	$("#map_container").width(0);
	$("#map_container").height(0);

	$("#map_container").css({display: 'block', opacity: '0'});
	
	// make container fullscreen
	$("#map_container").width($(document).width());
	$("#map_container").height($(document).height());	
	
	$("#map_container").fadeTo("slow", ".75", function() {
		$("#mapdiv").fadeIn(250, function() {
			gmap.checkResize();
			var new_lat = parseFloat(lat) + 0.035;
			gmap.panTo(new GLatLng(new_lat,lng));
		});											 
	});
}

function position_map() {

	docWidth = $(document).width();
	docHeight = $(document).height();
	
	// define map dimensions
	mapWidth = 640;
	mapHeight = 511;
	$("#mapdiv").width(mapWidth);
	$("#mapdiv").height(mapHeight);

	// get current position offset of document	
	heightOffset = document.documentElement.scrollTop ? document.documentElement.scrollTop : window.pageYOffset;
	widthOffset = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : window.pageXOffset;
	heightOffset = heightOffset ? heightOffset : 0; /* Internet Explorer returns scroll offset as undefined when value is 0 */
	widthOffset = widthOffset ? widthOffset : 0;


// calculate new co-ordinates for map
//	new_top = (docHeight - heightOffset - mapHeight) / 2;
	new_top = (($(window).height() - mapHeight) / 2) + heightOffset;
//	new_left = (docWidth - widthOffset - mapWidth) / 2;
	new_left = ($(window).width() - mapWidth) / 2 + widthOffset;
	
	$("#mapdiv").css({'left': new_left, 'top': new_top});

}