jQuery(function($) {
	var offices = [{
			latlng: new google.maps.LatLng(51.071161,-114.005985),
			title: 'Calgary - Head Office',
			info: '2325 20th Avenue NE<br />Calgary, AB T2E 8S4<br />Phone: (403) 250-8810<br />Fax: (403) 250-8815<br /><a href="mailto:info@sageenergy.ca">info@sageenergy.ca</a>'
		},
		{
			latlng: new google.maps.LatLng(55.19543,-118.760254),
			title: 'Grande Prairie',
			info: '15214C 89th ST<br />Grande Prairie, AB T8V 0V7<br />Phone: (780) 830-1002'
		},
		{
			latlng: new google.maps.LatLng(52.304128,-113.847360),
			title: 'Red Deer',
			info: '520, 7700 - 76th Street Close<br />Red Deer, AB T4P 4G6<br />Phone: (403) 343-7845'
		},
		{
			latlng: new google.maps.LatLng(56.233464,-120.841434),
			title: 'Fort St John',
			info: '9626 Sikanni Road<br />Fort St John, B.C. V1J 4V1<br />Phone: (250) 785-8509<br />Fax: (250) 785-8519'
		}],
		map = new google.maps.Map($("#map")[0], {
			zoom: 14,
			center: offices[0].latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}),
		markers = [],
		HTML_INFO = '';
	
	$.each(offices, function(i, n) {
		var marker = new google.maps.Marker({
			position: n.latlng,
			map: map,
			title: n.title
		});
		google.maps.event.addListener(marker, 'click', function() {
			map.setCenter(marker.position);
			map.setZoom(16);
		});
		markers.push(marker);
		HTML_INFO += '<li><h3>'+ n.title +'</h3>'+ n.info +'<br /><a href="#" rel="lightbox" class="office-'+ i +'">See Map &raquo;</a></li>';
	});
	
	// add address details to page
	$('#sidebar2 .contact-details').append(HTML_INFO);
	
	$("[rel='lightbox']").live('click', function() {
		if ($('#overlay').hasClass('hide-overlay')) {
			$('#overlay').removeClass('hide-overlay').hide();
		}
		$('#overlay:not(:visible)').fadeIn();
		var classes = $(this)[0].className.split(' ');
		$.each(classes, function(i, n) {
			var index = null;
			switch (n) {
				case 'office-0': index = 0; break;
				case 'office-1': index = 1; break;
				case 'office-2': index = 2; break;
				case 'office-3': index = 3; break;
			}
			if (index !== null) {
				google.maps.event.trigger(markers[index],'click');
				$('.lightbox-info').html('<h2>'+ offices[index].title +'</h2><p>'+ offices[index].info +'</p>');
				var HTML_OTHERMAPS = '';
				$.each(offices, function(j, m) {
					if (j !== index) {
						HTML_OTHERMAPS += '<li><a href="#" rel="lightbox" class="office-'+ j +'">&raquo; '+ m.title +'</a></li>';
					}
				});
				$('.lightbox-nav ul').html(HTML_OTHERMAPS);
			}
		});			
		return false;
	});

	// lightbox
	$('#overlay').click(function(event) {
		var $target = $(event.target);
		if ($target.hasClass('overlay-blocker') || $target.hasClass('lightbox-close')) {
			$(this).hide();
			return false;
		}
	});	
	$('.overlay-blocker').bgiframe();
});