// JavaScript Document

window.addEvent('domready', function() {
	SetupCalendar(); 
	PositionCalendar();
});

function PositionCalendar() {
	var calendarHover = $$('#calendar div.day');
	for (x=0; x<calendarHover.length; x++) {
		calendarHover[x].addEvent('mouseenter', function() {
			if (this.get('class') =='day') {
				this.set('class', 'day-hover');
			} else if (this.get('class') =='day has-events') {
				this.set('class', 'day has-events-hover');
			}
		});
		
		calendarHover[x].addEvent('mouseout', function() {
			this.set('class', this.get('class').replace('-hover','') );	
		});
	}
};

function SetupCalendar() {	
	 $$('#previousMonthButton').addEvent('click', function(event) {
		event.stop();
		GetPreviousMonth(this.href);
	});
	
	$$('#nextMonthButton').addEvent('click', function(event) {
		event.stop();
		GetNextMonth(this.href);
	});
	
	
	var shroud = new Element('div', {
		'id' : 'calendarShroud',
		'styles' : {
			display : 'block',
			opacity : 0
		}
	});
	if (Browser.Engine.trident4) {
		var windowheight = window.getSize().y;
		shroud.setStyle('height', windowheight + 'px');
	}
	shroud.inject(document.body);
	var wrapper = new Element('div', { 
		'id' : 'calendarClickWrapper',
		'styles' : {
			display : 'block',
			opacity : 0
		}
	});
	wrapper.inject(document.body);
	
	shroud.addEvent('click', function() {
		this.setStyle('opacity', 0);
		wrapper.empty();
		wrapper.setStyle('opacity', 0);
	});
	//var calendarUl = null;
	var active = $$('div#calendar div.has-events');
	for (x=0; x<active.length; x++) {
		active[x].addEvent('click', function() {
			var ev = this;
			wrapper.empty();
			var events = ev.getElements('div.datehover div.event');
			var date = ev.getElements('div.datehover p.date')[0].clone();
			date.set('class', 'date');
			date.inject(wrapper);
			var ul = new Element('ul');
			for (i=0; i<events.length; i++) {
				var li = new Element('li');
				/*var imgsrc = events[i].getChildren('img')[0].src;
				var img = new Element('img', { 'src' : imgsrc });
				img.inject(li);*/
				
				var div = new Element('div');
				var title = events[i].getChildren('p')[0].clone();
				title.set('class', 'title');
				var times = events[i].getChildren('p')[1].clone();
				times.set('class', 'times');
				var info = events[i].getChildren('p')[2].clone();
				info.set('class', 'info');
				var more = events[i].getChildren('a')[0].clone();
				more.set('href', events[i].getChildren('a')[0].get('href'));
				title.inject(div);
				times.inject(div);
				info.inject(div);
				more.inject(div);
				
				if (events[i].getChildren('a').length>1) {
					var book = events[i].getChildren('a')[1].clone();
					book.set('href', events[i].getChildren('a')[1].get('href'));
					book.inject(div);
				}
				
				div.inject(li);
				li.inject(ul);
			}
			ul.inject(wrapper);
			var h = wrapper.getSize().y;
			marginTop = (h/2) - h;
			wrapper.setStyle('margin-top', marginTop +'px');
			
			shroud.setStyle('opacity',0);
			wrapper.setStyle('opacity',0);
			var shroudFx = new Fx.Morph(shroud, {duration:250, transition:Fx.Transitions.Quad.easeInOut, link:'ignore'});
			shroudFx.start({ 'opacity': '0.8' });

			shroudFx.addEvent('complete', function() {
				var wrapperFx = new Fx.Morph(wrapper, {duration:150, transition:Fx.Transitions.Quad.easeInOut, link:'ignore'});
				//wrapperFx.addEvent('complete', function() {
					//alert(calendarUl);
				//});
				wrapperFx.start({ opacity : 1 });
			});
		});
	}
};

function GetPreviousMonth(targ) {
	targ = window.location.protocol + '//' + window.location.hostname + targ.substring(targ.indexOf('?url=')+5).replace('calendar-standalone.asp&','calendar-standalone.asp?') + '&xml=True';
	var xml = new Request({
		url:targ,
		autoCancel: true,
		onSuccess: function(html) {
			$$('#calendarOuterWrapper').set('html',html);
			SetupCalendar();
			PositionCalendar();
		},
		onFailure: function(error) {
			alert('There is currently a problem with the calendar please try again later');
		}
	}).send(); //Ends xml request
};

function GetNextMonth(targ) {
	targ = window.location.protocol + '//' + window.location.hostname + targ.substring(targ.indexOf('?url=')+5).replace('calendar-standalone.asp&','calendar-standalone.asp?') + '&xml=True';
	var xml = new Request({
		url:targ,
		autoCancel: true,
		onSuccess: function(html) {
			$$('#calendarOuterWrapper').set('html',html);
			SetupCalendar();
			PositionCalendar();
		},
		onFailure: function(error) {
			alert('There is currently a problem with the calendar please try again later');
		}
	}).send(); //Ends xml request
};