/** Select the calendar view, filter, axis, etc... */
function selectCalView()
{
    if (arguments.length > 0 && arguments[0] == getParamCalendarType())
    {
        if (gate()) {alert(getCalendarLoginMessage()); return;}
    }

    if (arguments.length > 1)
    {
        getElement(arguments[0]).value = arguments[1];
    }

    if (arguments.length > 3)
    {
        getElement(arguments[2]).value = arguments[3];
    }

    return doAction('', 'calendarForm');
}

/** Request a screening add to the server */
function addScreening(screeningId, festivalId)
{
    if (gate()) {alert(getCalendarLoginMessage()); return false;}

    send(handleAddScreening,
         getParamAction(), getActionAddScreening(),
         getParamScreeningId(), screeningId,
         getParamFestivalId(), festivalId);

    return false;
}

/** Request a screening remove from the server */
function removeScreening(screeningId, festivalId)
{
    if (gate()) {alert(getCalendarLoginMessage()); return false;}

    send(handleRemoveScreening,
         getParamAction(), getActionRemoveScreening(),
         getParamScreeningId(), screeningId,
         getParamFestivalId(), festivalId);

    return false;
}

var myRowClassName = 'mycalendarrow';
/** Screening added notification */
function handleAddScreening(response, args)
{
    var screeningId = args[3];
    highlightScreening(screeningId);

    var msg = (response == '1') ? " "+getCalendarPersonAdded() : " "+getCalendarPeopleAdded();
    setElementInnerHTML('buzzmessage', response + msg);

    return false;
}

function highlightScreening(screeningId)
{
    if(getElement('screening_' + screeningId) != undefined)
    {
        var classes = getElement('screening_' + screeningId).className;
    
        if ( classes.indexOf('mycalendarrow') < 0) {
            getElement('screening_' + screeningId).className += ' ' + myRowClassName;
        }
        getElement('addScreening_' + screeningId).style.display = 'none';
        getElement('removeScreening_' + screeningId).style.display = '';
    }
}

/** Screening removed notification */
function handleRemoveScreening(response, args)
{
    var screeningId = args[3];
    if(getElement('screening_' + screeningId) != undefined)
    {
        var classes = getElement('screening_' + screeningId).className;
        
        var clsIdx = classes.indexOf(myRowClassName);
        if ( clsIdx >= 0) {
            classes =  classes.substring(0, clsIdx) + ' calendarrow ';
            classes += classes.substring(clsIdx + myRowClassName.length + 1);
        }
        
        getElement('screening_' + screeningId).className = classes;
        getElement('addScreening_' + screeningId).style.display = '';
        getElement('removeScreening_' + screeningId).style.display = 'none';
    }

    var msg = (response == '1') ? " "+getCalendarPersonAdded() : " "+getCalendarPeopleAdded();
    setElementInnerHTML('buzzmessage', response + msg);

    return false;
}

/** Open the reminder window */
function openReminderWindow(screeningId)
{
    var url = 'reminderInfo.jsp?' + getParamScreeningId() + '=' + screeningId;
    window.open(url, '', 'width=300,height=150,scrollbars=no,left=350,top=300');
    return false;
}

/** Schedule the reminder */
function addReminder(email, userEmail, screeningId)
{
    send(handleAddReminder,
         getParamAction(), getActionAddReminder(),
         getParamScreeningId(), screeningId,
         getParamFestivalId(), getCurrentFestivalId(),
         getParamEmail(), email,
         getParamTimezoneOffset(), -(new Date().getTimezoneOffset() / 60));

    window.close();

    if (getAuthenticated() && (email == userEmail))
    {
        window.opener.highlightScreening(screeningId);
    }

    return false;
}

/** Confirm reminder */
function handleAddReminder()
{
    // do nothing
}

/* Determines the event status for the given user for the given screenings */
function getEventStatus(userId, screeningIds)
{
    send(handleEventStatus,
         getParamAction(), getActionGetEventStatusAjax(),
         getParamTemplate(), getTemplate(),
         getParamUserId(), userId,
         getParamScreeningId(), screeningIds);
}

function handleEventStatus(response, args)
{
    var eventStatus = eval(response);
    var screenings = eval('[' + args[7] + ']');

    for (i = 0; i < screenings.length; i++)
    {
        if (eventStatus[i] != null && eventStatus[i].length > 0)
        {
            // if the id gets us an anchor or span element, then we're working with the screening control. otherwise it's the calendar page
            var spurchase = document.getElementById('spurchase_' + screenings[i]);
            var onfilmpage = (spurchase != null) && (spurchase.nodeName.toLowerCase() == 'a' || spurchase.nodeName.toLowerCase() == 'img');

            // update the screening status
            if (onfilmpage)
            {
                if (eventStatus[i][1] != getScreeningStatusAvailable() && spurchase.innerHTML.indexOf(eventStatus[i][1]) == -1)
                {
                    switch(eventStatus[i][1])
                    {
                        case getScreeningStatusClosed():
                            spurchase.innerHTML = '<img src="images/atVenue.gif" alt="' + eventStatus[i][1] +'" width="85" height="17" border="0"/>';
                            break;
                        case getScreeningStatusSoldOut():
                            spurchase.innerHTML = '<img src="images/soldOut.gif" alt="' + eventStatus[i][1] + '" width="85" height="17" border="0"/>';
                            break;
                        case getScreeningStatusCancelled():
                            spurchase.innerHTML = '<img src="images/cancelled.gif" alt="' + eventStatus[i][1] + '" width="85" height="17" border="0"/>';
                            break;
                    }
                }
            }
            else if (spurchase != null)
            {
                var buytable = document.getElementById('buytable_' + screenings[i]);
                if (buytable != null && eventStatus[i][1] != getScreeningStatusAvailable()
                        && (buytable.className.match(/\bbuy\b/) || buytable.rows[0].innerHTML.indexOf(eventStatus[i][1]) == -1))
                {
                    buytable.className = buytable.className.replace(/\bbuy\b/g, 'nobuy');
                    buytable.rows[0].innerHTML = '<td class="nobr">' + eventStatus[i][1] + '</td>';
                }
            }

            // if the user purchased a ticket, reflect that on the site
            if (parseInt(eventStatus[i][4]) > 0)
            {
                // highlight a row that is part of the user's calendar
                highlightScreening(screenings[i]);

                // set the purchased icon.
                if (onfilmpage)
                {
                    if (eventStatus[i][1] == getScreeningStatusAvailable())
                    {
                        spurchase.innerHTML = '<img src="' + getImageDir() + 'icon-tix-bought.gif" alt="Purchased" width="18" height="18" align="absmiddle" border="0"/> '+getCalendarAddTix();
                    }
                    else
                    {
                        spurchase.innerHTML = '<img src="' + getImageDir() + 'icon-tix-bought.gif" alt="Purchased" width="18" height="18" border="0"/> ' + spurchase.innerHTML;
                    }
                }
                else if (spurchase != null)
                {
                    // it's a td on the calendar page
                    spurchase.innerHTML = '<img src="' + getImageDir() + 'icon-tix-bought.gif" alt="Purchased" width="18" height="18" align="absmiddle" border="0"/>';
                    var a = document.getElementById('spurchasea_' + screenings[i]);
                    if (a != null) a.innerHTML = getCalendarAddTix();
                }
            }
        }
    }

    // now that we're done processing, indicate that we've finished loading the event statuses (if we're on the schedule page)
    var purchasedHdr = document.getElementById('purchasedheader');
    if (purchasedHdr != null)
    {
        purchasedHdr.innerHTML = getCalendarPurchasedText();
        purchasedHdr.className = purchasedHdr.className.replace(/\bloading\b/g, '');
    }
}

