
$(function() {

	$('.faqs .item .title a').click(function() {
		$(this).parents('.item').find('.answer').slideToggle();
		$(this).parents('.item').siblings().find('.answer').slideUp();
		return false;
	});
	
	$('.map').each(function() {
		var address = parseAddress( $(this).siblings('.vcard').get(0) );
		$(this).attr( 'address', address );

		if (GBrowserIsCompatible()) {
			var map = new GMap2( this );
			map.addControl( new GSmallMapControl() );
			map.addControl( new GMapTypeControl() );
			$(this).attr( 'map', map );
		
			var address = parseAddress( $(this).parents('.item').find('.vcard').get(0) );
			$(this).attr( 'address', address );
			showAddress( address, map );
		}
		
	} );

	
	
	$('.directions').submit( function() {
		var address = parseAddress( $(this).parents('.item').find('.vcard').get(0) );
		var zip = $(this).find('.zip').attr( 'value' );
		
		showDirections(
			zip + ' to ' + address
		);
		
		return false;
	} );


	
	$('.email_image').click( function() {
		var address = $(this).parents('.contact').find('.email').text();
		url = this.href + '?to_address=' + address + '&redirect=' + window.location.href;
									  
		$('<div></div>').load( url ).dialog( {
			width: '330px', 
			height: '470px', 
			modal: true,
			overlay: { background: 'black', opacity: '.8', filter: 'alpha(opacity=80)' },
			draggable: false,
			title: 'Contact Us'
		} );
		
		return false;
	} );
	
	

});



function parseAddress( address ) {
	return $(address).find('.street-address').text() + ', '
		+ $(address).find('.locality').text() + ', '
		+ $(address).find('.region').text() + ' '
		+ $(address).find('.country-name').text() + ' '
		//+ $(address).find('.postal-code').text()
		;
}




var geocoder = '';
function showAddress(address, map) {
	if ( '' == geocoder ) {
		geocoder = new GClientGeocoder();
	}
	
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.checkResize();
				map.setCenter(point, 13);
				map.addOverlay( new GMarker(point) );
				//marker.openInfoWindowHtml(address);
			}
		}
	);
	
}


function showDirections( query ) {
	var directions_map = $( '<div style="position: relative; width: 300px; height: 300px; float: left; "></div>' );
	var directions_panel = $( '<div style="position: relative; width: 400px; float: left; margin-right: 8px;"></div>' );
	
	$('<div id="directions_div" ></div>').append( directions_panel ).append( directions_map ).dialog( {
		width: '748px', 
		height: '', 
		modal: true,
		overlay: { background: 'black', opacity: '.8', filter: 'alpha(opacity=80)' },
		draggable: false,
		title: 'Directions <a onclick="window.print();" href="javascript:;" style="margin-left:40px;font-weight:normal;" class="color1">print</a>'
	} );
	
	map = new GMap2( directions_map.get(0) );
	map.addControl( new GSmallMapControl() );
	map.addControl( new GMapTypeControl() );
	directions = new GDirections(map, directions_panel.get(0) );
	directions.load( query );
	
}