var eventCalendarId = '';
var popMouseOver = 0;
var clearCalendarTimer;
var pendingCalendarTimer;
var debugItem = document.getElementById('debug');
var popOpen = 0;

function resetCalandar() {

	eventCalendarId = '';
	popMouseOver = 0;
	clearCalendarTimer;
	pendingCalendarTimer;
	debugItem = document.getElementById('debug');
	popOpen = 0;

}

function coachPopOpen() {
	var action = document.getElementById('calendarAction').value;
	return (popOpen == 1 && (action == 'cc' || action == 'vc'))
}

function setMouseOver(){
	popMouseOver = 1;
}

function setCoachPop(){
	popOpen = 1;
}

function newCalendarTraining(theDate, theDayNumber){
	
	if (! coachPopOpen()) {
		toAjax('training',
			'a=pop_' + theDayNumber + '_new&' + 
			'o=pop_' + theDayNumber + '_new_content&' + 
			'r=new&' + 
			'd=' + theDate)
	}
}

function newVanReservation(theDate, theDayNumber){
	
	if (! coachPopOpen()) {
		toAjax('van',
			'a=pop_' + theDayNumber + '_new&' + 
			'o=pop_' + theDayNumber + '_new_content&' + 
			'r=new&' + 
			'd=' + theDate)
	}
}

function pendingCalendarEvent(hOffset, currentItem) {

	var command = "showCalendarEvent(" + hOffset + ",'" + currentItem + "')";
	pendingCalendarTimer = setTimeout(command, 500);
//debugItem.innerHTML = command;
}

function testCalendarEvent(hOffset, currentItem) {

	clearTimeout(clearCalendarTimer);

	if (eventCalendarId != currentItem) {
		pendingCalendarEvent(hOffset, currentItem)
	} else {
		clearTimeout(pendingCalendarTimer);
		showCalendarEvent(hOffset, currentItem)
	}
	
}

function eventCalendarMouseover(hOffset, currentItem) {
	testCalendarEvent(hOffset, currentItem)
}

function eventCalendarCoach(hOffset, currentItem) {
	if (coachPopOpen()) {
		if (currentItem == eventCalendarId) {
			hideCalendarPop();
			popOpen = 0;
		}
	} else {
		showCalendarEvent(hOffset, currentItem)
		popOpen = 1;
	}
}

function eventCalendarMouseout() {
	clearCalendarTimer = setTimeout('hideCalendarPop()', 1000);
}

function popCalendarMouseover(hOffset, currentItem) {
	clearTimeout(clearCalendarTimer);
	popMouseOver = 1;
}

function popCalendarMouseout() {
	hideCalendarPop();
}

function eventCalendarClick(url) {
	document.location = url
}

function showCalendarEvent(hOffset, eventItemId) {

	hideCalendarPop();

	if (eventItemId != '') {

		var theEvent = document.getElementById(eventItemId);
	
		with (theEvent.style) {
			display = 'block';

			left = '1px';
			var oH = clientWidth() + scrollWidth() - theEvent.offsetWidth - positionX(theEvent) - theEvent.offsetParent.offsetWidth
			if (oH < 0) {
				left = (- theEvent.offsetWidth) + 'px';
			} else {
				left = theEvent.offsetParent.offsetWidth + 'px';
			}
		
			var oV = clientHeight() + scrollHeight() - theEvent.offsetHeight - positionY(theEvent)
			if (oV < 0) {
				top = (oV) + 'px';
			}

			zIndex = 1;
		}
	}
	
	eventCalendarId = eventItemId
}
	
function hideCalendarPop() {

	if (eventCalendarId != '') {
		var theEvent = document.getElementById(eventCalendarId);
		if (theEvent != null) {
			with (theEvent.style) {
				display = 'none';
				zIndex = 0;
				left = 0;
				top = 0;
			}
		}
	}
	var action = document.getElementById('calendarAction').value;
	if (action == 'cc' || action == 'vc') {
		popOpen = 0;
	} else {
		popMouseOver = 0;
	}
}

function closeCalendarMouseover(theId) {

	var theEvent = document.getElementById(theId);
	
	with (theEvent.style) {
		cursor = 'pointer';
		backgroundColor = '#ff8080';
	}
	
}

function closeCalendarMouseout(theId) {

	var theEvent = document.getElementById(theId);
	
	with (theEvent.style) {
		cursor = 'default';
		backgroundColor = '';
	}
	
}

function scrollWeeksCalendar(weeks) {

	if (! coachPopOpen()) {
		document.getElementById('weeksToScroll').value = weeks;
		displayCalendar();
	}

}

function gotoTodayCalendar(the_date) {

	if (! coachPopOpen()) {
		document.getElementById('gotoDate').value = the_date;
		displayCalendar();
	}

}

function toggleCalendarList() {

	var a = document.getElementById('calendarAction')

	if (coachPopOpen())	{
		return false
	}

	if (a.value == 'l') {
		a.value = 'c'
	} else if (a.value == 'cl') {
		a.value = 'cc'
	} else if (a.value == 'vl') {
		a.value = 'vc'
	} else if (a.value == 'c') {
		a.value = 'l'
	} else if (a.value == 'vc') {
		a.value = 'vl'
	} else {
		a.value = 'cl'
	}
	
	var p = gatherParameters(true, false);
	popMouseOver = 0;
	toAjax('calendar', p)
	
}

function clearCalendarFilter() {

	var p = gatherParameters(false, true);
	popMouseOver = 0;
	toAjax('calendar', p)

}

function displayCalendar() {

//alertSize();

	var p = gatherParameters(true, true);
	popMouseOver = 0;
	toAjax('calendar', p)
}

function gotoFirstDate() {

	if (! coachPopOpen()) {
		document.getElementById('gotoDate').value = document.getElementById('firstDate').value;
		displayCalendar();
	}

}

function gatherParameters(filter, yearDate) {

	var s = '';
	var ct = document.getElementById('calendarType').value;
	var r = new RegExp('race');
	var t = new RegExp('training');
	var a = document.getElementById('calendarAction').value;
	var i
	var rg

	if (false && filter && (r.test(ct) || t.test(ct))) {
// not filtering on program at this time	
		s = 'd='
		var d = document.getElementById('disciplines')
		for (i=0;i< d.options.length;i=i+1) {
			if (d.options[i].selected) {
				s = s + d.options[i].value + ','
			}
		}
		
		i = document.getElementById('rb')
		
		if (i != null) {

			if (document.getElementById('rb').checked) rg = 'b';
			if (document.getElementById('rr').checked) rg = 'r';
			if (document.getElementById('rn').checked) rg = 'n';
			
		}
		
		s = s + '&rg=' + rg + '&'
	} else {
		s = 'd=_&rg=b&';
	}

	if (yearDate) {
		if (a.substr(a.length-1,1) == 'l') {
			var cy = document.getElementById('calendarYear');
			s = s + 'cy=' + cy.options[cy.selectedIndex].value + '&'
		} else if (a.substr(a.length-1,1) == 'c') {
			s = s +
				'fd=' + document.getElementById('firstDate').value + '&' + 
				'gt=' + document.getElementById('gotoDate').value + '&' + 
				'ws=' + document.getElementById('weeksToScroll').value + '&';
		}
	}
	
	return 'o=' + document.getElementById('calendarObject').value + '&' +
		'r=c&' +
		'a=' + a + '&' + 
		't=' + ct + '&' + 
		'i=' + document.getElementById('calendarId').value + '&' + 
		'it=' + document.getElementById('calendarIdType').value + '&' + 
		s + 
		'h=' + encodeURI(document.getElementById('calendarTitle').value)

}


