Event.observe(window, 'load', function() {
	if($('large-map') != null) {
		loadLargeMap();
		
		Event.observe('toggle-1', 'click', function() {
			moveToCounty(1);
		}, false);
		
		Event.observe('toggle-2', 'click', function() {
			moveToCounty(2);
		}, false);
		
		Event.observe('toggle-3', 'click', function() {
			moveToCounty(3);
		}, false);
		
		Event.observe('toggle-4', 'click', function() {
			moveToCounty(4);
		}, false);
		
		Event.observe('toggle-5', 'click', function() {
			moveToCounty(5);
		}, false);
		
		Event.observe('toggle-6', 'click', function() {
			moveToCounty(6);
		}, false);
	}
}, false);

var topAccordion = null;
Event.observe(window, 'load', loadAccordions, false);
function loadAccordions() {
	try
	{
		if (!$('vertical_container'))
		{
			throw("vertical_container doesn't exist!");
		}
	}
	catch(err)
	{
		return false;
	}
	
	topAccordion = new accordion('vertical_container', {
		classNames : {
			toggle : 'accordion_toggle',
			toggleActive : 'accordion_toggle_active',
			content : 'accordion_content'
		},
		direction : 'vertical'
	});
	
	if(active_county != "")
	{
		county_int = countyToInt(active_county);
		topAccordion.activate($$('#vertical_container .accordion_toggle')[county_int]);
	}
}

function countyToInt(county)
{
	switch(county)
	{
		case "bucks": return 0;
		case "chester": return 1;
		case "montgomery": return 2;
		case "philadelphia": return 3;
		case "burlington": return 4;
		case "camden": return 5;
		default: return 0;
	}
}

var active_apartment = "";
								
function show_apartment(id)
{
	if( active_apartment != id )
	{
		if( active_apartment != "" )
		{
			$('apartment-type-nav-'+active_apartment).removeClassName('active');
			$('apartment-type-'+active_apartment).hide();
		}
		$('apartment-type-nav-'+id).addClassName('active');
		$('apartment-type-'+id).show();
		active_apartment = id;
	}
}

var active_county = "philadelphia";
								
function show_county(county)
{
	if( active_county != county )
	{
		if( active_county != "" )
		{
			$(active_county+'-county-link').removeClassName('active');
			$(active_county+'-buildings').hide();
			$('main-right-column-map-'+active_county).hide();
		}
		$(county+'-county-link').addClassName('active');
		$(county+'-buildings').show();
		$('main-right-column-map-'+county).show();
		active_county = county;
	}
}

var active_tab = "";
								
function show_tab(tab)
{
	if( active_tab != tab )
	{
		if( active_tab != "" )
		{
			$('right-column-nav-'+active_tab).removeClassName('active');
			$('search-by-'+active_tab).hide();
		}
		$('right-column-nav-'+tab).addClassName('active');
		$('search-by-'+tab).show();
		active_tab = tab;
	}
}

var active_column = "";
								
function show_column(column)
{
	if( active_column != column )
	{
		if( active_column != "" )
		{
			if( active_column == "map" )
				$('header-bottom-right-nav-'+active_column).removeClassName('active');
			$('main-right-column-'+active_column+'-cont').hide();
		}
		$('header-bottom-right-nav-'+column).addClassName('active');
		$('main-right-column-'+column+'-cont').show();
		
		show_tab('map');
		show_county('bucks');

		active_column = column;
	}
}

var current_div = 'features-cont';

function show_form(active_div, id)
{
	// Clicked the same button
	if( $(id).visible() == true )
	{
		$(id).hide();
		current_div = 'features-cont';
		$(current_div).show();
		$(active_div).removeClassName('active');
	}
	else // clciked an unselected button
	{
		$(id).show();
		$(current_div).hide();
		current_div = id;
		
		$(active_div).addClassName('active');
		
		if($(active_div).id == 'main-header-nav-1')
		{
			if($('main-header-nav-2').hasClassName('active') == true)
				$('main-header-nav-2').removeClassName('active');
		}
		else if($(active_div).id == 'main-header-nav-2')
		{
			if($('main-header-nav-1').hasClassName('active') == true)
				$('main-header-nav-1').removeClassName('active');
		}
	}
}

function close_map()
{
	Effect.BlindUp('google-map-cont');
}

var geocoder;
var map;
var dirObj;

function requestdirections2(mapobj,toaddr,fromaddr)
{
	if( typeof(dirObj) != 'undefined')
	{
		dirObj.clear();
	}
	mapobj.clearOverlays();
	$('directions').innerHTML = "";
	dirObj = new GDirections(mapobj,document.getElementById("directions"));
	dirObj.load('from:'+fromaddr+' to: '+toaddr);
	return false;
}

function showAddress(address) {
	map = new GMap2(document.getElementById("map"));
	geocoder = new GClientGeocoder();
	
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.addControl(new GSmallMapControl());
				map.setCenter(point, 14);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(building_name+"<br />"+address+"<br />"+building_phone);
			}
		}
	);
}

function checkMap()
{
	if( $('google-map-cont').visible() == false )
	{
		Effect.SlideDown('google-map-cont', { afterFinish: loadMap });
	}
}

function loadMap()
{
	if(GBrowserIsCompatible())
	{
		$('directions').innerHTML = "";
		showAddress(building_address);
	}
}

function loadLargeMap()
{
	if(GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("large-map"));
		map.addControl(new GLargeMapControl());
		point = new GLatLng(40.1071451, -75.2361519);
		map.setCenter(point, 10);
		
		//GEvent.addListener(map, "moveend", function() {
        //  var center = map.getCenter();
        //  document.getElementById("message").innerHTML = center.toString();
        //});

		
		//Build Custom Icon
		var icon = new GIcon();
		//icon.shadow = "../images/gmaps/shadow-map-icons.png";
		icon.iconSize = new GSize(32.0, 38.0);
		//icon.shadowSize = new GSize(52.0, 38.0);
		icon.iconAnchor = new GPoint(15.0, 37.0);
		icon.infoWindowAnchor = new GPoint(25.0, 5.0);

		loadMarkers(icon);
	}
}

function loadMarkers(icon)
{
	properties.each(function(property)
	{
		if(property != undefined)
		{
			var numberIcon = new GIcon(icon);
			numberIcon.image = "../images/gmaps/map-icons-" + property.mapOrder + ".png";

			point = new GLatLng(property.latitude, property.longitude, true);
			marker = new GMarker(point, {icon: numberIcon, zIndexProcess: function() { return 19 - property.mapOrder; }});
			property.setMarker(marker);
			
			// Add a click event to each marker to display the infowindow
			GEvent.addListener(property.marker, "click", function() {
				moveToProperty(property.id);
				topAccordion.activate($$('#vertical_container .accordion_toggle')[countyToInt(property.county)]); // to open the accordion when a marker is clicked
			});
			map.addOverlay(property.marker);
		}
	});
}

function moveToProperty(id)
{
	newlat = properties[id].marker.getLatLng().lat() + 0.015;
	map.setCenter(new GLatLng(newlat, properties[id].marker.getLatLng().lng()), 13);
	properties[id].displayInfo();
}

function moveToCounty(id)
{
	switch(id)
	{
		case 1:
			map.setCenter(new GLatLng(40.2627606, -75.0263214), 11);
			break;
		
		case 2:
			map.setCenter(new GLatLng(39.980014, -75.5766677), 12);
			break;
		
		case 3:
			map.setCenter(new GLatLng(40.0402321, -75.3253555), 12);
			break;
		
		case 4:
			map.setCenter(new GLatLng(39.9913254, -75.2340316), 12);
			break;
		
		case 5:
			map.setCenter(new GLatLng(39.9086828, -74.9689865), 12);
			break;
		
		case 6:
			map.setCenter(new GLatLng(39.9063126, -75.0603103), 12);
			break;
		
		default:
			map.setCenter(new GLatLng(40.1071451, -75.2361519), 10);
			break;
	}
}
