var index = 0;
var visibility = new Array();
var currentVisible;

$(document).ready(
function ()
    {
        CreateAccordion();

        // initialize month selection        
        $('#MonthLinks .currentMonth').click();
    }
);


function CreateAccordion()
{

    $("#MonthLinks > ul").children().each
    (
        function()
        {
            $(this).click(function(event)
            {
                // don't do anything for disabled months
                if ($(this).is('.monthDisabled')) {return;}
            
                if(this.attributes.id.nodeValue != null && this.attributes.id.nodeValue !=  "")
                {
                    var eventID = "Event"+this.attributes.id.nodeValue;
                
                    var clickedElement = document.getElementById(this.attributes.id.nodeValue);
                    var noEvent = false;
                    if(clickedElement.className != "monthNoEvents")
                    {
                        clickedElement.className = "selected";
                    }
                    
                    var oldElement = document.getElementById(currentVisible[0].attributes.id.nodeValue.substring(5));
                    if(oldElement.className != "monthNoEvents" && oldElement.className != "monthDisabled")
                    {
                        oldElement.className = "";
                    }
                    
                    
                     var target = $("#"+eventID);
                     if(visibility[eventID] == 0 || visibility[eventID] == undefined)
                     {
                        target.children(".content").slideDown("slow");
                        visibility[eventID] = 1;
                        if(currentVisible[0].attributes.id.nodeValue != eventID)
                        {
                            currentVisible.children(".content").slideUp("slow");
                            currentVisible = target;
                            visibility[currentVisible[0].attributes.id.nodeValue] = 0;
                        }
                     }
                     else
                     {
                        target.children(".content").slideUp("slow");  
                        visibility[eventID] = 0;                   
                     }
                }
            });
        }
    )






    $("#CalendarEvents > ul").children().each
    (
        function()
        {
            $(this).click(function(event)
            {
                 if(visibility[this.attributes.id.nodeValue] == 0 || visibility[this.attributes.id.nodeValue] == undefined)
                 {
                    $(event.currentTarget).children(".content").slideDown("slow");
                    visibility[this.attributes.id.nodeValue] = 1;
                    if(currentVisible[0].attributes.id.nodeValue != this.attributes.id.nodeValue)
                    {
                        currentVisible.children(".content").slideUp("slow");
                        currentVisible = $(event.currentTarget);
                        visibility[currentVisible[0].attributes.id.nodeValue] = 0;
                    }
                 }
                 else
                 {
                    $(event.currentTarget).children(".content").slideUp("slow");  
                    visibility[this.attributes.id.nodeValue] = 0;                   
                 }
            });
            if(index == 0)
            {
                visibility["Event"+index] = 1;
                currentVisible = $("#Event1")
            }
            else
            {
                visibility["Event"+index] = 0;
                var content = $(this).children(".content");
                content.slideUp("slow");
            }
            index ++;
        }
    )
}
