var currentPos;
function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    
    currentPos = cursor;
}

Event.observe(document, 'mousemove', getPosition);

var popupContainer;
function showPopup(html)
{
	if (!popupContainer) {
		popupContainer = Builder.node('div', { className: 'popup', style: 'position: absolute; z-index: 1000;' } );
		$(popupContainer).hide();
		document.body.appendChild(popupContainer);
	}
	
	$(popupContainer).update('<div class="popup-content"><div style="text-align: right;"><a style="font-size: 70%;" href=\'javascript:hidePopup();\'>(close)</a></div>' + html + '</div><div class="topleft"></div><div class="topright"></div><div class="bottomleft"></div><div class="bottomright"></div><div class="right"></div><div class="left"></div><div class="bottom"></div><div class="top"></div>');
	
	var targetX = $(document.body).getWidth()/2;
	var targetY = (document.documentElement.scrollTop || document.body.scrollTop);
	popupContainer.style.left = Math.max(25, targetX - $(popupContainer).getWidth()/2) + 'px';
	popupContainer.style.top = targetY + 'px';
	$(popupContainer).show();
}

function hidePopup()
{
	$(popupContainer).hide();
}


function showBlockTimesPopup(calendarId, day)
{
	showPopup('Loading...');
	new Ajax.Request('appointments.php?action=newBlockHtml&calendarID='+calendarId+'&day='+day, {
	  method: 'post',
	  onSuccess: function(transport) {
	    showPopup(transport.responseText);
	    transport.responseText.evalScripts();
	  }
	});
}

function showAvailableTimesPopup(calendarId, day)
{
	showPopup('Loading...');
	new Ajax.Request('availableTimes.php?calendarID='+calendarId+'&day='+day, {
	  method: 'post',
	  onSuccess: function(transport) {
	    showPopup(transport.responseText);
	    transport.responseText.evalScripts();
	  }
	});
}