/* Author: 

*/

$(function() {
	$('a[rel="more"]').attr('href', '#').click(function() {
		$(this).parent().siblings('.more').fadeIn();
		$(this).fadeOut();
		return false;
	});
	$('a[rel="email"]').attr('href', '#').click(function(idx, bio) {
		location.hash = '#contact';
		idx = $('.bio').index($(this).closest('.bio'));
		$('select option').eq(idx + 1).prop('selected', true);
		$('select').change();
		return false;
	});
	$('form').submit(function() {
		$(this).ajaxSubmit({
			success: function(data) {
				if(data.message)
					$('.error').text(data.message);
			},
			dataType: 'json'
		});
		return false;
	});
	$('input, textarea').placeholder();
	if($('#map').length) {
		$('#map').GoogleMapV3();
		$.getJSON('/directory.json', function(data) {
			for(var x = 0, l = data.length; x < l; x++)
				$('#map').GoogleMapV3('addMarker', {
					lat: data[x][6],
					lng: data[x][7],
					title: data[x][0],
					clickable: true,
					content: '<p><strong>' + data[x][0] + '</strong><br>' + data[x][1].join('<br>') + '<br>' + data[x][2] + ', ' + data[x][3] + ' ' + data[x][4] + (data[x][5] ? '<br>Phone: ' + data[x][5] : '') + '</p>'
				});
		});
		$('input').change(function() {
			var geocoder = new google.maps.Geocoder();
			geocoder.geocode({ address: $(this).val() }, function(results, status) {
		    if(status == google.maps.GeocoderStatus.OK) {
		      $('#map').GoogleMapV3('setCenter', results[0].geometry.location);
		      $('#map').GoogleMapV3('fitBounds', results[0].geometry.viewport);
		    }
			});
		});
	}
	if($('#calendar').length) {
		$('#calendar').fullCalendar({
			height: 540,
			header: {
				left: 'title',
				right: 'prev,next',
			},
			events: 'http://www.google.com/calendar/feeds/vc9qr3jj1l3luou31drn812bh0%40group.calendar.google.com/public/basic',
			eventClick: function(event) {
				window.open(event.url, 'mwcs', 'width=700,height=600');
				return false;
			}
		});
	}
	$('select').change(function(idx, $bio) {
		idx = $('select option').index($('select option:selected'));
		if(!idx)
			return;
		$bio = $('.bio').eq(idx - 1);
		$('.name').text($bio.data('name'));
		$('.phone').text($bio.data('phone'));
		$('.email').attr('href', 'mailto:' + $bio.data('email')).text($bio.data('email'));
	});
});

