var map;
var geocoder;
var address = {};
var marker_title = {};
var sid = 'pointgrey';
		
Event.observe(window, 'load', function() {
	map = new GMap2(document.getElementById('map'));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(49.263753,-123.204468), 14);
	geocoder = new GClientGeocoder();
	address['pointgrey'] = '4346 West 10th Ave, Vancouver, BC, V6R 2H7, Canada';
	address['granvilledkny'] = '2625 Granville Street Vancouver, BC, V6H 3H2, Canada';
	address['oakridgedkny'] = '650 West 41st Ave Vancouver, BC, V5Z 2M9, Canada';

	geocoder.getLocations(address['pointgrey'], addAddressToMap);
	
	$('store-locator').select('[class="section"]').each(function(s,i) {
		if (i != 0)
			s.hide();
	});
	$('tabs').select('a').each(function(anchor,i) {
		anchor.observe('click',function(e) {

			var str = this.id;
			sid = str.substring(2);
			
			geocoder.getLocations(address[sid], addAddressToMap);
			$('store-locator').select('[class="section"]').each(function(s,index) {
				s.hide();
			});
			current = i;
			$(sid).show();
			
			checkButtons();
			Event.stop(e);
		});
	});
});

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.

function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert('Sorry, we were unable to geocode that address');
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
		place.Point.coordinates[0]);
		marker = new GMarker(point);
		map.addOverlay(marker);
	
		marker_title['pointgrey'] = '<span class=\'title\'><b>ENDA B Men &amp; Women</b></span><br/>'+place.address+'<br/>604.228.1214';
		marker_title['granvilledkny'] = '<span class=\'title\'><b>Granville DKNY</b></span><br/>'+place.address+'<br/>604.733.2000';
		marker_title['oakridgedkny'] = '<span class=\'title\'><b>Oakridge DKNY</b></span><br/>'+place.address+'<br/>604.263.8889';
		marker.openInfoWindowHtml(marker_title[sid]);
	}
}

function checkButtons() {	
	$('tabs').firstDescendant().childElements().each(function(s,i) {
		if (current == i)
			s.firstDescendant().addClassName('active');
		else
			s.firstDescendant().removeClassName('active');
	});
}
