//global var
Event.observe(window,'load',pageLoaded);
function pageLoaded(evt){
	Event.observe('farm tour','click',imageControlClick);
	Event.observe('family album','click',imageControlClick);
	Event.observe('calendar','click',getThisCalendar);
}

Ajax.Responders.register({onCreate: function(){
		$('contentLayer').update("<img id='working' src='/images/logos/working.gif' />");
}});	

//imageset functions

function imageControlClick(evt)
{
	Event.stop(evt);
	element = Event.element(evt);
	id = ((element.getAttribute('href')).split('/'))[2];
	setActive(id);
	var getCal = new Ajax.Request(
		element.getAttribute('href'),
		{
			method		: 'POST',
			parameters	: {ajax : 1},
			onComplete	: function(r){
							$('contentLayer').update(r.responseText);
							//alert(r.responseText);
							}
		}
	);
	
}

//calendar functions
function getThisCalendar(evt)
{
	Event.stop(evt);
	setActive('calendar');
	var getCal = new Ajax.Request(
		'/calendar',
		{
			method		: 'POST',
			parameters	: {ajax : 1},
			onComplete	: function(r){
							$('contentLayer').update(r.responseText);
							//alert(r.responseText);
							}
		}
	);
}

function calendarBtnClick(evt)
{
	Event.stop(evt);
	path = ((Event.element(evt)).getAttribute('href')).split('/');
	month = path[path.length-1];
	year = path[path.length-2];
	var getCal = new Ajax.Request(
		'/calendar/'+year+'/'+month,
		{
			method		: 'POST',
			parameters	: {ajax : 1},
			onComplete	: function(r){
							$('contentLayer').update(r.responseText);
							//alert(r.responseText);
							}
		}
	);
}

function editEventClick(evt)
{
	Event.stop(evt);
	path 	= ((Event.element(evt)).getAttribute('href')).split('/');
	day 	= path[path.length-1];
	month 	= path[path.length-2];
	year	= path[path.length-3];
	var getEditCal = new Ajax.Request(
		'/calendar/edit',
		{
			method		: 'GET',
			parameters	: 
				{ 	ajax 	: 1,
					day		: day,
					month	: month,
					year 	: year							
				},
			onComplete	: function(r)
				{
					$('contentLayer').update(r.responseText);
					//alert(r.responseText);
				}
		}
	);
}

function submitEditClick(evt)
{
	Event.stop(evt);
	path 	= ((Event.element(evt).up()).getAttribute('action')).split('/');
	day 	= path[path.length-1];
	month 	= path[path.length-2];
	year	= path[path.length-3];
	
	name = (Event.element(evt)).siblings()[1].value;
	if (!name){alert('please enter a name for the event'); exit;}
	
	description = (Event.element(evt)).siblings()[3].value;
	if(!description){alert('please enter a description for the event'); exit;}
	var doEditCal = new Ajax.Request(
		'/calendar/edit',
		{
			method		: 'POST',
			parameters	: 
				{ 	ajax 		: 1,
					day			: day,
					month		: month,
					year 		: year,
					name		: name,
					description	: description							
				},
			onComplete	: function(r)
				{
					$('contentLayer').update(r.responseText);
					//alert(r.responseText);
				}
		}
	);
}

function viewEvent(evt)
{
	Event.stop(evt);
	id = ((Event.element(evt)).getAttribute('id')).substr(5);
	var viewEvt = new Ajax.Request(
		'/event/'+id,
		{
			method		: 'POST',
			parameters	:{ 	ajax 		: 1	},
			onComplete	: function(r)
				{
					$('contentLayer').update(r.responseText);
					//alert(r.responseText);
				}
		}
	);
	
}

function deleteEvent(evt)
{
	Event.stop(evt);
	path 	= ((Event.element(evt).up()).getAttribute('action')).split('/');
	day 	= path[path.length-1];
	month 	= path[path.length-2];
	year	= path[path.length-3];
	
	del = confirm("Are you sure you want to delete the event for: "+month+'/'+day+'/'+year+'?');
	
	if(del){
		var doDeleteCal = new Ajax.Request(
			'/calendar/delete',
			{
				method		: 'POST',
				parameters	: 
					{ 	ajax 		: 1,
						day			: day,
						month		: month,
						year 		: year		
					},
				onComplete	: function(r)
					{
						$('contentLayer').update(r.responseText);
						//alert(r.responseText);
					}
			}
		);
	}
}

function setActive(btn){
	buttons = $('featureLayer').immediateDescendants();
	buttons.each(function(elmt){
						elmt.className = 'latent';				
						});
	$(btn).className = 'active';
}
