var EventCalendar = Class.create();
EventCalendar.prototype = {
    initialize: function(sspContainer, eventContainer, records) {
      this.sspContainer = $(sspContainer);
      this.eventContainer = $(eventContainer);
      this.records = records;
      
        if (typeof(this.records) != 'undefined') {
            var list = $("month");
            list.selectedIndex = (list.options.length == 1 ? 0 : 1);
            this.filter(list);
        }
    },
    
    filter: function(src) {
        var filteredList = this.records.data.findAll(
            function(p){
                if (p != null) {
                    return (p.StartingMonth == src[src.selectedIndex].value) || (src[src.selectedIndex].value == 0);
                }
            });
        this.fill(filteredList);
    },
    
    fill: function(data) {
        this.clear(this.eventContainer);
        
        if ( data.length > 0 )
        {
            var p = data[0];
            
            if (p != null)
            {                       
                this.renderEventView(p);
                this.renderSlideShow(p);
            }
        }
    },
    
    renderSlideShow: function(p) {
        // take the media list for the first event only
        function _flashPhotoContent() {
            var so = new SWFObject("/flash/ssp/sharpieCalendar.swf", "temp", "333", "335", "7", "#ffffff");
            so.addParam("wmode", "transparent");
            so.addParam("FlashVars", "xmlfile=" + p.GalleryPath);
            so.write("flashcontent");
        };
        
        function _flashVideoContent() {
          var so1 = new SWFObject("/flash/ssp/sharpieVideo.swf", "temp", "332", "76", "7", "transparent");
            so1.addParam("FlashVars", "xmlfile=" + p.GalleryPath);
          so1.addParam("wmode", "transparent");
          so1.write("flashcontent");
        }
        
        if ( p.GalleryType == "video" )
            _flashVideoContent();
        else if ( p.GalleryType == "sspimages" )
            _flashPhotoContent();
    },
    
    renderEventView: function(p) {
        if (p == null) return;       
         
        var element=$E({
            tag : "li",
            children : [{
                tag : "strong",
                children : p.City 
            },
            {
                tag: "br"
            },
                p.DateRange,
            {
                tag: "br"
            },
                p.Location,
            {
                tag: "br"
            },
                p.Title,
            ]
        });  
                       
        this.eventContainer.appendChild(element);
    },

    clear: function(elm) {
        if (elm != null) {
            for(var i = elm.childNodes.length-1; i >= 0; i--) {
                elm.removeChild(elm.childNodes[i]);
            }
        }
    }
    
};