window.addEvent('domready', function(){
                    // don't attach use as static calendar
                    dp = new DatePicker(null, {'pickerClass': 'rainbowCal',
                                               'dayShort': 1,
                                               'allowEmpty': true,
                                               'format': 'm/d/Y',
                                               'parent': $('calWrapper'),
                                               'position': 'static',
                                               'permanent': true,
                                               'startDay': 0,
                                               //'onSelect': getEventDay,
                                               'onMonthChange': function(){
                                                   getEventsMonth();
                                               }
                                              });
                    dp.show({left: 0, top: 0});

    //getUpcoming();
    displayEvent(upcoming);
    getEventsMonth();
});


var prevEventID = null;
var nextEventID = null;

function getUpcoming(){
    var url = "eventSvc.php";

    var req = new Request.JSON({url: url,
                                onSuccess: function(message){
                                    displayEvent(message.data);
                                }
                               });
    req.send("a=u");
}

function getEventsMonth(month){
    var url = "eventSvc.php";
    
    var req = new Request.JSON({url: url,
                                onSuccess: function(message){
                                    if (message){
                                        if (message.status == 'success'){
                                            data = new Hash(message.data);
                                            dp.highlightDays(data, function(day, id){
                                                                 location.href = 'calendar.php?a=v(' + id + ')';
                                                             });
                                        }
                                        else {
                                            alert(message.reason);
                                        }
                                    }
                                    else {
                                    }
                                }
                               });

    req.send("a=em&d=" + dp.d.format('%Y-%m-%d'));
}

function getEventDay(date){
    var url = "eventSvc.php";
    var req = new Request.JSON({url: url,
                                onSuccess: function(message){
                                    if (message){
                                        if (message.status == 'success'){
                                            if (message.data){
                                                displayEvent(message.data);
                                            }
                                        }
                                        else {
                                            alert(message.reason);
                                        }
                                    }
                                    else {
                                    }
                                }
                               });


    req.send("a=ed&d=" + date.format('%Y-%m-%d'));
}


function prevEvent(){
    getEvent(prevEventID);
}

function nextEvent(){
    getEvent(nextEventID);
}

function getEvent(id){
    var url = "eventSvc.php";
    
    var req = new Request.JSON({url: url,
                                onSuccess: function(message){
                                    displayEvent(message.data);
                                }
                               });
    req.send("a=e&id=" + id);
    
}

function displayEvent(data){
    if (!data || !data.title){
        return false;
    }

    if (data.thumbFile != ''){
        $('eventImage').setStyles({'background-image': 'url("' + eventDir + data.eventID + '/220x140.png' + '")',
                                   'cursor': 'pointer' })
            .removeEvents('click')
            .addEvent('click', function(){
                          location.href = 'calendar.php'; // ?a=v(' + data.eventID + ')';
                     });
    }
    $('eventDesc').set('html', (data.title.length > 45 ? data.title.substring(0, 45) + '...' : data.title))
        .setStyles({
                       'cursor': 'pointer' })
        .removeEvents('click')
        .addEvent('click', function(){
                      location.href = 'calendar.php'; // ?a=v(' + data.eventID + ')';
                  });
//    prevEventID = data.prevID;
//    nextEventID = data.nextID;
    
//    $('prevEvent').fade(prevEventID ? 'in' : 'out');
//    $('nextEvent').fade(nextEventID ? 'in' : 'out');
    
}                    

