var arrBookingPreview = null;

// Assumes dates are of the format 2007-04-17 12:25:00
function ConvertToJsDate(pDate)
{
	var splitDate = pDate.split("-");
	
	// Create a Javascript date from the string
	var jsDate = new Date();
	jsDate.setYear(splitDate[0]);
	jsDate.setMonth(splitDate[1]-1); // Js Month array is zero-based
	jsDate.setDate(splitDate[2].substring(0,splitDate[2].indexOf(" ")));

	var splitTime = (splitDate[2].substring(splitDate[2].indexOf(" ")).split(":"));
	jsDate.setHours(splitTime[0]);
	jsDate.setMinutes(splitTime[1]);
	jsDate.setSeconds(splitTime[2]);
	
	return jsDate;
}

function RegisterBookingRollovers()
{
    for (var i = 1; i <= 31; i++)
    {
        var daycell = document.getElementById("Cell" + i);
        if (daycell)
        {
            daycell.onmouseover = ShowBookingPreview;
        }
    }
}

function ShowBookingPreview()
{
    if (window.arrBookingPreview)
    {
        // Loop through the calendar dates and hide them all
        for(var i = 0; i < arrBookingPreview.length; i++)
        {
            arrBookingPreview[i].Hide();
        }

        // This is the day # of the calendar cell we've just rolled over
        var l_DayNum = this.id.substring(4);

        // Loop through the calendar dates and find a match
        for(var i = 0; i < arrBookingPreview.length; i++)
        {
            arrBookingPreview[i].DisplayOnMatch(l_DayNum);
        }
    }
    
}

BookingPreview = function(p_cellID, p_startDate, p_endDate)
{
    this.StartDate = ConvertToJsDate(p_startDate);
    this.EndDate = ConvertToJsDate(p_endDate);
    this.CellID = p_cellID;
    
    this.DisplayOnMatch = function(p_cellDay)
    {
        // Which repeater item are we managing the visibility of?
        var l_previewCell = document.getElementById(this.CellID);
        
        // Determine the date range covered by the calendar day
        var l_CellDayStart = ConvertToJsDate(CalendarYYYYMM + "-" + p_cellDay + " 00:00:00");
        var l_CellDayEnd = new Date(l_CellDayStart.getTime() + 24*60*60*1000);
        
        if (l_previewCell)
        {
            // Show or hide, depending on whether the event occurs on this date
            l_previewCell.style.display = IsDateRangeIntersecting(this.StartDate.getTime(), this.EndDate.getTime(), l_CellDayStart.getTime(), l_CellDayEnd.getTime()) ? "" : "none";
        }
    }
    
    this.Hide = function()
    {
        var l_previewCell = document.getElementById(this.CellID);
        if (l_previewCell)
        {
            l_previewCell.style.display = "none";
        }
    }
}

function IsDateRangeIntersecting(r1start, r1end, r2start, r2end)
{
    return (r1start == r2start) || (r1start > r2start ? r1start <= r2end : r2start <= r1end);
}


