jQuery(document).ready(function() {
	var map = new google.maps.Map2(document.getElementById('map'), {size: new GSize(726, 300)});
	map.setCenter(new google.maps.LatLng(46.312918,-79.44264), 8);
	map.addControl(new GSmallMapControl());
	
	jQuery("#map").before('<ul class="map-controls noprint noshow" id="map-controls"><li>Map:</li> <li><a href="#" class="toggle-map">Hide</a></li> <li><a href="#" class="resize" rel="150">Small</a></li> <li><a href="#" class="resize" rel="350">Medium</a></li> <li><a href="#" class="resize" rel="726">Large</a></li></ul>');
	
	if (google.loader.ClientLocation) {
		var geocoder = new google.maps.ClientGeocoder();
		var strSearch = new GLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
		
		geocoder.getLocations(strSearch, function (result) { 
			if (result.Status.code == G_GEO_SUCCESS) {
				jQuery("#query").attr({'value': result.Placemark[0].address});
			}
		});
	}
	
	// === create a GDirections Object ===
	var gdir = new GDirections(map, document.getElementById("directions"));
	
	jQuery("#submit").click(function() {
		jQuery('#directions').html('');						 
		gdir.load("from: [" + jQuery("#query").val() + "] to: [1500 Fisher St, North Bay, ON, Canada]");
		return false;
	});

	// === Array for decoding the failure codes ===
	var reasons = [];
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The Google Directions object could not compute directions between the points.";
	
	// === catch Directions errors ===
	GEvent.addListener(gdir, "error", function() {
		var code = gdir.getStatus().code;
		var reason="Code " + code;
		if (reasons[code]) {
			reason = reasons[code];
		} 
		jQuery("#directions").html("<p>Failed to obtain directions, " + reason + "</p>");
	});
								
	jQuery('#contents .noshow').removeClass('noshow');
	
	jQuery('#header-animation').flash({ src: '/swf/header.swf', width: 780, height: 92, flashvars: {autoPlay: true, autoRewind: false}});
	
	var point = new google.maps.LatLng(46.312918,-79.44264);
	var marker = new GMarker(point);
	html = '<p style="white-space:nowrap; margin: 0; padding: 0;"><img src="/img/northgate-logo.jpg" alt="Northgate Entrance" style="width: 193px; display: block;" /><strong>Northgate Shopping Centre</strong><br />1500 Fisher Street<br />North Bay ON&nbsp;&nbsp;P1B&nbsp;2H3 <br />1-705-472-8110<br /><a href="/contact-us/" title="Contact Northgate Shopping Centre">info@northgateshopping.com</a></p>';
	GEvent.addListener(marker, "click", function () {
		map.panTo(marker.getLatLng());
		marker.openInfoWindowHtml(html);
	});
	map.addOverlay(marker);
	
	jQuery('.toggle-map').toggle(function(){
		var name = jQuery('.toggle-map').html();
		jQuery('#map').fadeOut(function(){ 
			map.checkResize();
		});
		jQuery('.toggle-map').text('Show'); 
	}, function(){
		jQuery('#map').fadeIn(function(){ 
			map.checkResize();
		});
		jQuery('.toggle-map').text('Hide');
	});
	
	jQuery('.resize').click(function(){
		var size = jQuery(this).attr('rel') + 'px';
		jQuery('#map').animate({
			'width': 726, 
			'height': size
			}, function(){
				map.checkResize();
			}
		);
	});
	
	jQuery('#nav ul > li').hover(function() {
		jQuery('ul:first', this).fadeIn("fast");
	},
	function() {
		jQuery('ul:first', this).fadeOut("fast");
	});
	jQuery('#nav ul li li').hover(function() {
			jQuery('ul:first', this).each(function() {
			jQuery(this).css('top', jQuery(this).parent().position().top );
			jQuery(this).css('left', jQuery(this).parent().position().left + jQuery(this).parent().width() );
			jQuery(this).show();
		});
	},
	function() {
		jQuery('ul:first', this).hide();
	});
});