var cal = new Object();       
    cal.traversal =new Array()                    //could use a callback function to handle the return of each node

    cal.Event = function(d, ti, id, l, t){
        this.startDate = d;
        this.title = ti;
        this.ekId = id;
        this.location = l;
        this.type = (t) ? t : 'All Ages';
    }

     


    cal.Calendar = function (sorttype) {        //Constructor function for a Calendar object    
        
        this.index = null;
        this.eventBucket;
        this.before = null;
        this.after = null;
        this.sortType=sorttype;                        //user has the option of how the calendar will be indexed--Primary Index
    }

    cal.Calendar.prototype ={

        addEvent: function(e, id) {                    //would like to see self-balancing tree structure (AVL?)  and the possibility of secondary indexing
           // alert(e.location)
            var index;
           // if(e.constructor==cal.Event){
                switch (this.sortType){
                    case 'program':{
                        index = e.title;
                        break;
                    }
                    case 'location':{
                        index = e.location;
                        break;
                    }
                    case 'date':{
                        index = e.startDate;
                        break;
                    }
                    case 'id':{
                        index =(id)?id: e.ekId;
                        break;
                    }
                }
                if(this.isEmpty()==true){
                    this.index = index;
                    this.eventBucket = new Array ();
                    this.eventBucket[this.eventBucket.length] = e;
                    this.before = new cal.Calendar(this.sortType);  //these declarations would change with secondary indexing
                    this.after = new cal.Calendar (this.sortType);
                } else{
                    if(index > this.index){
                        this.after.addEvent(e);
                    } else if(index < this.index){
                        this.before.addEvent(e);
                    } else{
                        this.eventBucket[this.eventBucket.length] = e;
                    }
                }
            //} else{alert('There was an error loading events into the schedule. Please notify the Web Services Coordinator.')}
        },

        isEmpty: function(){
            if(this.index!=null){
                return false;
            } else{return true}
        },

        isIn:function (t){                            //good foundation for search?
            if(this.isEmpty()==true){
                return false ;
            }
            if(t.valueOf() == this.index.valueOf()||t==this.index){
                return this;
            } else if(t>this.index.valueOf()){
                return this.after.isIn(t);
            }
            else { return this.before.isIn(t);}
        },
    
        
        inOrder:function(){                           // review other traversal options to exploit
            if(!this.before.isEmpty()){
                this.before.inOrder();
            }
            cal.traversal.push(this);
            if(!this.after.isEmpty()){
                this.after.inOrder();
            }
            return cal.traversal
        }
        
    }//end cal.Calendar.prototype