
CalendarEvent = function ( )
{
	this.days = new Array ( );
}
CalendarEvent.prototype.hasDay = function ( day )
{
	for ( var a = 0; a < this.days.length; a++ )
		if ( this.days[ a ] == day )
			return true;
	return false;
}

var Calendar = new Object ( );
Calendar.loaded = false;
Calendar.init = function ( )
{
	if ( document.body && typeof ( bajax ) != 'undefined' && typeof ( addEvent ) != 'undefined' )
	{
		var j = new bajax ();
		var l = document.location + '';
		var url = l.indexOf ( '?' ) ? ( l + '&calendarGetEvents=1' ) : ( l + '?calendarGetEvents=1' );
		j.openUrl ( url, 'get', true );
		j.calendar = this;
		j.onload = function ( )
		{
			this.calendar.parseData ( this.getResponseText ( ) );
			addEvent ( 'onmousemove', function ( )
			{
				if ( Calendar.popup )
				{
					var y = mousey + 16;
					var x = mousex - 16;
					var h = getElementHeight ( Calendar.popup );
					var w = getElementWidth ( Calendar.popup );
					var sh = getDocumentHeight ( ) - 16;
					var sw = getDocumentWidth ( ) - 16;
					
					if ( x + w > sw )
						x = sw - w;
					if ( y + h > sh )
						y = sh - h;
					
					Calendar.popup.style.top = y + 'px';
					Calendar.popup.style.left = x + 'px';
					
					
					if ( Calendar.tableNode )
					{
						var obj = Calendar.tableNode;
						var x1 = getElementLeft ( obj );
						var y1 = getElementTop ( obj );
						var x2 = getElementWidth ( obj ) + x1;
						var y2 = getElementHeight ( obj ) + y1;
						if ( mousex >= x1 && mousex < x2 && mousey >= y1 && mousey < y2 && Calendar.popup.childNodes.length )
						{
							Calendar.popup.style.visibility = 'visible';
						}
						else Calendar.popup.style.visibility = 'hidden';
					}
				}
			} 
			);
		}
		j.send ( );
	}
	else setTimeout ( 'Calendar.init()', 100 );
}
Calendar.parseData = function ( dt )
{
	var da = document.createElement ( 'div' );
	da.innerHTML = dt;
	this.data = new Array ( );
	for ( var a = 0; a < da.childNodes.length; a++ )
	{
		var div = da.childNodes[ a ];
		if ( !div || ( div && !div.id ) ) return;
		var ob = new CalendarEvent ( );
		ob.days = div.id.split ( '_' )[1].split ( 'd' );
		ob.html = '<div class="Event">' + div.innerHTML + '</div>';
		this.data.push ( ob );
	}
	if ( !this.popup )
	{
		var p = document.createElement ( 'div' );
		p.id = 'CalendarPopup';
		document.body.insertBefore ( p, document.body.firstChild );
		this.popup = p;
	}
	this.loaded = true;	
}
Calendar.getEventsByDay = function ( d, obj )
{
	if ( !obj ) obj = false;
	if ( !this.loaded )
		return;
	var events = new Array ( );
	for ( var a = 0; a < this.data.length; a++ )
	{
		if ( this.data[ a ].hasDay ( d ) )
		{
			events.push  ( this.data[ a ].html );
		}
	}
	this.popup.innerHTML = events.join ( '' );
}
Calendar.init ( );


function calendarTriggerDate ( date, obj )
{
	Calendar.getEventsByDay ( date.split ( '-' )[2] );
	var o = obj;
	while ( o.nodeName.toLowerCase ( ) != 'table' )
		o = o.parentNode;
	Calendar.tableNode = o;
}

function calendarPrevMonth ( )
{
	var loc = document.location + '';
	loc = loc.split ( '?' )[0];
	document.location = loc + '?calendar_month=-1';
}

function calendarNextMonth ( )
{
	var loc = document.location + '';
	loc = loc.split ( '?' )[0];
	document.location = loc + '?calendar_month=1';
}
