var sidecal= new Object();
        sidecal.monthConversion = new Array()
        sidecal.monthConversion['Jan']={full: 0};
        sidecal.monthConversion['Feb']={full: 1};
        sidecal.monthConversion['Mar']={full: 2};
        sidecal.monthConversion['Apr']={full: 3};
        sidecal.monthConversion['May']={full: 4};
        sidecal.monthConversion['Jun']={full: 5};
        sidecal.monthConversion['Jul']={full: 6};
        sidecal.monthConversion['Aug']={full: 7};
        sidecal.monthConversion['Sep']={full: 8};
        sidecal.monthConversion['Oct']={full: 9};
        sidecal.monthConversion['Nov']={full: 10};
        sidecal.monthConversion['Dec']={full: 11};

 sidecal.DisplaySwitch = function(i,c){
    s = document.getElementById(i);
    if(c){
        t = document.getElementById(c);
        }
    if (s.style.display != "block"){
        s.style.display = "block";
        if(t){
            t.innerHTML= "close event drawer";
            }
        }
    else{
        s.style.display = "none";
        if(t){
            t.innerHTML= "open event drawer";
            }
        }
    }

sidecal.ScheduleLoader = function (evk, bksched, id){
    this.main = new cal.Calendar('location');
    this.currentDate()
    this.scheduleId = id;
    this.startdate = bksched.startdate;
    this.enddate = bksched.enddate; 
    this.buildSched(evk)
    this.addBookmobile(bksched)
}

sidecal.ScheduleLoader.prototype ={    

    currentDate: function(){var nd =new Date(); 
        var x = new Date(nd.getFullYear(), nd.getMonth(), nd.getDate());//new Date(2007, 8, 27);
        return x
        },

    weekdayAbbr: function (i){x =['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];return x[i];},
    weekday: function (i){x = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; return x[i];},
    monthnameAbbr: function (i){x = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.']; return x[i];},
    monthname: function (i){x = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; return x[i];},

        dateString:function (event){
            var m = this.monthnameAbbr(event.startDate.getMonth());
            var t = ""
            var d = event.startDate.getDate();
            var x = event.startDate.getHours();
            var y ="";
            if (x > 12){
                x = x - 12; 
                y = "p.m.";
            } else if(x==0){
                x = 12;
                y="a.m."
            }
            else if(x==12){y="p.m."}
            else { 
                y="a.m";
            } 
            min = event.startDate.getMinutes();
            min = min.toString()
            if(min==0){min='00'}     
                t = x +':'+min +' '+y
            if(event.location =='bk'){
                t=""
            } else if(t == "12:00 a.m."){t = 'call for time'};
            return m + ' ' + d.toString() + ', ' + t;
        },
        
    buildSched: function (e) {
        if(e.constructor == cal.Event){
            this.main.addEvent(e);
        }
        else{ 
            for(i = 0; i<e.length; i++){
                var t, h = 0, hrs, m, y, d, min = 0;
                t = e[i][evt_time].replace(/[&nbsp;]/g, "");                                            //t is the stripped version of the evt_time string from the EK array (e).  Form is 99:99 XX 
                if(t){h = t.slice(0, t.indexOf(":"))};                                                           //h is string value representing the starting hour of the event
                hrs = parseInt(h);                                                                                   //hrs is the integer value of the string h.  Converted to military time and used to set the hour of the start_time property of the event
                m = sidecal.monthConversion[e[i][evt_date].slice(0,3)].full;         //m is the integer representing the month of the start_time property of the event
                y = parseInt(e[i][evt_date].slice((e[i][evt_date].length-5)));              //y is the integer value of the year of event start_time
                d = parseInt(e[i][evt_date].slice(4, (e[i][evt_date].length-6)));         //d is the interger value of the day of the event start_time
                if(t){min = parseInt(t.slice((t.indexOf(":")+1), t.lastIndexOf(" ")))};   //d is the interger value of the minutes of the event start_time
                var special = new cal.Event(); 
                if(hrs !="12" && t.search("P")!=-1){                                                         //use info from the evt_time string to convert the integer value of the hour portion of the string to military time
                    hrs = hrs + 12;
                }
                if(hrs =="12"&& t.search("A")!=-1){// 
                    hrs =12;
                }
                special.startDate = new Date(y, m, d, hrs, min);
                special.title = e[i][evt_name];
                special.ekId = e[i][evt_id];
                special.location = e[i][evt_key1];
                special.type = e[i][evt_key2];
                special.dateString = this.dateString(special)
                this.main.addEvent(special);
            }
        }
    },

    createEventDrawerControls: function(id){
        var loc=[{id:"main_library", search:"Main Library", name:"Main Library"},{id:"bookmobile", search:"bk", name:"Bookmobile"},{id:"clendenin", search:"Clendenin", name:"Clendenin Branch Library"},{id:"cross_lanes", search:"Cross Lanes", name:"Cross Lanes Branch Library"},{id:"dunbar",search:"Dunbar", name:"Dunbar Branch Library"},{id:"elk_valley", search:"Elk Valley", name:"Elk Valley Branch Library"},{id:"glasgow",search:"Glasgow", name:"Glasgow Branch Library"},{id:"marmet",search:"Marmet", name:"Marmet Branch Library"},{id:"nitro",search:"Nitro", name:"Nitro Library"},{id:"riverside", search:"Riverside", name:"Riverside Public Library"},{id:"st_albans",search:"St Albans", name:"St. Albans Branch Library"},{id:"sissonville",search:"Sissonville", name:"Sissonville Branch Library"}];
        document.getElementById('drange').innerHTML = evtArray[0][evt_date] +' - '+evtArray[evtArray.length - 1][evt_date];
        var prid
        var cal=document.getElementById(id)
        for(var ii=0;ii<loc.length;ii++){
            if(ii%3==0){
               p= document.createElement("div");
               p.id = 'print'+ii;
               p.className = 'printrow';
            }
            p.appendChild(this.createEventDrawer(loc[ii]));
            cal.appendChild(p)
        }
    },

    createEventDrawer: function (location){
        var stockmessage = "No Events Scheduled";
        var handletext = 'close event drawer';
        var linktext = '/branches/'+location.id//+'.html';
        var eventdrawer = KCPL.buildElement('div', 'caldisplay', location.id +'display')
        var loclink = KCPL.buildElement('a','locationlink') 
        loclink.href = linktext;
        eventdrawer.appendChild(loclink).appendChild(document.createTextNode(location.name));
        var events = KCPL.buildElement('div', 'events', location.id)
        var found = this.main.isIn(location.search)
        if(found){
       
            var eb = found.eventBucket
            if(location.id=='bookmobile'){ 
            
                var header = document.createElement('h4')
                header.appendChild(document.createTextNode('Today\'s Stops'))
                events.appendChild(header);
            }
            for(var ev=0; ev<eb.length; ev++){
                
                var event = KCPL.buildElement('span', 'event')
                if(location.id=='bookmobile'&&KCPL.today<=bkschedule.enddate){
            
                    if(eb[ev].exceptionText){
                    //alert(event.className);
                        event.className = event.className + ' exception';
                    //alert(event.className);
                    event.appendChild(document.createTextNode(eb[ev].title +': '+eb[ev].exceptionText))
                    }else{event.appendChild(document.createTextNode(eb[ev].title +': '+eb[ev].timeString))}
                    }
                    
                //else{events.appendChild(document.createTextNode('The Bookmobile does not run today'));} 
                  if(location.id!='bookmobile'){
                  var eventlink = document.createElement('a');
                    eventlink.href ='http://www.eventkeeper.com/code/events.cfm?curOrg=KANAWHA#'+eb[ev].ekId
                    event.appendChild(document.createTextNode(eb[ev].dateString+ ' - '));
                    eventlink.appendChild(document.createTextNode(eb[ev].title));
                    event.appendChild(eventlink);
                }
                events.appendChild(event);
                events.style.display = 'block';
            }
                            if(location.id=='bookmobile'&&KCPL.today>bkschedule.enddate){
events.appendChild(document.createTextNode('The Bookmobile does not run today'));}
        }
        else{
            if(location.id=='bookmobile'){
                events.appendChild(document.createTextNode('The Bookmobile does not run today'));
            }
            else{
                events.appendChild(document.createTextNode(stockmessage));
            }
            events.style.display = 'none';
            handletext = 'open event drawer';
        }
        eventdrawer.appendChild(events);
        var control = KCPL.buildElement('div', 'calendarcontrol2')
        var handle = KCPL.buildElement('div', 'drawerhandle', location.id +2)
        var handlebar = KCPL.buildElement('span', 'switch2')
        var switch1 = KCPL.buildElement('a', null ,location.id+'1')
        switch1.href ="javascript:sidecal.DisplaySwitch('"+location.id+"', '"+location.id+1+"')";
        switch1.appendChild(document.createTextNode(handletext));
        handlebar.appendChild(switch1);
        var lefthandle = KCPL.buildElement('div', 'drawerleft')
        righthandle=document.createElement('div');
        righthandle.className ='drawerright';
        handle.appendChild(handlebar);
        handle.appendChild(lefthandle);
        handle.appendChild(righthandle);
        control.appendChild(handle);
        eventdrawer.appendChild(control);
        return eventdrawer
    },            

    addBookmobile: function(bksched){
        var stops=' ';
        var week =null;
        var curmonth= null;
        var i=0;
        var wk2i = bksched.startdate
        while(wk2i <= this.currentDate()){
            if(i%7==0 &&week!==(bksched.week1)){
                week = bksched.week1;
                weekid='1';
            } else if(i%7==0&&week===(bksched.week1)){
                week = bksched.week2;
                weekid='2';
            }
        switch(i%7){
            case(0):{
                stops = week.monday
                break;
            }
            case(1):{
                stops = week.tuesday
                break;
            }
            case(2):{
                stops = week.wednesday
                break;
            }
            case(3):{
                stops = week.thursday
                break;
            }
        }
        if(stops&&wk2i.toString()== this.currentDate().toString()){
            for(var s=0; s<stops.length;s++){
            if(curmonth!=wk2i.getMonth()){
                curmonth =wk2i.getMonth();
                }
                var stop_event = new cal.Event();
                stop_event.startDate = new Date(wk2i.getFullYear(), wk2i.getMonth(), wk2i.getDate());
                stop_event.title =stops[s].name +', '+stops[s].address;
                stop_event.location = 'bk';
                stop_event.timeString = stops[s].startTime +' - '+stops[s].endTime;
                stop_event.ekId = stop_event.location + stop_event.startDate.getMonth() +stop_event.startDate.getDate()+stop_event.startDate.getYear()+parseInt(stop_event.startDate.getDay()) +(s+1);
                stop_event.address= stops[s].address;
                stop_event.stopid=stop_event.location+weekid+parseInt(stop_event.startDate.getDay()) +(s+1);
                stop_event.exceptionText = null;
                if(bksched.exceptions){
                for(var t =0 ; t<bksched.exceptions.length; t++){
                if(bksched.exceptions[t].date.valueOf() ==stop_event.startDate.valueOf()){
                    if(bksched.exceptions[t].title=='all'||bksched.exceptions[t].title==stops[s].name){
                        stop_event.exceptionText = bksched.exceptions[t].text
                     }
                }
                }
                }
                this.main.addEvent(stop_event);
            }
        }
        stops = null;
        i=i+1;
        wk2i=new Date(wk2i.getFullYear(), wk2i.getMonth(), wk2i.getDate()+1);
        }
    }
}    