var GetCalendarScript = 'ajax/GetCalendar.php';
var GetMonthlyEventsScript = 'ajax/GetMonthlyEvents.php';
var DivWidth = 200;
var DivHeight = 100;
var ProcessStarted1 = false;
var ProcessStarted2 = false;
var isIE = (document.all) ? 1 : 0; 
var CurrentWidth = 0;
var CurrentHeight = 0;
var SelectedObj = null; 
var div = null;

function SelectPrevMonth()
{
	hidediv();
	Month = parseInt(Month) - 1;
	if(Month == 0)
	{
		Year--;
		Month = 12;
	}

	options = {
				 parameters:     	'&month=' + Month + '&year=' + Year + '&rand=' + Math.random(),
				 method:         	'get', 
				 onSuccess:     	ReloadCalendar
	};

	new Ajax.Request(GetCalendarScript, options);


}

function SelectNextMonth()
{
	hidediv();
	Month = parseInt(Month) + 1;
	if(Month > 12)
	{
		Month = 1;
		Year++;
	}

	options = {
				 parameters:     	'&month=' + Month + '&year=' + Year + '&rand=' + Math.random(),
				 method:         	'get', 
				 onSuccess:     	ReloadCalendar
	};

	new Ajax.Request(GetCalendarScript, options);
	
}

function ReloadCalendar(response)
{
	$('calendar').innerHTML = response.responseText;	
}


function SelectDay(day, obj)
{
	$('srcdiv').width = '0px';
	$('srcdiv').height = '0px';
	options = {
				 parameters:     	'&day=' + day + '&rand=' + Math.random(),
				 method:         	'get', 
				 onSuccess:     	DisplayEvents
	};

	new Ajax.Request(GetMonthlyEventsScript, options);
	SelectedObj = obj;
}

function DisplayEvents(response)
{
	eval('RVAL =' + response.responseText);

	div = $('srcdiv');
	div.innerHTML = '';
	
	if(!RVAL.TotalRows)
	    return false;
	
	for(i=0;i<RVAL.TotalRows;i++)
	{

		a = document.createElement("a");
		a.innerHTML = RVAL.Events[i].titlu;
		a.title = RVAL.Events[i].id;
		a.href= 'index.php?id=' + RVAL.Events[i].id + '&type=events';
		a.onclick = function(){ShowEvent(this.innerHTML);};
		div.appendChild(a);

		br = document.createElement("br");
		div.appendChild(br);
	}

	offset = getOffset(SelectedObj);

	div.style.top = (parseInt(offset.top) + 20) + "px";
	div.style.left = (parseInt(offset.left) + 5) + "px";

	if (CurrentWidth == 0 && CurrentHeight == 0)
	{
		showdiv(DivWidth,DivHeight);
	}
	
}


function showdiv(_x, _y)
{

	var div = $('srcdiv');
	if (!ProcessStarted2)
	{
		ProcessStarted1 = true;
		CurrentWidth += Math.abs(_x / 10);
		CurrentHeight += Math.abs(_y / 10);
		div.style.display = 'block';
		div.style.width = CurrentWidth + "px";
		div.style.height = CurrentHeight + "px";

		offset = getOffset(SelectedObj);

		if(isIE)
			div.style.bottom = offset.top + 10 + CurrentHeight;
		else
			div.style.bottom = offset.top + 25 + CurrentHeight;

		if (CurrentWidth < _x && CurrentHeight < _y)
		{
			setTimeout("showdiv(" + _x + ","+ _y +");", 10);
		}
		else
		{
			ProcessStarted1 = false;
		}
	}
}

function hidediv()
{
	var div = $('srcdiv');
	if(!ProcessStarted1)
	{
		if (CurrentWidth > 0 && CurrentHeight > 0)
		{
			ProcessStarted2 = true;
			CurrentWidth -= Math.abs(DivWidth / 10);
			CurrentHeight -= Math.abs(DivHeight / 10);
			div.style.display = 'block';
			div.style.width = CurrentWidth + "px";
			div.style.height = CurrentHeight + "px";

			offset = getOffset(SelectedObj);

			if(isIE)
				div.style.bottom = (offset.top + 10 + CurrentHeight) + "px";
			else
				div.style.bottom = (offset.top + 25 + CurrentHeight) + "px";

			if (CurrentWidth > 0 || CurrentHeight > 0)
			{
				setTimeout("hidediv();", 10);
				return;
			}
			else
			{
			     CurrentWidth = 0;
			     CurrentHeight = 0;
			}
		}
		
		div.innerHTML = '';
		div.style.display = 'none';
		ProcessStarted2 = false;
	}
}


function getOffset( el ) {
    var _x = 0;
    var _y = 0;

   var _x = el.offsetLeft;
   var _y = el.offsetTop;

   while ((el = el.offsetParent) != null)
   {
    _x += el.offsetLeft;
    _y += el.offsetTop;
   }

    return { top: _y, left: _x };
}

function ShowEvent(id)
{
}

