function EventCalendar()
{
    var self = this;
    
    var DivContainer = null;
    var DivContent = null;
    var IsVisible = false;

    // public variables
    
    this.EnableHover = true;

    // on-load constructor
    $(function()
    {
        // create
        DivContainer = document.createElement("div");
        DivContainer.id = "transp"; // legacy id
        document.body.appendChild(DivContainer);
        
        DivContent = document.createElement("div");
        DivContent.id = "eventView"; // legacy id
        document.body.appendChild(DivContent);
        
        // attach a handler to the document's onclick to hide the event window
        $(document).bind("click", DocumentClick);
        
        // and a handler to kill the click for the content area (so the links in it work)
        $(DivContent).bind("click", CancelEventClick);
        
        if (self.EnableHover)
        {
            $("a[event_id]").bind("mouseover", EventMouseOver).bind("mousemove", EventMouseMove).bind("mouseout", EventMouseOut);
        }
        else
        {
            $("a[event_id]").bind("click", EventClick);
        }
	DivContainer.style.zIndex = "1000";
	DivContent.style.zIndex = "1000";
    });

    function EventClick(e)
    {
	    var target;
	    if (!e) var e = window.event;
	    if (e.target) target = e.target;
	    else if (e.srcElement) target = e.srcElement;
	    if (target.nodeType == 3) // defeat Safari bug
		    target = target.parentNode;
		    
        var EventID = target.getAttribute("event_id");
        SetEventPosition(e);
        ShowEvent(EventID);

	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();	
    }
    
    function EventMouseOver(e)
    {
	    var target;
	    if (!e) var e = window.event;
	    if (e.target) target = e.target;
	    else if (e.srcElement) target = e.srcElement;
	    if (target.nodeType == 3) // defeat Safari bug
		    target = target.parentNode;

		var EventID = target.getAttribute("event_id");
		SetEventPosition(e);
        ShowEvent(EventID);
    }
    
    function EventMouseMove(e)
    {
        if (!e) var e = window.event;
        SetEventPosition(e);
    }
    
    function EventMouseOut(e)
    {
        HideEvent();
    }
    
    function ShowEvent(EventID, Position)
    {
        $(DivContent).load("../Ajax/CalendarEvents.aspx?evts=" + EventID);
		
		// show the stuff
		DivContainer.style.display = "block";
		DivContent.style.display = "block";
		DivContainer.style.zIndex = "1000";
		DivContent.style.zIndex = "1000";
    }
    
    function SetEventPosition(e)
    {
        var Position = GetMousePosition(e);
        //DivContainer.style.top = (Position.y+1) + "px";
        //DivContainer.style.left = (Position.x+1) + "px";
        //DivContent.style.top = (Position.y+1) + "px";
        //DivContent.style.left = (Position.x+1) + "px";

	if(Position.x < 350)
	{
		
        	DivContainer.style.left = (Position.x+1) + "px";
        	
        	DivContent.style.left = (Position.x+1) + "px";
	}
	else
        {
        	DivContainer.style.left = (Position.x-350) + "px";
        	
        	DivContent.style.left = (Position.x-350) + "px";
	}

	if(Position.y < 550)
	{
		DivContainer.style.top = (Position.y+1) + "px";
        	DivContent.style.top = (Position.y+1) + "px";

	}
	else
	{
		DivContainer.style.top = (Position.y-300) + "px";
        	DivContent.style.top = (Position.y-300) + "px";

	}
		
        

	
    }
    
    function HideEvent()
    {
        DivContainer.style.display = "none";
        DivContent.style.display = "none";
    }
    
    function CancelEventClick(e)
    {
	    if (!e) var e = window.event;
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();	
    }
    
    function DocumentClick(e)
    {
	    HideEvent();
    }
    
    function GetMousePosition(e)
    {
        var position = {x: 0, y: 0};

	    if (e.pageX || e.pageY)
	    {
		    position.x = e.pageX;
		    position.y = e.pageY;
	    }
	    else if (e.clientX || e.clientY)
	    {
		    position.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		    position.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	    }
	    
	    return position;
    }
}

var EventCalendar = new EventCalendar;