var CALENDAR = {
    require: ["yui/yahoo/yahoo-min", "yui/event/event-min", "yui/dom/dom-min", "yui/calendar/calendar-min"],

    ycal: null,


    init: function(container, args) {
        if(args.two_up){
            this.ycal = new YAHOO.widget.CalendarGroup("calendar_table", container, {
                LOCALE_WEEKDAYS: '1char',
                title: "Please make a selection:",
                close: true
            });
        }
        else{
            this.ycal = new YAHOO.widget.Calendar("calendar_table", container, {
                LOCALE_WEEKDAYS: '1char'
            });
        }

        if (args.script_name) {
            var select_event = function() { CALENDAR.select_date(args.script_name); }

            this.ycal.selectEvent.subscribe(select_event, this.ycal, true);
            this.ycal.buildMonthLabel = function() {
                var cal_date = CALENDAR.page_date();
                var month = cal_date.getMonth();
                var year  = cal_date.getFullYear();
                var smonth = CALENDAR.date2sdate(cal_date, true);
                var path = (this._is_edit_mode) ? args.script_name : "";
                var url = path + '?smonth=' + smonth; 
                var label = this.Locale.LOCALE_MONTHS[month] + " " + year;
                var text = '<a href="' + url + '">' + label + '</a>'; 
                return text;
            }
        }
        if (args.smonth) {
            var array = args.smonth.split("/");
            var month = array[0];
            var year  = array[1];
            this.ycal.setMonth(month - 1);
            this.ycal.setYear(year);

        }

        if (args.selected) {
            this.ycal.addRenderer(args.selected, this.ycal.renderCellStyleSelected);
            var array = args.selected.split("/");
            var month = array[0];
            var year  = array[2];
            this.ycal.setMonth(month - 1);
            this.ycal.setYear(year);
        }


        if (args.sdate_json) {
            eval ('args.sdate_hash = ' + args.sdate_json);
            this.sdate_hash = args.sdate_hash;
        }
        if (args.sdate_hash)   this.ycal.addRenderer(CALENDAR.hash2sdates(args.sdate_hash), this.ycal.renderCellStyleHighlight1);
        if (args.on_select)    this.ycal.selectEvent.subscribe(args.on_select, this.ycal, true);
        if (args.selected)     this.ycal.addRenderer(args.selected, this.ycal.renderCellStyleSelected);
        if (args.select)       this.ycal.select(args.select);
        if (args.title)        this.ycal.title = args.title;

        if (!this._is_edit_mode) this.ycal.maxDate = new Date(); // can't select after today


        if (args.sdate_hash) {
            this.ycal.previousMonth = function() {
                this.subtractMonths(1);
                this.renderStack = new Array();
                this.addRenderer(CALENDAR.hash2sdates(args.sdate_hash), this.renderCellStyleHighlight1);
            };

            this.ycal.nextMonth = function() {
                this.addMonths(1);
                this.renderStack = new Array();
                this.addRenderer(CALENDAR.hash2sdates(args.sdate_hash), this.renderCellStyleHighlight1);
            };
        }
        this.ycal.render();
    },

    _is_future_sdate: function(sdate) {

        var sel = sdate.split("/");
        var month = sel[0];
        var day   = sel[1];
        var year  = sel[2];

        var cur = CALENDAR.stoday().split("/");
        var cur_month = cur[0];
        var cur_day   = cur[1];
        var cur_year  = cur[2];

        // future year
        if (year > cur_year) return true;
        if (year == cur_year) {
            // future month
            if (month > cur_month) return true;
            if (month == cur_month) {
                // future day
                if (day > cur_day) return true;
            }
        }
        return false;

    },

    cal2form: function(args) {
        args = args || {};

        var date = this.ycal.getSelectedDates()[0];
        var sdate = CALENDAR.date2sdate(date);
        var sel = sdate.split("/");
        var month = sel[0];
        var day   = sel[1];
        var year  = sel[2];

        this.byid(args.day_element || 'day').value = day;
        this.byid(args.month_element || 'month').value = month;
        this.byid(args.year_element || 'year').value = year;
    },

    page_date: function() {
        return this.ycal.cfg.getProperty("pagedate");
    },

    hash2sdates: function(hash) {

        var sdates = '';
        var cal_date = this.page_date();
        var month = this._zero_pad(cal_date.getMonth() + 1);
        var year  = cal_date.getFullYear();

        var is_site = (! this._is_edit_mode);
        // don't show future sdates on the site
        var page_sdate = month + '/' + '01' + '/' + year;

        if (is_site && this._is_future_sdate(page_sdate)) return '';

        if (hash[year] && hash[year][month]) {
            for (day in hash[year][month]) {
                var sdate = month + '/' + day + '/' + year;
                if (!is_site || !this._is_future_sdate(sdate)) {
                    sdates += (sdates) ? ',' + sdate : sdate;
                }
            }
        }
        return sdates;
    },


    show_cal: function(link_id) {
        var container = (this.ycal.oDomContainer) ? this.ycal.oDomContainer : this.ycal.outerContainer;
        container.style.display = 'block';
        if (link_id) {
            var link = document.getElementById(link_id);
            var pos = YAHOO.util.Dom.getXY(link);
            YAHOO.util.Dom.setXY(container, [pos[0],pos[1] + link.offsetHeight+1]);
        }
    },

    days_in_month: function(Y, M) {
        with (new Date(Y, M, 1, 12)) {
            setDate(0);
            return getDate();
        }
    },

    valid_date: function(args) {
        args = args || {};
        var month = this.byid(args.month || 'month').value;
        var day   = this.byid(args.day || 'day').value;
        var year  = this.byid(args.year || 'year').value;
        if (this.days_in_month(year, month) < day) {
            alert("Invalid Date: Out of Range");
            return false;
        }
        return true;
    },

    date2sdate: function(date, exclude_day) {
        var month = this._zero_pad(date.getMonth() + 1);
        var day   = this._zero_pad(date.getDate());
        var year  = date.getFullYear();
        return month + ((!exclude_day) ? '/' + day : '') + '/' + year;
    },

    stoday: function() { return this.date2sdate(new Date()) },

    byid: function(id) { return document.getElementById(id); },

    select_curtime: function(args) {
        args = args || {};
        var now = new Date();

        var sec = this.byid(args.insec || 'second');
        if (sec) sec.value = this._zero_pad(now.getSeconds());

        var min = this.byid(args.inmin || 'minute')
        if (min) min.value = this._zero_pad(now.getMinutes());

        var hour = this.byid(args.inhour || 'hour')
        if (hour) hour.value = this._zero_pad( (now.getHours() % 12) || 12);

        var ampm = this.byid(args.inampm || 'ampm')
        if (ampm) ampm.value = (now.getHours() >= 12) ? 'PM' : 'AM';

    },


    update_input: function(input_id) {
        var el = this.byid(input_id);
        var date  = this.ycal.getSelectedDates()[0];
        el.value = this.date2sdate(date);
    },

    _is_edit_mode: (location.pathname.match(/cgi-bin/) && !window.sbPreview),

    _redirect_by_date: function(sdate, script_name, field_name) {
        var path = (this._is_edit_mode) ? script_name : "";
        return path + '?' + field_name + '=' + sdate;
    },

    select_date: function(script_name) {
        var date  = this.ycal.getSelectedDates()[0];
        var sdate = CALENDAR.date2sdate(date);
        var r = this._redirect_by_date(sdate, script_name, "sdate_exact");
        setTimeout( 'document.location.href = "' + r + '";', 0 );
    },

    // change 1 to 01 etc (2 columns)
    _zero_pad: function(n) {
        if(n.constructor == Number) n = n.toString();
        return (n.length == 1) ? '0' + n : n;
    }
};

for (index in CALENDAR.require) {
    var src = '<script type="text/javascript" src="/fs_img/js/' + CALENDAR.require[index] + '.js"></script>';
    document.write(src);
}


