/*********
*   jsCelendar 1.2 beta
*   Copyright (c) 2005-2008 Igor Pavloviæ
*   This script can't be used freely without explicit permission author
*   Updated: 03.09.2007.
**********/
function JSCalendar() {

    // weekday constants
    JSCalendar.SUNDAY   = 0;
    JSCalendar.MONDAY   = 1;
    JSCalendar.TUESDAY  = 2;
    JSCalendar.WENSDAY  = 3;
    JSCalendar.THURSDAY = 4;
    JSCalendar.FRIDAY   = 5;
    JSCalendar.SATURDAY = 6;

    this.startingWeekday = 0;   // starting weekday sunday (can be changed)
    this.daysOfWeek = new Array('SU','MO','TU','WE','TH','FR','SA'); // array od weekday names
    this.months     = new Array('Januray','February','March','April','May','Juni','July','August','Spetember','October','November','December'); // array of month names (can be changed)
    this.date       = new Date();   // default date is current date (can be changed)
    this.HTML       = ''; // html code to be diplayed after renderign
    this.activeDates= new Object(); // array of active (selectable dates in the calendar)
    this.festivityDates = new Array(); // array of festivity dates (displayd  as defined in the jsCalendar.css file)
    this.container = null; // reference to a container object that will be used to cntain rendered html code
    this.seletedDay = null; // inidcated the selected day after is selected in the calendar
    this.closeButton = null; // button that will be use to hide the calendar

    var _this = this; // referenc to the this (self) object itself (the reference is used to call current object methods)


    // helper method to create the html code
    this.createCalendar = function() {

        count = 1;
        arrCell = new Array(42);

        _this.date.setDate(1);
        day = _this.date.getDay();

        if (_this.startingWeekday >= day+1) day = (7 - _this.startingWeekday);
        else day = day - _this.startingWeekday;
        //day = day - this.startingWeekday

        currMonth = _this.date.getMonth();
        
        sCurrMonth = currMonth+1;
        if (sCurrMonth < 10) sCurrMonth = '0' + sCurrMonth;
        //day-=1;
        key = _this.date.getFullYear() + '_' + sCurrMonth;
        currMonthSelections = _this.activeDates[key];

        if (!currMonthSelections)
            for (i = 0;i < 32;i++) currMonthSelections += '0';

        while (count <= 42) {

            if (count <= day) arrCell[count-1] = '<TD>&nbsp;</TD>';
            else if(currMonth == _this.date.getMonth()) {

                dayDate = parseInt(count) - parseInt(day);

                if (currMonthSelections.charAt(dayDate) == '1') 
                    arrCell[count-1] = '<TD class="selecatbleDate" >' + dayDate + '</TD>';
                else if (_this.festivityDates[key + '-' + dayDate] == '1')
                    arrCell[count-1] = '<TD class="festivityDate" >' + dayDate + '</TD>';
                else 
                    arrCell[count-1] = '<TD class="unselecableDate" >' + dayDate + '</TD>';

                _this.date.setDate(parseInt(_this.date.getDate())+parseInt(1));
            }
            else arrCell[count-1] = '<TD>&nbsp;</TD>';

            count += 1;
        }

        _this.date.setMonth(_this.date.getMonth()-1);


        arrCell[0] = '<TR>' + arrCell[0];
        arrCell[6] += '</TR><TR>';
        arrCell[13] += '</TR><TR>';
        arrCell[20] += '</TR><TR>';
        arrCell[27] += '</TR><TR>';
        arrCell[34] += '</TR><TR>';
        arrCell[41] += '</TR>';

        return arrCell.join('');
    }
    
    // method used to add festivity dates
    this.addFestivityDate = function(y,m,d) {
        key =
            parseInt(y) + '' +
            parseInt(m) + '-' +
            parseInt(d);

        _this.festivityDates[key] = '1';
    }

    // method used to remove festivity dates
    this.removeFestivityDate = function(y,m,d) {
        key = parseInt(y) + '-' + parseInt(m) + '-' + parseInt(d);
        _this.festivityDates[key] = '0';
    }

    // helper method to generate html code for the header containing days names;
    this.daysHeader = function() {
        strDays = '<TR>';

        for(i = 0;i < 7;i++) {
            iDay = parseInt(_this.startingWeekday) + i;

            if (iDay < 7) strDays += '<TD>' + _this.daysOfWeek[iDay] + '</TD>';
            else strDays += '<TD>' + _this.daysOfWeek[iDay - 7] + '</TD>';
        }

        strDays += '</TR><tr><td colspan="7"><div style="width:170px;height:1px;background-color:#EEEEEE;overflow:hidden;">&nbsp;</div></td></tr>';
        return strDays;
    }

    // clear all active dates that were  added
    this.clearActiveDates = function() {
        _this.activeDates = new Object();
    }

    // method to set selectable dates from the callendar ()
    this.setActiveMonthDates = function(y,m,days) {
        //if (m < 10) m = '0' + m;

        while (days.length < 32) days += new String('0');
        //_this.activeDates[y + '_' + m] = days;
        _this.activeDates[new String(y) + '_' + new String(m)] = days;

    }

    // method used to change the calendar month 
    this.navigate = function(month) {
        _this.date.setMonth(
            parseInt(this.date.getMonth()) + parseInt(month)
        );

        _this.render(this.container);
    }

    // helper method used to display the calendar
    this.render = function(containerObject) {

        _this.container = containerObject;
        //if (arguments[1]) this._this = arguments[1];

        // folowing 2 rows creates the header with navigation buttons and year and month info
        strHTML = '<DIV class="JSCalendar">';
        strHTML += '<TABLE border="0" cellpadding="0" cellspacing="0">';
        
        strHTML += '<TR><TD class="navigationCell">&lt;&lt;</TD><TD class="month_year" colspan="5">' + this.months[this.date.getMonth()] + ' - ' + this.date.getFullYear() + '</TD><TD class="navigationCell">&gt;&gt;</TD></TR>';
        strHTML += _this.daysHeader();
        strHTML += _this.createCalendar();
        if (_this.closeButton != null)
            strHTML += '<TR><TD colspan="7" align="right" class="closeButton">' + _this.closeButton + '</TD></TR>';
        
        strHTML += '</TABLE>';
        strHTML += '</DIV>';

        try {
            // put html code into a container object;
            _this.container.innerHTML = strHTML;
        } catch (e) {
            alert(e.message);
            alert('Inavlid container object reference');
        }
        
        // add events
        elems = document.getElementsByTagName('TD');
        for (var i = 0;i < elems.length;i++) {
            switch(elems[i].className) {
                case "navigationCell":
                    if (! elems[i].onclick) elems[i].onclick = _this.changeMonth;
                break;

                case "selecatbleDate":
                    if (! elems[i].onclick) elems[i].onclick = _this.setSelectedDate;
                break;

                case "closeButton":
                    if (! elems[i].onclick) elems[i].onclick = _this.onCloseCalendar;
                break;
            }
        }
        //alert(elems.length);
    }
    
    
    this.onCloseCalendar = function (e) {
        //if (!e) e = window.event;
        alert('Ovveride this method : onCloseCalendar');
    }

    this.changeMonth = function(e) {
        if (!e) e = window.event;
        obj = (e.target ? e.target : e.srcElement);
        
        switch(obj.innerHTML) {
            case "&lt;&lt;":
                _this.navigate(-1);
            break;
            case "&gt;&gt;":
                _this.navigate(1);
            break;
        }
    }

    // helper method for setting a selected day in th current year , month
    this.setSelectedDate = function (e) {

        if (!e) e = window.event;
        tCell = (e.target ? e.target : e.srcElement);
        _this.selectedDay = tCell.innerHTML; //day;

        //try { tCell.className = 'selectedDate'; } catch(e) {}
        
        try {
            _this.onDateSelected();
        } catch(e) {}
    }

    // returns the selected date from the calendar
    this.getSelectedDate = function() {
        selDate = this.date;
        selDate.setDate(this.selectedDay);

        if (arguments.length == 0)
            return selDate;

        sYear =  new String(selDate.getFullYear());
        sMonth  = selDate.getMonth() + parseInt(1);
        sDay    = selDate.getDate();

        arguments[0] = arguments[0].replace('Y',selDate.getFullYear());
        arguments[0] = arguments[0].replace('y',sYear.substring(2,4));
        if (sMonth < 10) sMonth = '0' + sMonth;
        arguments[0] = arguments[0].replace('M',sMonth);
        arguments[0] = arguments[0].replace('m',selDate.getMonth() + parseInt(1));
        if (sDay < 10) sDay = '0' + sDay;
        arguments[0] = arguments[0].replace('D',sDay);
        arguments[0] = arguments[0].replace('d',selDate.getDate());

        return arguments[0];
    }

    this.onDateSelected = function() {
        alert('Override this method' + this.name);
    }
}

/******** usage example of the calendar script ****************

<html>
<head>
<link rel="stylesheet" type="text/css" href="jsCalendar_1.2.css" />
<title>Example of evened columns - Liquid design</title>
<script language="javscript" type="text/javascript" src="jsCalendar_1.2.js"></script>
<script language="javascript">
function Init() {
    jsc = new JSCalendar();
    jsc.date = new Date('2007/8/01');
    jsc.setActiveMonthDates(2007,8,'1100001111');
    jsc.render(document.getElementById('container'));
    jsc.onDateSelected = function() {         alert(jsc.getSelectedDate("Y-M-D"));    }
}
</script>
</head>
<body onLoad="javascript:Init();">
<span id="container"></span>
</body>
</html>

**************************************************************/


