Event.addBehavior({
  'body:onReady' : function() {
		load();
  }
});


/* functions * /

function showMarker(point, title, description, address, id, response_count){
	var marker = new GMarker(point);

	marker.title = title;
	marker.description = description;
	marker.address = address;
	marker.wall_id = id;
	marker.response_count = response_count;
	map.addOverlay(marker);

	GEvent.addListener(marker, "click", function() {
	  /* marker.openInfoWindowHtml(marker.description); * / 
		window.location = '/walls/'+marker.wall_id
	});

	return marker;
}

/* display * /
function updateList(){
	var bounds = map.getBounds();

	for(var i=0; i < all_markers.length; i++){			
		var marker = all_markers[i];
		var latlng = marker.getLatLng();

		// add element
		if(bounds.containsLatLng(latlng)){
			if(current_markers.indexOf(marker) == -1){
				current_markers.push(marker);

				if((i+1)%2 == 0){
					oddOrEven = 'even';
				}
				else{
					oddOrEven = 'odd';					
				}

				Element.insert($('list_of_walls'), 
					'<tr id="wall-'+ marker.wall_id +'">'+ /* class='+oddOrEven+' * / 
						'<td></td>' +
						'<td><h3><a href="/walls/'+marker.wall_id+'">'+marker.title+'</a></h3> <small>' + marker.address + '</small></td>' +
						'<td class="response_counter"><small>'+marker.response_count+' responses</small></td>' +
					'</tr>');
			}
		}
		// remove element
		else{
			if(current_markers.indexOf(marker) != -1){
				current_markers = current_markers.without(marker);
				Element.remove($('wall-'+marker.wall_id));
			}					
		}
	}
	var j = 1;
	$('list_of_walls').childElements().each(function(element){
		j++;
	});

}
function updateCounter(){
	$('wall_counter').update(current_markers.length);
	$('wall_counter_pluralized').update("<%= pluralize('"+current_markers.length+"', 'wall').gsub('"+current_markers.length+" ', '') %>");
}
function updateDisplay(){
	updateList();
	updateCounter();
}
function resetDisplay(){
	Element.update($('wall_counter'));
	Element.update($('list_of_walls'));
}

/* add new markers * /

function createMarker(point){
	var marker = new GMarker(point, {draggable:true});
	var infoWindowContent = "<h3>Your wall</h3> will be placed here.<br /><br />" +
			"<small>Drag the marker around if you want to replace it.</small><br />" +
			"<small>Once you're done, <a href=\"javascript:void(0)\"onclick=\"new Effect.ScrollTo('form_top');\">scroll down.</a></small>";

	map.addOverlay(marker);
	marker.openInfoWindowHtml(infoWindowContent);

	GEvent.addListener(marker,"dragstart", function() {     
			marker.closeInfoWindow();
	});
	GEvent.addListener(marker,"dragend", function() {     
			marker.openInfoWindowHtml(infoWindowContent);
	});
	GEvent.addListener(marker,"click", function() {     
			marker.openInfoWindowHtml(infoWindowContent);
	});

	// set form data
	$('wall_lat').value = point.lat();
	$('wall_lng').value = point.lng();
	reversegeocoder.reverseGeocode(point);
}

/* end functions */
