/**
 * GoogleMap Class
 *
 * This class updates some of the properties of the elements in
 * the Map section of the events page so that, instead of having
 * to refresh the page, an Ajax request is made and the page
 * updated with it's response.
 */
var GoogleMap;

(GoogleMap = Class.extend('AjaxReplace')).prototype =
{
	/**
	 * Parse the map element to add handlers to the toggle link
	 */
	parse : function()
	{
		this.addClickHandler('mapToggleClick', 'span.toggle a');
	},
	
	mapToggleClick : function(e)
	{
		this.load(e.getAttribute('href'),
		{
			method: 'post',
			parameters: 'action=map'
		});
		
		this.showMapLoading();
		return false;
	},
	
	showMapLoading : function()
	{
		var pageList = document.getElementsBySelector('h3 span.toggle', this.element);
		if (!(pageList = pageList[0])) return;
		with (pageList) {
			innerHTML = '';
			with (appendChild(document.createElement('span'))) {
				style.color = '#666';
				appendChild(document.createTextNode('Loading '));
			}
			appendChild(document.createElement('img')).src = 'themes/sas/images/load.gif';
		}
	}
};

/**
 * PhotoGallery Class
 *
 * This class updates some of the properties of the elements in
 * the photo gallery section of the events page so that, instead
 * of the user having to reload the entire page to view the next
 * page of the gallery images, an Ajax request is made and the
 * page updated with it's response. 
 */
var PhotoGallery;

(PhotoGallery = Class.extend('AjaxReplace')).prototype =
{
	/**
	 * Parse the photogallery element to add handlers to all of the page and image links
	 */
	parse : function()
	{
		this.addClickHandler('pageClick', 'h3 a');
		this.addClickHandler('imageClick', 'ul.photolist a');
	},
	
	imageClick : function(e)
	{
		e.setAttribute('rel', 'lightbox');
		myLightbox.start(e);
		return false;
	},
	
	pageClick : function(e)
	{
		this.load(e.getAttribute('href'),
		{
				method: 'post',
				parameters: 'action=gallery'
		});
		
		this.showPageLoading();
		return false;
	},
	
	showPageLoading : function()
	{
		var pageList = document.getElementsBySelector('h3 span.pages', this.element);
		if (!(pageList = pageList[0])) return;
		with (pageList) {
			innerHTML = '';
			appendChild(document.createElement('img')).src = 'themes/sas/images/load.gif';
			with (appendChild(document.createElement('span'))) {
				style.color = '#666';
				appendChild(document.createTextNode(' Loading'));
			}
		}
	}
};

/*
 * Set up the script on the events' page.
 */
 
var googleMap = null;
var photoGallery = null;
 
appendLoader(function ()
{
	var e;
	if ((e = document.getElementById('map')) && (e = e.getElementsByTagName('div')[0])) {
		googleMap = new GoogleMap(e);
	}
	if ((e = document.getElementById('gallery')) && (e = e.getElementsByTagName('div')[0])) {
		photoGallery = new PhotoGallery(e);
	}
});