var jcal = function(){

    // Init public vars
    var busyDates = new Array();
    var monthStartDate = new Date();
    var dateClickHandler = 'myDateClickHandler';
    
    // Push month start to the first of the current month
    monthStartDate.setFullYear(monthStartDate.getFullYear(), monthStartDate.getMonth(), 1);

    function setBusyDates(dates) {
        busyDates = dates;
    }

    function next() {
        monthStartDate.setFullYear(monthStartDate.getFullYear(), monthStartDate.getMonth()+1, 1);
        show();
    }

    function prev() {
        monthStartDate.setFullYear(monthStartDate.getFullYear(), monthStartDate.getMonth()-1, 1);
        show();
    }

    function show(dates) {

        if (dates != undefined) {
            setBusyDates(dates);
        }
        
        // Define some var(s)
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var target = document.getElementById('jcal');
        
        // Open table
        var html = '<table>';
        
        // Month header
        html+= '<tr class="header controls">';
        html+= '<th class="prev button" onclick="jcal.prev()">&lt;</th>';
        html+= '<th class="month" colspan="5">'+months[monthStartDate.getMonth()]+' '+monthStartDate.getFullYear()+'</th>';
        html+= '<th class="next button" onclick="jcal.next()">&gt;</th>';
        
        // Calc calendar start date
        var calStartDate = new Date();
        calStartDate.setFullYear(monthStartDate.getFullYear(), monthStartDate.getMonth(), monthStartDate.getDate()-monthStartDate.getDay());
        
        // Day header
        html+= '<tr class="header days">';
        html+= '<th>S</th>';
        html+= '<th>M</th>';
        html+= '<th>T</th>';
        html+= '<th>W</th>';
        html+= '<th>T</th>';
        html+= '<th>F</th>';
        html+= '<th>S</th>';
        html+= '</tr>';
        
        // Week rows
        var busyClass;
        var currDate = new Date(calStartDate);
        var currMonthClass;
        var weekdayClass;
        for (var w=0; w < 6; w++) {

            html+= '<tr class="week">';
            for (var d=0; d < 7; d++) {
                
                if (d==0 || d==6) {
                    weekdayClass = 'weekend';
                } else {
                    weekdayClass = 'weekday';
                }
                
                if (currDate.getMonth() < monthStartDate.getMonth() || currDate.getFullYear() < monthStartDate.getFullYear()) {
                    currMonthClass = 'prev';
                } else if (currDate.getMonth() > monthStartDate.getMonth() || currDate.getFullYear() > monthStartDate.getFullYear()) {
                    currMonthClass = 'next';
                } else {
                    currMonthClass = 'curr';
                }
                
                busyClass = 'free';
                for (var i=0; i < busyDates.length; i++) {
                    eventDate = new Date(busyDates[i]);
                    if (eventDate.getFullYear() == currDate.getFullYear() 
                    && eventDate.getMonth() == currDate.getMonth() 
                    && eventDate.getDate() == currDate.getDate()) {
                        busyClass = 'busy';
                    }
                }
                
                var dateString = (currDate.getMonth()+1)+'/'+currDate.getDate()+'/'+currDate.getFullYear();
                html+= '<td abbr="' + dateString + '" class="day ' + weekdayClass + ' ' + currMonthClass + ' ' + busyClass + '">'+ currDate.getDate() +'</td>';

                currDate.setDate(currDate.getDate()+1);
                
            }
            html+= '</tr>';
        }
        
        // Close table
        html+= '</table>';

        // Output calendar
        target.innerHTML = html;
    }
    return {
        monthStartDate:monthStartDate, 
        next:next,
        prev:prev,
        setBusyDates:setBusyDates,
        show:show
    }
}();