// JavaScript Document

window.addEvent('domready', function() {
	SetupCalendar(); 
	PositionCalendar();
	ClickableCalendar();
});

function ClickableCalendar() {
	var day = $$('div#calendar div.day');
	for (x=0; x<day.length; x++) {
		
		day[x].addEvent('click', function() {
			//alert(this);				
			var a = this.getChildren('a.hidden');
			window.location.href = a;
		});
	}
};


function PositionCalendar() {
	var calendarHover = $$('#calendar div.day');
	for (x=0; x<calendarHover.length; x++) {
		calendarHover[x].addEvent('mouseenter', function() {
			/*calendarHover.set('class', calendarHover.get('class').replace('-hover','') );*/
			this.set('class', this.get('class').concat('-hover'));
			var dateHover = this.getElement('div.datehover');
			if (dateHover!=null) {
				if (!Browser.Engine.trident) {
					dateHover.setStyle('margin-top', (dateHover.getSize().y-(dateHover.getSize().y*2))+26);
				} else {
					dateHover.setStyle('margin-top', (dateHover.getSize().y-(dateHover.getSize().y*2))+48);
				}
			}
			
			//used to fix ie6 events slider buttons
			if (Browser.Engine.trident4) {
				// IE6 hide select begins
				var genreselect =  $$('#bygenreform select')[0];
				var titleselect =  $$('#bytitleform select')[0];
				genreselect.setStyle('visibility', 'hidden');
				titleselect.setStyle('visibility', 'hidden');
				// IE6 hide select Ends
				if  ($$('#eventsSliderNav a#previous')!=null) {
					$$('#eventsSliderNav a#previous').setStyle('margin-left','-1000000px');
					$$('#eventsSliderNav a#next').setStyle('margin-left','-1000000px');
				}
			}
		});
		
		calendarHover[x].addEvent('mouseout', function() {
			this.set('class', this.get('class').replace('-hover','') );	
			
			//used to fix ie6 events slider buttons
			if (Browser.Engine.trident4) {
				// IE6 hide select begins
				var genreselect =  $$('#bygenreform select')[0];
				var titleselect =  $$('#bytitleform select')[0];
				genreselect.setStyle('visibility', 'visible');
				titleselect.setStyle('visibility', 'visible');
				// IE6 hide select Endsa
				if ($$('#eventsSliderNav a#previous')!=null) {
					$$('#eventsSliderNav a#previous').setStyle('margin-left','85px');
					$$('#eventsSliderNav a#next').setStyle('margin-left','75px');
				}
			}
			
			
		});

	}
};

function SetupCalendar() {	
	 $$('#previousMonthButton').addEvent('click', function(event) {
		event.stop();
		GetPreviousMonth(this.href);
	});
	
	$$('#nextMonthButton').addEvent('click', function(event) {
		event.stop();
		GetNextMonth(this.href);
	});
	
};

function GetPreviousMonth(targ) {
	targ = window.location.protocol + '//' + window.location.hostname + targ.substring(targ.indexOf('?url=')+5) + '&xml=True';
	var xml = new Request({
		url:targ,
		autoCancel: true,
		onSuccess: function(html) {
			$$('#eventsWrapper').getParent().set('html',html);
			$$('#previousMonthButton').addEvent('click', function(event) {
				event.stop();
				GetPreviousMonth(this.href);
			});
			$$('#nextMonthButton').addEvent('click', function(event) {
				event.stop();
				GetNextMonth(this.href);
			});
			PositionCalendar();
			ClickableCalendar();
		},
		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) + '&xml=True';
	var xml = new Request({
		url:targ,
		autoCancel: true,
		onSuccess: function(html) {
			$$('#eventsWrapper').getParent().set('html',html);
			$$('#previousMonthButton').addEvent('click', function(event) {
				event.stop();
				GetPreviousMonth(this.href);
			});
			$$('#nextMonthButton').addEvent('click', function(event) {
				event.stop();
				GetNextMonth(this.href);
			});
			PositionCalendar();
			ClickableCalendar();
		},
		onFailure: function(error) {
			alert('There is currently a problem with the calendar please try again later');
		}
	}).send(); //Ends xml request
};