onClickedEvent=function(event, actionLink)
{
    loading= jQuery("#eventListDisplay #divLoadingEventList");
    contents = jQuery("#eventListDisplay #divContentsEventList")

    loading.fadeIn(1000);
    contents.fadeOut(1000);

    event.preventDefault(); 

    $("eventListDisplay").load(actionLink);

    refreshEventList();                

    return false;
}

getResponseCell=function(label, action, eventId)
{
    tmpSubSubCell=document.createElement("td");
    link = document.createElement("A");
    link.setAttribute("id",action + eventId);
    link.appendChild( document.createTextNode( label));
    actionLink = "/EventResponse/"+ action + "/" + eventId;
    link.setAttribute("href", actionLink);
    className = "eventResponseLink"+ action + eventId;
    link.setAttribute("className", className);

    $('a.'+className).live('click', function (event)
        {
            onClickedEvent(event,$(this).attr('href'));
        });

    tmpSubSubCell.appendChild(link);
    
    return tmpSubSubCell;
}
getReservationCell=function(label, action, eventId)
{
    tmpSubSubCell=document.createElement("td");
    link = document.createElement("A");
    link.setAttribute("id",action + eventId);
    link.appendChild( document.createTextNode( label));
    actionLink = "/Reservation/"+ action + "/" + eventId;
    link.setAttribute("href", actionLink);
    className = "eventReservationLink"+ action + eventId;
    link.setAttribute("className", className);

    tmpSubSubCell.appendChild(link);
    
    return tmpSubSubCell;
}

getSingleEventRowResponse=function(item, i)
{
    tmpSubRow=document.createElement("tr");
    tmpSubRowCell=document.createElement("td");
    tmpSubRow.appendChild(tmpSubRowCell);
    
    tmpTable = document.createElement("table");
    tmpTable.id="singleEventResponse"+i;
    tmpTable.setAttribute("className","singleEventResponse");
    tmpSubRowCell.appendChild(tmpTable);
    
    tmpTableBody = document.createElement("tbody");
    tmpTable.appendChild(tmpTableBody);
    
    tmpSubSubRow=document.createElement("tr");
    tmpTableBody.appendChild(tmpSubSubRow);

    responseCell = getResponseCell("Kommer", "Coming", item.EventId);
    tmpSubSubRow.appendChild(responseCell);

    responseCell = getResponseCell("Kommer ikke", "NotComing", item.EventId);
    tmpSubSubRow.appendChild(responseCell);

    responseCell = getResponseCell("Kommer maaske", "PerhapsComing", item.EventId);
    tmpSubSubRow.appendChild(responseCell);
    
    return tmpSubRow;
}

getSingleEventRowReservation=function(item, i)
{
    tmpSubRow=document.createElement("tr");
    tmpSubRowCell=document.createElement("td");
    tmpSubRow.appendChild(tmpSubRowCell);
    
    tmpTable = document.createElement("table");
    tmpTable.id="singleEventReservation"+i;
    tmpTable.setAttribute("className","singleEventReservation");
    tmpSubRowCell.appendChild(tmpTable);
    
    tmpTableBody = document.createElement("tbody");
    tmpTable.appendChild(tmpTableBody);
    
    tmpSubSubRow=document.createElement("tr");
    tmpTableBody.appendChild(tmpSubSubRow);

    responseCell = getReservationCell("Reservation: " + item.CurrentReservation + " / " + item.CurrentNeed, "ChangeReservation", item.EventId);
    tmpSubSubRow.appendChild(responseCell);
 
    return tmpSubRow;
}

getSingleEventRow2=function(className, innerText)
{
    tableSingleEventRow=document.createElement("tr");
    tableSingleEventCell=document.createElement("td");
    tableSingleEventCell.setAttribute("className",className);
    tableSingleEventCell.appendChild( document.createTextNode(innerText));
    tableSingleEventRow.appendChild(tableSingleEventCell);
    
    return tableSingleEventRow;
}
getSingleEventRowStartTime = function (item)
{
    return getSingleEventRow2("singleEventStartTime", item.StartTime );
}

getSingleEventRowTitle =  function (item)
{
    tableSingleEventRow=document.createElement("tr");
    tableSingleEventCell=document.createElement("td");
    tableSingleEventCell.setAttribute("className","singleEventTitle");
    link = document.createElement("A");
    link.setAttribute("href","/Event/Edit/" + item.EventId);
    link.appendChild( document.createTextNode( item.Title));
    tableSingleEventCell.appendChild( link);
    tableSingleEventRow.appendChild(tableSingleEventCell);

    return tableSingleEventRow;
}

getSingleEventRowAttendance= function (nameList, label)
{
    if(nameList=="")
    {
        return "";
    }
    tmpSubRow=document.createElement("tr");
    tmpSubCell=document.createElement("td");
    tmpSubCell.setAttribute("className","singleEventAttendance");
    tmpSubCell.appendChild( document.createTextNode( label + nameList));
    
    tmpSubRow.appendChild(tmpSubCell);
    return tmpSubRow;
}

getSingleEventRow= function(table1body, item, i)
{
    singleEventRow=document.createElement("tr");
    singleEventCell=document.createElement("td");
    singleEventRow.appendChild(singleEventCell);

    var tableSingleEvent=document.createElement("table");
    singleEventCell.appendChild(tableSingleEvent);    
    tableSingleEvent.id="singleEvent"+i;
    tableSingleEvent.setAttribute("className","singleEvent");
    tableSingleEventBody=document.createElement("tbody");
    tableSingleEvent.appendChild(tableSingleEventBody);

    tableSingleEventBodyRowTitle = getSingleEventRowTitle(item);
    tableSingleEventBody.appendChild(tableSingleEventBodyRowTitle);

    tableSingleEventBodyRowStartTime = getSingleEventRowStartTime(item);
    tableSingleEventBody.appendChild(tableSingleEventBodyRowStartTime);

    tableSingleEventBodyRowAttendance = getSingleEventRowAttendance(item.NamesComing, "Kommer: ");
    if (tableSingleEventBodyRowAttendance!="")
    {
        tableSingleEventBody.appendChild(tableSingleEventBodyRowAttendance);
    }

    tableSingleEventBodyRowAttendance = getSingleEventRowAttendance(item.NamesNotComing, "Kommer ikke: ");
    if (tableSingleEventBodyRowAttendance!="")
    {
        tableSingleEventBody.appendChild(tableSingleEventBodyRowAttendance);
    }

    tableSingleEventBodyRowAttendance = getSingleEventRowAttendance(item.NamesPerhapsComing, "Kommer maaske: ");
    if (tableSingleEventBodyRowAttendance!="")
    {
        tableSingleEventBody.appendChild(tableSingleEventBodyRowAttendance);
    }

    tableSingleEventBodyRowResponse = getSingleEventRowResponse(item,i);
    tableSingleEventBody.appendChild(tableSingleEventBodyRowResponse);

    tableSingleEventBodyRowReservation = getSingleEventRowReservation(item,i);
    tableSingleEventBody.appendChild(tableSingleEventBodyRowReservation);

    
    return singleEventRow;
}

refreshEventList = function()
{ 
    jQuery.getJSON("/Event/GetEventList",
    function(json)
    {
        loading= jQuery("#eventListDisplay #divLoadingEventList");
        contents = jQuery("#eventListDisplay #divContentsEventList")

        // Abort if there is no eventlist list on the page to update.
        if (contents.length==0)
        {
            return;
        }

        loading.fadeIn(1000);
        contents.fadeOut(1000);
        
        // If no response was given, the list of events was not yet available.
        // Ask again in two seconds!
        if (json == "")
        {
            setTimeout(refreshEventList, 2*1000);
            return;
        }                
        
        loading.fadeOut(1000);

        while (contents[0].childNodes[0]) {
            contents[0].removeChild(contents[0].childNodes[0]);
        }

        var table1=document.createElement("table");
        table1.id="eventList";
        table1.setAttribute("className","eventList");
                   
        var table1body=document.createElement("tbody");     
        table1.appendChild(table1body);
        
        jQuery.each(json, function(i, item)
        {
          singleEventRow = getSingleEventRow(table1body,item,i);
          table1body.appendChild(singleEventRow);

        });

        contents[0].appendChild(table1);
        
        jQuery(contents).fadeIn(1000);
        
        // Success! The responses may change over time, however, and we refresh every five minutes.
        setTimeout(refreshEventList, 5*60*1000);
    });         
}
jQuery("eventListDisplay").ready( refreshEventList )
