document.observe("dom:loaded", function() {

	$$('a.ajax_prayerwall_cell').each( function(currentElement){
		currentElement.observe('click', function(currentEvent){
	
			activatePrayerWallBox(currentElement);
	
			Event.stop(currentEvent);
	
		});
	});

	activatePrayerWallBox = function(currentBox){

		myTd = currentBox.up(0);
		myDiv = currentBox.next(0);
		
		myTdClass = myTd.getAttribute('class');
		if ( !myTdClass.include('loading') ) {
			myTd.writeAttribute('class', myTdClass+'-loading');
		}

		clearAllBoxes();

		myDiv.writeAttribute('class', 'prayerwall_ajax_box_active');

		new Ajax.Updater(myDiv, currentBox.readAttribute('href'), { 
			method: 'get',
			onComplete: function() {

				myTd.writeAttribute('class', myTdClass);

				myDiv.select('a.closeButton').each( function(currentCloseButton){
					currentCloseButton.observe('click', function(currentCloseEvent){

						currentCloseButton.up(2).update();
						
						// Clear the time slot from the session
						new Ajax.Request('/data/birmingham/scripts/clear_session_ts.php');
						
						Event.stop(currentCloseEvent);
					});
				});

				makeModalLinks( myDiv.select('a.modal_link') ); 
			}
		});

	}
	
	
	// Clear all boxes before opening a new one - this keeps the boxes clear and only one open at a time
	clearAllBoxes = function(){
		$$('div.prayerwall_ajax_box_active').each( function(currentDiv){
			currentDiv.writeAttribute('class', 'prayerwall_ajax_box');
			currentDiv.update('&nbsp;');
		});
	}


	// Check the global 'activeTimeSlot' variable - if exists then activate the current time slot ajax
	if ( activeTimeSlot != '' ){

		activatePrayerWallBox( $('ts' + activeTimeSlot) );
		window.location.replace('#ts' + activeTimeSlot);
		
	}

}); 
